Posts

Showing posts from October, 2012

Dijkstra's Algorithm in JavaScript

Image
I happend to read through a series of blog posts by Nicholas C. Zakas here -  http://www.nczonline.net/blog/2009/06/09/computer-science-in-javascript-binary-search-tree-part-1 . These are really interesting thoughts. So I decided to take it another step by implementing the shortest span/path solution using Dijkstra's Algorithm. A beautiful explanation of the algorithm is available in YouTube at -  http://www.youtube.com/watch?v=8Ls1RqHCOPw  - so that I can stay focussed on the implementation part. Now, let me walk you through the steps taken to implement the algorithm in javascript. First of all, I need to define the graph that I am going to traverse. The best way to represent the graph is in metrix form, where each of the entry will indicate the weighted edge from one node to the other. To do this in JS, I created an array of arrays, with a header element at the first row. Figure 1. The above graph can be represented as follows.     [['A',