Skip to content

Commit d003b0a

Browse files
Add files via upload
0 parents  commit d003b0a

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

articulatoin.cpp

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define IO_operations freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
4+
unordered_map<int, vector<int>> adj;
5+
vector<int> ap; //articulation point true, false
6+
vector<int> disc; // discovery time of u
7+
vector<int> low; //low node of 'u'
8+
int time_discovered = 0;
9+
//Articulation point
10+
11+
//graph ex:
12+
// n = no of nodes
13+
// m = no of edges
14+
// n = 9, m = 9
15+
//next m lines contains (u, v) which denotes edges btw u and v
16+
// 9 9
17+
// 1 3
18+
// 1 2
19+
// 2 3
20+
// 2 4
21+
// 4 5
22+
// 4 6
23+
// 2 7
24+
// 7 8
25+
// 8 9
26+
//Condition for Articulation point
27+
// Now we need to know if some vertex 𝑈
28+
// is an articulation point. So, for that we will check the following conditions:
29+
30+
// If there is NO way to get to a node 𝑉
31+
// with strictly smaller discovery time than the discovery time of 𝑈
32+
// following the DFS traversal, then 𝑈
33+
// is an articulation point. (it has to be strictly because if it is equal it means that 𝑈
34+
// is the root of a cycle in the DFS traversal which means that 𝑈
35+
// is still an articulation point).
36+
37+
// If 𝑈
38+
// is the root of the DFS tree and it has at least 2 children subgraphs disconnected from each other, then 𝑈
39+
// is an articulation point.
40+
41+
42+
int dfsAP(int u, int p) {
43+
int children = 0; // req for condition 2
44+
disc[u] = low[u] = ++time_discovered; //add discovery time of node u and low[u] when first discovered
45+
for(int &to: adj[u]) {
46+
if(to == p) continue; // we do not go back to the same path again or already discovered
47+
if(!disc[to]) {
48+
children++;
49+
dfsAP(to, u);
50+
if(disc[u] <= low[to]) { // means there is a no node which is ancestor of both u and to coz
51+
ap[u] = 1; //u is a articulation point
52+
} // if the condition do not hold then low[to] < disc[u] => low[to] is reachablve from some already discovered node
53+
low[u] = min(low[u], low[to]); // low[v] might be an ancestor of u
54+
} else {
55+
//if already discovered
56+
low[u] = min(low[u], disc[to]);
57+
}
58+
}
59+
return children;
60+
}
61+
62+
int main() {
63+
#ifndef ONLINE_JUDGE
64+
IO_operations;
65+
#endif
66+
int n, m; cin>>n>>m;
67+
time_discovered = 0;
68+
for(int i = 0; i < m; i++) {
69+
int u, v; cin>>u>>v;
70+
adj[u].push_back(v);
71+
adj[v].push_back(u);
72+
}
73+
74+
ap = low = disc = vector<int> (n + 1, 0);
75+
76+
for(int i = 1; i <= n; i++) {
77+
if(!disc[i]) {
78+
ap[i] = dfsAP(i, i) > 1; //condition 2 if i is root and atleast 2 subgraphs connected to it
79+
}
80+
}
81+
82+
cout<<"Articulation points are: ";
83+
for(int i = 1; i <= n; i++) if(ap[i]) cout<<i<<" ";
84+
return 0;
85+
}

bridges.cpp

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
#define IO_operations freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
4+
//Bridges
5+
6+
//graph ex:
7+
// n = no of nodes
8+
// m = no of edges
9+
// n = 9, m = 9
10+
//next m lines contains (u, v) which denotes edges btw u and v
11+
// 9 9
12+
// 1 3
13+
// 1 2
14+
// 2 3
15+
// 2 4
16+
// 4 5
17+
// 4 6
18+
// 2 7
19+
// 7 8
20+
// 8 9
21+
22+
//Condition for Articulation point will be used same
23+
// Now we need to know if some edge (u, v)
24+
// is an Bridge. So, for that we will check the following conditions:
25+
26+
// If there is NO way to get to a node 𝑉
27+
// with strictly smaller discovery time than the discovery time of 𝑈
28+
// following the DFS traversal, then 𝑈
29+
// is an articulation point. (it has to be strictly because if it is equal it means that 𝑈
30+
// is the root of a cycle in the DFS traversal which means that 𝑈
31+
// is still an articulation point).
32+
33+
34+
// Difference is disc[u] < low[to] instead of disc[u] <= low[to] because if disc[u] == low[to] => there is another path
35+
// from to to U. so it can't be a bridge.
36+
37+
unordered_map<int, vector<int>> adj;
38+
vector<vector<int>> br; //Bridges
39+
vector<int> disc; // discovery time of u
40+
vector<int> low; //low node of 'u'
41+
int time_discovered = 0;
42+
43+
void dfsBR(int u, int p) {
44+
disc[u] = low[u] = ++time_discovered; //mark when first discovered
45+
46+
for(int &to: adj[u] ) {
47+
if(to == p) continue;
48+
if(!disc[to]) {
49+
dfsBR(to,u);
50+
if(disc[u] < low[to]) {
51+
//this implies that there is no way to reach 'to' except crossing 'u'
52+
br.push_back({u, to});
53+
}
54+
55+
//update low in case there is some other ancestor which is discovered earlier than 'u' and connected to it.
56+
low[u] = min(low[u], low[to]);
57+
} else {
58+
//already discovered
59+
low[u] = min(low[u], disc[to]); //min of discover time of 'u' and 'to'
60+
}
61+
}
62+
}
63+
64+
//to find the no of bridges
65+
void BR(int n) {
66+
low = disc = vector<int> (n + 1, 0);
67+
for(int i = 1; i <= n; i++) {
68+
if(!disc[i]) dfsBR(i, i);
69+
}
70+
cout<<"Bridge edges are: \n";
71+
for(auto it: br) cout<<it[0]<<" "<<it[1]<<"\n";
72+
}
73+
74+
int main() {
75+
#ifndef ONLINE_JUDGE
76+
IO_operations;
77+
#endif
78+
int n, m; cin>>n>>m;
79+
time_discovered = 0;
80+
for(int i = 0; i < m; i++) {
81+
int u, v; cin>>u>>v;
82+
adj[u].push_back(v);
83+
adj[v].push_back(u);
84+
}
85+
BR(n);
86+
return 0;
87+
}

0 commit comments

Comments
 (0)