Skip to content

Instantly share code, notes, and snippets.

@benjaminparnell
Created October 31, 2013 18:29
Show Gist options
  • Save benjaminparnell/7254574 to your computer and use it in GitHub Desktop.
Save benjaminparnell/7254574 to your computer and use it in GitHub Desktop.
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