-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdijkstra.py
38 lines (25 loc) · 922 Bytes
/
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
import math
def select(candidates_set, cost):
r = math.inf
vertex_selected = None
for vertex in candidates_set:
if cost[vertex] < r:
r = cost[vertex]
vertex_selected = vertex
return vertex_selected
def dijkstra(cost_matrix, number_of_vertices, initial_vertex):
candidate_set = []
cost = [0]*number_of_vertices-1
path = [0]*number_of_vertices-1
for i in range(0,number_of_vertices):
candidate_set.append(i)
cost[i] = cost_matrix[initial_vertex, i]
path[i] = initial_vertex
candidate_set.remove(initial_vertex)
while candidate_set.count() != 0:
k = select(candidate_set, cost)
candidate_set.remove(k)
for vertex in candidate_set:
if cost[k] + cost_matrix[k, vertex] < cost[vertex]:
cost[vertex] = cost[k] + cost_matrix[k, vertex]
path[vertex] = k