![]() |
VOOZH | about |
Given a graph with V vertices and E edges, along with a specific edge connecting vertices c and d, the task is to determine whether this edge is a bridge. An edge (c–d) is said to be a bridge if removing it results in an increase in the number of connected components — meaning that vertices c and d were part of the same connected component before, but become disconnected after its removal.
Examples:
Input: V = 4, edges[][] = [[0, 1], [1, 2], [2, 3], [3, 4]]
c = 1, d = 2
Output: true
Explanation: From the graph, we can clearly see that blocking the edge 1-2 will result in disconnection of the graph.
Hence, it is a Bridge.Input: V = 5, edges[][] = [[0, 1], [1, 2], [0, 2], [0, 3], [3, 4]]
c = 0, d = 2
Output: false
Explanation: Blocking the edge between nodes 0 and 2 won't affect the connectivity of the graph.
So, it's not a Bridge Edge.
The following are some example graphs with bridges highlighted in red.
The idea is to temporarily remove the edge
(c, d)from the graph and check if it increases the number of connected components.
If removing the edge results incanddno longer being connected — meaning there is no path between them — then the edge(c, d)is a bridge, as its removal disconnects the graph.
On the other hand, ifcanddare still connected through other paths, then the edge is not a bridge.
Step by Step implementation:
(c, d) from the graph.c and d are still connected.(c, d) was the only path between them, so it's a bridge.true
Time Complexity: O(V + E) since DFS visits each vertex once O(V) and traverses all edges once O(E).
Auxiliary Space: O(V) for the visited array and recursive call stack (excluding adjacency list storage). We do not count the adjacency list in auxiliary space as it is necessary for representing the input graph.