Skip to content

Commit 464237e

Browse files
committed
Renamed some variables and updated the README
1 parent 500fa26 commit 464237e

File tree

3 files changed

+26
-24
lines changed

3 files changed

+26
-24
lines changed

README.md

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,22 @@ def a_star(graph, start, end):
4848
4949
"""
5050

51-
frontier = DijkstraHeap( Node(0, start, None) )
51+
frontier = DijkstraHeap( Node(priority = 0, point = start, came_from = None) )
5252

5353
while frontier:
5454

5555
current_node = frontier.pop()
5656

57-
if current_node.value == end:
57+
if current_node.point == end:
5858
return frontier
5959

60-
for neighbor in graph.neighbors( current_node.value ):
60+
for neighbor in graph.neighbors( current_node.point ):
6161

6262
new_cost = ( current_node.priority
63-
+ graph.cost(current_node.value, neighbor)
63+
+ graph.cost(current_node.point, neighbor)
6464
+ heuristic( neighbor, end) )
6565

66-
new_node = Node(new_cost, neighbor, current_node.value)
66+
new_node = Node(priority = new_cost, point = neighbor, came_from=current_node.point)
6767

6868
frontier.insert(new_node)
6969

@@ -75,13 +75,15 @@ Lets go line by line:
7575
frontier = DijkstraHeap( Node(0, start, None) )
7676
```
7777

78-
This line creates a DijkstraHeap object. We will see later how this can be implemented but the best part is that....This is not part of the algorithm! What is a DijkstraHeap then? This is a **priority queue** that has the following properties:
78+
This line creates a DijkstraHeap object and puts the starting point in it. We will see later how this can be implemented but the best part is that....This is not part of the algorithm! What is a DijkstraHeap then? This is a **priority queue** that has the following properties:
7979

8080
* If we try to insert an already visited element in the queue the DijkstraHeap will do nothing.
8181
* The DijkstraHeap always pop the element that has the lowest cost and NEVER pops an already visited element.
8282

8383
Cool! So this DijkstraHeap knows the visiting order of the elements. Its **like a heap but never pops an already visited element**.
8484

85+
By the way, a Node object is a tuple of the form ( cost_so_far, point, point_from_we_came ).
86+
8587
```python
8688
while frontier:
8789
```
@@ -95,24 +97,24 @@ current_node = frontier.pop()
9597
Each iteration we pop an element from the DijkstraHeap. This element always has the lowest cost element because the DijkstraHeap has this property ( because is a heap and heaps are awesome ).
9698

9799
```python
98-
if current_node.value == end:
100+
if current_node.point == end:
99101
return frontier
100102
```
101103

102104
If we have reached the end, we stop and return the DijkstraHeap that has all the information about our path (because it knows how we reach each element).
103105

104106
```python
105-
for neighbor in graph.neighbors( current_node.value ):
107+
for neighbor in graph.neighbors( current_node.point ):
106108
```
107109

108110
We get each of the current point neighbors
109111

110112
```python
111113
new_cost = ( current_node.priority
112-
+ graph.cost(current_node.value, neighbor)
114+
+ graph.cost(current_node.point, neighbor)
113115
+ heuristic( neighbor, end) )
114-
115-
new_node = Node(new_cost, neighbor, current_node.value)
116+
117+
new_node = Node(priority = new_cost, point = neighbor, came_from=current_node.point)
116118

117119
frontier.insert(new_node)
118120

@@ -161,7 +163,7 @@ class DijkstraHeap(list):
161163
:return: None
162164
"""
163165

164-
if element.value not in self.visited:
166+
if element.point not in self.visited:
165167
heapq.heappush(self,element)
166168

167169
def pop(self):
@@ -171,11 +173,11 @@ class DijkstraHeap(list):
171173
:return: A Node object
172174
"""
173175

174-
while self and self[0].value in self.visited:
176+
while self and self[0].point in self.visited:
175177
heapq.heappop(self)
176178

177179
next_elem = heapq.heappop(self)
178-
self.visited[next_elem.value] = next_elem.came_from
179-
self.costs[next_elem.value] = next_elem.priority
180+
self.visited[next_elem.point] = next_elem.came_from
181+
self.costs[next_elem.point] = next_elem.priority
180182
return next_elem
181183
```

__pycache__/a_star.cpython-35.pyc

-4 Bytes
Binary file not shown.

a_star.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def insert(self, element):
4343
:return: None
4444
"""
4545

46-
if element.value not in self.visited:
46+
if element.point not in self.visited:
4747
heapq.heappush(self,element)
4848

4949
def pop(self):
@@ -53,18 +53,18 @@ def pop(self):
5353
:return: A Node object
5454
"""
5555

56-
while self and self[0].value in self.visited:
56+
while self and self[0].point in self.visited:
5757
heapq.heappop(self)
5858

5959
next_elem = heapq.heappop(self)
60-
self.visited[next_elem.value] = next_elem.came_from
61-
self.costs[next_elem.value] = next_elem.priority
60+
self.visited[next_elem.point] = next_elem.came_from
61+
self.costs[next_elem.point] = next_elem.priority
6262
return next_elem
6363

6464

6565

6666

67-
Node = collections.namedtuple("Node","priority value came_from")
67+
Node = collections.namedtuple("Node","priority point came_from")
6868

6969
def a_star(graph, start, end):
7070
"""
@@ -87,16 +87,16 @@ def a_star(graph, start, end):
8787

8888
current_node = frontier.pop()
8989

90-
if current_node.value == end:
90+
if current_node.point == end:
9191
return frontier
9292

93-
for neighbor in graph.neighbors( current_node.value ):
93+
for neighbor in graph.neighbors( current_node.point ):
9494

9595
new_cost = ( current_node.priority
96-
+ graph.cost(current_node.value, neighbor)
96+
+ graph.cost(current_node.point, neighbor)
9797
+ heuristic( neighbor, end) )
9898

99-
new_node = Node(new_cost, neighbor, current_node.value)
99+
new_node = Node(new_cost, neighbor, current_node.point)
100100

101101
frontier.insert(new_node)
102102

0 commit comments

Comments
 (0)