-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreedyIC.cpp
67 lines (54 loc) · 1.92 KB
/
greedyIC.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
67
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <chrono>
#include "difusioIC.h"
#include "readData.h"
#include "graph.h"
using namespace std;
bool compareMarginalGain(const int& u, const int& v, const vector<vector<int>>& G, double p) {
return simulate_IC(G, {u}, p) < simulate_IC(G, {v}, p);
}
vector<int> greedy_IC(const vector<vector<int>>& G, double p){
int graph_size = G.size();
set<int> S;
vector<int> node_order(graph_size);
for (int i = 0; i < graph_size; ++i) {
node_order[i] = i;
}
// Sort nodes by their marginal gain
sort(node_order.begin(), node_order.end(), [&](int u, int v) {
return compareMarginalGain(u, v, G, p);
});
int influence = 0;
// Select the node with the highest marginal gain and add it to the set S
for (int i = graph_size - 1; i >= 0; --i) {
int node = node_order[i];
int marginal_gain = simulate_IC(G, {node}, p) - influence;
if (marginal_gain > 0) {
S.insert(node);
influence += marginal_gain;
}
}
return vector<int>(S.begin(), S.end());
}
int main(int argc, char **argv) {
vector<vector<int> > G = read_Data(argc, argv);
double p = 0.7;
auto start = std::chrono::high_resolution_clock::now();
vector<int> result = greedy_IC(G,p);
auto end = std::chrono::high_resolution_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
cout << "Execution time: " << elapsed_time.count() << " ms" << std::endl;
cout << "-------------IC GREEDY-------------" << endl;
cout << "MINIMUM INITIAL NODES: {";
int result_size = result.size();
for(int k = 0; k < result_size; ++k) {
if(k == result_size - 1) cout << result[k]+1;
else cout << result[k]+1 << ", ";
}
cout << "}" << endl;
cout << "With a probability p of: " << p << endl;
cout << "SIZE: " << result_size << endl;
}