-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.coffee
More file actions
31 lines (27 loc) · 882 Bytes
/
algorithms.coffee
File metadata and controls
31 lines (27 loc) · 882 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
module.exports =
blank:
name: "Empty"
text: ""
dijkstra:
name: "Dijkstra's algorithm"
text: """nodes = ({ node: n, cost: Infinity } for n in [0...VG.getNodeCount()])
nodes[0].cost = 0
VG.highlightNode(0, "red") # start node
unvisited = nodes.slice()
while unvisited.length
current = do ->
minIndex = 0
for node, i in unvisited
if unvisited[minIndex].cost > node.cost
minIndex = i
unvisited.splice(minIndex, 1)[0]
VG.highlightNode(current.node, "green")
for adj in VG.getAdjacentNodes(current.node)
VG.highlightEdge(current.node, adj.node, "green")
if nodes[adj.node].cost > current.cost + adj.cost
nodes[adj.node].cost = current.cost + adj.cost
VG.unhighlightEdge(current.node, adj.node)
if current.node == 0
VG.highlightNode(current.node, "red")
else
VG.highlightNode(current.node, "orange")"""