This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathFloydWarshall.py
67 lines (43 loc) · 1.45 KB
/
FloydWarshall.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
65
66
67
# robin-0 ( Nabil Ibtehaz)
'''
This code implements the Floyd-Warshall Algorithm
to solve the all pair shortest path problem.
'''
def FloydWarshall():
global adjMatrix
for i in range(nVertices):
for j in range(nVertices):
for k in range(nVertices):
if(adjMatrix[i][k]==-1 or adjMatrix[k][j]==-1):
continue
if(adjMatrix[i][j]==-1):
adjMatrix[i][j]=adjMatrix[i][k]+adjMatrix[k][j]
else:
adjMatrix[i][j]=min(adjMatrix[i][j],adjMatrix[i][k]+adjMatrix[k][j])
return
if __name__ == "__main__":
'''
This is the main function.
A simple UI is provided
'''
nVertices = int(input('Number of Vertices: '))
adjMatrix=([([-1]*nVertices)]*nVertices)
print('Input the adjacency matrix\nPut -1 if not connected\n')
for i in range(nVertices):
adjMatrix[i]=[int(x) for x in input('').split()]
#print (adjMatrix)
FloydWarshall()
for i in range(nVertices):
for j in range(nVertices):
if(adjMatrix[i][j]==-1):
adjMatrix[i][j]="Not Connected"
print("****************************************\n")
print("Please select one of the following\n")
choice = int(input('1. Find Shortest Path Between Nodes\n2. End\n'))
while(choice==1):
u=int(input('Starting Node(0-indexed) : '))
v=int(input('Ending Node(0-indexed) : '))
print(adjMatrix[u][v])
print("****************************************\n")
print("Please select one of the following\n")
choice = int(input('1. Find Shortest Path Between Nodes\n2. End\n'))