-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdijkstra.py
More file actions
60 lines (57 loc) · 1.56 KB
/
dijkstra.py
File metadata and controls
60 lines (57 loc) · 1.56 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding:utf-8 -*-
#fuction: using DP
#Author: Toryun
import heapq
import math
graph = {
"A": {"B":5,"C":1},
"B": {"A":5,"C":2,"D":1},
"C": {"A":1,"B":2,"D":4,"E":8},
"D": {"B":1,"C":4,"E":3,"F":6},
"E": {"C":8,"D":3},
"F": {"D":6}
}
def __init__distance(graph,s):
#初始化距离字典
distance = {s:0}
for vertex in graph:
if vertex != s:
distance[vertex] = float('inf')
return distance
def get_key (dict, value):
for k, v in dict.items():
if v == value:
return k
def dijkstra(graph,distance,s):
pqueue = []
seen = set()
parent = {s:None}
heapq.heappush(pqueue,(0,s))
while (len(pqueue)>0):
pair = heapq.heappop(pqueue)
#print pqueue
dist = pair[0]
vertex = pair[1]
seen.add(vertex)
nodes = graph[vertex].keys()
#print nodes
for w in nodes:
if w not in seen:
if dist + graph[vertex][w] < distance[w]:
heapq.heappush(pqueue,(dist + graph[vertex][w],w))
parent[w] = vertex
distance[w] = dist + graph[vertex][w]
return parent,distance
if __name__ == '__main__':
print graph.keys()
s = raw_input("Please choose one key to caculate the shortest path:\n")
distance = __init__distance(graph,s)
parent,distance = dijkstra(graph,distance,s)
print parent
print distance
distance = sorted(distance.items(), key=lambda x: x[1])
d = []
for i in distance:
d.append(i[0])
lon = "-->"
print "The shortest path of this graph is \n-----------------------\n"+lon.join(d)+"\n-----------------------"