Add doctests to dijkstra_2.py

This commit is contained in:
Pratyush Banerjee 2024-10-06 16:11:03 +05:30
parent f8fcd5572c
commit 451c132435

View File

@ -9,6 +9,13 @@ def print_dist(dist, v):
def min_dist(mdist, vset, v):
"""
Returns the vertex with the minimum distance from the source vertex\
that has not been visited.
>>> min_dist([0, 1, 6], [True, False, False], 3)
1
"""
min_val = float("inf")
min_ind = -1
for i in range(v):
@ -19,6 +26,20 @@ def min_dist(mdist, vset, v):
def dijkstra(graph, v, src):
"""
Calculate the shortest path from source to all other vertices\
using Dijkstra's algorithm.
>>> graph = [[0.0, 1.0, 6.0],\
[float("inf"), 0.0, 3.0],\
[float("inf"), float("inf"), 0.0]]
>>> dijkstra(graph, 3, 0) # doctest: +NORMALIZE_WHITESPACE
<BLANKLINE>
Vertex Distance
0 0
1 1
2 4
"""
mdist = [float("inf") for _ in range(v)]
vset = [False for _ in range(v)]
mdist[src] = 0.0