Skip to content

Commit

Permalink
slight optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
semibran committed Mar 6, 2018
1 parent 316d311 commit f6be136
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/graph-mst.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,35 @@
module.exports = function span(graph) {
var prospects = graph.edges.slice()
var edges = []
var indices = [ 0 ]
while (indices.length < graph.nodes.length) {
while (prospects.length && indices.length < graph.nodes.length) {
var shortest = null
var target = null
var index = null
for (var i = 0; i < indices.length; i++) {
var index = indices[i]
var node = graph.nodes[index]
for (var j = 0; j < graph.edges.length; j++) {
var edge = graph.edges[j]
var o = indices[i]
for (var j = 0; j < prospects.length; j++) {
var edge = prospects[j]
var a = edge.endpoints[0]
var b = edge.endpoints[1]
var t = -1
if (a === index) {
if (a === o) {
t = b
} else if (b === index) {
} else if (b === o) {
t = a
}
if ((t !== -1 && indices.indexOf(t) === -1)
&& (!shortest || edge.weight < shortest.weight)
) {
if (t === -1 || indices.indexOf(t) !== -1) continue
if (!shortest || edge.weight < shortest.weight) {
shortest = edge
target = t
index = j
}
}
}
if (shortest) {
edges.push(shortest)
indices.unshift(target)
indices.push(target)
prospects.splice(index, 1)
}
}
return {
Expand Down

0 comments on commit f6be136

Please sign in to comment.