-
Notifications
You must be signed in to change notification settings - Fork 0
/
Articulation points and bridges - tarzan - graph.cpp
122 lines (103 loc) · 3.43 KB
/
Articulation points and bridges - tarzan - graph.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
#include<bits/stdc++.h>
/* define the maximum node this program can use */
#define NODE 1000
using namespace std;
typedef pair<int,int> Pair;
/* :::::::::::::: function variables :::::::::::::::::
adjacent vector will carry the graph
bridges vector will carry the articulation bridge
points set will carry the articulation points
parent array will carry the initial parent of a node
discTime array will carry the time of discover of a node
low array will carry the lowest possible time to reach a node
counter will count the time for every passed node
visit array will carry the visiting status of a node
*/
vector<int>adjacent[NODE];
vector<Pair>bridges;
set<int>points;
int parent[NODE] = {-1};
int discTime[NODE] = {0};
int low[NODE] = {0};
int counter = 0;
bool visit[NODE] = {false};
void Tarzan(int node);
void showNodes();
void showBridges();
int main()
{
/* input the number of nodes and edges for particular graph */
int nodes; int edges;
cin >> nodes >> edges;
/* input the adjacent nodes */
for(int i=0; i<edges; i++)
{
int node1; int node2;
cin >> node1 >> node2;
adjacent[node1].push_back(node2);
adjacent[node2].push_back(node1);
}
/* call the function for find out articulation nodes and paths */
Tarzan(0);
/* display articulation nodes and paths */
showNodes();
showBridges();
}
void Tarzan(int node)
{
/* update the visiting status for the node*/
visit[node] = true;
/* increase the timer and update the discover and lower time for this node*/
counter ++;
discTime[node] = counter;
low[node] = counter;
/* this will count the child number of the node */
int child = 0;
for(int i=0; i<adjacent[node].size(); i++)
{
/* access all the adjacent of the node */
int thisNode = adjacent[node][i];
/* checking whether this node is already visited or not */
if(! visit[thisNode])
{
/* increase the child number of the node */
child++;
/* update this child parent and pass this child for next checking */
parent[thisNode] = node;
Tarzan(thisNode);
/* condition for articulation path */
if(low[thisNode] > discTime[node])
{
bridges.push_back(make_pair(node,thisNode));
}
/* condition for articulation node */
if(parent[node] == -1 && child > 1)
{
points.insert(node);
}
else if(parent[node] != -1 && low[thisNode] >= discTime[node])
{
points.insert(node);
}
/* update the lowest time to reach the node */
low[node] = min(low[node],low[thisNode]);
}
/* update the lowest time with this condition :: for visited adjacent*/
else if(thisNode != parent[node])
low[node] = min(low[node],discTime[thisNode]);
}
}
void showNodes()
{
/* iterate all nodes in the set STL */
set<int>::iterator i = points.begin();
for(; i!= points.end(); i++)
cout << *i << " ";
cout << endl;
}
void showBridges()
{
/* iterate all pairs in the vector STL*/
for(int i=0; i<bridges.size(); i++)
cout << bridges[i].first << " --- " << bridges[i].second << endl;
}