Skip to content

Commit 9112217

Browse files
committed
Graph represented by single function
1 parent 95c5697 commit 9112217

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

a_star/a_star.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,13 @@ def backtrack(self, current):
5656

5757
Node = collections.namedtuple("Node", ["cost_estimate", "cost_so_far", "vertex", "came_from"])
5858

59-
def a_star_search(graph, start, end, heuristic):
59+
def a_star_search(neighbors, start, end, heuristic):
6060
"""
6161
Calculates the shortest path from start to end.
6262
63-
:param graph: A graph object. The graph object can be anything that implements the following method for a vertex of any comparable and hashable type V:
63+
:param neighbors: The graph, represented by a function that takes a vertex of a comparable and hashable type V and returns its neighbors.
6464
65-
graph.neighbors( from_vertex:V ) : Iterable( (to_vertex:V,edge_cost:float), (to_vertex:V,edge_cost:float), ...)
65+
neighbors( from_vertex:V ) : Iterable( (to_vertex:V,edge_cost:float), (to_vertex:V,edge_cost:float), ...)
6666
6767
:param start: The starting vertex, as type V.
6868
:param end: The ending vertex, as type V.
@@ -83,11 +83,14 @@ def a_star_search(graph, start, end, heuristic):
8383
if current_node.vertex == end:
8484
return frontier
8585

86-
for neighbor, edge_cost in graph.neighbors( current_node.vertex ):
86+
for neighbor, edge_cost in neighbors( current_node.vertex ):
8787

88-
new_cost = current_node.cost_so_far + edge_cost
88+
cost_at_neighbor = current_node.cost_so_far + edge_cost
8989

90-
new_node = Node(new_cost + heuristic(neighbor, end), new_cost, neighbor, current_node.vertex)
90+
new_node = Node(cost_at_neighbor + heuristic(neighbor, end),
91+
cost_at_neighbor,
92+
neighbor,
93+
current_node.vertex)
9194

9295
frontier.insert(new_node)
9396

examples/maze_solving_example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def from_id_width(point, width):
4141
(7, 3), (7, 4), (7, 5)]}
4242

4343
# Call the A* algorithm and get the frontier
44-
frontier = a_star.a_star_search(graph = graph, start=(1, 4), end=(7, 8), heuristic=manhattan_distance)
44+
frontier = a_star.a_star_search(neighbors=graph.neighbors, start=(1, 4), end=(7, 8), heuristic=manhattan_distance)
4545
solution = list(frontier.backtrack((7,8)))
4646

4747
# Print the results

tests/test_maze.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def test_maze_1(self):
1919
start = (0,2)
2020

2121
# Call the A* algorithm and get the frontier
22-
frontier = a_star.a_star_search(graph = maze, start=start, end=end, heuristic=manhattan_distance)
22+
frontier = a_star.a_star_search(neighbors=maze.neighbors, start=start, end=end, heuristic=manhattan_distance)
2323
solution = list(frontier.backtrack(end))
2424
self.assertTrue( solution == my_solution )
2525

@@ -38,7 +38,7 @@ def test_weights_instead_of_walls(self):
3838
(2, 0), (1, 0), (0, 0), (0, 1),
3939
(0, 2), (0, 3)]
4040
# Call the A* algorithm and get the frontier
41-
frontier = a_star.a_star_search(graph = maze, start=start, end=end, heuristic=manhattan_distance)
41+
frontier = a_star.a_star_search(neighbors=maze.neighbors, start=start, end=end, heuristic=manhattan_distance)
4242

4343
solution = list(frontier.backtrack(end))
4444

0 commit comments

Comments
 (0)