-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDijkstra.py
40 lines (35 loc) · 1.41 KB
/
Dijkstra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import osmnx as ox
from data.PriorityQueue import PriorityQueue
class dijkstraGraph():
def __init__(self, graph):
self.graph = graph
def dijkstra(self, start, to_find):
toVisit = PriorityQueue()
toVisit.put(start, 0)
visitedFromVertex = {}
costToVertex= {}
visitedFromVertex[start] = None
costToVertex[start] = 0
vertices_explored = 0
edges_explored = 0
while not toVisit.empty():
current = toVisit.get()
vertices_explored += 1
if current == to_find:
path = []
while current != start:
path.append(current)
current = visitedFromVertex[current]
path.append(start)
path.reverse()
return path, vertices_explored, edges_explored, costToVertex[to_find]
for neighbor in list(self.graph.neighbors(current)):
distance = self.graph[current][neighbor][0]["length"]
newCost = costToVertex[current] + distance
edges_explored += 1
if neighbor not in costToVertex or newCost < costToVertex[neighbor]:
costToVertex[neighbor] = newCost
priority = newCost
toVisit.put(neighbor, priority)
visitedFromVertex[neighbor] = current
return