-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindArticulationPoints.cpp
300 lines (263 loc) · 8.48 KB
/
findArticulationPoints.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
/*****************************************************************************
* * * * * CARNAC: Clustering coefficient-based Acquisition of RNA Communities
* * * * *
* * * * * Authors: Camille Marchet
* * * * * Contact: [email protected], INRIA/IRISA/GenScale, Campus de Beaulieu, 35042 Rennes Cedex, France
* * * * * Source: https://github.com/Kamimrcht/CARNAC
* * * * *
* * * * *
* * * * * This program is free software: you can redistribute it and/or modify
* * * * * it under the terms of the GNU Affero General Public License as
* * * * * published by the Free Software Foundation, either version 3 of the
* * * * * License, or (at your option) any later version.
* * * * *
* * * * * This program is distributed in the hope that it will be useful,
* * * * * but WITHOUT ANY WARRANTY; without even the implied warranty of
* * * * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* * * * * GNU Affero General Public License for more details.
* * * * *
* * * * * You should have received a copy of the GNU Affero General Public License
* * * * * along with this program. If not, see <http://www.gnu.org/licenses/>.
* * * * * *****************************************************************************/
#include <iostream>
#include <list>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>
#include "findArticulationPoints.hpp"
using namespace std;
// A class that represents an undirected graph of reads
//~ class Graph
//~ {
//~ public:
//~ int nbNodes;
//~ list<uint> *edges;
//~ void APUtil(uint v, bool visited[], uint disc[], uint low[],
//~ uint parent[], bool ap[]);
//~ Graph(uint nbNodes); // Constructor
//~ void addEdge(uint v, uint w); // function to add an edge to graph
//~ void AP(bool* ap); // get articulation points
//~ };
Graph::Graph(uint nbNodes){
this->nbNodes = nbNodes;
edges = new vector<int>[nbNodes];
}
void Graph::addEdge(int v, int w)
{
edges[v].push_back(w);
edges[w].push_back(v);
}
Graph::~Graph(){
delete [] edges;
}
//~ // A recursive function that find articulation points using DFS traversal
//~ // u --> The vertex to be visited next
//~ // visited[] --> keeps tract of visited vertices
//~ // disc[] --> Stores discovery times of visited vertices
//~ // parent[] --> Stores parent vertices in DFS tree
//~ // ap[] --> Store articulation points
void Graph::APUtil(int U, bool visited[], int disc[], int low[], int parent[], vector<bool>& ap){
vector<int> children(ap.size(),0);
vector<pair<int,int>> mem;
mem.push_back({U,0});
static int time = 0;
while(not mem.empty()){
pair<int,int> balls(mem[mem.size()-1]);
int u=balls.first;
mem.pop_back();
if(balls.second>=0){
if(not visited[u]){
visited[u] = true;
disc[u] = low[u] = ++time;
}
int i=balls.second;
if( edges[u].empty()){
continue;
}
int v = edges[u][i]; // v is current adjacent of u
if((uint)i+1<edges[u].size()){
mem.push_back({u,i+1});
}
if (not visited[v]){
children[u]++;
parent[v] = u;
mem.push_back({u,-v-1});
mem.push_back({v,0});
}else if (v != parent[u]){// Update low value of u for parent function calls.
low[u] = min(low[u], disc[v]);
}
}else{
int v=-(balls.second+1);
low[u] = min(low[u], low[v]);
if (parent[u] == -1 && children[u]>1)
ap[u] = true;
if (parent[u] != -1 && low[v] >= disc[u])
ap[u] = true;
}
}
}
//~ void Graph::APUtil(int u, bool visited[], int disc[], int low[], int parent[], vector<bool>& ap){
//~ static int time = 0;
//~ int children = 0;
//~ visited[u] = true;
//~ disc[u] = low[u] = ++time;
//~ for (int i = 0; i != edges[u].size(); ++i){
//~ int v = edges[u][i]; // v is current adjacent of u
//~ if (!visited[v])
//~ {
//~ children++;
//~ parent[v] = u;
//~ APUtil(v, visited, disc, low, parent, ap);
//~ low[u] = min(low[u], low[v]);
//~ if (parent[u] == -1 && children > 1)
//~ ap[u] = true;
//~ if (parent[u] != -1 && low[v] >= disc[u])
//~ ap[u] = true;
//~ }
//~ else if (v != parent[u])
//~ low[u] = min(low[u], disc[v]);
//~ }
//~ }
//~ bool Graph::APUtilBool(int u, bool visited[], int disc[], int low[], int parent[], vector<bool>& ap, set<uint>& interC){
//~ bool found(false);
//~ static int time = 0;
//~ int children = 0;
//~ visited[u] = true;
//~ disc[u] = low[u] = ++time;
//~ for (uint i = 0; i < edges[u].size(); ++i){
//~ int v = edges[u][i];
//~ if (!visited[v])
//~ {
//~ children++;
//~ parent[v] = u;
//~ APUtilBool(v, visited, disc, low, parent, ap, interC);
//~ low[u] = min(low[u], low[v]);
//~ if (parent[u] == -1 && children > 1)
//~ ap[u] = true;
//~ if (parent[u] != -1 && low[v] >= disc[u])
//~ ap[u] = true;
//~ }else if (v != parent[u]){
//~ low[u] = min(low[u], disc[v]);
//~ }
//~ if (ap[u] and interC.count((uint)u)){
//~ return true;
//~ }
//~ }
//~ return found;
//~ }
bool Graph::APUtilBool(int U, bool visited[], int disc[], int low[], int parent[], vector<bool>& ap, set<uint>& interC){
vector<int> children(ap.size(),0);
vector<pair<int,int>> mem;
mem.push_back({U,0});
static int time = 0;
while(not mem.empty()){
pair<int,int> balls(mem[mem.size()-1]);
int u=balls.first;
mem.pop_back();
if(balls.second>=0){
if(not visited[u]){
visited[u] = true;
disc[u] = low[u] = ++time;
}
int i=balls.second;
if( edges[u].empty()){
continue;
}
int v = edges[u][i]; // v is current adjacent of u
if((uint)i+1<edges[u].size()){
mem.push_back({u,i+1});
}
if (not visited[v]){
children[u]++;
parent[v] = u;
mem.push_back({u,-v-1});
mem.push_back({v,0});
}else{
if (v != parent[u]){// Update low value of u for parent function calls.
low[u] = min(low[u], disc[v]);
}
if (ap[u] and interC.count((uint)u)){
return true;
}
}
}else{
int v=-(balls.second+1);
low[u] = min(low[u], low[v]);
if (parent[u] == -1 && children[u]>1)
ap[u] = true;
if (parent[u] != -1 && low[v] >= disc[u])
ap[u] = true;
if (ap[u] and interC.count((uint)u)){
return true;
}
if (v != parent[u]){// Update low value of u for parent function calls.
low[u] = min(low[u], disc[v]);
}
}
}
return false;
}
// The function to do DFS traversal. It uses recursive function APUtil()
//~ void Graph::AP(bool* ap)
void Graph::AP(vector<bool>& ap){
// Mark all the vertices as not visited
bool *visited = new bool[nbNodes];
int *disc = new int[nbNodes];
int *low = new int[nbNodes];
int *parent = new int[nbNodes];
// Initialize parent and visited, and ap(articulation point) arrays
for (uint i = 0; i < (uint)nbNodes; i++)
{
parent[i] = -1;
visited[i] = false;
ap.push_back(false);
//~ ap[i] = false;
}
// Call the recursive helper function to find articulation points
// in DFS tree rooted with vertex 'i'
for (uint i = 0; i < (uint)nbNodes; i++)
if (visited[i] == false){
//~ cout<<"go"<<endl;
APUtil(i, visited, disc, low, parent, ap);
}
delete [] visited;
delete [] disc;
delete [] low;
delete [] parent;
}
bool Graph::APBool(vector<bool>& ap, set<uint>& interC){
bool found(false);
// Mark all the vertices as not visited
bool *visited = new bool[nbNodes];
int *disc = new int[nbNodes];
int *low = new int[nbNodes];
int *parent = new int[nbNodes];
// Initialize parent and visited, and ap(articulation point) arrays
for (uint i = 0; i < (uint)nbNodes; i++)
{
parent[i] = -1;
visited[i] = false;
ap.push_back(false);
//~ ap[i] = false;
}
// Call the recursive helper function to find articulation points
// in DFS tree rooted with vertex 'i'
for (uint i = 0; i < (uint)nbNodes; i++){
if (visited[i] == false){
found = APUtilBool(i, visited, disc, low, parent, ap, interC);
if (found){
delete [] visited;
delete [] disc;
delete [] low;
delete [] parent;
return false;
}
}
}
delete [] visited;
delete [] disc;
delete [] low;
delete [] parent;
return found;
}