-
Notifications
You must be signed in to change notification settings - Fork 0
/
articulation_point.cpp
108 lines (104 loc) · 2.6 KB
/
articulation_point.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
/*
Problem Statement: Find Articulation point in a graph
For a disconnected undirected graph, an articulation point is a vertex removing which
increases number of connected components.
NOTE: You are not allowed to use STLs or any other inbuilt libraries.
*/
#include<bits/stdc++.h>
using namespace std;
int time1=0;
class Graph {
public:
int V;
int **adj;
public:
Graph(int);
void addedge(int,int);
void printgraph(int);
void findartpt(int,int,bool[],int[],int[],int[],int[]);
};
Graph::Graph(int v) {
this->V=v;
adj=new int*[v];
for(int i=0;i<v;i++) {
adj[i]=new int[v];
for(int j=0;j<v;j++)
adj[i][j]=0;
}
}
void Graph::addedge(int u,int v) {
adj[u][v]=1;
adj[v][u]=1;
}
void Graph::printgraph(int n) {
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
cout<<adj[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
void Graph::findartpt(int v,int n,bool visited[],int parent[],int lowtime[],int vistime[],int artpts[]) {
visited[v]=true;
lowtime[v]=time1+1;
vistime[v]=time1+1;
time1++;
int child=0;
bool isartpt=false;
for(int k=0;k<n;k++) {
if(adj[v][k]==1 && k!=v) {
int it=k;
if(!visited[it]) {
child++;
parent[it]=v;
findartpt(it,n,visited,parent,lowtime,vistime,artpts);
if(vistime[v]<=lowtime[it])
isartpt=true;
else
lowtime[v]=min(lowtime[v],lowtime[it]);
}
else
lowtime[v]=min(lowtime[v],vistime[it]);
}
}
if(parent[v]==-1 && child>=2)
artpts[v]=1;
else if(parent[v]!=-1 && isartpt==true)
artpts[v]=1;
}
int main() {
int v,v1,v2,e,i;
cout<<"Enter the number of vertices in the graph: "<<endl;
cin>>v;
Graph g(v);
cout<<"Enter the number of edges: "<<endl;
cin>>e;
for(i=0;i<e;i++) {
cin>>v1;
cin>>v2;
g.addedge(v1,v2);
}
int *vistime,*lowtime,*artpts,*parent;
bool *visited;
vistime=new int[v];
lowtime=new int[v];
artpts=new int[v];
parent=new int[v];
visited=new bool[v];
for(i=0;i<v;i++) {
visited[i]=false;
parent[i]=-1;
artpts[i]=0;
}
for(i=0;i<v;i++) {
if(visited[i]==false)
g.findartpt(i,v,visited,parent,lowtime,vistime,artpts);
}
cout<<"The articulation points are: "<<endl;
for(i=0;i<v;i++) {
if(artpts[i]==1)
cout<<i<<" ";
}
cout<<endl;
}