Skip to content

Commit 79c1eb6

Browse files
committed
Minor tweaks
1 parent 9112217 commit 79c1eb6

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

a_star/a_star.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def __init__(self):
1919

2020
def insert(self, node):
2121
"""
22-
Insert a node into the Dijkstra Heap.
22+
Insert a node into the Dijkstra Heap if the vertex is not already visited.
2323
2424
:param node: A Node object.
2525
:return: None
@@ -30,18 +30,15 @@ def insert(self, node):
3030

3131
def pop(self):
3232
"""
33-
Pop a node from the Dijkstra Heap, adding it to the visited dict.
33+
Pop an unvisited node from the Dijkstra Heap, adding it to the visited dict.
3434
3535
:return: A Node object
3636
"""
3737

38-
while self and self[0].vertex in self.visited:
39-
heapq.heappop(self)
40-
41-
if self:
38+
while self:
4239
next_elem = heapq.heappop(self)
43-
self.visited[next_elem.vertex] = next_elem
44-
return next_elem
40+
if self.visited.setdefault(next_elem.vertex, next_elem) is next_elem:
41+
return next_elem
4542

4643
def backtrack(self, current):
4744
"""
@@ -60,14 +57,17 @@ def a_star_search(neighbors, start, end, heuristic):
6057
"""
6158
Calculates the shortest path from start to end.
6259
63-
:param neighbors: The graph, represented by a function that takes a vertex of a comparable and hashable type V and returns its neighbors.
60+
The graph is defined implicitly by a pair of functions working on vertices. The vertex representation
61+
may be any comparable and hashable type such as int, tuple, etc.
62+
63+
:param neighbors: A function returning the (possibly directed) neighbors of a vertex, along with their costs.
6464
6565
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.
6969
:param heuristic: Heuristic lower-bound cost function taking arguments ( from_vertex:V, end:V ) and returning float.
70-
:returns: A DijkstraHeap object.
70+
:returns: A DijkstraHeap object. The shortest path is: `reversed(list(d_heap.backtrack(end)))`.
7171
7272
"""
7373

a_star/tools.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _draw_tile(self, point, style, width):
6363
if y2 == y1 - 1:
6464
character = "\u2191"
6565
if 'start' in style and point == style['start']:
66-
character = "S\u00ab"
66+
character = "%s\u00bb" % character
6767
if 'goal' in style and point == style['goal']:
6868
character = "%s\u00ab" % character
6969
if 'path' in style and point in style['path']:

0 commit comments

Comments
 (0)