Skip to content

Commit

Permalink
Add approach for finding b bridges in graph
Browse files Browse the repository at this point in the history
  • Loading branch information
nandwalritik committed Jul 21, 2021
1 parent b6e13cf commit 943c3ba
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions graph/bridge.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include<bits/stdc++.h>
using namespace std;
int timer=0;
void dfs(int node,int par,vector<int> &in,vector<int> &low,vector<bool> &vis,vector<int> adj[]){
vis[node]=true;
in[node]=low[node]=timer;
timer++;
for(auto child:adj[node]){
if(child == par)
continue;
else if(vis[child]){
// the edge node-child is a back edge
low[node]=min(low[node],in[child]);
}
else{
dfs(child,node,in,low,vis,adj);
if(low[child] > in[node]){
// for 1 based numbering
cout<<node+1<<" "<<child+1<<" is a bridge\n";
}
low[node]=min(low[node],low[child]);
}
}
}
int main(){
int n,e;
cin>>n>>e;
vector<int> adj[n];
for(int i=0;i<e;i++){
int u,v;
cin>>u>>v;
adj[u-1].push_back(v-1);
adj[v-1].push_back(u-1);
}
vector<int> in(n),low(n);
vector<bool> vis(n,false);
dfs(0,-1,in,low,vis,adj);
return 0;
}

0 comments on commit 943c3ba

Please sign in to comment.