11from typing import List
22
3+
34def print_distances (distances : List [float ]) -> None :
45 print ("\n Vertex Distance" )
56 for vertex , distance in enumerate (distances ):
67 print (f"{ vertex } \t { int (distance ) if distance != float ('inf' ) else 'INF' } " )
78
9+
810def find_min_distance (distances : List [float ], visited : List [bool ]) -> int :
911 min_distance = float ("inf" )
1012 min_index = - 1
11-
13+
1214 for i in range (len (distances )):
1315 if not visited [i ] and distances [i ] < min_distance :
1416 min_distance = distances [i ]
1517 min_index = i
16-
18+
1719 return min_index
1820
21+
1922def dijkstra (graph : List [List [float ]], num_vertices : int , source : int ) -> List [float ]:
2023 distances = [float ("inf" )] * num_vertices
2124 visited = [False ] * num_vertices
@@ -26,17 +29,21 @@ def dijkstra(graph: List[List[float]], num_vertices: int, source: int) -> List[f
2629 visited [u ] = True
2730
2831 for v in range (num_vertices ):
29- if (not visited [v ] and graph [u ][v ] != float ("inf" )
30- and distances [u ] + graph [u ][v ] < distances [v ]):
32+ if (
33+ not visited [v ]
34+ and graph [u ][v ] != float ("inf" )
35+ and distances [u ] + graph [u ][v ] < distances [v ]
36+ ):
3137 distances [v ] = distances [u ] + graph [u ][v ]
3238
3339 return distances
3440
41+
3542def is_connected (graph : List [List [float ]], num_vertices : int ) -> bool :
3643 visited = [False ] * num_vertices
3744 stack = [0 ] # Start DFS from the first vertex
3845 visited [0 ] = True
39-
46+
4047 while stack :
4148 node = stack .pop ()
4249 for neighbor in range (num_vertices ):
@@ -46,38 +53,43 @@ def is_connected(graph: List[List[float]], num_vertices: int) -> bool:
4653
4754 return all (visited )
4855
56+
4957def main () -> None :
5058 V = int (input ("Enter number of vertices: " ).strip ())
5159 if V <= 0 :
5260 print ("Error: The number of vertices must be greater than 0." )
5361 return
54-
62+
5563 E = int (input ("Enter number of edges (must be >= V-1): " ).strip ())
5664 if E < V - 1 :
57- print (f"Error: The number of edges must be at least { V - 1 } for a connected graph." )
65+ print (
66+ f"Error: The number of edges must be at least { V - 1 } for a connected graph."
67+ )
5868 return
59-
69+
6070 graph = [[float ("inf" )] * V for _ in range (V )]
61-
71+
6272 for i in range (V ):
6373 graph [i ][i ] = 0.0
6474
6575 for i in range (E ):
6676 print (f"\n Edge { i + 1 } " )
6777 src = int (input (f"Enter source (0 to { V - 1 } ): " ).strip ())
6878 dst = int (input (f"Enter destination (0 to { V - 1 } ): " ).strip ())
69-
79+
7080 if src < 0 or src >= V or dst < 0 or dst >= V :
7181 print ("Error: Source and destination must be valid vertex indices." )
7282 return
73-
83+
7484 weight = float (input ("Enter weight (non-negative): " ).strip ())
7585 if weight < 0 :
7686 print ("Error: Weight must be non-negative." )
7787 return
78-
88+
7989 if src == dst :
80- print ("Warning: Self-loop detected; it will be allowed but consider its implications." )
90+ print (
91+ "Warning: Self-loop detected; it will be allowed but consider its implications."
92+ )
8193
8294 # Handle duplicate edges: (optionally overwrite or warn)
8395 if graph [src ][dst ] != float ("inf" ):
@@ -86,7 +98,9 @@ def main() -> None:
8698 graph [src ][dst ] = weight
8799
88100 if not is_connected (graph , V ):
89- print ("Warning: The graph is not connected. Dijkstra's algorithm may not yield valid results for all vertices." )
101+ print (
102+ "Warning: The graph is not connected. Dijkstra's algorithm may not yield valid results for all vertices."
103+ )
90104
91105 gsrc = int (input (f"\n Enter shortest path source (0 to { V - 1 } ): " ).strip ())
92106 if gsrc < 0 or gsrc >= V :
@@ -96,5 +110,6 @@ def main() -> None:
96110 distances = dijkstra (graph , V , gsrc )
97111 print_distances (distances )
98112
113+
99114if __name__ == "__main__" :
100115 main ()
0 commit comments