Created
October 31, 2013 18:29
-
-
Save benjaminparnell/7254574 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node | |
constructor: (@name, @time, @next = null) -> | |
each: (callback) -> | |
current = this | |
while current.next? | |
current = current.next | |
callback current | |
a = new Node "a", 0 | |
a.next = new Node "b", 20 | |
a.next.next = new Node "c", 30 | |
b = new Node "b", 0 | |
b.next = new Node "d", 10 | |
c = new Node "c", 0 | |
c.next = new Node "d", 20 | |
graph = {} | |
graph["a"] = a | |
graph["b"] = b | |
graph["c"] = c | |
graph["d"] = new Node "d", 0 | |
get_cpath = (graph, path, start, end_node, path_lengths = []) -> | |
graph[start].each (node) -> | |
if node.name = end_node | |
path += node.time | |
path_lengths.push path | |
get_cpath graph, path + node.time, node.name, end_node, path_lengths | |
path_lengths.sort().slice(-1) | |
get_cpath graph, 0, "a", "d" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment