-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathGraphUtil.cpp
320 lines (288 loc) · 8.46 KB
/
GraphUtil.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* Copyright (c) Hilmi Yildirim 2010,2011.
The software is provided on an as is basis for research purposes.
There is no additional support offered, nor are the author(s)
or their institutions liable under any circumstances.
*/
#include "GraphUtil.h"
// depth first search given a start node
void GraphUtil::dfs(Graph& g, int vid, vector<int>& preorder, vector<int>& postorder, vector<bool>& visited) {
visited[vid] = true;
preorder.push_back(vid);
EdgeList el = g.out_edges(vid);
EdgeList::iterator eit;
int nextid = -1;
// check whether all child nodes are visited
for (eit = el.begin(); eit != el.end(); eit++) {
if (!visited[*eit]) {
nextid = *eit;
dfs(g, nextid, preorder, postorder, visited);
}
}
postorder.push_back(vid);
}
void GraphUtil::topo_leveler(Graph& g){
int N = g.num_vertices();
vector<int>::iterator sit;
// depth-first-search whole graph
for (int i=0; i < N ; i++)
topo_level(g, i);
}
int GraphUtil::topo_level(Graph& g, int vid){
if(g[vid].top_level != -1){
return g[vid].top_level;
}
int min = g.num_vertices();
int max = -1;
g[vid].top_level = 0;
EdgeList el = g.in_edges(vid);
EdgeList::iterator eit;
for(eit = el.begin(); eit != el.end(); eit++){
max = max > topo_level(g,*eit) ? max : g[*eit].top_level;
min = min < g[*eit].top_level ? min : g[*eit].top_level;
}
g[vid].top_level = max + 1;
g[vid].min_parent_level = (min == g.num_vertices() ? -1 : min );
return g[vid].top_level;
}
// topological sorting by depth-first search
// return reverse order of topological_sorting list
void GraphUtil::topological_sort(Graph g, vector<int>& ts) {
vector<bool> visited(g.num_vertices(), false);
vector<int> preorder;
vector<int> postorder;
vector<int> roots = g.getRoots();
vector<int>::iterator sit;
// depth-first-search whole graph
for (sit = roots.begin(); sit != roots.end(); sit++)
if (!visited[*sit])
dfs(g, (*sit), preorder, postorder, visited);
ts = postorder;
// for test
/*
cout << "Topo sorting(GraphUtil): ";
vector<int>::iterator vit;
for (vit = ts.begin(); vit != ts.end(); vit++)
cout << (*vit) << " ";
cout << endl;
*/
}
// implement tarjan's algorithm to find Strongly Connected Component from a given start node
void GraphUtil::tarjan(Graph& g, int vid, int& index, hash_map< int, pair<int,int> >& order,
vector<int>& sn, multimap<int,int>& sccmap, int& scc) {
// cout << " inside tarjan " << endl;
order[vid].first = index;
order[vid].second = index;
index++;
// cout << " before pushing vid = " << vid << endl;
sn.push_back(vid);
// cout << " after pushing vid = " << vid << endl;
g[vid].visited = true;
EdgeList el = g.out_edges(vid);
EdgeList::iterator eit;
for (eit = el.begin(); eit != el.end(); eit++) {
if (!g[*eit].visited) {
tarjan(g, *eit, index, order, sn, sccmap, scc);
order[vid].second = min(order[*eit].second, order[vid].second);
}
else if (find(sn.begin(), sn.end(), (*eit)) != sn.end()) {
order[vid].second = min(order[*eit].first, order[vid].second);
}
}
// cout << " in the middle of tarjan "<< endl;
if (order[vid].first == order[vid].second) {
vector<int>::reverse_iterator rit;
for (rit = sn.rbegin(); rit != sn.rend(); rit++) {
if ((*rit) != vid) {
sccmap.insert(make_pair(scc, *rit));
// sccmap[*rit] = scc;
sn.pop_back();
}
else {
sccmap.insert(make_pair(scc, *rit));
// sccmap[*rit] = scc;
sn.pop_back();
break;
}
}
scc++;
}
// cout << " outside tarjan "<< endl;
}
// merge Strongly Connected Component
// return vertex map between old vertex and corresponding new merged vertex
void GraphUtil::mergeSCC(Graph& g, int* on, vector<int>& reverse_topo_sort) {
vector<int> sn;
hash_map< int, pair<int, int> > order;
int ind = 0;
multimap<int, int> sccmap; // each vertex id correspond with a scc num
int scc = 0;
int vid;
int origsize = g.num_vertices();
// cout << " inside MergeSCC "<< endl;
for (int i = 0; i < origsize; i++) {
vid = i;
if (g[vid].visited)
continue;
tarjan(g, vid, ind, order, sn, sccmap, scc);
}
// cout << " inside MergeSCC after tarjan "<< endl;
// no component need to merge
if (scc == origsize) {
for (int i = 0; i < origsize; i++)
on[i] = i;
// topological sort
topological_sort(g, reverse_topo_sort);
// update graph's topological id
for (int i = 0; i < reverse_topo_sort.size(); i++)
g[reverse_topo_sort[i]].topo_id = reverse_topo_sort.size()-i-1;
return;
}
hash_map<int, vector<int> > inlist, outlist;
g.extract(inlist, outlist);
multimap<int,int>::iterator mit;
mit = sccmap.begin();
int num_comp;
int maxid = g.num_vertices()-1;
while (mit != sccmap.end()) {
num_comp = mit->first;
if (++sccmap.lower_bound(num_comp) == sccmap.upper_bound(num_comp)) {
on[mit->second] = mit->second;
++mit;
continue;
}
maxid++;
inlist[maxid] = vector<int>();
outlist[maxid] = vector<int>();
for (; mit != sccmap.upper_bound(num_comp); ++mit) {
on[mit->second] = maxid;
vector<int> vec = inlist[mit->second];
vector<int>::iterator vit, vit1;
vector<int> vec1;
bool hasEdge = false;
// copy all incoming edges
for (vit = vec.begin(); vit != vec.end(); vit++) {
hasEdge = false;
vec1 = outlist[*vit];
for (vit1 = vec1.begin(); vit1 != vec1.end(); vit1++) {
if (*vit1 == maxid) {
hasEdge = true;
break;
}
}
if (!hasEdge && (*vit != maxid)) {
inlist[maxid].push_back(*vit);
outlist[*vit].push_back(maxid);
}
}
// copy all outgoing edges
vec = outlist[mit->second];
for (vit = vec.begin(); vit != vec.end(); vit++) {
hasEdge = false;
vec1 = inlist[*vit];
for (vit1 = vec1.begin(); vit1 != vec1.end(); vit1++)
if (*vit1 == maxid) {
hasEdge = true;
break;
}
if (!hasEdge && (*vit != maxid)) {
outlist[maxid].push_back(*vit);
inlist[*vit].push_back(maxid);
}
}
// delete old vertex
vec = inlist[mit->second];
for (vit = vec.begin(); vit != vec.end(); vit++) {
for (vit1 = outlist[*vit].begin(); vit1 != outlist[*vit].end(); )
if (*vit1 == mit->second)
outlist[*vit].erase(vit1);
else
vit1++;
}
vec = outlist[mit->second];
for (vit = vec.begin(); vit != vec.end(); vit++) {
for (vit1 = inlist[*vit].begin(); vit1 != inlist[*vit].end(); )
if (*vit1 == mit->second)
inlist[*vit].erase(vit1);
else
vit1++;
}
outlist.erase(mit->second);
inlist.erase(mit->second);
}
}
g = Graph(inlist, outlist);
// topological sort
topological_sort(g, reverse_topo_sort);
// update graph's topological id
for (int i = 0; i < reverse_topo_sort.size(); i++)
g[reverse_topo_sort[i]].topo_id = reverse_topo_sort.size()-i-1;
// update index map
hash_map<int,int> indexmap;
hash_map<int, vector<int> >::iterator hit;
int k;
for (hit = outlist.begin(), k=0; hit != outlist.end(); hit++, k++) {
indexmap[hit->first] = k;
}
for (k = 0; k < origsize; k++)
on[k] = indexmap[on[k]];
/*
cout << "Index Map" << endl;
for (int i = 0; i < origsize; i++)
cout << on[i] << " ";
cout << endl;
cout << "roots: " << g.getRoots().size() << endl;
*/
}
// traverse tree to label node with pre and post order by giving a start node
// using GRIPP's labeling method
void GraphUtil::traverse(Graph& tree, int vid, int& pre_post, vector<bool>& visited) {
visited[vid] = true;
EdgeList el = tree.out_edges(vid);
EdgeList::iterator eit;
int pre_order;
for (eit = el.begin(); eit != el.end(); eit++) {
pre_order = pre_post;
pre_post++;
if (!visited[*eit])
traverse(tree, *eit, pre_post, visited);
tree[*eit].pre_order = pre_order;
tree[*eit].post_order = pre_post;
pre_post++;
}
}
// compute interval label for each node of tree (pre_order, post_order)
void GraphUtil::pre_post_labeling(Graph& tree) {
vector<int> roots = tree.getRoots();
vector<int>::iterator sit;
int pre_post = 0;
int pre_order = 0;
vector<bool> visited(tree.num_vertices(), false);
for (sit = roots.begin(); sit != roots.end(); sit++) {
pre_order = pre_post;
pre_post++;
traverse(tree, *sit, pre_post, visited);
tree[*sit].pre_order = pre_order;
tree[*sit].post_order = pre_post;
pre_post++;
}
}
// for test
void GraphUtil::genRandomGraph(int n, double c, char* filename) {
int threshold = int(c*1.00/(n*1.00)*10000);
Graph g;
int i, j;
int rand_num;
for (i = 0; i < n; i++)
g.addVertex(i);
srand(time(NULL));
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
if (i != j) {
rand_num = rand() % 10000;
if (rand_num < threshold)
g.addEdge(i,j);
}
}
ofstream out(filename);
g.writeGraph(out);
}