Skip to content

Commit

Permalink
restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
semibran committed Mar 6, 2018
1 parent 4be0b39 commit 51914dc
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions lib/graph-mst.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 51914dc

Please sign in to comment.