Skip to content

Commit

Permalink
Add example to README
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-isaza committed Nov 17, 2017
1 parent cb26964 commit 2c2ec49
Show file tree
Hide file tree
Showing 17 changed files with 133 additions and 1 deletion.
12 changes: 12 additions & 0 deletions A-Star/Images/graph.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "h = 3" ] }
{ rank = same; B [ label = "h = 2" ]; C [ label = "h = 2" ]; D [ label = "h = 2" ] }
{ rank = same; E [ label = "h = 1" ]; F [ label = "h = 1" ]; G [ label = "h = 1" ] }
{ H [ label = "h = 0", style = filled, color = green ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/graph.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step1.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = deepskyblue1 ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = lightgrey ]; C [ label = "g = 1\nh = 2", style = filled, color = lightgrey ]; D [ label = "g = 1\nh = 2", style = filled, color = lightgrey ] }
{ rank = same; E [ label = "g = \?\nh = 1" ]; F [ label = "g = \?\nh = 1" ]; G [ label = "g = \?\nh = 1" ] }
{ H [ label = "g = \?\nh = 0" ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step2.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = lightblue ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = deepskyblue1 ]; C [ label = "g = 1\nh = 2", style = filled, color = lightgrey ]; D [ label = "g = 1\nh = 2", style = filled, color = lightgrey ] }
{ rank = same; E [ label = "g = 2\nh = 1", style = filled, color = lightgrey ]; F [ label = "g = \?\nh = 1" ]; G [ label = "g = \?\nh = 1" ] }
{ H [ label = "g = \?\nh = 0" ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step3.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = lightblue ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; C [ label = "g = 1\nh = 2", style = filled, color = deepskyblue1 ]; D [ label = "g = 1\nh = 2", style = filled, color = lightgrey ] }
{ rank = same; E [ label = "g = 2\nh = 1", style = filled, color = lightgrey ]; F [ label = "g = \?\nh = 1" ]; G [ label = "g = \?\nh = 1" ] }
{ H [ label = "g = \?\nh = 0" ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step4.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = lightblue ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; C [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; D [ label = "g = 1\nh = 2", style = filled, color = deepskyblue1 ] }
{ rank = same; E [ label = "g = 2\nh = 1", style = filled, color = lightgrey ]; F [ label = "g = \?\nh = 1" ]; G [ label = "g = 2\nh = 1", style = filled, color = lightgrey ] }
{ H [ label = "g = \?\nh = 0" ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step5.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = lightblue ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; C [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; D [ label = "g = 1\nh = 2", style = filled, color = lightblue ] }
{ rank = same; E [ label = "g = 2\nh = 1", style = filled, color = deepskyblue1 ]; F [ label = "g = 3\nh = 1", style = filled, color = lightgrey ]; G [ label = "g = 2\nh = 1", style = filled, color = lightgrey ] }
{ H [ label = "g = \?\nh = 0" ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step6.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = lightblue ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; C [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; D [ label = "g = 1\nh = 2", style = filled, color = lightblue ] }
{ rank = same; E [ label = "g = 2\nh = 1", style = filled, color = lightblue ]; F [ label = "g = 3\nh = 1", style = filled, color = lightgrey ]; G [ label = "g = 2\nh = 1", style = filled, color = deepskyblue1 ] }
{ H [ label = "g = 3\nh = 0", style = filled, color = lightgrey ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions A-Star/Images/step7.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
digraph G {
rankdir=LR;
{ A [ label = "g = 0\nh = 3", style = filled, color = lightblue ] }
{ rank = same; B [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; C [ label = "g = 1\nh = 2", style = filled, color = lightblue ]; D [ label = "g = 1\nh = 2", style = filled, color = lightblue ] }
{ rank = same; E [ label = "g = 2\nh = 1", style = filled, color = lightblue ]; F [ label = "g = 3\nh = 1", style = filled, color = lightgrey ]; G [ label = "g = 2\nh = 1", style = filled, color = lightblue ] }
{ H [ label = "g = 3\nh = 0", style = filled, color = deepskyblue1 ] }
A -> { B C D }
B -> E
E -> F
D -> G
G -> H
}
Binary file added A-Star/Images/step7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 37 additions & 1 deletion A-Star/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,40 @@ A* (pronounced "ay star") is a heuristic best-first search algorithm. A* minimiz

A* works by expanding the most promising nodes first, according to the heuristic function. In the city example it would choose streets which go in the general direction of the target first and, only if those are dead ends, backtrack and try other streets. This speeds up search in most sitations.

A* is optimal (it always find the shortest path) if the heuristic function is admissible. A heuristic function is admissible if it never overestimates the cost of reaching the goal. In the extreme case of the heuristic function always retuning `0` A* acts exactly the same as [Dijkstra's Algorithm](../Dijkstra). The closer the heuristic function is the the actual distance the faster the search.
A* is optimal (it always find the shortest path) if the heuristic function is admissible. A heuristic function is admissible if it never overestimates the cost of reaching the goal. In the extreme case of the heuristic function always retuning `0` A* acts exactly the same as [Dijkstra's Algorithm](../Dijkstra). The closer the heuristic function is to the actual distance the faster the search.

## Example

Let's run through an example on this simple directed graph. We are going to assume that all edges have a cost of 1 and the heuristic function is going to be the column starting at goal and going back:

![Graph](Images/graph.png)

On the first step we expand the root node on the left (blue). We set the cost `g` to zero and add all neighbors to the open list (grey).

![Step 1](Images/step1.png)

We put the first not in the closed list (light blue) so that we don't try expanding it again if there were to be loops in the graph. Next we take the node on the open list with the smallest value of `g + h` where `g` is the current cost (0) plus the edge cost (1). Since all nodes in the open list have the same value we choose the top one.

![Step 2](Images/step2.png)

We repeat the process and pick the next node from the open list. In this case there are no new nodes to add to the open list.

![Step 3](Images/step3.png)

We expand the next node. One more node on the open list, but nothing exciting yet.

![Step 4](Images/step4.png)

Sicne the top and the bottom nodes have the same value we choose the top one. This is not a great choice but we could do better if we had a better heuristic function.

![Step 5](Images/step5.png)

Now we expand the bottom node because it has a smaller value than the middle node (2 + 1 < 3 + 1).

![Step 6](Images/step6.png)

And we finally reach the goal! We never even expanded that middle node. We didn't have to because its value is 4, which is equal to the total lenght of our solution and therefore guaranteed to not be part of the optimal solution.

![Step 7](Images/step7.png)

The final step is to backtrack from the goal node to buld the optimal path.

0 comments on commit 2c2ec49

Please sign in to comment.