Tarjan's Algorithm in Python Last Updated : 01 Jun, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Tarjan's algorithm is used to find strongly connected components (SCCs) in a directed graph. It efficiently finds groups of vertices such that each vertex in a group has a path to every other vertex in the same group. Let's illustrate the working of Tarjan's algorithm with an example: Consider the following directed graph: 0 --> 1 --> 2 <--+^ || v+--- 6 <-- 5 ---+| ^v |3 --> 4 --> 7 --+How to implement Tarjan's Algorithm in Python?Initialization:Initialize TarjanSCC object with the graph.Initialize variables to track low link values, visited vertices, and SCCs.DFS Traversal:Start DFS traversal from each unvisited vertex.Assign low link values to vertices.Maintain a stack of vertices on the current DFS path.Identify SCCs based on low link values.Output SCCs:Output the strongly connected components found by the algorithm.Tarjan's Algorithm in Python: Python class TarjanSCC: def __init__(self, graph): self.graph = graph self.time = 0 self.stack = [] self.low_link = {} self.visited = set() self.sccs = [] def _dfs(self, v): self.low_link[v] = self.time self.time += 1 self.stack.append(v) self.visited.add(v) for neighbor in self.graph[v]: if neighbor not in self.low_link: self._dfs(neighbor) self.low_link[v] = min(self.low_link[v], self.low_link[neighbor]) elif neighbor in self.stack: self.low_link[v] = min(self.low_link[v], self.low_link[neighbor]) if self.low_link[v] == v: scc = [] while True: u = self.stack.pop() scc.append(u) if u == v: break self.sccs.append(scc) def find_sccs(self): for v in self.graph: if v not in self.low_link: self._dfs(v) return self.sccs # Example usage: graph = { 0: [1], 1: [2], 2: [0, 3], 3: [4], 4: [5, 7], 5: [6], 6: [0, 2, 5], 7: [3, 5, 8], 8: [4, 7] } tarjan = TarjanSCC(graph) sccs = tarjan.find_sccs() print("Strongly Connected Components:") for scc in sccs: print(scc) OutputStrongly Connected Components: [8, 7, 6, 5, 4, 3, 2, 1, 0] Complexity Analysis:Time Complexity: Tarjan's algorithm has a time complexity of O(V + E), where V is the number of vertices and E is the number of edges in the graph.Space Complexity: The space complexity of the algorithm is O(V), where V is the number of vertices, due to the stack used in DFS traversal Comment More infoAdvertise with us Next Article Prim's Algorithm in Python S srinam Follow Improve Article Tags : DSA Python-DSA Similar Reads Strassen algorithm in Python Strassen's algorithm is an efficient method for matrix multiplication. It reduces the number of arithmetic operations required for multiplying two matrices by decomposing them into smaller submatrices and performing recursive multiplication. Strassen's algorithm is based on the divide-and-conquer ap 3 min read Searching Algorithms in Python Searching algorithms are fundamental techniques used to find an element or a value within a collection of data. In this tutorial, we'll explore some of the most commonly used searching algorithms in Python. These algorithms include Linear Search, Binary Search, Interpolation Search, and Jump Search. 6 min read Prim's Algorithm in Python Prim's algorithm is a greedy algorithm used to find the Minimum Spanning Tree (MST) of a connected, undirected graph. The MST is a subset of the edges that connects all vertices in the graph with the minimum possible total edge weight.The algorithm starts with an empty spanning tree.The idea is to m 5 min read Tarjanâs Algorithm in C Language In this post, we will see the implementation of Tarjan's Algorithm in C language.What is Tarjan's Algorithm?Tarjan's Algorithm is a classic algorithm used for finding strongly connected components (SCCs) in a directed graph. An SCC is a maximal subgraph where every vertex is reachable from every oth 5 min read Bitwise Algorithm in Python Bitwise algorithms refer to the use of bitwise operators to manipulate individual bits of data. Python provides a set of bitwise operators such as AND (&), OR (|), XOR (^), NOT (~), shift left (<<), and shift right (>>). These operators are commonly used in tasks like encryption, com 6 min read RoadMap for DSA in Python Mastering Data Structures and Algorithms (DSA) is key to optimizing code and solving problems efficiently. Whether you're building applications or preparing for tech interviews at companies like Google, Microsoft, or Netflix, DSA knowledge is crucial. This roadmap will guide you from basic concepts 4 min read Like