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
- Edge Coloring of a 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
- 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
- Check if a given graph is Bipartite using DFS
- Check for star graph
- Check whether a given graph is Bipartite or not
- Check if a given graph is tree or not
- Check if a directed graph is connected or not
- Check if the given graph represents a Bus Topology
- Check if the given permutation is a valid DFS of graph
- Check if a given tree graph is linear or not
- Check if the given graph represents a Ring Topology
Improved By : PranchalKatiyar