Bridge edge in a graph | Practice | GeeksforGeeks

Given a Graph of V vertices and E edges and another edge(c - d), the task is to find if the given edge is a Bridge. i.e., removing the edge disconnects the graph.
 
Example 1:
Input:

c = 1, d = 2
Output:
1
Explanation:
From the graph, we can clearly see that
blocking the edge 1-2 will result in 
disconnection of the graph. So, it is 
a Bridge and thus the Output 1.


Example 2:
Input:

c = 0, d = 2
Output:
0
Explanation:

blocking the edge between nodes 0 and 2
won't affect the connectivity of the graph.
So, it's not a Bridge Edge. All the Bridge
Edges in the graph are marked with a blue
line in the above image.

 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isBridge()  which takes number of vertices V, the number of edges E, an adjacency lsit adj and two integers c and d denoting the edge as input parameters and returns 1 if the given edge c-d is a Bridge. Else, it returns 0.
 
Expected Time Complexity: O(V + E).
Expected Auxiliary Space: O(V).
 
Constraints:
1  ≤  V,E  ≤  105
0  ≤  c, d  ≤  V-1
0
amishasahu3286 days ago
void dfs(int node, vector<int> adj[], int c, int d, vector<int> &visited){
        visited[node] = 1;
        for(auto it: adj[node]){
            if(!visited[it] && (node != c || it != d))
                dfs(it, adj, c, d, visited);
        }
    }
    int isBridge(int V, vector<int> adj[], int c, int d) 
    {
        // Code here
        vector<int> visited(V, 0);
        dfs(c, adj, c, d, visited);
        // if d visited  i.e it can be reached with some other path as well apart from throught c
        // hence, it can't be bridges
        return !visited[d];
    }
0
sharadyaduvanshi2 weeks ago
 void dfs(vector<int>adj[],int v,int index,vector<bool>&visited)   {       if(visited[index] == true)       return ;       visited[index] = true;              for(int i = 0; i < adj[index].size(); i ++)       {           if(visited[adj[index][i]] != true)           dfs(adj,v,adj[index][i],visited);       }   }      int isBridge(int v, vector<int> adj[], int c, int d)    {              vector<int>::iterator position1 = find(adj[c].begin(), adj[c].end(), d);if (position1 != adj[c].end()) // == adj[c].end() means the element was not found   adj[c].erase(position1);      vector<int>::iterator position2 = find(adj[d].begin(), adj[d].end(), c);if (position2 != adj[d].end()) // == adj[d.end() means the element was not found   adj[d].erase(position2);             vector<bool>visited(v+1,false);             dfs(adj,v,c,visited);              if(visited[d] == true)       return 0;        return 1;   }
+2
madhukartemba1 month ago
JAVA SOLUTION USING PLAIN DFS:
class Solution
{
    
    private static void dfs(int node, boolean visited[], ArrayList<ArrayList<Integer>> adj)
    {
        visited[node] = true;
        for(int x : adj.get(node))
        {
            if(visited[x]==false)
            {
                dfs(x, visited, adj);
            }
        }
    }
    
    
    
    
    //Function to find if the given edge is a bridge in graph.
    static int isBridge(int V, ArrayList<ArrayList<Integer>> adj, int c, int d)
    {
        
        boolean visited[] = new boolean[V];
        
        
        adj.get(c).remove(new Integer(d));
        adj.get(d).remove(new Integer(c));
        
        dfs(c, visited, adj);
        
        
        if(visited[d]==true) return 0;
        
        return 1;
        
        
    }
}
0
shivkatoch5632 months ago
c++ BFS easy
 
dfs(int x,vector<int>adj[],int c,int d,vector<int>&vis){
       vis[x]=1;
       for(auto it:adj[x]){
           if(!vis[it] && (x!=c || it!=d))dfs(it,adj,c,d,vis);
       }
       return 0;
   }
   int isBridge(int v, vector<int> adj[], int c, int d) 
   {
       vector<int>vis(v,0);
       dfs(c,adj,c,d,vis);
       return !vis[d];
   }
Read Less


+2
kronizerdeltac2 months ago
JAVA SOLUTION:
 
static void dfs(ArrayList<ArrayList<Integer>> graph, int src, boolean[] vis) {
        vis[src] = true;
        for(int e : graph.get(src)) {
            if(!vis[e])
                dfs(graph, e, vis);
        }
    }
    static int findEdge(ArrayList<ArrayList<Integer>> graph, int u, int v) {
        for(int i = 0; i < graph.get(u).size(); i++) {
            int e = graph.get(u).get(i);
            if (e == v)
                return i;
        }
        return -1;
    }
    static int isBridge(int V, ArrayList<ArrayList<Integer>> graph,int c,int d)
    {
        int initialComponent = 0;
        boolean[] vis = new boolean[V];
        
        for(int i = 0; i < V; i++) {
            if(!vis[i]) {
                initialComponent++;
                dfs(graph, i, vis);
            }
        }
        
        //removeEdge(graph, u, v);
        int idx1 = findEdge(graph, c, d);
        graph.get(c).remove(idx1);
        int idx2 = findEdge(graph, d, c);
        graph.get(d).remove(idx2);
        
        int finalComponent = 0;
        
        vis = new boolean[V];
        
        for(int i = 0; i < V; i++) {
            if(!vis[i]) {
                finalComponent++;
                dfs(graph, i, vis);
            }
        }
        
        return initialComponent != finalComponent ? 1 : 0;
    }
0
avenvy2 months ago
PYTHON SOLUTION
def isBridge(self, V, adj, c, d):
        # code here
        def dfs(node,adj,vis):
            vis[node]=1
            for j in adj[node]:
                if vis[j]==0:
                    dfs(j,adj,vis)
        vis1=[0]*V   
        initial=0
        for i in range(V):
            if vis1[i]==0:
                initial+=1
                dfs(i,adj,vis1)
        
        vis2=[0]*V
        if d in adj[c]:
            adj[c].remove(d)
        if c in adj[d]:
            adj[d].remove(c)
        final=0
        for i in range(V):
            if vis2[i]==0:
                final+=1
                dfs(i,adj,vis2)
        if initial==final:
            return 0
        else:
            return 1
0
ac_7
This comment was deleted.
0
soumo2k153 months ago
DFS
int dfs(int x,vector<int>adj[],int c,int d,vector<int>&vis){       vis[x]=1;       for(auto it:adj[x]){           if(!vis[it] && (x!=c || it!=d))dfs(it,adj,c,d,vis);       }       return 0;   }   int isBridge(int v, vector<int> adj[], int c, int d)    {       vector<int>vis(v,0);       dfs(c,adj,c,d,vis);       return !vis[d];   }
0
mufaddalshakir553 months ago
C++ Beast BFS
    int isBridge(int V, vector<int> adj[], int c, int d) 
    {
        vector<int> vis(V,-1);
        queue<int> q;
        q.push(c);
        vis[c] = 1;
        int count = 0;
        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(auto elem : adj[u]){
                if(elem == d){
                    count++;
                    if(count>1) return 0;
                    continue;
                }
                else if(vis[elem] == -1){
                    q.push(elem);
                    vis[elem] = 1;
                }
            }
        }
        
        return 1;
    }
0
sagittarius3 months ago
void bfs(vector<int> adj[],bool visited[],int V,int src){       visited[src]=true;       queue<int> q;       q.push(src);              while(!q.empty()){           int node = q.front();           q.pop();           for(int i=0;i<adj[node].size();i++){               if(visited[adj[node][i]]==false){                   visited[adj[node][i]]=true;                   q.push(adj[node][i]);               }           }       }   }         int isBridge(int V, vector<int> adj[], int c, int d)    {       // Code here       bool visited[V];       memset(visited,false,sizeof(visited));              int count1 = 0;       int count2 = 0;              for(int i=0;i<V;i++){           if(visited[i]==false){               count1++;               bfs(adj,visited,V,i);           }       }              vector<int>::iterator it = find(adj[c].begin(),adj[c].end(),d);       adj[c].erase(it);       it = find(adj[d].begin(),adj[d].end(),c);       adj[d].erase(it);              memset(visited,false,sizeof(visited));       for(int i=0;i<V;i++){           if(visited[i]==false){               count2++;               bfs(adj,visited,V,i);           }       }       if(count1==count2)return 0;       return 1;                 }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.