Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
33 views
in Information Technology by (114k points)
How can you modify Tarjan's algorithm to find all bridges in a graph?

Please log in or register to answer this question.

1 Answer

0 votes
by (114k points)

By performing a DFS traversal and applying Tarjan's algorithm on each unvisited neighbor of a node, you can find all bridges in the graph.

Example Code in Python:

from collections import defaultdict

class Graph:
    def __init__(self):
        self.graph = defaultdict(list)
        self.time = 0

    def add_edge(self, u, v):
        self.graph[u].append(v)
        self.graph[v].append(u)

    def bridge_util(self, u, visited, parent, low, disc):
        visited[u] = True
        disc[u] = self.time
        low[u] = self.time
        self.time += 1

        for v in self.graph[u]:
            if not visited[v]:
                parent[v] = u
                self.bridge_util(v, visited, parent, low, disc)
                low[u] = min(low[u], low[v])
                if low[v] > disc[u]:
                    print("Bridge found:", u, "-->", v)
            elif v != parent[u]:
                low[u] = min(low[u], disc[v])

    def find_bridges(self):
        V = len(self.graph)
        visited = [False] * V
        disc = [float("inf")] * V
        low = [float("inf")] * V
        parent = [-1] * V

        for i in range(V):
            if not visited[i]:
                self.bridge_util(i, visited, parent, low, disc)

# Example usage:
g = Graph()
g.add_edge(0, 1)
g.add_edge(1, 2)
g.add_edge(2, 0)
g.add_edge(1, 3)
g.add_edge(3, 4)
g.find_bridges()
 

This Python code demonstrates how to find bridges in a graph using Tarjan's algorithm. The Graph class represents an undirected graph, and the find_bridges method identifies and prints the bridges in the graph.

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...