<?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>Wyatt Baldwin &#187; algorithms</title>
	<atom:link href="http://wyattbaldwin.com/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://wyattbaldwin.com</link>
	<description></description>
	<lastBuildDate>Tue, 20 Jul 2010 17:43:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Implementation of Dijkstra&#8217;s Single-Source Shortest-Paths in JavaScript</title>
		<link>http://wyattbaldwin.com/2008/08/19/implementation-of-dijkstras-single-source-shortest-paths-in-javascript/</link>
		<comments>http://wyattbaldwin.com/2008/08/19/implementation-of-dijkstras-single-source-shortest-paths-in-javascript/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 23:52:03 +0000</pubDate>
		<dc:creator>Wyatt</dc:creator>
				<category><![CDATA[planet python]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[dijkstra]]></category>
		<category><![CDATA[geek-fun]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://wyattbaldwin.com/?p=223</guid>
		<description><![CDATA[I&#8217;m working on a project where the client wants a cool sliding navigation effect. We&#8217;re implementing this with JavaScript/AJAX/DHTML. One of the constraints is that pages can only be reached via certain other pages. For example, if you&#8217;re on the &#8230; <a href="http://wyattbaldwin.com/2008/08/19/implementation-of-dijkstras-single-source-shortest-paths-in-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a project where the client wants a cool sliding navigation effect. We&#8217;re implementing this with JavaScript/AJAX/DHTML.</p>
<p>One of the constraints is that pages can only be reached via certain other pages. For example, if you&#8217;re on the /portland/contact page and want to go to the /seattle/contact page, you&#8217;ll first slide up to /portland, then over to /seattle, then finally down to /seattle/contact.</p>
<p>After a while, it occurred to me that there were some similarities with another project I&#8217;ve been working on off and on for the last few years, byCycle.org, which is a bicycle trip planner ala Google Maps.</p>
<p>I had written a Python version of Dijkstra&#8217;s Single-Source Shortest-Paths (SSSP) for byCycle.org. That&#8217;s available on <a title="Dijkstar on PyPi" href="http://pypi.python.org/pypi/Dijkstar">PyPi as Dijkstar</a> (so named because it also <span style="text-decoration: line-through;">does</span> has the potential to do A*). I figured it wouldn&#8217;t be too hard to port the Python version to JavaScript, and it wasn&#8217;t.</p>
<p>There were a few snags, though. Most of it was just syntactic and semantic differences between the two languages. The biggest issue was that I use &#8220;heapq&#8220; in the Python version to maintain the costs to previously visited nodes in sorted order. JavaScript has no priority queue implementation that I could find, so I came up with a different solution that involves updating an Object (AKA &#8220;dict&#8220;) with costs to newly visited nodes and sorting the keys to pick the next node to visit. I&#8217;m assuming/hoping the underlying sort implementation is highly optimized.</p>
<p>Interestingly, I think I found at least one bug in the Python version, although I&#8217;ve been using that version for a couple years now with no known problems, so it must only be applicable in certain edge (no pun intended) cases (or maybe it&#8217;s due to some difference in the languages&#8211;need to take a closer look). I think the JS version came out cleaner, too.</p>
<p>If anyone&#8217;s interested, I&#8217;m releasing this under an MIT license. For now, you can <a title="Dijkstra in JavaScript" href="http://wyattbaldwin.com/code/dijkstra.js">get it from here</a>. Note that it depends on the util module that you can <a title="Namespaces and classes in JavaScript" href="http://wyattbaldwin.com/code/util.js">get from here</a>. The util module contains some other Python-inspired JavaScript, in particular a couple of functions for generating namespaces and classes. I might write another post about that at some point.</p>
]]></content:encoded>
			<wfw:commentRss>http://wyattbaldwin.com/2008/08/19/implementation-of-dijkstras-single-source-shortest-paths-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Google Maps Encoded Polylines</title>
		<link>http://wyattbaldwin.com/2007/04/19/google-maps-encoded-polylines/</link>
		<comments>http://wyattbaldwin.com/2007/04/19/google-maps-encoded-polylines/#comments</comments>
		<pubDate>Thu, 19 Apr 2007 15:19:03 +0000</pubDate>
		<dc:creator>Wyatt</dc:creator>
				<category><![CDATA[planet python]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://wyattbaldwin.com/2007/04/19/google-maps-encoded-polylines/</guid>
		<description><![CDATA[Update 6/30/07: Fixed links to glineenc.py so that it’s actually accessible. Update 7/15/08: Fixed links to glineenc.py again because of change to HTTPS on Trac site. Here’s some Python code (complete with unit and doc tests) for converting a series &#8230; <a href="http://wyattbaldwin.com/2007/04/19/google-maps-encoded-polylines/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><em>Update 6/30/07: Fixed links to glineenc.py so that it’s actually accessible.</em><br />
<em>Update 7/15/08: Fixed links to glineenc.py again because of change to HTTPS on Trac site.</em></p>
<p><a href="http://project.bycycle.org/browser/spinoffs/glineenc.py">Here’s some Python code</a> (complete with unit and doc tests) for converting a series of latitude/longitude points (i.e., a polyline) to the Base64 encoding that Google Maps understands. It’s particularly useful for long and/or complicated lines.</p>
<p>It’s based on the algorithm listed <a href="http://www.google.com/apis/maps/documentation/polylinealgorithm.html">here</a> and the JavaScript code <span style="text-decoration: line-through;"><a href="http://www.google.com/apis/maps/documentation/polyline.js">here</a></span> [page disappeared].</p>
<p><a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/">This site</a> gives some more insight into it and has a pretty cool example of a fractal line <a href="http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/fractalLog5Log4.html">here</a>.</p>
<p>Here’s the code:</p>
<p><em>Previously, I had pasted the Python code right into this article, but I recently made a bunch of revisions and it was way too long. Here’s a link to the code instead:</em></p>
<p><a href="http://project.bycycle.org/browser/spinoffs/glineenc.py">glineenc.py on Trac</a></p>
<p>Please note that this code is still in somewhat of a rough state. I have plans to polish and package it up, but for now, I’m <a href="http://bycycle.org/">using it</a> as is and it’s <a href="http://bycycle.org/regions/portlandor/routes;find?s=longitude%3D-123.42041%2C%20latitude%3D45.769439&amp;e=longitude%3D-121.786194%2C%20latitude%3D44.957024&amp;pref=default">working quite well</a> (you’ll have to be patient to click that link as it takes ~20-30 seconds to generate the route, even though the line drawing itself is almost instantaneous).</p>
<p>JavaScript that uses results from `glineenc` looks something like this (assuming you’ve returned some <span class="caps">JSON</span>, say, with `encoded_points` and `encoded_lines` keys):</p>
<pre><code>
map.addOverlay(new GPolyline.fromEncoded({
  color: "#0000ff",
  weight: 4,
  opacity: 0.8,
  points: result.encoded_points,
  levels: result.encoded_levels,
  zoomFactor: 32,
  numLevels: 4
}));
</code></pre>
<p>`points` is the encoded lat/long points. `levels` indicates which zoom levels each point should displayed at; there is one character per point. See the links above for a more complete explanation.</p>
]]></content:encoded>
			<wfw:commentRss>http://wyattbaldwin.com/2007/04/19/google-maps-encoded-polylines/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>
