-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDijkstraNaive.py
More file actions
40 lines (30 loc) · 1.2 KB
/
Copy pathDijkstraNaive.py
File metadata and controls
40 lines (30 loc) · 1.2 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
import os
from qgis.networkanalysis import *
''' Implementation of Dijkstra-Algorithm first introduced by E. Dijkstra --> Naive Implementation'''
''' Temporaray nodes are organised in an unordered list'''
from GraphAnalyzer_extended import *
def dijkstraNaive(graph, s_idx):
global l, F
n = graph.vertexCount()
# F: tree, l: cost
F = []; l = []; k={}
Temp = [s_idx]
for i in range(0, n):
l.append(float('inf'))
l[s_idx] = 0
for i in range(0, n):
F.append(-1)
while Temp != []:
lv, v_idx = min([(l[node], node) for node in Temp])
Temp.remove(v_idx)
if v_idx != s_idx:
F[v_idx] = k[v_idx]
for neighb in neighborhood(graph, v_idx):
if l[neighb] == float('inf') :
Temp.append(neighb)
l[neighb] = l[v_idx] + weight(graph, v_idx, neighb)
k[neighb]= findEdge(graph, v_idx, neighb)
elif l[v_idx] + weight(graph, v_idx, neighb) < l[neighb]:
l[neighb] = l[v_idx] + weight(graph, v_idx, neighb)
k[neighb] = findEdge(graph, v_idx, neighb)
return F, l