VOOZH about

URL: https://www.geeksforgeeks.org/dsa/check-removing-given-edge-disconnects-given-graph/

⇱ Check if removing a given edge disconnects a graph - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Check if removing a given edge disconnects a graph

Last Updated : 8 Apr, 2025

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.

👁 Bridge1

👁 Bridge2

👁 Bridge3

Approach: Using DFS - O(V + E) Time and O(V) Space

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 in c and d no 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, if c and d are still connected through other paths, then the edge is not a bridge.

Step by Step implementation:

  • Remove the edge (c, d) from the graph.
  • Check if nodes c and d are still connected.
    • If they are not connected, it means the edge (c, d) was the only path between them, so it's a bridge.
    • If they are still connected through other paths, then the edge is not a bridge.

Output
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.

Comment
Article Tags: