-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshortest_path_first.py
60 lines (48 loc) · 1.92 KB
/
shortest_path_first.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
"""
Dijkstra's Algo
"""
from queue import Queue
class Graph:
def __init__(self, vertices):
self.vertices = vertices
self.visited = [False for v in range(vertices)]
self.graph = [[] for i in range(vertices)]
def add_edge(self, vertex, adjacent_vertex, weight):
self.graph.append(vertex)
#self.graph[vertex].append((adjacent_vertex, weight))
self.graph[vertex].append({"vertex": adjacent_vertex, "weight": weight})
def print_shortest_path(self, source_vertex, target_vertex):
path = []
distance = 0
queue = Queue()
queue.put(source_vertex)
path.append(source_vertex)
while not queue.empty():
current_vertex = queue.get()
if target_vertex == current_vertex:
break;
if not self.visited[current_vertex]:
next_vertex = {'vertex': float('inf'), 'weight': float('inf')}
# find next vertex with minimum distance
for adjacent_vertex in self.graph[current_vertex]:
if next_vertex['weight'] > adjacent_vertex['weight']:
next_vertex = adjacent_vertex
queue.put(next_vertex['vertex'])
path.append(next_vertex['vertex'])
distance = distance + next_vertex['weight']
self.visited[current_vertex] = True
print("shortest path = " + str(path) + ", minimum distance = " + str(distance))
def print_graph(self):
for v in range(self.vertices):
print("vertex = " + str(v) + " edge = " + str(self.graph[v]) , end = " ")
print()
graph = Graph(6)
graph.add_edge(0, 1, 4)
graph.add_edge(0, 2, 2)
graph.add_edge(1, 2, 5)
graph.add_edge(1, 3, 10)
graph.add_edge(2, 4, 3)
graph.add_edge(4, 3, 4)
graph.add_edge(3, 5, 11)
graph.print_graph()
graph.print_shortest_path(0, 5)