Skip to content

Commit 50eb8c9

Browse files
Add doctests to dijkstra_2.py (#11823)
* Fix arg for print_dist function * Add doctests to dijkstra_2.py * Add polars dependency to pyproject.toml --------- Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 6421e9c commit 50eb8c9

2 files changed

Lines changed: 23 additions & 1 deletion

File tree

graphs/dijkstra_2.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ def print_dist(dist, v) -> None:
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) -> None:
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
@@ -35,7 +56,7 @@ def dijkstra(graph, v, src) -> None:
3556
):
3657
mdist[i] = mdist[u] + graph[u][i]
3758

38-
print_dist(mdist, i)
59+
print_dist(mdist, v)
3960

4061

4162
if __name__ == "__main__":

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ dependencies = [
2222
"numpy>=2.1.3",
2323
"pandas>=2.3.3",
2424
"pillow>=11.3",
25+
"polars>=1.44.2",
2526
"rich>=13.9.4",
2627
"scikit-learn>=1.9.1",
2728
"scipy>=1.18.1",

0 commit comments

Comments
 (0)