|
1 | | -function kruskal () { |
2 | | - var vcount = G.length; |
3 | | - |
4 | | - // Preprocess: sort edges by weight. |
5 | | - var edges = []; |
6 | | - for (var vi = 0; vi < vcount - 1; vi++) { |
7 | | - for (var vj = vi + 1; vj < vcount; vj++) { |
8 | | - edges.push({ |
9 | | - 0: vi, |
10 | | - 1: vj, |
11 | | - weight: G[vi][vj] |
12 | | - }); |
| 1 | +function kruskal() { |
| 2 | + var vcount = G.length; |
| 3 | + |
| 4 | + // Preprocess: sort edges by weight. |
| 5 | + var edges = []; |
| 6 | + for (var vi = 0; vi < vcount - 1; vi++) { |
| 7 | + for (var vj = vi + 1; vj < vcount; vj++) { |
| 8 | + edges.push({ |
| 9 | + 0: vi, |
| 10 | + 1: vj, |
| 11 | + weight: G[vi][vj] |
| 12 | + }); |
| 13 | + } |
13 | 14 | } |
14 | | - } |
15 | | - edges.sort(function (ei, ej) { return ei.weight - ej.weight }); |
16 | | - |
17 | | - // Give each vertex a tree to decide if they are already in the same tree. |
18 | | - var t = []; |
19 | | - for (var i = 0; i < vcount; i++) { |
20 | | - t[i] = {}; |
21 | | - t[i][i] = true; |
22 | | - } |
23 | | - |
24 | | - var wsum = 0; |
25 | | - for (var n = 0; n < vcount - 1 && edges.length > 0;) { |
26 | | - var e = edges.shift(); // Get the edge of min weight |
27 | | - tracer._visit(e[0], e[1]); |
28 | | - if (t[e[0]] === t[e[1]]) { |
29 | | - // e[0] & e[1] already in the same tree, ignore |
30 | | - tracer._leave(e[0], e[1]); |
31 | | - continue; |
| 15 | + edges.sort(function (ei, ej) { |
| 16 | + return ei.weight - ej.weight |
| 17 | + }); |
| 18 | + |
| 19 | + // Give each vertex a tree to decide if they are already in the same tree. |
| 20 | + var t = []; |
| 21 | + for (var i = 0; i < vcount; i++) { |
| 22 | + t[i] = {}; |
| 23 | + t[i][i] = true; |
32 | 24 | } |
33 | 25 |
|
34 | | - // Choose the current edge. |
35 | | - wsum += e.weight; |
36 | | - |
37 | | - // Merge tree of e[0] & e[1] |
38 | | - var tmerged = {}; |
39 | | - for (i in t[e[0]]) tmerged[i] = true; |
40 | | - for (i in t[e[1]]) tmerged[i] = true; |
41 | | - for (i in tmerged) t[i] = tmerged; |
42 | | - |
43 | | - n += 1; |
44 | | - } |
| 26 | + var wsum = 0; |
| 27 | + for (var n = 0; n < vcount - 1 && edges.length > 0;) { |
| 28 | + var e = edges.shift(); // Get the edge of min weight |
| 29 | + tracer._visit(e[0], e[1])._wait(); |
| 30 | + if (t[e[0]] === t[e[1]]) { |
| 31 | + // e[0] & e[1] already in the same tree, ignore |
| 32 | + tracer._leave(e[0], e[1])._wait(); |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // Choose the current edge. |
| 37 | + wsum += e.weight; |
| 38 | + |
| 39 | + // Merge tree of e[0] & e[1] |
| 40 | + var tmerged = {}; |
| 41 | + for (i in t[e[0]]) tmerged[i] = true; |
| 42 | + for (i in t[e[1]]) tmerged[i] = true; |
| 43 | + for (i in tmerged) t[i] = tmerged; |
| 44 | + |
| 45 | + n += 1; |
| 46 | + } |
45 | 47 |
|
46 | | - tracer._print("The sum of all edges is: " + wsum); |
| 48 | + logger._print("The sum of all edges is: " + wsum); |
47 | 49 | } |
48 | 50 |
|
49 | | -tracer._pace(500); |
50 | 51 | kruskal(); |
0 commit comments