-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.py
73 lines (57 loc) · 1.79 KB
/
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
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
68
69
70
71
72
73
import operator
__author__ = 'guotengfei'
__time__ = 2019 / 9 / 27
"""
Module comment
"""
graph = {'start': {}, 'a': {}, 'b': {}, 'c': {}, 'd': {}, 'fin': {}}
costs = {}
parents = {}
processed = []
def get_lowest_cost_noed(costs):
sorted_costs = get_sorted(costs)
for cost in sorted_costs:
if cost[0] not in processed:
processed.append(cost[0])
return cost
def dijkstra():
for key, value in graph['start'].items():
costs.setdefault(key, value)
parents.setdefault(key, "start")
costs.setdefault('fin', float('inf'))
parents.setdefault('fin', None)
# sorted_start = sorted(graph['start'].items(), key=lambda item: item[1])
# sorted_start = sorted(graph['start'].items(), key=operator.itemgetter(1))
cost = get_lowest_cost_noed(costs)
while cost is not None:
update_cost(cost)
cost = get_lowest_cost_noed(costs)
print(costs['fin'])
print(parents['fin'])
def update_cost(cost):
key, value = cost
sorted_keys = get_sorted(graph[key])
for a_key, a_value in sorted_keys:
if a_key not in costs.keys():
costs[a_key] = a_value + value
parents[a_key] = key
else:
cost = costs[a_key]
if (value + a_value) < cost:
costs[a_key] = (value + a_value)
parents[a_key] = key
def get_sorted(graph):
sorted_start = sorted(graph.items(), key=operator.itemgetter(1))
# sorted_start = sorted(zip(graph.keys(), graph.values()))
return sorted_start
if __name__ == '__main__':
graph['start']['a'] = 5
graph['start']['b'] = 2
graph['a']['d'] = 4
graph['a']['c'] = 2
graph['b']['a'] = 8
graph['b']['c'] = 7
graph['d']['c'] = 6
graph['d']['fin'] = 3
graph['c']['fin'] = 1
dijkstra()