Skip to content

Commit c608b68

Browse files
PranavTamadapre-commit-ci[bot]cclauss
authored
Tests/doctests dijkstra 2 (#13036)
* Update dijkstra_2.py * Update dijkstra_2.py * Update dijkstra_2.py * Update dijkstra_2.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dijkstra_2.py * Converting tab indents to spaces --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 19524f3 commit c608b68

1 file changed

Lines changed: 56 additions & 9 deletions

File tree

‎graphs/dijkstra_2.py‎

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,41 @@
11
def print_dist(dist, v) -> None:
2-
print("\nVertex Distance")
2+
"""
3+
Print vertex distances.
4+
>>> print_dist([0.0, 5.0, 8.0, 9.0], 4)
5+
Vertex Distance
6+
0 0
7+
1 5
8+
2 8
9+
3 9
10+
>>> print_dist([0.0, float('inf')], 2)
11+
Vertex Distance
12+
0 0
13+
1 INF
14+
>>> print_dist([0.0], 1)
15+
Vertex Distance
16+
0 0
17+
"""
18+
print("Vertex Distance")
319
for i in range(v):
420
if dist[i] != float("inf"):
5-
print(i, "\t", int(dist[i]), end="\t")
21+
print(i, " ", int(dist[i]))
622
else:
7-
print(i, "\t", "INF", end="\t")
8-
print()
23+
print(i, " ", "INF")
924

1025

1126
def min_dist(mdist, vset, v):
1227
"""
13-
Returns the vertex with the minimum distance from the source vertex\
14-
that has not been visited.
28+
Returns the vertex with the minimum distance from the source vertex
29+
that has not yet been visited.
1530
31+
>>> min_dist([0, 4, 2, float('inf')], [True, False, False, False], 4)
32+
2
33+
>>> min_dist([0, 4, 2, 1], [True, False, True, False], 4)
34+
3
35+
>>> min_dist([0, 4, 2, 1], [True, True, True, True], 4)
36+
-1
37+
>>> min_dist([float('inf'), float('inf')], [False, False], 2)
38+
-1
1639
>>> min_dist([0, 1, 6], [True, False, False], 3)
1740
1
1841
"""
@@ -27,14 +50,38 @@ def min_dist(mdist, vset, v):
2750

2851
def dijkstra(graph, v, src) -> None:
2952
"""
30-
Calculate the shortest path from source to all other vertices\
31-
using Dijkstra's algorithm.
53+
Runs Dijkstra's algorithm and prints distances.
3254
55+
Calculate the shortest path from source to all other vertices
56+
using Dijkstra's algorithm.
57+
58+
>>> g = [
59+
... [0.0, 5.0, float('inf'), 10.0],
60+
... [float('inf'), 0.0, 3.0, float('inf')],
61+
... [float('inf'), float('inf'), 0.0, 1.0],
62+
... [float('inf'), float('inf'), float('inf'), 0.0],
63+
... ]
64+
>>> dijkstra(g, 4, 0)
65+
Vertex Distance
66+
0 0
67+
1 5
68+
2 8
69+
3 9
70+
>>> g2 = [
71+
... [0.0, float('inf')],
72+
... [float('inf'), 0.0],
73+
... ]
74+
>>> dijkstra(g2, 2, 0)
75+
Vertex Distance
76+
0 0
77+
1 INF
78+
>>> dijkstra([[0.0]], 1, 0)
79+
Vertex Distance
80+
0 0
3381
>>> graph = [[0.0, 1.0, 6.0],\
3482
[float("inf"), 0.0, 3.0],\
3583
[float("inf"), float("inf"), 0.0]]
3684
>>> dijkstra(graph, 3, 0) # doctest: +NORMALIZE_WHITESPACE
37-
<BLANKLINE>
3885
Vertex Distance
3986
0 0
4087
1 1

0 commit comments

Comments
 (0)