-
Notifications
You must be signed in to change notification settings - Fork 0
/
dijkstra.py
74 lines (54 loc) · 1.93 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
import grid
class DijkstraGrid(grid.Grid):
# builds self.distances, measuring the all the distances from root
def build_distances(self, root):
self.distances = dict()
self.root = root
self.last_path = None
d = 0
frontier = [root]
while len(frontier) > 0:
new_frontier = []
for c in frontier:
if c in self.distances:
continue
self.distances[c] = d
new_frontier.extend(c.cells())
frontier = new_frontier
d += 1
self.max_cell = max(self.distances, key=self.distances.get)
def cell_count(self, c):
if self.last_path != None and not c in self.last_path:
return " "
d = self.distances[c]
base = "0123456789abcdefghijklmnopqrstuvwxyz"
if d >= len(base):
raise IndexError(f"{d} is too large for {base}")
return base[d]
# return the path to t
def path_to(self, t):
path = [t]
current = t
while current != self.root:
for c in current.cells():
if self.distances[c] < self.distances[current]:
current = c
path.append(c)
break
path.reverse()
self.last_path = path
return path
# sets last_path to longest path
def set_longest_path(self):
# a longest path starts at the furtherest cell from (0,0)
self.build_distances(self.grid[0][0])
origin = self.max_cell
self.build_distances(origin)
self.path_to(self.max_cell)
# returns a number between 0 and 1. 0= at origin, 1= at furtherest point
def dist_weight(self, c):
max_dist = self.distances[self.max_cell]
return self.distances[c] / max_dist
# returns a list of cells on the last path set
def get_last_path(self):
return self.last_path