Skip to content

Commit f3d6012

Browse files
authored
Update graphs_floyd_warshall.py
1 parent 2c3e58a commit f3d6012

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

graphs/graphs_floyd_warshall.py

+45-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,51 @@ def floyd_warshall(graph: list[list[float]], vertex: int) -> tuple:
100100
):
101101
dist[i][j] = dist[i][k] + dist[k][j]
102102
return dist, vertex
103-
104-
103+
105104
if __name__ == "__main__":
106105
import doctest
107-
108106
doctest.testmod()
107+
108+
v = int(input("Enter number of vertices: "))
109+
e = int(input("Enter number of edges: "))
110+
111+
graph = [[float("inf") for i in range(v)] for j in range(v)]
112+
113+
for i in range(v):
114+
graph[i][i] = 0.0
115+
116+
# src and dst are indices that must be within the array size graph[e][v]
117+
# failure to follow this will result in an error
118+
for i in range(e):
119+
print("\nEdge ", i + 1)
120+
src = int(input("Enter source:"))
121+
dst = int(input("Enter destination:"))
122+
weight = float(input("Enter weight:"))
123+
graph[src][dst] = weight
124+
125+
floyd_warshall(graph, v)
126+
127+
# Example Input
128+
# Enter number of vertices: 3
129+
# Enter number of edges: 2
130+
131+
# # generated graph from vertex and edge inputs
132+
# [[inf, inf, inf], [inf, inf, inf], [inf, inf, inf]]
133+
# [[0.0, inf, inf], [inf, 0.0, inf], [inf, inf, 0.0]]
134+
135+
# specify source, destination and weight for edge #1
136+
# Edge 1
137+
# Enter source:1
138+
# Enter destination:2
139+
# Enter weight:2
140+
141+
# specify source, destination and weight for edge #2
142+
# Edge 2
143+
# Enter source:2
144+
# Enter destination:1
145+
# Enter weight:1
146+
147+
# # Expected Output from the vertice, edge and src, dst, weight inputs!!
148+
# 0 INF INF
149+
# INF 0 2
150+
# INF 1 0

0 commit comments

Comments
 (0)