Skip to content

Instantly share code, notes, and snippets.

@sanandnarayan
Created June 8, 2013 04:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sanandnarayan/5733992 to your computer and use it in GitHub Desktop.
Save sanandnarayan/5733992 to your computer and use it in GitHub Desktop.
shortestDistance
/*
Question: Find the shortest path and distance between two cities
Roads is an array of routes between cities.
The routes are of the format [Source, Destination1, distance, Destination2, distance, etc]
That is ["kangeyam", "hosur", 19,
"faridabad", 15, "trichy", 15]
Signifies that the road betweeen
kangeyam and hosur is 19kms
kangeyam and faridabad is 15kms
kangeyam and trichy is 15kms
All roads are bi-directional. The distance between hosur and kangeyam is also 19kms
------------------
Write a function "shortestDistance" that gives that takes two paramets,
Parameter 1 is source
Parameter 2 is destination
And gives out the shortest path and distance between the two cities
Example
INPUT
shortestDistance("tirupur", "kangeyam")
OUTPUT
{"length": 57, "places": ["tirupur", "palani", "madurai", "cemetery", "himalayas", "arakonam", "trichy", "kangeyam"]}
you can use the http://underscorejs.org/ ...
please use jsbin or jsfiddle
you can take 3 hours to solve the problem, max 4.
Please optimize for code readability, comment your code well.
Keep sharing and saving your code ,so that i can help you during the process if needed.
*/
var roads = [];
roads.push(["kangeyam", "hosur", 19,
"faridabad", 15, "trichy", 15]);
roads.push(["airport", "hosur", 6, "faridabad", 5,
"arakonam", 4, "madurai", 11]);
roads.push(["tambaram", "faridabad", 8, "trichy", 4]);
roads.push(["arakonam", "trichy", 3, "himalayas", 1]);
roads.push(["cemetery", "himalayas", 6, "madurai", 5]);
roads.push(["ooty", "madurai", 3]);
roads.push(["palani", "madurai", 13, "tirupur", 14]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment