Given an undirected graph and an edge, the task is to find if the given edge is a bridge in graph, i.e., removing the edge disconnects the graph.
Following are some example graphs with bridges highlighted with red color.
One solution is to find all bridges in given graph and then check if given edge is a bridge or not.
A simpler solution is to remove the edge, check if graph remains connect after removal or not, finally add the edge back. We can always find if an undirected is connected or not by finding all reachable vertices from any vertex. If count of reachable vertices is equal to number of vertices in graph, then the graph is connected else not. We can find all reachable vertices either using BFS or DFS. Below are complete steps.
1) Remove the given edge
2) Find all reachable vertices from any vertex. We have chosen first vertex in below implementation.
3) If count of reachable nodes is V, then return false [given is not Bridge]. Else return yes.
Time Complexity : O(V + E)
This article is contributed by Pankaj Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Check if a given Graph is 2-edge connected or not
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Program to Calculate the Edge Cover of a Graph
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Maximize number of nodes which are not part of any edge in a Graph
- Edge Coloring of a Graph
- Add and Remove Edge in Adjacency List representation of a Graph
- Add and Remove Edge in Adjacency Matrix representation of a Graph
- Find the maximum component size after addition of each edge to the graph
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Count number of times each Edge appears in all possible paths of a given Tree
- Maximize shortest path between given vertices by adding a single edge
- Find maximum number of edge disjoint paths between two vertices
- Delete Edge to minimize subtree sum difference
- Minimum edge reversals to make a root
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Product of minimum edge weight between all pairs of a Tree
Improved By : PranchalKatiyar