Check if removing a given edge disconnects a graph
Last Updated :
08 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.



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.
C++
#include <bits/stdc++.h>
using namespace std;
// dfs function to explore all reachable nodes
void dfs(vector<vector<int>> &adj, int c, vector<bool> &visited) {
visited[c] = true;
for (auto i : adj[c]) {
if (!visited[i])
dfs(adj, i, visited);
}
}
// Construct adjacency list without the edge (c, d)
vector<vector<int>> constructadj(int V,
vector<vector<int>> &edges, int c, int d) {
vector<vector<int>> adj(V);
for (auto &edge : edges) {
// Skip the edge (c, d) or (d, c)
if ((edge[0] == c && edge[1] == d) || (edge[0] == d && edge[1] == c))
continue;
adj[edge[0]].push_back(edge[1]);
adj[edge[1]].push_back(edge[0]);
}
return adj;
}
// Check if the edge (c, d) is a bridge
bool isBridge(int V, vector<vector<int>> &edges, int c, int d) {
vector<vector<int>> adj = constructadj(V, edges, c, d);
vector<bool> visited(V, false);
dfs(adj, c, visited);
// if d is not reachable from c → bridge
return !visited[d];
}
int main() {
int V = 4;
vector<vector<int>> edges = {{0, 1}, {1, 2}, {2,3}};
int c = 1, d = 2;
cout << (isBridge(V, edges, c, d) ? "true" : "false") << endl;
return 0;
}
Java
import java.util.*;
public class GfG {
// dfs function to visit nodes
static void dfs(List<Integer>[] adj, int c,
boolean[] visited) {
visited[c] = true;
for (int neighbor : adj[c]) {
if (!visited[neighbor]) {
dfs(adj, neighbor, visited);
}
}
}
// Build adjacency list from edge list, excluding edge
// (c, d)
static List<Integer>[] constructAdj(int V,
int[][] edges,
int c, int d) {
List<Integer>[] adj = new ArrayList[V];
for (int i = 0; i < V; i++)
adj[i] = new ArrayList<>();
for (int[] edge : edges) {
int a = edge[0], b = edge[1];
if ((a == c && b == d) || (a == d && b == c))
continue;
adj[a].add(b);
adj[b].add(a);
}
return adj;
}
static boolean isBridge(int V, int[][] edges, int c,
int d) {
List<Integer>[] adj = constructAdj(V, edges, c, d);
boolean[] visited = new boolean[V];
dfs(adj, c, visited);
// if d not reachable from c →
// edge (c, d) is a bridge
return !visited[d];
}
public static void main(String[] args)
{
int V = 4;
int[][] edges = {{ 0, 1 }, { 1, 2 }, { 2,3 }};
int c = 1, d = 2;
System.out.println(
isBridge(V, edges, c, d) ? "true" : "false");
}
}
Python
def dfs(adj, c, visited):
# Standard DFS traversal from node c
visited[c] = True
for neighbor in adj[c]:
if not visited[neighbor]:
dfs(adj, neighbor, visited)
def constructadj(V, edges, c, d):
# Build adjacency list, skipping the edge (c, d)
adj = [[] for _ in range(V)]
for a, b in edges:
# Skip the edge we're testing as a potential bridge
if (a == c and b == d) or (a == d and b == c):
continue
adj[a].append(b)
adj[b].append(a)
return adj
def isBridge(V, edges, c, d):
# Build the graph without edge (c, d)
adj = constructadj(V, edges, c, d)
visited = [False] * V
# Run DFS starting from one end of the removed edge
dfs(adj, c, visited)
# If we can't reach the other node, it's a bridge
return not visited[d]
if __name__ == "__main__":
# Number of vertices
V = 4
# Edges of the graph
edges = [[0, 1], [1, 2], [2, 3]]
# Edge we want to test
c, d = 1, 2
# Output true if the edge is a bridge, false otherwise
print("true" if isBridge(V, edges, c, d) else "false")
C#
using System;
using System.Collections.Generic;
class GfG {
// Perform dfs from node 'd' and mark all reachable nodes
static void dfs(List<int>[] adj, int d, bool[] visited) {
visited[d] = true;
foreach (int neighbor in adj[d]) {
if (!visited[neighbor]) {
dfs(adj, neighbor, visited);
}
}
}
// Build adjacency list while skipping the edge (c, d)
static List<int>[] constructAdj(int V, int[,] edges, int c, int d) {
var adj = new List<int>[V];
// Initialize each adjacency list
for (int i = 0; i < V; i++) {
adj[i] = new List<int>();
}
// Get the number of edges
int E = edges.GetLength(0);
for (int i = 0; i < E; i++) {
int a = edges[i, 0];
int b = edges[i, 1];
// Skip the edge that we are testing for bridge
if ((a == c && b == d) || (a == d && b == c))
continue;
adj[a].Add(b);
adj[b].Add(a);
}
return adj;
}
// Check if edge (c, d) is a bridge
static bool isBridge(int V, int[,] edges, int c, int d) {
// Build the graph without the edge (c, d)
var adj = constructAdj(V, edges, c, d);
// Track visited nodes during DFS
bool[] visited = new bool[V];
// Start dfs from one end of the removed edge
dfs(adj, c, visited);
// If the other end is not reachable, it's a bridge
return !visited[d];
}
static void Main() {
// Number of vertices
int V = 4;
// Graph edges (undirected)
int[,] edges = {{ 0, 1 },{ 1, 2 },{ 2, 3 }};
// Edge to check if it’s a bridge
int c = 1, d = 2;
// Output whether the edge is a bridge
Console.WriteLine(isBridge(V, edges, c, d) ? "true" : "false");
}
}
JavaScript
// Perform DFS from node 'd' to mark all reachable nodes
function dfs(adj, d, visited) {
visited[d] = true;
for (const neighbor of adj[d]) {
if (!visited[neighbor]) {
dfs(adj, neighbor, visited);
}
}
}
// Build the adjacency list while skipping the edge (c, d)
function constructAdj(V, edges, c, d) {
// Create an empty adjacency list for each vertex
const adj = Array.from({ length: V }, () => []);
for (const [a, b] of edges) {
// Skip the edge we're testing for being a bridge
if ((a === c && b === d) || (a === d && b === c)) {
continue;
}
adj[a].push(b);
adj[b].push(a);
}
return adj;
}
// Check if the edge (c, d) is a bridge
function isBridge(V, edges, c, d) {
// Construct the graph without the edge (c, d)
const adj = constructAdj(V, edges, c, d);
// Track visited nodes during DFS
const visited = new Array(V).fill(false);
// Start DFS from one end of the removed edge
dfs(adj, c, visited);
// If the other end is not reachable, it's a bridge
return !visited[d];
}
// Number of vertices
const V = 4;
// Graph edges (undirected)
const edges = [[0, 1],[1, 2],[2, 3]];
// Edge to test
const c = 1, d = 2;
// Output the result
console.log(isBridge(V, edges, c, d) ? "true" : "false");
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.
Similar Reads
Check if a given Graph is 2-edge connected or not
Given an undirected graph G, with V vertices and E edges, the task is to check whether the graph is 2-edge connected or not. A graph is said to be 2-edge connected if, on removing any edge of the graph, it still remains connected, i.e. it contains no Bridges. Examples: Input: V = 7, E = 9 Output: Ye
12 min read
Find edges removing which does not disconnect the Graph
Given N vertices numbered from 0 to N - 1 and E edges to form an undirected graph. All the edges should be added in the given order. The task is to find the edges removing which do not disconnect the graph. If there are multiple possible edges return the ones which occur later in the sequence. Examp
7 min read
Check if a directed graph is connected or not
Given a directed graph. The task is to check if the given graph is connected or not.Examples: Input: Output: YesInput: Output: No Approach: Take two bool arrays vis1 and vis2 of size N (number of nodes of a graph) and keep false in all indexes.Start at a random vertex v of the graph G, and run a DFS
7 min read
Check if a given graph is Bipartite using DFS
Given a graph with V vertices numbered from 0 to V-1 and a list of edges, determine whether the graph is bipartite or not. Note: A bipartite graph is a type of graph where the set of vertices can be divided into two disjoint sets, say U and V, such that every edge connects a vertex in U to a vertex
9 min read
Check if a given directed graph is strongly connected | Set 2 (Kosaraju using BFS)
Given a directed graph, find out whether the graph is strongly connected or not. A directed graph is strongly connected if there is a path between any two pairs of vertices. There are different methods to check the connectivity of directed graph but one of the optimized method is Kosarajuâs DFS base
14 min read
Check if equal sum components can be obtained from given Graph by removing edges from a Cycle
Given an undirected graph with N vertices and N edges that contain only one cycle, and an array arr[] of size N, where arr[i] denotes the value of the ith node, the task is to check if the cycle can be divided into two components such that the sum of all the node values in both the components is the
15+ min read
Check if a graph is strongly connected | Set 1 (Kosaraju using DFS)
Given a directed graph, find out whether the graph is strongly connected or not. A directed graph is strongly connected if there is a path between any two pair of vertices. For example, following is a strongly connected graph. It is easy for undirected graph, we can just do a BFS and DFS starting fr
13 min read
Check if there exists a connected graph that satisfies the given conditions
Given two integers N and K. The task is to find a connected graph with N vertices such that there are exactly K pairs (i, j) where the shortest distance between them is 2. If no such graph exists then print -1. Note: The first-line output should be the number of edges(say m) in the graph and the nex
7 min read
Check if given graph is connected or not after removal of ith node sequential
Given an undirected graph with N nodes, M edges given by array edges[][2] and permutation array A[] of size N which indicates the order in which each and every node of graph will be removed. The task is to find if graph is connected or not after removal of ith node sequential from given array A[]. G
15 min read
Check if given Circles form a single Component
Given a 2-D array A[][] of size NÃ3, where each element of the array consists of {x, y, r}, where x and y are coordinates of that circle and r is the radius of that circle, the task is to check all the circles form a single component where a component is the set of connected circles. Note: Two circl
14 min read