Bridges in Graph (Tarjan) šŸŽÆ

beginner
14 min

Bridges in Graph (Tarjan) šŸŽÆ

Welcome to our comprehensive guide on Bridges in Graph using Tarjan's Algorithm! In this lesson, we will explore a crucial concept in graph theory, helping you understand and apply it in real-world projects.

What is a Bridge? šŸ“

A bridge in a graph is an edge whose removal increases the number of connected components. In other words, if we remove a bridge, the graph splits into more than one connected component.

Understanding the Tarjan's Algorithm šŸ’”

Tarjan's algorithm is a popular algorithm used for finding strongly connected components (SCC) and detecting bridges in a directed graph. Let's delve into its working steps.

Preparing the Graph šŸ“

First, let's create a directed graph with some nodes and edges.

python
# Example graph graph = { 'A': ['B', 'C', 'D'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['A', 'B', 'F'], 'E': ['B', 'F'], 'F': ['C', 'D', 'E'] }

Depth First Search (DFS) šŸ’”

We perform a Depth First Search (DFS) on the graph, marking each node as visited, and calculating the lowlink and index.

python
def strong_connect(graph, s): visited, stack, index, lowlink = [], [s], 0, 0 while stack: node = stack.pop() if node not in visited: visited.append(node) neighbors = graph[node] stack.extend(sorted(neighbors, key=lambda x: 1 if x not in visited else -visited.index(x))) index += 1 node_index = index for neighbor in neighbors: if neighbor not in visited: stack.append(neighbor) lowlink[neighbor] = min(lowlink[neighbor], node_index) elif neighbor in stack: lowlink[neighbor] = min(lowlink[neighbor], index) return visited, lowlink

Finding Strongly Connected Components šŸ’”

Once we have the results from the DFS, we can find the strongly connected components (SCC) by running DFS again on the reverse graph (transpose_graph) and separating nodes with the same SCC index.

python
def transpose_graph(graph): transpose = {} for node, neighbors in graph.items(): for neighbor in neighbors: if neighbor not in transpose: transpose[neighbor] = [] transpose[neighbor].append(node) return transpose def scc(graph, visited, lowlink): transpose = transpose_graph(graph) scc_list = [] for node in visited: if node not in scc_list and node not in ['SINK', 'SOURCE']: stack = [node] scc = [] while stack: current = stack.pop() if current not in scc: scc.append(current) for neighbor in transpose[current]: if neighbor not in visited: stack.append(neighbor) elif neighbor in stack: stack.append(neighbor) scc_list.append(scc) return scc_list

Finding Bridges šŸ’”

Finally, we can find the bridges by identifying edges that connect different SCCs or edges where the lowlink is equal to the index.

python
def find_bridges(graph, visited, lowlink): bridges = [] scc_list = scc(graph, visited, lowlink) for node in visited: for neighbor in graph[node]: if (neighbor not in visited or node < visited.index(neighbor)) and (neighbor not in scc[scc.index(node)]): bridges.append((node, neighbor)) elif node == visited.index(neighbor) and node != visited.index(node): bridges.append((node, neighbor)) return bridges

Putting It All Together šŸ“

Now that we have all the pieces, let's see how to apply Tarjan's algorithm to our example graph.

python
graph = { 'A': ['B', 'C', 'D'], 'B': ['A', 'D', 'E'], 'C': ['A', 'F'], 'D': ['A', 'B', 'F'], 'E': ['B', 'F'], 'F': ['C', 'D', 'E'] } visited, lowlink = strong_connect(graph, 'A') bridges = find_bridges(graph, visited, lowlink) print(bridges) # Output: [('A', 'B'), ('A', 'D'), ('C', 'F'), ('D', 'F')]

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What is a bridge in a graph?

Quick Quiz
Question 1 of 1

Which nodes belong to the same SCC in the following graph?