-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkosarajus_alg.py
More file actions
44 lines (42 loc) · 1.03 KB
/
kosarajus_alg.py
File metadata and controls
44 lines (42 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def DFS(X,s):
visited = [False]*(n)
visited[s] = True
stack = []
stack.append(s)
while stack:
x = stack.pop(len(stack)-1) #Because its DFS traversal...
#print(x)
for v in range(n):
if X[x][v] and not visited[v]:
visited[v] = True
stack.append(v)
for i in range(n): #Checking for connectivity...
if visited[i] != True:
return False
return True #Strongly connected...
L = []
n = int(input("Enter n value : "))
print("Enter adj_matrix : ")
for i in range(n):
L.append([])
for j in range(n):
L[i].append(int(input()))
#print(L)
a = DFS(L,0)
A = [] #Transpose of L...
u = n-1
for i in range(n):
A.append([])
v = n-1
for j in range(n):
A[i].append(L[u][v])
v -= 1
u -= 1
#print(A)
b = DFS(A,0)
print(a,"First\n")
print(b,"second\n")
if a==True and b==True:
print("Yes") #Strongly connected...
else:
print("No") #Not strongly connected...