Skip to content

Commit dd9fc61

Browse files
committed
display best path as result of djikstra's
1 parent cefdcd3 commit dd9fc61

File tree

5 files changed

+26
-5
lines changed

5 files changed

+26
-5
lines changed

Diff for: __pycache__/distance_grid.cpython-36.pyc

-26 Bytes
Binary file not shown.

Diff for: __pycache__/distances.cpython-36.pyc

321 Bytes
Binary file not shown.

Diff for: distance_grid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def __init__(self, rows, columns):
1010
self.distances = {}
1111

1212
def contents_of(self, cell):
13-
if self.distances:
13+
if self.distances.get(cell) is not None:
1414
return base36.dumps(self.distances.get(cell))
1515
else:
16-
super
16+
return " "

Diff for: distances.py

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,24 @@ def get_cells(self):
88
return self.cells.keys()
99

1010
def get(self, cell):
11-
return self.cells[cell]
11+
if cell in self.cells:
12+
return self.cells[cell]
13+
else:
14+
return None
1215

1316
def set(self, cell, distance):
1417
self.cells[cell] = distance
18+
19+
def path_to(self, goal):
20+
current = goal
21+
breadcrumbs = Distances(self.root)
22+
breadcrumbs.cells[current] = self.cells[current]
23+
24+
while not current == self.root:
25+
for neighbor in current.get_links():
26+
if self.cells[neighbor] < self.cells[current]:
27+
breadcrumbs.cells[neighbor] = self.cells[neighbor]
28+
current = neighbor
29+
break
30+
31+
return breadcrumbs

Diff for: djikstra.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
grid = DistanceGrid(12,12)
55
BinaryTree.mutate(grid)
66

7-
start = grid.grid[0][11]
7+
start = grid.grid[0][0]
8+
finish = grid.grid[11][0]
89
distances = start.distances()
910

1011
grid.distances = distances
1112

1213
grid.to_string()
13-
grid.to_grid()
14+
15+
grid.distances = distances.path_to(finish)
16+
17+
grid.to_string()

0 commit comments

Comments
 (0)