-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshd.py
executable file
·67 lines (55 loc) · 1.78 KB
/
shd.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 sys, heapq
class Vertex:
'''
Node in a Graph
'''
def __init__(self, name):
self.name = name
self.visited = False
self.predecessor = None
self.min_distance = sys.maxsize
self.adjancency_list = []
def __cmp__(self, other):
return self.cmp(self.min_distance, other.min_distance)
def __lt__(self, other):
return self.min_distance < other.min_distance
class Edge:
'''
Edge b/w 2 Vertices of a Graph
'''
def __init__(self, vertex1, vertex2):
self.v1 = vertex1
self.v2 = vertex2
def get_shortest_path(vertex_list, start_vertex):
heap = []
start_vertex.min_distance = 0
heapq.heappush(heap, start_vertex)
while len(heap) > 0:
current_vertex = heapq.heappop(heap)
for edge in current_vertex.adjancency_list:
u = edge.v1
v = edge.v2
new_distance = u.min_distance + 1
if new_distance < v.min_distance:
v.predecessor = u
v.min_distance = new_distance
heapq.heappush(heap, v)
def get_shortest_path_to(target_vertex):
print("Shortest path to ", target_vertex.name, " is ", target_vertex.min_distance)
node = target_vertex
while node is not None:
print("%s -> " % node.name)
node = node.predecessor
n1, n2, n3 = Vertex('A'), Vertex('B'), Vertex('C')
e1, e2, e3 = Edge(n1, n2), Edge(n2, n3), Edge(n1, n3)
n1.adjancency_list.append(e1)
n1.adjancency_list.append(e2)
n2.adjancency_list.append(e1)
n2.adjancency_list.append(e2)
n3.adjancency_list.append(e2)
n3.adjancency_list.append(e3)
vertex_list = (n1, n2, n3)
source = int(input())
dest = int(input())
get_shortest_path(vertex_list, vertex_list[source])
get_shortest_path_to(vertex_list[dest])