Skip to content

Commit cfc479e

Browse files
authored
Merge pull request #127 from pankajsingh92533/main
Implemented Tarjan's Algorithm.
2 parents c23b16e + b93f55d commit cfc479e

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

Graphs/Tarjan's_Algorithm.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
""" "defaultdict" in Python
2+
is a dictionary-like container from the collections
3+
module that provides a default value for keys that do not exist."""
4+
5+
from collections import defaultdict
6+
7+
# Function to run Tarjan's algorithm
8+
def tarjan(graph):
9+
10+
index = 0
11+
stack = []
12+
components = []
13+
14+
# Track visited and index for each node
15+
indexes = {}
16+
lowlinks = {}
17+
18+
def strongconnect(node):
19+
20+
# Set the depth index for this node to the smallest unused index
21+
nonlocal index
22+
indexes[node] = index
23+
lowlinks[node] = index
24+
index += 1
25+
stack.append(node)
26+
27+
# Consider successors of `node`
28+
try:
29+
successors = graph[node]
30+
except:
31+
32+
successors = []
33+
for successor in successors:
34+
if successor not in indexes:
35+
# Successor has not yet been visited; recurse on it
36+
strongconnect(successor)
37+
lowlinks[node] = min(lowlinks[node], lowlinks[successor])
38+
elif successor in stack:
39+
# Successor is in the stack, hence in the current SCC
40+
lowlinks[node] = min(lowlinks[node], indexes[successor])
41+
42+
# If `node` is a root node, pop the stack and generate an SCC
43+
if lowlinks[node] == indexes[node]:
44+
connected_component = []
45+
46+
while True:
47+
successor = stack.pop()
48+
connected_component.append(successor)
49+
if successor == node:
50+
break
51+
components.append(connected_component)
52+
53+
for node in graph:
54+
if node not in indexes:
55+
strongconnect(node)
56+
57+
return components
58+
59+
# Accept dynamic input for the graph
60+
graph = defaultdict(list)
61+
num_nodes = int(input("Enter the number of nodes: "))
62+
for i in range(num_nodes):
63+
node = int(input(f"Enter the successors of node {i}: "))
64+
successors = list(map(int, input().split()))
65+
graph[node] = successors
66+
67+
print("Strongly Connected Components:")
68+
print(tarjan(graph))
69+
70+
71+
72+
""" Explanation:->
73+
74+
1) Tarjan's algorithm performs a DFS on the graph to find strongly connected components.
75+
76+
2) It maintains an index (incremented for each visited node), a stack of visited nodes, and a lowlink value for each node (lowest index reachable from that node).
77+
78+
3) When visiting a node, if any successor is in the stack, the lowlink is updated to be the minimum of its current value and the successor's index.
79+
80+
4) If the lowlink of a node equals its own index, it is a root node and the current stack represents an SCC. This SCC is popped from the stack and added to the final components list.
81+
82+
5) After Tarjan's finishes, the components list contains all the SCCs in the graph."""

0 commit comments

Comments
 (0)