Professional Documents
Culture Documents
Bridges in a graph
An edge in an undirected connected graph is a bridge iff removing it disconnects the graph. For a
disconnected undirected graph, definition is similar, a bridge is an edge removing which increases
number of connected components.
Like Articulation Points,bridges represent vulnerabilities in a connected network and are useful for
designing reliable networks. For example, in a wired computer network, an articulation point indicates
the critical computers and a bridge indicates the critical wires or connections.
Following are some example graphs with bridges highlighted with red color.
http://www.geeksforgeeks.org/bridge-in-a-graph/ 1/11
7/9/2017 Bridges in a graph - GeeksforGeeks
..…b) See if the graph remains connected (We can either use BFS or DFS)
…..c) Add (u, v) back to the graph.
Time complexity of above method is O(E*(V+E)) for a graph represented using adjacency list. Can we
do better?
g1.addEdge(0, 3);
http://www.geeksforgeeks.org/bridge-in-a-graph/ 3/11
7/9/2017 Bridges in a graph - GeeksforGeeks
g1.addEdge(0, 3);
g1.addEdge(3, 4);
g1.bridge();
return 0;
}
Run on IDE
Java
// A Java program to find bridges in a given undirected graph
import java.io.*;
import java.util.*;
import java.util.LinkedList;
// Constructor
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
}
Python
# Python program to find bridges in a given undirected graph
#Complexity : O(V+E)
def __init__(self,vertices):
self.V= vertices #No. of vertices
self.graph = defaultdict(list) # default dictionary to store graph
self.Time = 0
# Mark all the vertices as not visited and Initialize parent and visited,
# and ap(articulation point) arrays
visited = [False] * (self.V)
disc = [float("Inf")] * (self.V)
low = [float("Inf")] * (self.V)
parent = [-1] * (self.V)
http://www.geeksforgeeks.org/bridge-in-a-graph/ 7/11
7/9/2017 Bridges in a graph - GeeksforGeeks
g2 = Graph(4)
g2.addEdge(0, 1)
g2.addEdge(1, 2)
g2.addEdge(2, 3)
print "\nBridges in second graph "
g2.bridge()
g3 = Graph (7)
g3.addEdge(0, 1)
g3.addEdge(1, 2)
g3.addEdge(2, 0)
g3.addEdge(1, 3)
g3.addEdge(1, 4)
g3.addEdge(1, 6)
g3.addEdge(3, 5)
g3.addEdge(4, 5)
print "\nBridges in third graph "
g3.bridge()
Output:
Time Complexity: The above function is simple DFS with additional arrays. So time complexity is same
as DFS which is O(V+E) for adjacency list representation of graph.
References:
https://www.cs.washington.edu/education/courses/421/04su/slides/artic.pdf
http://www.slideshare.net/TraianRebedea/algorithm-design-and-complexity-course-8
http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Weiss/L25-Connectivity.htm
http://www.youtube.com/watch?v=bmyyxNyZKzI
Please write comments if you find anything incorrect, or you want to share more information about the
topic discussed above.
▲
http://www.geeksforgeeks.org/bridge-in-a-graph/ 8/11
7/9/2017 Bridges in a graph - GeeksforGeeks
Graph graph-connectivity
Recommended Posts:
http://www.geeksforgeeks.org/bridge-in-a-graph/ 9/11
7/9/2017 Bridges in a graph - GeeksforGeeks
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
@geeksforgeeks, Some rights reserved Contact Us! About Us! Advertise with us! Privacy
Policy
http://www.geeksforgeeks.org/bridge-in-a-graph/ 10/11
7/9/2017 Bridges in a graph - GeeksforGeeks
http://www.geeksforgeeks.org/bridge-in-a-graph/ 11/11