-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGreedy.cpp
66 lines (57 loc) · 1.61 KB
/
Greedy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "Greedy.h"
#include <iostream>
Greedy::Greedy(Network *g) : Framework(g), no_queries(0) {}
Greedy::~Greedy() {}
double Greedy::get_solution(bool is_ds) {
kseeds seeds;
int no_nodes = g->get_no_nodes();
double burned_budgets = 0.0, max_inf = 0;
vector<bool> selected(no_nodes, false);
while (true) {
int e_max = -1, i_max = -1;
double max_delta = 0;
for (int e = 0; e < no_nodes; ++e) {
if (selected[e]) {
continue;
}
int exceed_times = 0;
for (int i = 0; i < Constants::K; ++i) {
if (burned_budgets + cost_matrix[e][i] <= Constants::BUDGET) {
kseeds tmp_seeds = seeds;
tmp_seeds.push_back(kpoint(e, i));
double current_max_delta =
(estimate_influence(tmp_seeds) - max_inf) / cost_matrix[e][i];
no_queries++;
if (current_max_delta > max_delta) {
e_max = e;
i_max = i;
max_delta = current_max_delta;
}
} else {
exceed_times++;
}
}
if (exceed_times == Constants::K) {
selected[e] = true;
};
}
if (e_max > -1 && i_max > -1) {
selected[e_max] = true;
seeds.push_back(pair<uint, uint>(e_max, i_max));
burned_budgets += cost_matrix[e_max][i_max];
max_inf = estimate_influence(seeds);
}
// Check if all the edge are selected
int selected_count = 0;
for (bool x : selected) {
if (x) {
selected_count++;
}
}
if (selected_count == no_nodes) {
break;
}
}
return max_inf;
}
int Greedy::get_no_queries() { return no_queries; }