diff --git a/lib/graph-mst.js b/lib/graph-mst.js index f830c4c..1cec773 100644 --- a/lib/graph-mst.js +++ b/lib/graph-mst.js @@ -6,30 +6,26 @@ module.exports = function span(graph) { var edges = graph.edges .slice() .sort(function (a, b) { return a.weight - b.weight }) - var indices = [ 0 ] - while (indices.length < graph.nodes.length) { - for (var i = 0; i < edges.length; i++) { - var edge = edges[i] - var a = edge.endpoints[0] - var b = edge.endpoints[1] - for (var j = 0; j < indices.length; j++) { - var o = indices[j] - var t = -1 - if (a === o) { - t = b - } else if (b === o) { - t = a - } - if (t !== -1) { - break - } + var nodes = [ 0 ] + for (var i = 0; nodes.length < graph.nodes.length; i++) { + var edge = edges[i] + var a = edge.endpoints[0] + var b = edge.endpoints[1] + for (var j = 0; j < nodes.length; j++) { + var origin = nodes[j] + var target = -1 + if (a === origin) { + target = b + } else if (b === origin) { + target = a } - if (t !== -1) { - if (indices.indexOf(t) === -1) { - indices.push(t) + if (target !== -1) { + if (nodes.indexOf(target) === -1) { + nodes.push(target) tree.edges.push(edge) + edges.splice(i, 1) + i = -1 } - edges.splice(i, 1) break } }