1
1
from typing import List
2
2
3
+
3
4
def print_distances (distances : List [float ]) -> None :
4
5
print ("\n Vertex Distance" )
5
6
for vertex , distance in enumerate (distances ):
6
7
print (f"{ vertex } \t { int (distance ) if distance != float ('inf' ) else 'INF' } " )
7
8
9
+
8
10
def find_min_distance (distances : List [float ], visited : List [bool ]) -> int :
9
11
min_distance = float ("inf" )
10
12
min_index = - 1
11
-
13
+
12
14
for i in range (len (distances )):
13
15
if not visited [i ] and distances [i ] < min_distance :
14
16
min_distance = distances [i ]
15
17
min_index = i
16
-
18
+
17
19
return min_index
18
20
21
+
19
22
def dijkstra (graph : List [List [float ]], num_vertices : int , source : int ) -> List [float ]:
20
23
distances = [float ("inf" )] * num_vertices
21
24
visited = [False ] * num_vertices
@@ -26,17 +29,21 @@ def dijkstra(graph: List[List[float]], num_vertices: int, source: int) -> List[f
26
29
visited [u ] = True
27
30
28
31
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
+ ):
31
37
distances [v ] = distances [u ] + graph [u ][v ]
32
38
33
39
return distances
34
40
41
+
35
42
def is_connected (graph : List [List [float ]], num_vertices : int ) -> bool :
36
43
visited = [False ] * num_vertices
37
44
stack = [0 ] # Start DFS from the first vertex
38
45
visited [0 ] = True
39
-
46
+
40
47
while stack :
41
48
node = stack .pop ()
42
49
for neighbor in range (num_vertices ):
@@ -46,38 +53,43 @@ def is_connected(graph: List[List[float]], num_vertices: int) -> bool:
46
53
47
54
return all (visited )
48
55
56
+
49
57
def main () -> None :
50
58
V = int (input ("Enter number of vertices: " ).strip ())
51
59
if V <= 0 :
52
60
print ("Error: The number of vertices must be greater than 0." )
53
61
return
54
-
62
+
55
63
E = int (input ("Enter number of edges (must be >= V-1): " ).strip ())
56
64
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
+ )
58
68
return
59
-
69
+
60
70
graph = [[float ("inf" )] * V for _ in range (V )]
61
-
71
+
62
72
for i in range (V ):
63
73
graph [i ][i ] = 0.0
64
74
65
75
for i in range (E ):
66
76
print (f"\n Edge { i + 1 } " )
67
77
src = int (input (f"Enter source (0 to { V - 1 } ): " ).strip ())
68
78
dst = int (input (f"Enter destination (0 to { V - 1 } ): " ).strip ())
69
-
79
+
70
80
if src < 0 or src >= V or dst < 0 or dst >= V :
71
81
print ("Error: Source and destination must be valid vertex indices." )
72
82
return
73
-
83
+
74
84
weight = float (input ("Enter weight (non-negative): " ).strip ())
75
85
if weight < 0 :
76
86
print ("Error: Weight must be non-negative." )
77
87
return
78
-
88
+
79
89
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
+ )
81
93
82
94
# Handle duplicate edges: (optionally overwrite or warn)
83
95
if graph [src ][dst ] != float ("inf" ):
@@ -86,7 +98,9 @@ def main() -> None:
86
98
graph [src ][dst ] = weight
87
99
88
100
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
+ )
90
104
91
105
gsrc = int (input (f"\n Enter shortest path source (0 to { V - 1 } ): " ).strip ())
92
106
if gsrc < 0 or gsrc >= V :
@@ -96,5 +110,6 @@ def main() -> None:
96
110
distances = dijkstra (graph , V , gsrc )
97
111
print_distances (distances )
98
112
113
+
99
114
if __name__ == "__main__" :
100
115
main ()
0 commit comments