-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPractical_7.py
65 lines (57 loc) · 1.66 KB
/
Practical_7.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def get_connectivity_lst(i,lst,lst2):
if i not in lst.keys():
return False
nodes=lst[i]
for j in range(len(nodes)):
if nodes[j][1] not in lst2:
lst2.append(nodes[j][1])
lst2=get_connectivity_lst(nodes[j][1],lst,lst2)
return lst2
def check_connectivity(graph):
lst=dict()
for i in range(len(graph)):
for j in range(len(graph)):
if graph[i][j]==1:
if i not in lst.keys():
lst[i]=[[i,j]]
else:
lst[i].append([i,j])
print(lst)
for i in lst.keys():
lst2=list()
lst2.append(i)
lst2=get_connectivity_lst(i,lst,lst2)
print(lst2)
if lst2==False or len(lst2)!=len(graph):
return False
return True
def print_graph(graph):
n=len(graph)
print(' ',end='')
[print(f'\033[1;33;40m{x}',end=" ") for x in range(1,n+1)]
print('\n')
for i in range(1,n+1):
print(f'\033[1;31;40m{i}\033[1;37;40m',end=" ")
for j in range(1,n+1):
print(f'{graph[i-1][j-1]}',end=" ")
print("\n")
#Driver Code
if True:
#Strongly Connected Graph
graph=[[0,0,0,1,1],
[1,0,0,0,0],
[0,0,0,1,0],
[0,1,1,0,0],
[1,0,0,0,0]]
#Strongly Connected Graph
# graph=[[0,0,0,1],
# [1,0,0,0],
# [0,0,0,1],
# [0,1,1,0]]
print_graph(graph)
print("\033[1;37;40m")
if check_connectivity(graph):
print('The graph is strongly connected.')
else:
print('The graph is not strongly connected.')
print("\033[0;37;40m")