Monthly Archive for August, 2008

Twitter Updates for 2008-08-21

  • “Rational self-interest.” Talk amongst yourselves. #

Implementation of Dijkstra’s Single-Source Shortest-Paths in JavaScript

I’m working on a project where the client wants a cool sliding navigation effect. We’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’re on the /portland/contact page and want to go to the /seattle/contact page, you’ll first slide up to /portland, then over to /seattle, then finally down to /seattle/contact.

After a while, it occurred to me that there were some similarities with another project I’ve been working on off and on for the last few years, byCycle.org, which is a bicycle trip planner ala Google Maps.

I had written a Python version of Dijkstra’s Single-Source Shortest-Paths (SSSP) for byCycle.org. That’s available on PyPi as Dijkstar (so named because it also does has the potential to do A*). I figured it wouldn’t be too hard to port the Python version to JavaScript, and it wasn’t.

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 “heapq“ 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 “dict“) with costs to newly visited nodes and sorting the keys to pick the next node to visit. I’m assuming/hoping the underlying sort implementation is highly optimized.

Interestingly, I think I found at least one bug in the Python version, although I’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’s due to some difference in the languages–need to take a closer look). I think the JS version came out cleaner, too.

If anyone’s interested, I’m releasing this under an MIT license. For now, you can get it from here. Note that it depends on the util module that you can get from here. 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.

Twitter Updates for 2008-08-16

  • Finished reading Tortilla Curtain, starting Atlas Shrugged. #

Twitter Updates for 2008-08-13

  • started reading “Tortilla Curtain” yesterday. Only 20 pages in, and I can tell it’s gonna be excellent. #

Twitter Updates for 2008-08-12

  • @bike4myLIFE Dragonforce #
  • theShorts just tore into a bag of goji berries and started eating them. #
  • theShorts just got a reflective, breakaway kitty collar, complete with name tag. She looks more official now, somehow. #

Twitter Updates for 2008-08-11

  • saw a bunch of dolphins yesterday. Mostly fins, but one jumped up and I saw it’s face through binocs. #

Twitter Updates for 2008-08-10

  • iPhone v2.0.1 is available, FYI. #
  • My mom’s new cat, Cougar-Cossette, eats mango and peaches. #