Skip to content

Commit 0891cbc

Browse files
committed
Added Kosaraju's algorithm in python
1 parent 86f3a3c commit 0891cbc

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
'''
2+
Code to find the strongly connected components of a graph using Kosaraju's Algorithm
3+
Time complexity: O(V + E)
4+
'''
5+
6+
from collections import defaultdict
7+
8+
class Graph:
9+
10+
#Constructor that basically initiallises every new vertex in dict as an
11+
#empty list
12+
def __init__(self):
13+
self.graph = defaultdict(list)
14+
self.transpose = defaultdict(list)
15+
self.vertexList = []
16+
17+
def addEdgeDirected(self, u, v, w = 1):
18+
self.graph[u].append([v, w])
19+
self.transpose[v].append([u, w])
20+
if u not in self.vertexList:
21+
self.vertexList.append(u)
22+
if v not in self.vertexList:
23+
self.vertexList.append(v)
24+
25+
def addEdgeUndirected(self, u, v, w = 1):
26+
self.graph[u].append([v, w])
27+
self.graph[v].append([u, w])
28+
if u not in self.vertexList:
29+
self.vertexList.append(u)
30+
if v not in self.vertexList:
31+
self.vertexList.append(v)
32+
33+
#s here is the source node
34+
def BFS(self, s):
35+
color = ['w']*len(self.graph)
36+
#Queue that will store the nodes in BFS
37+
queue = []
38+
queue.append(s)
39+
color[s] = 'g'
40+
while(queue):
41+
s = queue.pop(0) #dequeue operation
42+
color[s] = 'b'
43+
print s,
44+
for i in self.graph[s]:
45+
if (color[i] == 'w'):
46+
queue.append(i)
47+
color[i] = 'g'
48+
49+
def DFS(self, s, time = 0, startTime = {}, endTime = {}, visited = defaultdict(bool), DFSList = []):
50+
startTime[s] = time
51+
visited[s] = True
52+
DFSList.append(s)
53+
for i in self.graph[s]:
54+
if(visited[i[0]] == False):
55+
time += 1
56+
self.DFS(i[0], time, startTime, endTime, visited, DFSList)
57+
endTime[i[0]] = time
58+
return DFSList
59+
60+
61+
def TopologicalSortUtil(self, v, visited, stack):
62+
visited[v] = True
63+
print visited
64+
for i in self.graph[v]:
65+
if (visited[i[0]] == False):
66+
self.TopologicalSortUtil(i[0], visited, stack)
67+
68+
stack.insert(0, v) #adding to bottom of stack same as adding to top then printing in reverse
69+
70+
71+
def TopologicalSort(self):
72+
#self.vertexList.sort()
73+
visited = defaultdict(bool)
74+
stack = []
75+
76+
for i in self.vertexList:
77+
if (visited[i] == False):
78+
self.TopologicalSortUtil(i, visited, stack)
79+
80+
print "The Graph vertices after topological sort are:"
81+
print stack
82+
83+
def DFSUtil(self, s, visited):
84+
visited[s] = True
85+
print s,
86+
for i in self.transpose[s]:
87+
if (visited[i[0]] == False):
88+
self.DFSUtil(i[0], visited)
89+
90+
def FillOrder(self, s, visited, stack):
91+
visited[s] = True
92+
for i in self.graph[s]:
93+
if (visited[i[0]] == False):
94+
self.FillOrder(i[0], visited, stack)
95+
96+
stack.append(s)
97+
98+
def Kosarajus(self):
99+
#Step1: Create the stack
100+
stack = []
101+
visited = defaultdict(bool)
102+
for i in self.vertexList:
103+
if (visited[i] == False):
104+
self.FillOrder(i, visited, stack)
105+
106+
#Step2: Empty the stack, and print the SCC's
107+
108+
visited = defaultdict(bool)
109+
while (stack):
110+
i = stack.pop()
111+
if (visited[i] == False):
112+
self.DFSUtil(i, visited)
113+
print ""
114+
115+
#Testing on graph
116+
117+
g = Graph()
118+
g.addEdgeDirected(0, 3)
119+
g.addEdgeDirected(3, 2)
120+
g.addEdgeDirected(2, 1)
121+
g.addEdgeDirected(1, 0)
122+
g.addEdgeDirected(4, 2)
123+
g.addEdgeDirected(5, 4)
124+
125+
print "Following are strongly connected components in given graph"
126+
127+
g.Kosarajus()
128+
129+
130+

0 commit comments

Comments
 (0)