-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdextra_algo.py
More file actions
46 lines (40 loc) · 1.56 KB
/
dextra_algo.py
File metadata and controls
46 lines (40 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
class DextraAlgo:
graph = {'start': {'A': 2, 'B': 5}, 'A': {'C': 7, 'B': 8}, 'B': {'C': 2, 'D': 4}, 'C': {'end': 1},
'D': {'C': 6, 'end': 3}, 'end': {}}
costs = {'A': graph['start']['A'], 'B': graph['start']['B'], 'C': float('inf'), 'D': float('inf'),
'end': float('inf')}
parents = {'A': 'start', 'B': 'start', 'C': None, 'D': None, 'end': None}
processed_nodes = []
def find_lowest_cost(self, cost):
lowest_cost_node = None
lowest_cost = float('inf')
for n in cost:
if cost[n] < lowest_cost and n not in self.processed_nodes:
lowest_cost = cost[n]
lowest_cost_node = n
return lowest_cost_node
def dextra_algo(self):
node = self.find_lowest_cost(self.costs)
while node is not None:
cost = self.costs[node]
neighbors = self.graph[node]
for n in neighbors:
new_cost = cost + neighbors[n]
if self.costs[n] > new_cost:
self.costs[n] = new_cost
self.parents[n] = node
self.processed_nodes.append(node)
node = self.find_lowest_cost(self.costs)
self.get_chain()
def get_chain(self):
parents = self.parents
chain = []
start_node = 'end'
while start_node != 'start':
chain.append(start_node)
start_node = parents[start_node]
chain.append('start')
print('The fastest route:')
print(chain[::-1])
dex = DextraAlgo()
dex.dextra_algo()