<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brian Grinstead &#187; algorithm</title>
	<atom:link href="http://www.briangrinstead.com/blog/tag/algorithm/feed" rel="self" type="application/rss+xml" />
	<link>http://www.briangrinstead.com/blog</link>
	<description></description>
	<lastBuildDate>Wed, 11 Jan 2012 20:23:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A* Search Algorithm in JavaScript (Updated)</title>
		<link>http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript-updated</link>
		<comments>http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript-updated#comments</comments>
		<pubDate>Wed, 15 Sep 2010 21:00:51 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[All]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[astar]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[pathfinding]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[search]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/blog/?p=386</guid>
		<description><![CDATA[See the updated pathfinding demo of A* Search in JavaScript. Get all the updated source code from github. When I first wrote the A* Search in JavaScript article I knew there were some things that could make the pathfinding faster. I didn&#8217;t know realize that it would be this much faster. Here are my (totally [...]]]></description>
			<content:encoded><![CDATA[<h4>See the <a href='http://briangrinstead.com/files/astar/'>updated pathfinding demo</a> of A* Search in JavaScript.<br />
Get all the <a href='http://github.com/bgrins/javascript-astar'>updated source code</a> from github.<br />
</h4>
<p>When I first wrote the <a href="http://briangrinstead.com/blog/astar-search-algorithm-in-javascript">A* Search in JavaScript</a> article I knew there were some things that could make the pathfinding faster.  <strong>I didn&#8217;t know realize that it would be this much faster.</strong>  Here are my (totally unscientific) results:</p>
<table>
<thead>
<tr>
<th colspan='3'>Observed Speed of A* Search on Chrome 6.0.472.55 on OS X</th>
</tr>
</thead>
<tr>
<th># rows</th>
<th><a href='http://www.briangrinstead.com/files/astar-original'>original</a></th>
<th><a href='http://www.briangrinstead.com/files/astar/'>updated</a></th>
</tr>
<tr>
<td>100</td>
<td>700-2500ms (wildly variant)</td>
<td>5-10ms</td>
</tr>
<tr>
<td>50</td>
<td>160ms</td>
<td>5-10ms</td>
</tr>
<tr>
<td>15</td>
<td>0-5ms</td>
<td>0-2ms</td>
</tr>
</table>
<h3>Here&#8217;s What Changed</h3>
<p>I got some feedback about using a heap or a priority queue instead of a list (basically keep the open items in sorted order the whole time so we can just grab it off of the top instead of needing to traverse the whole list every time.  <strong>As predicted, this makes a huge difference as the number of nodes goes up, but may actually be a little slower for a very low number of nodes because of the overhead of keeping them sorted.</strong>  I found a binary heap implementation from <a href='http://eloquentjavascript.net/appendix2.html'>http://eloquentjavascript.net/appendix2.html</a> and put it in.</p>
<p>Also, I realized two places that I was traversing lists that could be avoided.  Getting rid of extra traversals (especially ones that ran inside nested loops) made a very big difference in performance.</p>
<ol>
<li>I was keeping all of the nodes inside a closed list, which required traversing that list every time to see if it was already closed.  This was replaced with a simple bit on the node to track if it had been closed.</li>
<li>I was checking to see if a node was visited already by seeing if it was in the open list.  This was replaced by a simple bit to see if it had been visited.</li>
</ol>
<h3>Old Pseudocode</h3>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  push startNode onto openList
  <span style="color: #b1b100;color: #0000FF;">while</span><span style="color: #009900;color: #000000;">&#40;</span>openList is not empty<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
     currentNode <span style="color: #339933;color: #000000;">=</span> find lowest f in openList
     <span style="color: #b1b100;color: #0000FF;">if</span> currentNode is final<span style="color: #339933;color: #000000;">,</span> <span style="color: #b1b100;color: #0000FF;">return</span> the successful path
     push currentNode onto closedList and remove from openList
     foreach neighbor of currentNode <span style="color: #009900;color: #000000;">&#123;</span>
         <span style="color: #b1b100;color: #0000FF;">if</span> neighbor is not in openList <span style="color: #009900;color: #000000;">&#123;</span>
                save g<span style="color: #339933;color: #000000;">,</span> h<span style="color: #339933;color: #000000;">,</span> and f then save the current parent
                add neighbor to openList
         <span style="color: #009900;color: #000000;">&#125;</span>
         <span style="color: #b1b100;color: #0000FF;">if</span> neighbor is in openList but the current g is better than previous g <span style="color: #009900;color: #000000;">&#123;</span>
                 save g and f<span style="color: #339933;color: #000000;">,</span> then save the current parent
         <span style="color: #009900;color: #000000;">&#125;</span>
     <span style="color: #009900;color: #000000;">&#125;</span></pre></div></div>

<h3>Updated Pseudocode (* lines have changed)</h3>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">  <span style="color: #339933;color: #000000;">*</span> push startNode onto openHeap
  <span style="color: #b1b100;color: #0000FF;">while</span><span style="color: #009900;color: #000000;">&#40;</span>openList is not empty<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
     <span style="color: #339933;color: #000000;">*</span> currentNode <span style="color: #339933;color: #000000;">=</span> pop from openHeap
     <span style="color: #b1b100;color: #0000FF;">if</span> currentNode is final<span style="color: #339933;color: #000000;">,</span> <span style="color: #b1b100;color: #0000FF;">return</span> the successful path
     <span style="color: #339933;color: #000000;">*</span> set currentNode as closed
     foreach neighbor of currentNode <span style="color: #009900;color: #000000;">&#123;</span>
         <span style="color: #339933;color: #000000;">*</span> <span style="color: #b1b100;color: #0000FF;">if</span> neighbor is not set visited <span style="color: #009900;color: #000000;">&#123;</span>
                <span style="color: #339933;color: #000000;">*</span> save g<span style="color: #339933;color: #000000;">,</span> h<span style="color: #339933;color: #000000;">,</span> and f then save the current parent and set visited
                <span style="color: #339933;color: #000000;">*</span> add neighbor to openHeap
         <span style="color: #009900;color: #000000;">&#125;</span>
         <span style="color: #b1b100;color: #0000FF;">if</span> neighbor is in openList but the current g is better than previous g <span style="color: #009900;color: #000000;">&#123;</span>
                 save g and f<span style="color: #339933;color: #000000;">,</span> then save the current parent
                 <span style="color: #339933;color: #000000;">*</span> reset position in openHeap <span style="color: #009900;color: #000000;">&#40;</span>since f changed<span style="color: #009900;color: #000000;">&#41;</span> 
         <span style="color: #009900;color: #000000;">&#125;</span>
     <span style="color: #009900;color: #000000;">&#125;</span></pre></div></div>

<p>I also did some general cleanup, getting rid of dependancies on the demo, and I split out the code so that the search function can be used standalone (taking a two dimensional array as a parameter), so if someone just wanted the optimized astar search, you could call it like so:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;script type='text/javascript' src='graph.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript' src='astar.js'&gt;&lt;/script&gt;
&lt;script type='text/javascript'&gt;
var graph = new Graph([
	[0,0,0,0], // 0 is open, 1 is wall
	[1,0,0,1],
	[1,1,0,0]
]);
var start = graph.nodes[0][0];
var end = graph.nodes[1][2];
var result = astar.search(graph.nodes, start, end);
// result is an array containing the shortest path
&lt;/script&gt;</pre></div></div>

<h3>What Could Make It Better?</h3>
<p>Last time I posted about this, I got a lot of feedback (mostly about how slow it was).  If anyone reading this knows of anything that could make it better, let me know.</p>
<h3>Source Code (direct links)</h3>
<p><a href='http://github.com/bgrins/javascript-astar/raw/master/astar.js'>astar.js</a> |<br />
<a href='http://github.com/bgrins/javascript-astar/raw/master/graph.js'>graph.js</a> |<br />
<a href='http://github.com/bgrins/javascript-astar/raw/master/demo/demo.js'>demo.js</a> |<br />
<a href='http://github.com/bgrins/javascript-astar/raw/master/astar.js'>astar-list.js (old implementation)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript-updated/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>A* Search Algorithm in JavaScript</title>
		<link>http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript</link>
		<comments>http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript#comments</comments>
		<pubDate>Wed, 03 Jun 2009 17:29:36 +0000</pubDate>
		<dc:creator>Brian</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[All]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.briangrinstead.com/?p=178</guid>
		<description><![CDATA[Note that this code has been updated. I have an updated blog post detailing what changed. The full source code is available at https://github.com/bgrins/javascript-astar View the online demo I first implemented the A* algorithm for a research group I was in through school (Computer Graphics and Image Understanding). A* is a best-first, graph search algorithm. [...]]]></description>
			<content:encoded><![CDATA[<p><em>Note that this code has been updated.  I have an <a href='http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript-updated'>updated blog post</a> detailing what changed.  The full source code is available at <a href='https://github.com/bgrins/javascript-astar'>https://github.com/bgrins/javascript-astar</a></em></p>
<p><strong><a href="/files/astar/">View the online demo</a></strong></p>
<p>I first implemented the A* algorithm for a research group I was in through school (Computer Graphics and Image Understanding).  A* is a best-first, graph search algorithm.  Some basic information can be found on the <a href="http://en.wikipedia.org/wiki/A*_search_algorithm">Wikipedia page for A*</a> and the external links contained in it.  Please refer to that page for general reference about the algorithm, I will simply explain in my own words how it works and how I got it working.</p>
<div id="attachment_191" class="wp-caption alignright" style="width: 410px"><a href="/files/astar/"><img src="http://www.briangrinstead.com/files/astarscreenshot.png" alt="A* algorithm in JavaScript" title="View Demo" width="400" class="size-full wp-image-191" /></a><p class="wp-caption-text">A* algorithm in JavaScript</p></div>
<h3>Why JavaScript?</h3>
<p>Because it was easy to deploy!  </p>
<p>Since I know JavaScript pretty well, and most of the examples you can find are in C, Java or a similar language that you cannot run without downloading source code or executables, I thought it would be a good idea to program it on an html page.  This way, people could see what was going on and view the source very easily by <a href="/files/astar/">using the online demo</a>.  </p>
<p>My hope was to build a page that could be extended with other search algorithms by separating the UI code (that generates a graph with walls and animates the path that is determined by an algorithm), and the algorithm that finds the path.  Maybe I will get around to plugging in some more algorithms sometime and making it into a little resource for graph search algorithms.</p>
<h3>How?</h3>
<h4>search.html</h4>
<p>Just a basic html file that includes jQuery, the excellent JavaScript library, main.css, graph.js, and astar.js.  Also, I have a JavaScript block that initializes the page.</p>
<h4>graph.js</h4>
<p>The point of this file is to build the graph, call the search function, and animate the results after the search has returned.  It also has an option to show the debugging information created by the search algorithm.  I won&#8217;t get too into the code here, since it distracts from the search algorithm.  </p>
<p>Please take a look at it, be aware that there are some improvements I would make if I was to rewrite this today.  There are some performance issues that could be addressed, and It modifies the Array.prototype to add on specific methods (findGraphNode and removeGraphNode) for search algorithms, which may not be ideal for bigger projects.  For this little page, I&#8217;m not too worried about it, but if I do get around to adding in more algorithms, I will probably improve this code.</p>
<h4>astar.js</h4>
<p>This is the actual implementation of the algorithm.  I will do my best to explain what is going on, but feel free to just look at the source of the example, or just download astar.js.</p>
<p>There are three functions that we keep track of for nodes that we look at:</p>
<ul>
<li>g(x):  The total cost of getting to that node (pretty straightforward).  If we reach a node for the first time or reach a node in less time than it currently took, then update the g(x) to the cost to reach this node.</li>
<li>h(x):  The estimated time to reach the finish from the current node.  This is also called a heuristic.  We online need to update this if it is not set already, since the distance to the finish will not change even if the path we took to arrive at a node changes.  <em>Note: There are many different ways to guess how far you are from the end, I use the <a href="http://en.wikipedia.org/wiki/Taxicab_geometry">Manhattan distance</a> in this implementation.</em>
</li>
<li>f(x):  Simply g(x) + h(x).  The lower the f(x), the better.  Think about it like this: the best node is one that takes the least total amount of time to arrive at and to get to the end.  So, a node that took only 1 step to arrive at and 5 to get to the end is more ideal than one that took 10 to arrive and and only 1 to get to the end.
</li>
</ul>
<p>Here is some high level pseudocode of what is happening in the algorithm.  Also see the <a href="http://en.wikipedia.org/wiki/A*_search_algorithm#Algorithm_description">Wikipedia pseudocode</a> for another example.</p>
<pre>
  push startNode onto openList
  while(openList is not empty) {
     currentNode = find lowest f in openList
     if currentNode is final, return the successful path
     push currentNode onto closedList and remove from openList
     foreach neighbor of currentNode {
         if neighbor is not in openList {
                save g, h, and f then save the current parent
                add neighbor to openList
         }
         if neighbor is in openList but the current g is better than previous g {
                 save g and f, then save the current parent
         }
     }
</pre>
<p>Here is the JavaScript:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&nbsp;
<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> astar <span style="color: #339933;color: #000000;">=</span> <span style="color: #009900;color: #000000;">&#123;</span>
	init<span style="color: #339933;color: #000000;">:</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> x <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> x <span style="color: #339933;color: #000000;">&lt;</span> grid.<span style="color: #660066;">length</span><span style="color: #339933;color: #000000;">;</span> x<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> y <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> y <span style="color: #339933;color: #000000;">&lt;</span> grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">length</span><span style="color: #339933;color: #000000;">;</span> y<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
				grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">f</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span>
				grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">g</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span>
				grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">h</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span>
				grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">debug</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #3366CC;color: #808080;">&quot;&quot;</span><span style="color: #339933;color: #000000;">;</span>
				grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">parent</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">null</span><span style="color: #339933;color: #000000;">;</span>
			<span style="color: #009900;color: #000000;">&#125;</span>	
		<span style="color: #009900;color: #000000;">&#125;</span>
	<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #339933;color: #000000;">,</span>
	search<span style="color: #339933;color: #000000;">:</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #339933;color: #000000;">,</span> start<span style="color: #339933;color: #000000;">,</span> end<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
		astar.<span style="color: #660066;">init</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> openList   <span style="color: #339933;color: #000000;">=</span> <span style="color: #009900;color: #000000;">&#91;</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> closedList <span style="color: #339933;color: #000000;">=</span> <span style="color: #009900;color: #000000;">&#91;</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
		openList.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>start<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;color: #0000FF;">while</span><span style="color: #009900;color: #000000;">&#40;</span>openList.<span style="color: #660066;">length</span> <span style="color: #339933;color: #000000;">&gt;</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #008000;">// Grab the lowest f(x) to process next</span>
			<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> lowInd <span style="color: #339933;color: #000000;">=</span> <span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span>
			<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> i<span style="color: #339933;color: #000000;">=</span><span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">&lt;</span>openList.<span style="color: #660066;">length</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
				<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>openList<span style="color: #009900;color: #000000;">&#91;</span>i<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">f</span> <span style="color: #339933;color: #000000;">&lt;</span> openList<span style="color: #009900;color: #000000;">&#91;</span>lowInd<span style="color: #009900;color: #000000;">&#93;</span>.<span style="color: #660066;">f</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span> lowInd <span style="color: #339933;color: #000000;">=</span> i<span style="color: #339933;color: #000000;">;</span> <span style="color: #009900;color: #000000;">&#125;</span>
			<span style="color: #009900;color: #000000;">&#125;</span>
			<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> currentNode <span style="color: #339933;color: #000000;">=</span> openList<span style="color: #009900;color: #000000;">&#91;</span>lowInd<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #008000;">// End case -- result has been found, return the traced path</span>
			<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>currentNode.<span style="color: #660066;">pos</span> <span style="color: #339933;color: #000000;">==</span> end.<span style="color: #660066;">pos</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
				<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> curr <span style="color: #339933;color: #000000;">=</span> currentNode<span style="color: #339933;color: #000000;">;</span>
				<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> ret <span style="color: #339933;color: #000000;">=</span> <span style="color: #009900;color: #000000;">&#91;</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #000066; font-weight: bold;color: #0000FF;">while</span><span style="color: #009900;color: #000000;">&#40;</span>curr.<span style="color: #660066;">parent</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
					ret.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>curr<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
					curr <span style="color: #339933;color: #000000;">=</span> curr.<span style="color: #660066;">parent</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #009900;color: #000000;">&#125;</span>
				<span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> ret.<span style="color: #660066;">reverse</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
			<span style="color: #009900;color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #006600; font-style: italic;color: #008000;">// Normal case -- move currentNode from open to closed, process each of its neighbors</span>
			openList.<span style="color: #660066;">removeGraphNode</span><span style="color: #009900;color: #000000;">&#40;</span>currentNode<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
			closedList.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>currentNode<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
			<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> neighbors <span style="color: #339933;color: #000000;">=</span> astar.<span style="color: #660066;">neighbors</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #339933;color: #000000;">,</span> currentNode<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
			<span style="color: #000066; font-weight: bold;color: #0000FF;">for</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> i<span style="color: #339933;color: #000000;">=</span><span style="color: #CC0000;color: #808080;">0</span><span style="color: #339933;color: #000000;">;</span> i<span style="color: #339933;color: #000000;">&lt;</span>neighbors.<span style="color: #660066;">length</span><span style="color: #339933;color: #000000;">;</span>i<span style="color: #339933;color: #000000;">++</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
				<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> neighbor <span style="color: #339933;color: #000000;">=</span> neighbors<span style="color: #009900;color: #000000;">&#91;</span>i<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>closedList.<span style="color: #660066;">findGraphNode</span><span style="color: #009900;color: #000000;">&#40;</span>neighbor<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #339933;color: #000000;">||</span> neighbor.<span style="color: #660066;">isWall</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
					<span style="color: #006600; font-style: italic;color: #008000;">// not a valid node to process, skip to next neighbor</span>
					<span style="color: #000066; font-weight: bold;color: #0000FF;">continue</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #009900;color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #006600; font-style: italic;color: #008000;">// g score is the shortest distance from start to current node, we need to check if</span>
				<span style="color: #006600; font-style: italic;color: #008000;">//	 the path we have arrived at this neighbor is the shortest one we have seen yet</span>
				<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> gScore <span style="color: #339933;color: #000000;">=</span> currentNode.<span style="color: #660066;">g</span> <span style="color: #339933;color: #000000;">+</span> <span style="color: #CC0000;color: #808080;">1</span><span style="color: #339933;color: #000000;">;</span> <span style="color: #006600; font-style: italic;color: #008000;">// 1 is the distance from a node to it's neighbor</span>
				<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> gScoreIsBest <span style="color: #339933;color: #000000;">=</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">false</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
&nbsp;
				<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span><span style="color: #339933;color: #000000;">!</span>openList.<span style="color: #660066;">findGraphNode</span><span style="color: #009900;color: #000000;">&#40;</span>neighbor<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
					<span style="color: #006600; font-style: italic;color: #008000;">// This the the first time we have arrived at this node, it must be the best</span>
					<span style="color: #006600; font-style: italic;color: #008000;">// Also, we need to take the h (heuristic) score since we haven't done so yet</span>
&nbsp;
					gScoreIsBest <span style="color: #339933;color: #000000;">=</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">true</span><span style="color: #339933;color: #000000;">;</span>
					neighbor.<span style="color: #660066;">h</span> <span style="color: #339933;color: #000000;">=</span> astar.<span style="color: #660066;">heuristic</span><span style="color: #009900;color: #000000;">&#40;</span>neighbor.<span style="color: #660066;">pos</span><span style="color: #339933;color: #000000;">,</span> end.<span style="color: #660066;">pos</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
					openList.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>neighbor<span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #009900;color: #000000;">&#125;</span>
				<span style="color: #000066; font-weight: bold;color: #0000FF;">else</span> <span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>gScore <span style="color: #339933;color: #000000;">&lt;</span> neighbor.<span style="color: #660066;">g</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
					<span style="color: #006600; font-style: italic;color: #008000;">// We have already seen the node, but last time it had a worse g (distance from start)</span>
					gScoreIsBest <span style="color: #339933;color: #000000;">=</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">true</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #009900;color: #000000;">&#125;</span>
&nbsp;
				<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>gScoreIsBest<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
					<span style="color: #006600; font-style: italic;color: #008000;">// Found an optimal (so far) path to this node.	 Store info on how we got here and</span>
					<span style="color: #006600; font-style: italic;color: #008000;">//	just how good it really is...</span>
					neighbor.<span style="color: #660066;">parent</span> <span style="color: #339933;color: #000000;">=</span> currentNode<span style="color: #339933;color: #000000;">;</span>
					neighbor.<span style="color: #660066;">g</span> <span style="color: #339933;color: #000000;">=</span> gScore<span style="color: #339933;color: #000000;">;</span>
					neighbor.<span style="color: #660066;">f</span> <span style="color: #339933;color: #000000;">=</span> neighbor.<span style="color: #660066;">g</span> <span style="color: #339933;color: #000000;">+</span> neighbor.<span style="color: #660066;">h</span><span style="color: #339933;color: #000000;">;</span>
					neighbor.<span style="color: #660066;">debug</span> <span style="color: #339933;color: #000000;">=</span> <span style="color: #3366CC;color: #808080;">&quot;F: &quot;</span> <span style="color: #339933;color: #000000;">+</span> neighbor.<span style="color: #660066;">f</span> <span style="color: #339933;color: #000000;">+</span> <span style="color: #3366CC;color: #808080;">&quot;&lt;br /&gt;G: &quot;</span> <span style="color: #339933;color: #000000;">+</span> neighbor.<span style="color: #660066;">g</span> <span style="color: #339933;color: #000000;">+</span> <span style="color: #3366CC;color: #808080;">&quot;&lt;br /&gt;H: &quot;</span> <span style="color: #339933;color: #000000;">+</span> neighbor.<span style="color: #660066;">h</span><span style="color: #339933;color: #000000;">;</span>
				<span style="color: #009900;color: #000000;">&#125;</span>
			<span style="color: #009900;color: #000000;">&#125;</span>
		<span style="color: #009900;color: #000000;">&#125;</span>
&nbsp;
		<span style="color: #006600; font-style: italic;color: #008000;">// No result was found -- empty array signifies failure to find path</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> <span style="color: #009900;color: #000000;">&#91;</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
	<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #339933;color: #000000;">,</span>
	heuristic<span style="color: #339933;color: #000000;">:</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span>pos0<span style="color: #339933;color: #000000;">,</span> pos1<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #006600; font-style: italic;color: #008000;">// This is the Manhattan distance</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> d1 <span style="color: #339933;color: #000000;">=</span> Math.<span style="color: #660066;">abs</span> <span style="color: #009900;color: #000000;">&#40;</span>pos1.<span style="color: #660066;">x</span> <span style="color: #339933;color: #000000;">-</span> pos0.<span style="color: #660066;">x</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> d2 <span style="color: #339933;color: #000000;">=</span> Math.<span style="color: #660066;">abs</span> <span style="color: #009900;color: #000000;">&#40;</span>pos1.<span style="color: #660066;">y</span> <span style="color: #339933;color: #000000;">-</span> pos0.<span style="color: #660066;">y</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> d1 <span style="color: #339933;color: #000000;">+</span> d2<span style="color: #339933;color: #000000;">;</span>
	<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #339933;color: #000000;">,</span>
	neighbors<span style="color: #339933;color: #000000;">:</span> <span style="color: #003366; font-weight: bold;color: #0000FF;">function</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #339933;color: #000000;">,</span> node<span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> ret <span style="color: #339933;color: #000000;">=</span> <span style="color: #009900;color: #000000;">&#91;</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> x <span style="color: #339933;color: #000000;">=</span> node.<span style="color: #660066;">pos</span>.<span style="color: #660066;">x</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #003366; font-weight: bold;color: #0000FF;">var</span> y <span style="color: #339933;color: #000000;">=</span> node.<span style="color: #660066;">pos</span>.<span style="color: #660066;">y</span><span style="color: #339933;color: #000000;">;</span>
&nbsp;
		<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #339933;color: #000000;">-</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span> <span style="color: #339933;color: #000000;">&amp;&amp;</span> grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #339933;color: #000000;">-</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			ret.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #339933;color: #000000;">-</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #009900;color: #000000;">&#125;</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #339933;color: #000000;">+</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span> <span style="color: #339933;color: #000000;">&amp;&amp;</span> grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #339933;color: #000000;">+</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			ret.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #339933;color: #000000;">+</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #009900;color: #000000;">&#125;</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #339933;color: #000000;">-</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span> <span style="color: #339933;color: #000000;">&amp;&amp;</span> grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #339933;color: #000000;">-</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			ret.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #339933;color: #000000;">-</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #009900;color: #000000;">&#125;</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">if</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #339933;color: #000000;">+</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span> <span style="color: #339933;color: #000000;">&amp;&amp;</span> grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #339933;color: #000000;">+</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span> <span style="color: #009900;color: #000000;">&#123;</span>
			ret.<span style="color: #660066;">push</span><span style="color: #009900;color: #000000;">&#40;</span>grid<span style="color: #009900;color: #000000;">&#91;</span>x<span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#91;</span>y<span style="color: #339933;color: #000000;">+</span><span style="color: #CC0000;color: #808080;">1</span><span style="color: #009900;color: #000000;">&#93;</span><span style="color: #009900;color: #000000;">&#41;</span><span style="color: #339933;color: #000000;">;</span>
		<span style="color: #009900;color: #000000;">&#125;</span>
		<span style="color: #000066; font-weight: bold;color: #0000FF;">return</span> ret<span style="color: #339933;color: #000000;">;</span>
	<span style="color: #009900;color: #000000;">&#125;</span>
<span style="color: #009900;color: #000000;">&#125;</span><span style="color: #339933;color: #000000;">;</span></pre></div></div>

<h3>Conclusion</h3>
<p>This A* search implementation could be used as a component to larger system (like a game &#8211; maybe tower defense or puzzle), or just for learning purposes.  I have done my best to make the code understandable and to present the concepts in a way that would help someone who has never seen the algorithm before, or someone who is not very familiar with JavaScript.</p>
<p>Feel free to view the demo, or download <a href="/files/astar/graph.js">graph.js</a> and <a href="/files/astar/astar.js">astar.js</a> to mess around with it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript/feed</wfw:commentRss>
		<slash:comments>43</slash:comments>
		</item>
	</channel>
</rss>

