-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd_2.py
More file actions
53 lines (45 loc) · 2.29 KB
/
d_2.py
File metadata and controls
53 lines (45 loc) · 2.29 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
''' в описании графа для каждого узла
есть словарь соседей со стоимостями
перехода к ним '''
graph = {}
graph['start'] = {'a': 10}
graph['a'] = {'b': 20}
graph['b'] = {'c': 1, 'finish': 30}
graph['c'] = {'a': 1}
graph['finish'] = {}
''' в эту таблицу по ходу работы
алгоритма для каждого узла вносятся
стоимости перехода к ним от начала '''
infinity = float('inf')
costs = {}
costs['a'] = 10 # сначала известны только стоимости
costs['b'] = infinity # непосредственных соседей
costs['c'] = infinity # у остальных стоимость = бесконечность
costs['finish'] = infinity
parents = {}
parents['a'] = 'start'
parents['finish'] = None
passed = [] # список уже проверенных узлов
def most_chip_node():
chipest_node = None
last_cost = float('inf')
for node in costs:
if costs[node] < last_cost and node not in passed:
last_cost = costs[node]
chipest_node = node
return chipest_node
def find_shortest():
node = most_chip_node() #начинаем с узла с наименьшей стоимостью
while node: # пока все узлы не попадут в список passed
cost = costs[node] # берём стоимость узла
neighbors = graph[node] # берём список его соседей
for n in neighbors: # вычисляем стоимость до этих соседей через node
new_cost = cost + neighbors[n]
if new_cost < costs[n]: # если она оказалсь меньше прежней
costs[n] = new_cost # тогда новая цена пишется в словарь стоимостей
parents[n] = node # и node становится родителем этого соседа
passed.append(node) # записываем node в список обработанных узлов
node = most_chip_node() # и берём следующий узел с наименьшей стоимостью
print(parents, costs['finish'])
if __name__ == '__main__':
find_shortest()