Check if removing a given edge disconnects a graph
Last Updated :
28 Feb, 2023
Given an undirected graph and an edge, the task is to find if the given edge is a bridge in graph, i.e., removing the edge disconnects the graph.
Following are some example graphs with bridges highlighted with red color.
One solution is to find all bridges in given graph and then check if given edge is a bridge or not.
A simpler solution is to remove the edge, check if graph remains connect after removal or not, finally add the edge back. We can always find if an undirected is connected or not by finding all reachable vertices from any vertex. If count of reachable vertices is equal to number of vertices in graph, then the graph is connected else not. We can find all reachable vertices either using BFS or DFS. Below are complete steps.
- Remove the given edge
- Find all reachable vertices from any vertex. We have chosen first vertex in below implementation.
- If count of reachable nodes is V, then return false [given is not Bridge]. Else return yes.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
class Graph
{
int V;
list< int > *adj;
void DFS( int v, bool visited[]);
public :
Graph( int V);
void addEdge( int v, int w);
bool isConnected();
bool isBridge( int u, int v);
};
Graph::Graph( int V)
{
this ->V = V;
adj = new list< int >[V];
}
void Graph::addEdge( int u, int v)
{
adj[u].push_back(v);
adj[v].push_back(u);
}
void Graph::DFS( int v, bool visited[])
{
visited[v] = true ;
list< int >::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
DFS(*i, visited);
}
bool Graph::isConnected()
{
bool visited[V];
memset (visited, false , sizeof (visited));
DFS(0, visited);
for ( int i=1; i<V; i++)
if (visited[i] == false )
return false ;
return true ;
}
bool Graph::isBridge( int u, int v)
{
adj[u]. remove (v);
adj[v]. remove (u);
bool res = isConnected();
addEdge(u, v);
return (res == false );
}
int main()
{
Graph g(4);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.isBridge(1, 2)? cout << "Yes" : cout << "No" ;
return 0;
}
|
Java
import java.util.*;
class Graph {
int V;
ArrayList<ArrayList<Integer> > adj;
private void DFS( int v, boolean [] visited)
{
visited[v] = true ;
for (Integer i : adj.get(v)) {
if (!visited[i]) {
DFS(i, visited);
}
}
}
public Graph( int V)
{
this .V = V;
adj = new ArrayList<>();
for ( int i = 0 ; i < V; i++) {
adj.add( new ArrayList<>());
}
}
public void addEdge( int u, int v)
{
adj.get(u).add(v);
adj.get(v).add(u);
}
public boolean isConnected()
{
boolean [] visited = new boolean [V];
DFS( 0 , visited);
for ( int i = 1 ; i < V; i++)
if (visited[i] == false )
return false ;
return true ;
}
public boolean isBridge( int u, int v)
{
adj.get(u).remove(Integer.valueOf(v));
adj.get(v).remove(Integer.valueOf(u));
boolean res = isConnected();
addEdge(u, v);
return (res == false );
}
public static void main(String[] args)
{
Graph g = new Graph( 4 );
g.addEdge( 0 , 1 );
g.addEdge( 1 , 2 );
g.addEdge( 2 , 3 );
if (g.isBridge( 1 , 2 )) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
class Graph:
def __init__( self , V):
self .V = V
self .adj = [[] for i in range (V)]
def addEdge( self , u, v):
self .adj[u].append(v)
self .adj[v].append(u)
def DFS( self , v, visited):
visited[v] = True
i = 0
while i ! = len ( self .adj[v]):
if ( not visited[ self .adj[v][i]]):
self .DFS( self .adj[v][i], visited)
i + = 1
def isConnected( self ):
visited = [ False ] * self .V
self .DFS( 0 , visited)
for i in range ( 1 , self .V):
if (visited[i] = = False ):
return False
return True
def isBridge( self , u, v):
indU = self .adj[v].index(u)
indV = self .adj[u].index(v)
del self .adj[u][indV]
del self .adj[v][indU]
res = self .isConnected()
self .addEdge(u, v)
return (res = = False )
if __name__ = = '__main__' :
g = Graph( 4 )
g.addEdge( 0 , 1 )
g.addEdge( 1 , 2 )
g.addEdge( 2 , 3 )
if g.isBridge( 1 , 2 ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class Graph {
int V;
List<List< int >> adj;
private void DFS( int v, bool [] visited) {
visited[v] = true ;
foreach ( int i in adj[v]) {
if (!visited[i]) {
DFS(i, visited);
}
}
}
public Graph( int V) {
this .V = V;
adj = new List<List< int >>();
for ( int i = 0; i < V; i++) {
adj.Add( new List< int >());
}
}
public void addEdge( int u, int v) {
adj[u].Add(v);
adj[v].Add(u);
}
public bool isConnected() {
bool [] visited = new bool [V];
DFS(0, visited);
for ( int i = 1; i < V; i++)
if (!visited[i])
return false ;
return true ;
}
public bool isBridge( int u, int v) {
adj[u].Remove(v);
adj[v].Remove(u);
bool res = isConnected();
addEdge(u, v);
return (res == false );
}
public static void Main( string [] args) {
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3);
if (g.isBridge(1, 2)) {
Console.WriteLine( "Yes" );
}
else {
Console.WriteLine( "No" );
}
}
}
|
Javascript
class Graph {
constructor(V) {
this .V = V;
this .adj = new Array(V);
for (let i = 0; i < V; i++) {
this .adj[i] = new Array();
}
}
addEdge(u, v) {
this .adj[u].push(v);
this .adj[v].push(u);
}
DFS(v, visited) {
visited[v] = true ;
for (let i = 0; i < this .adj[v].length; i++) {
if (!visited[ this .adj[v][i]]) {
this .DFS( this .adj[v][i], visited);
}
}
}
isConnected() {
let visited = new Array( this .V);
for (let i = 0; i < this .V; i++) {
visited[i] = false ;
}
this .DFS(0, visited);
for (let i = 1; i < this .V; i++) {
if (visited[i] == false ) {
return false ;
}
}
return true ;
}
isBridge(u, v) {
let indU = this .adj[v].indexOf(u);
let indV = this .adj[u].indexOf(v);
this .adj[u].splice(indV, 1);
this .adj[v].splice(indU, 1);
let res = this .isConnected();
this .addEdge(u, v);
return (res == false );
}
}
let g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(1, 2);
g.addEdge(2, 3);
if (g.isBridge(1, 2)) {
console.log( "Yes" );
} else {
console.log( "No" );
}
|
Time Complexity : O(V + E)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...