-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
142 lines (101 loc) · 3.04 KB
/
Copy pathmain.cpp
File metadata and controls
142 lines (101 loc) · 3.04 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
#include <vector>
using namespace std;
typedef unordered_multimap<int, int> Graph;
typedef unordered_map<int, int> List;
class FollowerGraph {
int nnodes;
int nedges;
Graph followers;
List degrees;
Graph reverse_degrees;
public:
FollowerGraph (int, int);
void save (string);
vector<int> get_followers (int);
};
FollowerGraph::FollowerGraph (int nnodes_, int nedges_) {
nnodes = nnodes_;
nedges = nedges_;
int x, y;
int size;
int break_condition;
// init degrees list
for (auto i = 0; i<nnodes; i++) {
degrees.at(i) = 0;
}
// set random edges
for (auto i=0; i<nedges; i++) {
break_condition = 0;
while (break_condition == 0) {
x = rand() % nnodes;
y = rand() % nnodes;
if (x==y) continue;
break_condition = 1;
auto its = followers.equal_range(x);
//size = distance(its.first, its.second);
size = followers.count(x);
cout << size << endl;
for (auto it = its.first; it != its.second; it++) {
if (it->second == y) break_condition = 0;
break;
}
}
followers.insert(Graph::value_type(x, y));
}
}
void FollowerGraph::save(string filename) {
ofstream graph_file;
graph_file.open(filename);
for (auto it = followers.begin(); it != followers.end(); it++) {
graph_file << it->first << ',' << it->second << endl;
}
graph_file.close();
}
vector<int> FollowerGraph::get_followers (int node) {
vector<int> followers_;
auto its = followers.equal_range(node);
for (auto it = its.first; it != its.second; it++) {
followers_.push_back(it->second);
}
return followers_;
}
int main(int argc, char **argv)
{
srand(time(0));
int nnodes;
int nedges;
nnodes = 10;
nedges = 10;
FollowerGraph graph (nnodes, nedges);
graph.save("random.graph");
bool infected[nnodes] = {false};
//for (auto i=0; i<nnodes; i++) cout << infected[i] << endl;
int origin = rand() % nnodes;
infected[origin] = true;
ofstream infected_file;
infected_file.open("0.nodes");
for (auto i=0; i<nnodes; i++) infected_file << infected[i] << endl;
infected_file.close();
// first infection step
vector<int> followers = graph.get_followers(origin);
for (auto it = followers.begin(); it != followers.end(); it++) {
infected[*it] = true;
}
infected_file.open("1.nodes");
for (auto i=0; i<nnodes; i++) infected_file << infected[i] << endl;
infected_file.close();
/*
for (auto it = followers.begin(); it != followers.end(); it++) {
cout << it->first << ' ' << it->second << endl;
}
auto its = followers.equal_range(4);
for (auto it = its.first; it != its.second; it++) {
cout << it->first << ' ' << it->second << endl;
}
*/
}