-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.py
71 lines (52 loc) · 1.67 KB
/
graph.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
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
import heapq
class Graph:
V = None
E = None
def __init__(self):
self.V = {}
self.E = []
def addVertex(self,v):
if v not in self.V:
self.V[v] = []
def addEdge(self, e):
if (e[0] not in self.V):
self.V[e[0]] = []
if (e[1] not in self.V):
self.V[e[1]] = []
self.V[e[0]].append((e[1],e[2]))
self.V[e[1]].append((e[0],e[2]))
self.E.append(e)
def path(self,prev,v1,v2):
v = v2
path = [v]
while v != v1:
path.append(prev[v])
v = prev[v]
path.reverse()
return path
def djikstra(self,v1,v2):
dist = {i:1e9 for i in self.V}
dist[v1] = 0
unvisited = set(self.V)
prev = {}
pathNodes = {}
heap = [(0,(v1,None))]
while (unvisited):
min_node = None
min_node = heapq.heappop(heap)[1]
if (min_node[0] not in unvisited):
continue
unvisited.remove(min_node[0])
prev[min_node[0]] = min_node[1]
if (min_node[0] == v2):
return self.path(prev,v1,v2)
for neighbor in self.V[min_node[0]]:
if (neighbor[0] in unvisited):
#print(neighbor[0])
this_dist = dist[min_node[0]] + neighbor[1]
if (this_dist < dist[neighbor[0]]):
dist[neighbor[0]] = this_dist
heapq.heappush(heap, (dist[neighbor[0]], (neighbor[0], min_node[0])))
def print(self):
print(self.V)
print(self.E)