-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmst.js
More file actions
55 lines (49 loc) · 1006 Bytes
/
mst.js
File metadata and controls
55 lines (49 loc) · 1006 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
function MST(graph, time) {
this.nodes = graph.nodes.slice();
this.graph = graph;
this.interval = null;
this.done = false;
this.time = time || 100;
this.prim();
}
MST.prototype.prim = function() {
var that = this;
this.interval = setInterval(function() {
if(!that.done)
that.primInside();
else {
clearInterval(that.interval);
that.interval = null;
}
}, this.time);
}
MST.prototype.primInside = function() {
var Q = this.nodes;
if(Q.length == 0) {
this.done = true;
return;
}
var u = extractMinNode(Q);
removeFromArray(Q, u);
for(var i = 0; i < Q.length; ++i) {
var v = Q[i];
var length = getLength(u,v);
if(v.key == null || v.key > length) {
v.parent = u;
v.key = length;
}
}
this.graph.draw();
this.graph.drawNode(u, "#00FF00", 5);
}
function extractMinNode(nodes) {
var j = -1;
var min = null;
for(var i = 0; i < nodes.length; ++i) {
if(min == null || nodes[i].key < min) {
j = i;
min = nodes[i].key;
}
}
return nodes[j];
}