- Notifications
You must be signed in to change notification settings - Fork180
Add the A* algorithm.#35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
zjl9959 wants to merge1 commit intoalgorithm-visualizer:masterChoose a base branch fromzjl9959:AStarPathFinding
base:master
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
21 changes: 21 additions & 0 deletionsBranch and Bound/AStar Path Finding/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# AStar path finding | ||
<img src="https://cdn.jsdelivr.net/gh/zjl9959/algviz-launch@master/svgs/AStarPathFinding_sec.svg" width=240px/> | ||
AStar(A*) is an effective path-finding algorithm for a grid map (such as a 2D maze). | ||
A* algorithm uses breadth-first search as the basic framework, but it uses a priority queue to sort the point to be explored. The algorithm will explore the point in the front of the priority queue first. | ||
The key of the priority queue is the `hamiltonDistance(start, candidate) + hamiltonDistance(candidate, end)` and the value is the candidate point. The keys in the priority queue are heuristic information to guide the algorithm to explore the points that are more likely to be on the shortest path. So A* algorithm is more effective than the breadth-first search algorithm. | ||
## Complexity | ||
**Time Complexity:** `O(R·C)` for the worst case, `O(R + C)` for the best case. | ||
**Space Complexity:** `O(R·C) ~ O(R + C)` cost by the priority queue. | ||
*`R` is the grid map's row number and `C` is the grid map's column number.* | ||
## Reference | ||
+ [redblobgames - introduction to the A* Algorithm](https://www.redblobgames.com/pathfinding/a-star/introduction.html) | ||
+ [algviz - an algorithm animation engine for Python](https://github.com/zjl9959/algviz-launch) |
133 changes: 133 additions & 0 deletionsBranch and Bound/AStar Path Finding/code.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// import visualization libraries { | ||
const { Tracer, Array2DTracer, Array1DTracer, Layout, VerticalLayout } = require('algorithm-visualizer'); | ||
// } | ||
// define map information. { | ||
const row = 10; | ||
const col = 10; | ||
let map = [ | ||
['', '', '', '', '', '', '', '', '', ''], // 0 | ||
['', '', '💣', '', '', '', '', '', '', ''], // 1 | ||
['', 'S', '', '💣', '', '', '', '', '', ''], // 2 | ||
['', '', '', '💣', '', '', '', '', '', ''], // 3 | ||
['', '', '', '', '', '💣', '', '', '', ''], // 4 | ||
['', '', '', '', '💣', '💣', '', '', '', ''], // 5 | ||
['', '', '', '', '', '💣', '', '', '', ''], // 6 | ||
['', '', '', '', '', '', '💣', '', 'E', ''], // 7 | ||
['', '', '', '', '', '', '💣', '', '', ''], // 8 | ||
['', '', '', '', '', '', '', '', '', ''] // 9 | ||
]; | ||
const start = [2, 1]; | ||
const end = [7, 8]; | ||
let pqueue = [start]; | ||
let que_keys = [0]; | ||
let visit = new Set(); | ||
// } | ||
// define tracer variables { | ||
const mapTracer = new Array2DTracer('Map'); | ||
const priorityQueueTracer = new Array1DTracer('PriorityQueue-key'); | ||
Layout.setRoot(new VerticalLayout([mapTracer, priorityQueueTracer])); | ||
mapTracer.set(map); | ||
priorityQueueTracer.set(que_keys); | ||
Tracer.delay(); | ||
// } | ||
function hamiltonDistance(pos1, pos2) { | ||
return Math.abs(pos1[0]-pos2[0]) + Math.abs(pos1[1]-pos2[1]); | ||
} | ||
function insertQueue(que_keys, k, v) { | ||
let put = false; | ||
que_keys.push(0); pqueue.push(0); | ||
if (que_keys.length > 1) { | ||
for (let i = que_keys.length - 2; i >= 0; i--) { | ||
if (k < que_keys[i]) { | ||
pqueue[i+1] = v; | ||
que_keys[i+1] = k; | ||
put = true; | ||
priorityQueueTracer.patch(i+1, k); | ||
break; | ||
} else { | ||
pqueue[i+1] = pqueue[i]; | ||
que_keys[i+1] = que_keys[i]; | ||
priorityQueueTracer.patch(i+1, que_keys[i]); | ||
} | ||
} | ||
} | ||
if (put === false) { | ||
que_keys[0] = k; | ||
pqueue[0] = v; | ||
priorityQueueTracer.patch(0, k); | ||
} | ||
} | ||
function isEnd(pos) { return pos[0] == end[0] && pos[1] == end[1]; } | ||
function isBlock(pos) { | ||
return map[pos[0]][pos[1]] === '💣'; | ||
} | ||
function isVisited(pos) { | ||
return visit.has(pos[0]*col + pos[1]); | ||
} | ||
function findPath() { | ||
while (pqueue.length > 0) { | ||
let cur = pqueue.pop(); que_keys.pop(); | ||
if (isEnd(cur)) break; | ||
if (isVisited(cur)) continue; | ||
visit.add(cur[0]*col + cur[1]); | ||
let up = [cur[0]-1, cur[1]]; | ||
let down = [cur[0]+1, cur[1]]; | ||
let left = [cur[0], cur[1]-1]; | ||
let right = [cur[0], cur[1]+1]; | ||
let cur_dist = hamiltonDistance(cur, start); | ||
if (up[0] >= 0 && !isVisited(up) && !isBlock(up)) { | ||
let dist = cur_dist + hamiltonDistance(up, end); | ||
insertQueue(que_keys, dist, up); | ||
map[up[0]][up[1]] = '↓'; | ||
// visualize { | ||
if (!isEnd(up)) { | ||
mapTracer.patch(up[0], up[1], '↓'); | ||
} | ||
// } | ||
} | ||
if (down[0] < row && !isVisited(down) && !isBlock(down)) { | ||
let dist = cur_dist + hamiltonDistance(down, end); | ||
insertQueue(que_keys, dist, down); | ||
map[down[0]][down[1]] = '↑'; | ||
// visualize { | ||
if (!isEnd(down)) { | ||
mapTracer.patch(down[0], down[1], '↑'); | ||
} | ||
// } | ||
} | ||
if (left[1] >= 0 && !isVisited(left) && !isBlock(left)) { | ||
let dist = cur_dist + hamiltonDistance(left, end); | ||
insertQueue(que_keys, dist, left); | ||
map[left[0]][left[1]] = '→'; | ||
// visualize { | ||
if (!isEnd(left)) { | ||
mapTracer.patch(left[0], left[1], '→'); | ||
} | ||
// } | ||
} | ||
if (right[1] < col && !isVisited(right) && !isBlock(right)) { | ||
let dist = cur_dist + hamiltonDistance(right, end); | ||
insertQueue(que_keys, dist, right); | ||
map[right[0]][right[1]] = '←'; | ||
// visualize { | ||
if (!isEnd(right)) { | ||
mapTracer.patch(right[0], right[1], '←'); | ||
} | ||
// } | ||
} | ||
// visualize { | ||
Tracer.delay(); | ||
// } | ||
} | ||
} | ||
findPath(); |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.