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 firstname.lastname@example.org. 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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Edge Coloring of a Graph
- Program to Calculate the Edge Cover of a Graph
- Maximize number of nodes which are not part of any edge in a Graph
- Find weight of MST in a complete graph with edge-weights either 0 or 1
- Tree, Back, Edge and Cross Edges in DFS of Graph
- Add and Remove Edge in Adjacency List representation of a Graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Find the maximum component size after addition of each edge to the graph
- Maximum Possible Edge Disjoint Spanning Tree From a Complete Graph
- Check whether a given graph is Bipartite or not
- Check for star graph
- Check if a given graph is Bipartite using DFS
- Check if a given graph is tree or not
- Check if a given tree graph is linear or not
- Check if the given permutation is a valid DFS of graph
Improved By : PranchalKatiyar