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
- Maximum cost path in an Undirected Graph such that no edge is visited twice in a row
- Maximum number of edges that N-vertex graph can have such that graph is Triangle free | Mantel's Theorem
- Convert the undirected graph into directed graph such that there is no path of length greater than 1
- Path length having maximum number of bends
- Maximum number of edges in Bipartite graph
- Maximum number of nodes which can be reached from each node in a graph.
- Maximum number of edges among all connected components of an undirected graph
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Eulerian Path in undirected graph
- Shortest path in a complement graph
- Shortest path in an unweighted graph
- Multistage Graph (Shortest Path)
- Eulerian path and circuit for undirected graph
- Longest Path in a Directed Acyclic Graph | Set 2
- Find if there is a path between two vertices in a directed graph | Set 2
- Path with minimum XOR sum of edges in a directed graph
- 0-1 BFS (Shortest Path in a Binary Weight Graph)
- Shortest Path in Directed Acyclic 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.