-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
86 lines (63 loc) · 2.51 KB
/
graph.py
File metadata and controls
86 lines (63 loc) · 2.51 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
class Vertex:
def __init__(self, node):
self.id = node
self.adjacent = {}
def set_neighbor(self, neighbor, cost=0):
self.adjacent[neighbor] = cost
def get_connections(self):
return self.adjacent.keys()
def get_id(self):
return self.id
def get_weight(self, neighbor):
return self.adjacent[neighbor]
class Graph:
def __init__(self):
self.vert_dict = {}
def __iter__(self):
return iter(self.vert_dict.values())
def add_vertex(self, node):
if not node in self.vert_dict:
new_vertex = Vertex(node)
self.vert_dict[node] = new_vertex
def get_vertex(self, n):
if n in self.vert_dict:
return self.vert_dict[n]
else:
return None
def add_edge(self, frm, to, cost = 0):
if frm not in self.vert_dict:
self.add_vertex(frm)
if to not in self.vert_dict:
self.add_vertex(to)
self.vert_dict[frm].set_neighbor(self.vert_dict[to], cost)
self.vert_dict[to].set_neighbor(self.vert_dict[frm], cost)
def get_vertices(self):
return self.vert_dict.keys()
# get vertex with minimum distance
def minDistance(self, dist, sptSet):
min = None
# Search nearest vertex not yet in the shortest path set
for v in self.vert_dict.values():
if dist[v.get_id()] != None and (min == None or dist[v.get_id()] < min) and not sptSet[v.get_id()]:
min = dist[v.get_id()]
min_index = v.get_id()
return self.vert_dict[min_index]
# searches shortest paths from start node to all other nodes
def dijkstra(self, start):
dist = { key: None for key in [*self.vert_dict] }
path = { key: [] for key in [*self.vert_dict] }
dist[start] = 0
path[start] = []
sptSet = { key: False for key in [*self.vert_dict] }
# repeat until all shortest paths have been found
while not np.all(list(sptSet.values())):
# closest node not yet finished
u = self.minDistance(dist, sptSet)
sptSet[u.get_id()] = True
for v in u.get_connections():
cost = u.get_weight(v)
if dist[v.get_id()] == None or dist[v.get_id()] > dist[u.get_id()] + cost:
dist[v.get_id()] = dist[u.get_id()] + cost
path[v.get_id()] = path[u.get_id()] + [u.get_id()]
return dist, path