Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Some exercises in javascript, implementing common algorithms.
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
README.md
dijkstra.js
server.js

README.md

algorithms-js

The purpose of this project is an exercise for hackerschool. The purpose is to refamiliarize myself with common algorithms while learning a bit about the architecture of node.js web applications, with some light front end work. The idea is to see for common graph algorithms: is it faster in your browser or to offload it to the cloud?

As it is now, the application listens on port 3000 for a JSON POST request, where the JSON adjacency matrix describes a directed weighted graph of the form:

[
  [ _, _, _, _, _, _, _, _, 4, 2, 3 ],
  [ _, _, 5, 2, 2, _, _, _, _, _, _ ],
  [ _, 5, _, _, _, 1, 4, _, _, _, _ ],
  [ _, 2, _, _, 3, 6, _, 3, _, _, _ ],
  [ _, 2, _, 3, _, _, _, 4, 3, _, _ ],
  [ _, _, 1, 6, _, _, 2, 5, _, _, _ ],
  [ _, _, 4, _, _, 2, _, 5, _, _, 3 ],
  [ _, _, _, 3, 4, 5, 5, _, 3, 2, 4 ],
  [ 4, _, _, _, 3, _, _, 3, _, 3, _ ],
  [ 2, _, _, _, _, _, _, 2, 3, _, 3 ],
  [ 3, _, _, _, _, _, 3, 4, _, 3, _ ]
];

The output will show the shortest path from the first node in the adjacency matrix to all other nodes in the graph.

Setup

You should have node and npm installed. I'm using the latest stable node version, v0.6.10. That version comes with npm out of the box. You'll also want curl. You'll want the node packages this application requires, namely express.

You can get there by cloning the node repo anywhere you like, configure/make/sudo make install it, clone my repo, cd into it, npm install express.

Usage

It ain't pretty (yet). And it's a bit broken due to the fact that JSON doesn't support Infinity/-Infinity/NaN, which are used to indicated that there is no edge between two nodes in the graph.

git clone git@github.com:jconnolly/algorithms-js.git
cd algorithms-js
node server.js

Then we'll issue the JSON Post from curl. This will be done in the browser soon, but for now I'm testing with curl.

curl -H "Content-Type:application/json" -X POST -d '[ [ "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", 4, 2, 3 ], [ "Infinity", "Infinity", 5, 2, 2, "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", "Infinity" ], [ "Infinity", 5, "Infinity", "Infinity", "Infinity", 1, 4, "Infinity", "Infinity", "Infinity", "Infinity" ], [ "Infinity", 2, "Infinity", "Infinity", 3, 6, "Infinity", 3, "Infinity", "Infinity", "Infinity" ], [ "Infinity", 2, "Infinity", 3, "Infinity", "Infinity", "Infinity", 4, 3, "Infinity", "Infinity" ], [ "Infinity", "Infinity", 1, 6, "Infinity", "Infinity", 2, 5, "Infinity", "Infinity", "Infinity" ], [ "Infinity", "Infinity", 4, "Infinity", "Infinity", 2, "Infinity", 5, "Infinity", "Infinity", 3 ], [ "Infinity", "Infinity", "Infinity", 3, 4, 5, 5, "Infinity", 3, 2, 4 ], [ 4, "Infinity", "Infinity", "Infinity", 3, "Infinity", "Infinity", 3, "Infinity", 3, "Infinity" ], [ 2, "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", 2, 3, "Infinity", 3 ], [ 3, "Infinity", "Infinity", "Infinity", "Infinity", "Infinity", 3, 4, "Infinity", 3, "Infinity" ] ]' http://localhost:3000

output:

[null,[],[2],[3],[4],[2,5],[2,5,6],[3,7],[4,8],[3,7,9],[3,7,10]] 

Where each element i of the outer array contains a path to the ith node defined in our adjacency matrix.

License

(The MIT License)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.