-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathdigraph.cpp
More file actions
315 lines (260 loc) · 7.79 KB
/
Copy pathdigraph.cpp
File metadata and controls
315 lines (260 loc) · 7.79 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
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
/*
* Main authors:
* Erik Ekstrom <eeks@sics.se>
* Mats Carlsson <mats.carlsson@ri.se>
* Noric Couderc <noric@sics.se>
* Roberto Castaneda Lozano <rcas@acm.org>
*
* This file is part of Unison, see http://unison-code.github.io
*
* Copyright (c) 2015-2016, Erik Ekstrom
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Simple Digraph implementation with functions for computing reachability and
* SCC.
*/
#include "digraph.hpp"
template<typename T>
void vector_insert(vector<T>& v, const T& e){
typename vector<T>::iterator it = lower_bound(v.begin(), v.end(), e);
if(it == v.end() || e < *it){
v.insert(it, e);
}
}
template<typename T>
void vector_erase(vector<T>& v, const T& e){
typename vector<T>::iterator it = lower_bound(v.begin(), v.end(), e);
if(it != v.end()) {
v.erase(it);
}
}
Digraph::Digraph(const vector<vector<vertex> >& edges) {
for(auto& e : edges) {
vector_insert(adjacency_list[e[0]], e[1]);
vector_insert(V, e[0]);
vector_insert(V, e[1]);
}
}
Digraph::Digraph(const vector<pair<int,int> >& edges) {
for(auto& e : edges) {
vector_insert(adjacency_list[e.first], e.second);
vector_insert(V, e.first);
vector_insert(V, e.second);
}
}
vector<vertex> Digraph::vertices() const {
return V;
}
vector<edge> Digraph::edges() const {
vector<edge> edges;
// Collect edges
for(auto& a : adjacency_list) {
vertex v1 = a.first;
for(vertex v2 : a.second) {
vector_insert(edges, {v1,v2});
}
}
return edges;
}
vector<vertex> Digraph::neighbors(vertex v) {
return adjacency_list[v];
}
Digraph Digraph::transpose() {
vector<edge> edges;
// Make list of reversed edges
for(auto& a : adjacency_list) {
vertex v1 = a.first;
for(vertex v2 : a.second) {
edges.push_back({v2,v1});
}
}
// Generate a transpoesed graph fromt the reversed edges.
return Digraph(edges);
}
Digraph Digraph::closure() {
vector<vertex> vs = vertices();
vector<edge> es = edges();
for(vertex v : vs) {
// Add edges to all vertices that
// one can reach from v
for(vertex u : reachables(v)) {
if(u != v) {
es.push_back(make_pair(v,u));
}
}
}
return Digraph(es);
}
Digraph Digraph::product(Digraph& B) {
vector<edge> es;
for(const edge& uv : edges()) {
vertex u = uv.first;
vertex v = uv.second;
for(vertex w : B.neighbors(v))
es.push_back(make_pair(u,w));
}
return Digraph(es);
}
// Computing the reduction using the closure
// To prove that transitive reduction is as easy as transitive
// closure, Aho et al. rely on the already-known equivalence with
// Boolean matrix multiplication. They let A be the adjacency matrix
// of the given graph, and B be the adjacency matrix of its transitive
// closure (computed using any standard transitive closure
// algorithm). Then an edge uv belongs to the transitive reduction if
// and only if there is a nonzero entry in row u and column v of
// matrix A, and there is not a nonzero entry in the same position of
// the matrix product AB. In this construction, the nonzero elements
// of the matrix AB represent pairs of vertices connected by paths of
// length two or more.
Digraph Digraph::reduction() {
Digraph B = closure();
Digraph AB = product(B);
vector<edge> es;
for(const edge& uv : edges()) {
vertex u = uv.first;
vertex v = uv.second;
if(!vector_contains(AB.neighbors(u), v))
es.push_back(uv);
}
return Digraph(es);
}
vector<vertex> Digraph::reachables(const vertex v) {
vector<vertex> r;
map<vertex, bool> visited;
stack<vertex> s;
for(vertex u : V) {
visited[u] = false;
}
// skip own vertex by pre adding
for(vertex w : adjacency_list[v]) {
s.push(w);
}
while(!s.empty()) {
vertex u = s.top();
s.pop();
if(!visited[u]) {
visited[u] = true;
vector_insert(r, u);
// For each edge u -> w, push w.
for(vertex w : adjacency_list[u]) {
s.push(w);
}
}
}
return r;
}
void Digraph::bron_kerbosch2(vector<vertex>& R,
const vector<vertex>& P,
const vector<vertex>& X,
vector<vector<vertex>>& cliques) {
if (P.size()==0 && X.size()==0) {
vector<vertex> R1(R.begin(), R.end());
sort(R1.begin(), R1.end());
cliques.push_back(R1);
} else if (P.size()>0) {
vector<vertex> P1(P.begin(), P.end());
vector<vertex> X1(X.begin(), X.end());
for (vertex v : vector_difference(P, neighbors(P[0]))) {
R.push_back(v);
bron_kerbosch2(R,
vector_intersection(P1, neighbors(v)),
vector_intersection(X1, neighbors(v)),
cliques);
R.pop_back();
vector_erase(P1, v);
vector_insert(X1, v);
}
}
}
// Return all maximal cliques
// Warning: assumes not a digraph, but an undirected graph
vector<vector<vertex>> Digraph::max_cliques() {
vector<vector<vertex>> cliques;
vector<vertex> R;
if (!V.empty()) {
bron_kerbosch2(R, V, {}, cliques);
sort(cliques.begin(), cliques.end());
}
return cliques;
}
vector<vector<vertex> > Digraph::scc() {
vector<vector<vertex> > sccomponents;
// Based on Kosaraju's algorithm
// No vertices visited yet.
map<vertex, bool> visited;
for(vertex v : V) {
visited[v] = false;
}
stack<vertex> s;
// While s does not contain all vertices
for(vertex v : V) {
if(!visited[v]) {
dfs2(v, visited, s);
}
}
// Reverese edges of graph (*this)
Digraph t = transpose();
// Reset visisted
for(vertex v : V) {
visited[v] = false;
}
while(!s.empty()) {
vertex v = s.top();
s.pop();
if(!visited[v]) {
vector<vertex> component;
// extract component of vertex v
t.dfs2(v, visited, component);
vector_insert(sccomponents, component);
}
}
return sccomponents;
}
void Digraph::dfs2(vertex v, map<vertex, bool>& visited, stack<vertex>& s) {
visited[v] = true;
for(vertex w : adjacency_list[v]) {
if (!visited[w]) {
dfs2(w, visited, s);
}
}
s.push(v);
}
void Digraph::dfs2(vertex v, map<vertex, bool>& visited, vector<vertex>& s) {
visited[v] = true;
for(vertex w : adjacency_list[v]) {
if (!visited[w]) {
dfs2(w, visited, s);
}
}
vector_insert(s, v);
}
ostream& operator<<(ostream& os, const Digraph& g){
for(auto& a : g.adjacency_list) {
os << a.first << " -> " << show(a.second) << endl;
}
return os;
}