Bridge: If removal of an edge a-b splits the graph into 2 different components such that now vertex a and b are not connected to each other, then this edge is also called a bridge.
I use Tarjan's Algorithm to find bridge which uses https://www.geeksforgeeks.org/dsa/bridge-in-a-graph/ ; 2 arrays one for insertion time and other for low.
But instead of this we can simply do a dfs traversal from node 0 and keeping cur=1 (marks lowest_current_time of each node) and for all its child (except its parent) if that node is already visited then we first check this edge against cur (if returned_value<=cur then this edge cannot be a bridge) and update mini(which was initialized to cur) and after iterating to all its childs we simply returned its mini (indicating the minimum current(number) of node that is reachable through this node).
class Solution { public: vector<vector> criticalConnections(int V, vector<vector>& edges) { vectoradj[V]; for(auto&it:edges) { adj[it[0]].push_back(it[1]); adj[it[1]].push_back(it[0]); } vectorno(V); vectorvis(V,false); vector<vector>ans;
for(int i=0;i<V;i++)
{
if(!vis[i])
dfs(i,-1,adj,vis,no,1,ans);// to check all different components of given graph.
}
return ans;
}
int dfs(int i,int parent,vector<int>adj[],vector<bool>&vis,vector<int>&no,int cur,vector<vector<int>>&ans)
{
vis[i]=1;
no[i]=cur;
int mini=cur;
for(auto it:adj[i])
{
if(vis[it]){
if(it==parent) continue;
mini=min(mini,no[it]);
}
else {
int xx=dfs(it,i,adj,vis,no,cur+1,ans);
if(xx>no[i]) ans.push_back({i,it});
mini=min(mini,xx);
}
}
return no[i]=mini;
}};
Time Complexity : O(V+E) Space Complexity : O(V+E) I checked this code against several test cases but I didn't got any mismatch in answer compared to standard Tarjan's Algorithm. I will be very helpful to know your insights that if this code is correct then why did Tarjan didn't used this easy looking code or why isn't it available anywhere else.








