Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion graphs/dijkstra_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ def print_dist(dist, v) -> None:


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):
Expand All @@ -19,6 +26,20 @@ def min_dist(mdist, vset, v):


def dijkstra(graph, v, src) -> None:
"""
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
Expand All @@ -35,7 +56,7 @@ def dijkstra(graph, v, src) -> None:
):
mdist[i] = mdist[u] + graph[u][i]

print_dist(mdist, i)
print_dist(mdist, v)


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = [
"numpy>=2.1.3",
"pandas>=2.3.3",
"pillow>=11.3",
"polars>=1.44.2",
"rich>=13.9.4",
"scikit-learn>=1.9.1",
"scipy>=1.18.1",
Expand Down
Loading