Skip to content

Commit 451c132

Browse files
Add doctests to dijkstra_2.py
1 parent f8fcd55 commit 451c132

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎graphs/dijkstra_2.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def print_dist(dist, v):
99

1010

1111
def min_dist(mdist, vset, v):
12+
"""
13+
Returns the vertex with the minimum distance from the source vertex\
14+
that has not been visited.
15+
16+
>>> min_dist([0, 1, 6], [True, False, False], 3)
17+
1
18+
"""
1219
min_val = float("inf")
1320
min_ind = -1
1421
for i in range(v):
@@ -19,6 +26,20 @@ def min_dist(mdist, vset, v):
1926

2027

2128
def dijkstra(graph, v, src):
29+
"""
30+
Calculate the shortest path from source to all other vertices\
31+
using Dijkstra's algorithm.
32+
33+
>>> graph = [[0.0, 1.0, 6.0],\
34+
[float("inf"), 0.0, 3.0],\
35+
[float("inf"), float("inf"), 0.0]]
36+
>>> dijkstra(graph, 3, 0) # doctest: +NORMALIZE_WHITESPACE
37+
<BLANKLINE>
38+
Vertex Distance
39+
0 0
40+
1 1
41+
2 4
42+
"""
2243
mdist = [float("inf") for _ in range(v)]
2344
vset = [False for _ in range(v)]
2445
mdist[src] = 0.0

0 commit comments

Comments
 (0)