Skip to content

Commit 5994f00

Browse files
committed
Dijkstra
1 parent a61d656 commit 5994f00

File tree

13 files changed

+94
-79
lines changed

13 files changed

+94
-79
lines changed

.idea/Problem-Solving-with-Algorithms-and-Data-Structures-using-Python.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/dijkstra/priority-queue-impl-adjacency-map/.idea/.gitignore

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/dijkstra/priority-queue-impl-adjacency-map/.idea/inspectionProfiles/profiles_settings.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/dijkstra/priority-queue-impl-adjacency-map/.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/dijkstra/priority-queue-impl-adjacency-map/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

graphs/dijkstra/priority-queue-impl-adjacency-map/.idea/priority-queue-impl-adjacency-map.iml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.

graphs/dijkstra/priority-queue-impl-adjacency-map/graph.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,38 @@
55
class Graph:
66
def __init__(self):
77
self._vertices: dict = {}
8-
self._prev: dict = {}
98
self._adjacency_map: dict = {}
9+
self._prev: dict = {}
1010

11-
def add_vertex(self, label: str = None, weight: int = float("inf")):
12-
self._vertices[label] = Vertex(label)
11+
def add_vertex(self, label: str):
12+
v: Vertex = Vertex(label)
13+
self._vertices[label] = v
14+
self._adjacency_map[label]: list = []
1315
self._prev[label] = None
14-
self._adjacency_map[label]: dict = {}
1516

16-
def add_edge(self, label1: str = None, label2: str = None, weight: int = float("inf")):
17-
self._adjacency_map[label1][label2] = Vertex(label2, weight)
17+
def add_edge(self, label1: str, label2: str, weight: int):
18+
self._adjacency_map[label1].append(Vertex(label2, weight))
1819

1920
def dijkstra(self, label: str):
20-
self._vertices[label].set_weight(0)
21+
self._vertices[label].weight = 0
2122
pq: PriorityQueue = PriorityQueue()
22-
for vertex_label in self._vertices:
23-
pq.insert(self._vertices[vertex_label])
23+
for label in self._vertices:
24+
pq.insert(self._vertices[label])
2425
while not pq.is_empty():
25-
vertex: Vertex = pq.delete_min()
26-
for neighbour_label in self._adjacency_map[vertex.get_label()]:
27-
neighbour_from_adjacency_map: Vertex = self._adjacency_map[vertex.get_label()][neighbour_label]
28-
neighbour_from_vertices: Vertex = self._vertices[neighbour_label]
29-
if neighbour_from_vertices.get_weight() > \
30-
vertex.get_weight() + neighbour_from_adjacency_map.get_weight():
31-
neighbour_from_vertices.set_weight(vertex.get_weight() + neighbour_from_adjacency_map.get_weight())
32-
self._prev[neighbour_from_vertices.get_label()] = vertex.get_label()
33-
pq.decrease_key(neighbour_from_vertices.get_key())
34-
35-
def return_path(self, end_label: str = None) -> str:
36-
if self._prev[end_label] is None:
37-
return end_label
26+
current: Vertex = pq.delete_min()
27+
for neighbour in self._adjacency_map[current.label]:
28+
v: Vertex = self._vertices[neighbour.label]
29+
if current.weight + neighbour.weight < v.weight:
30+
v.weight = current.weight + neighbour.weight
31+
self._prev[v.label] = current.label
32+
pq.decrease_key(v.key)
33+
34+
def show_path(self, label: str) -> str:
35+
if self._prev[label] is None:
36+
return label
3837
else:
39-
return self.return_path(self._prev[end_label]) + " -> " + end_label
38+
return self.show_path(self._prev[label]) + " -> " + label
39+
4040

4141

4242

0 commit comments

Comments
 (0)