Input: Graph 1 ------- 2 ------- 3 -------- 4 | | | | 5 ------- 6 Output: 2 Explanation: There are 2 bridges, (1 - 2) and (3 - 4), in the path from 1 to 4. Input: Graph: 1 ------- 2 ------- 3 ------- 4 Output: 3 Explanation: There are 3 bridges, (1 - 2), (2 - 3) and (3 - 4) in the path from 1 to 4.
Follow the steps below to solve the problem:
- Find all the bridges in the graph and store them in a vector.
- Removal of all the bridges reduces the graph to small components.
- These small components do not have any bridges and they are weakly connected components which does not contain bridges in them.
- Generate a tree consisting of the nodes connected by bridges, with the bridges as the edges.
- Now, the maximum bridges in a path between any node is equal to the diameter of this tree.
- Hence, find the diameter of this tree and print it as the answer.
Below is the implementation of the above approach
Time Complexity: O(N + M)
Auxiliary Space: O(N + M)
- Bridges in a graph
- Paths to travel each nodes using each edge (Seven Bridges of Königsberg)
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Check if given path between two nodes of a graph represents a shortest paths
- Check if alternate path exists from U to V with smaller individual weight in a given Graph
- Minimum Cost Path in a directed graph via given set of intermediate nodes
- Maximum XOR with given value in the path from root to given node in the tree
- Shortest path from source to destination such that edge weights along path are alternatively increasing and decreasing
- Find if there is a path between two vertices in a directed graph
- Shortest Path in Directed Acyclic Graph
- Eulerian path and circuit for undirected graph
- Longest Path in a Directed Acyclic Graph
- Shortest path with exactly k edges in a directed and weighted graph
- Shortest Path in a weighted Graph where weight of an edge is 1 or 2
- Longest Path in a Directed Acyclic Graph | Set 2
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Multistage Graph (Shortest Path)
- Eulerian Path in undirected graph
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet