Skip to content

Commit 4829fea

Browse files
AtulRajput01pre-commit-ci[bot]cclauss
authored
Create graphs/dijkstra_alternate.py (TheAlgorithms#7405)
* Update dijkstra.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dijkstra.py * Update graphs/dijkstra.py Co-authored-by: Christian Clauss <[email protected]> * Update graphs/dijkstra.py Co-authored-by: Christian Clauss <[email protected]> * Update graphs/dijkstra.py Co-authored-by: Christian Clauss <[email protected]> * Update dijkstra.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dijkstra.py * Apply suggestions from code review * Create dijkstra_alternate.py * Update dijkstra.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * int(1e7) * Update dijkstra_alternate.py * Update graphs/dijkstra_alternate.py * sptset --> visited Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 2859d4b commit 4829fea

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

graphs/dijkstra_alternate.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from __future__ import annotations
2+
3+
4+
class Graph:
5+
def __init__(self, vertices: int) -> None:
6+
"""
7+
>>> graph = Graph(2)
8+
>>> graph.vertices
9+
2
10+
>>> len(graph.graph)
11+
2
12+
>>> len(graph.graph[0])
13+
2
14+
"""
15+
self.vertices = vertices
16+
self.graph = [[0] * vertices for _ in range(vertices)]
17+
18+
def print_solution(self, distances_from_source: list[int]) -> None:
19+
"""
20+
>>> Graph(0).print_solution([]) # doctest: +NORMALIZE_WHITESPACE
21+
Vertex Distance from Source
22+
"""
23+
print("Vertex \t Distance from Source")
24+
for vertex in range(self.vertices):
25+
print(vertex, "\t\t", distances_from_source[vertex])
26+
27+
def minimum_distance(
28+
self, distances_from_source: list[int], visited: list[bool]
29+
) -> int:
30+
"""
31+
A utility function to find the vertex with minimum distance value, from the set
32+
of vertices not yet included in shortest path tree.
33+
34+
>>> Graph(3).minimum_distance([1, 2, 3], [False, False, True])
35+
0
36+
"""
37+
38+
# Initialize minimum distance for next node
39+
minimum = 1e7
40+
min_index = 0
41+
42+
# Search not nearest vertex not in the shortest path tree
43+
for vertex in range(self.vertices):
44+
if distances_from_source[vertex] < minimum and visited[vertex] is False:
45+
minimum = distances_from_source[vertex]
46+
min_index = vertex
47+
return min_index
48+
49+
def dijkstra(self, source: int) -> None:
50+
"""
51+
Function that implements Dijkstra's single source shortest path algorithm for a
52+
graph represented using adjacency matrix representation.
53+
54+
>>> Graph(4).dijkstra(1) # doctest: +NORMALIZE_WHITESPACE
55+
Vertex Distance from Source
56+
0 10000000
57+
1 0
58+
2 10000000
59+
3 10000000
60+
"""
61+
62+
distances = [int(1e7)] * self.vertices # distances from the source
63+
distances[source] = 0
64+
visited = [False] * self.vertices
65+
66+
for _ in range(self.vertices):
67+
u = self.minimum_distance(distances, visited)
68+
visited[u] = True
69+
70+
# Update dist value of the adjacent vertices
71+
# of the picked vertex only if the current
72+
# distance is greater than new distance and
73+
# the vertex in not in the shortest path tree
74+
for v in range(self.vertices):
75+
if (
76+
self.graph[u][v] > 0
77+
and visited[v] is False
78+
and distances[v] > distances[u] + self.graph[u][v]
79+
):
80+
distances[v] = distances[u] + self.graph[u][v]
81+
82+
self.print_solution(distances)
83+
84+
85+
if __name__ == "__main__":
86+
graph = Graph(9)
87+
graph.graph = [
88+
[0, 4, 0, 0, 0, 0, 0, 8, 0],
89+
[4, 0, 8, 0, 0, 0, 0, 11, 0],
90+
[0, 8, 0, 7, 0, 4, 0, 0, 2],
91+
[0, 0, 7, 0, 9, 14, 0, 0, 0],
92+
[0, 0, 0, 9, 0, 10, 0, 0, 0],
93+
[0, 0, 4, 14, 10, 0, 2, 0, 0],
94+
[0, 0, 0, 0, 0, 2, 0, 1, 6],
95+
[8, 11, 0, 0, 0, 0, 1, 0, 7],
96+
[0, 0, 2, 0, 0, 0, 6, 7, 0],
97+
]
98+
graph.dijkstra(0)

0 commit comments

Comments
 (0)