From 451c132435509a894559b21cfd9bf5628c7c5953 Mon Sep 17 00:00:00 2001 From: Pratyush Banerjee Date: Sun, 6 Oct 2024 16:11:03 +0530 Subject: [PATCH] Add doctests to dijkstra_2.py --- graphs/dijkstra_2.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/graphs/dijkstra_2.py b/graphs/dijkstra_2.py index 68102cc55..a50f09934 100644 --- a/graphs/dijkstra_2.py +++ b/graphs/dijkstra_2.py @@ -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 + + 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