Skip to content

Commit 5cd7f9c

Browse files
author
duaraghav8@gmail
committed
Merge remote-tracking branch 'upstream/gh-pages'
2 parents 5556d9f + 7a93107 commit 5cd7f9c

File tree

7 files changed

+86
-23
lines changed

7 files changed

+86
-23
lines changed

algorithm/category.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"mst": {
1414
"name": "Minimum Spanning Tree",
1515
"list": {
16-
"prim": "Prim's Algorithm"
16+
"prim": "Prim's Algorithm",
17+
"kruskal": "Kruskal's Algorithm"
1718
}
1819
},
1920
"search": {

algorithm/mst/kruskal/desc.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Kruskal's Algorithm": "Greedy algorithm that finds a minimum spanning tree for a weighted undirected graph.",
3+
"Applications": [
4+
],
5+
"Complexity": {
6+
"time": "worst O(E log(E))"
7+
},
8+
"References": [
9+
"<a href='https://en.wikipedia.org/wiki/Kruskal%27s_algorithm'>Wikipedia</a>"
10+
],
11+
"files": {
12+
"normal": "Finds minimum spanning tree of a given graph."
13+
}
14+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
}
14+
}
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;
24+
}
25+
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+
}
47+
48+
logger._print("The sum of all edges is: " + wsum);
49+
}
50+
51+
kruskal();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var tracer = new WeightedUndirectedGraphTracer();
2+
var logger = new LogTracer();
3+
/*var G = [ // G[i][j] indicates the weight of the path from the i-th node to the j-th node
4+
[0, 3, 0, 1, 0],
5+
[5, 0, 1, 2, 4],
6+
[1, 0, 0, 2, 0],
7+
[0, 2, 0, 0, 1],
8+
[0, 1, 3, 0, 0]
9+
];*/
10+
var G = WeightedUndirectedGraph.random(5, 1, 1, 9);
11+
tracer._setData(G);

js/module/array2d.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,20 @@ Array2DTracer.prototype = $.extend(true, Object.create(Tracer.prototype), {
1212
this.$table = this.capsule.$table = $('<div class="mtbl-table">');
1313
this.$container.append(this.$table);
1414
},
15-
_notify: function (x1, y1, v1, x2, y2, v2) {
16-
var second = x2 !== undefined && y2 !== undefined;
15+
_notify: function (x, y, v) {
1716
tm.pushStep(this.capsule, {
1817
type: 'notify',
19-
x: x1,
20-
y: y1,
21-
v: v1
22-
}, !second);
23-
if (second) tm.pushStep(this.capsule, {
24-
type: 'notify',
25-
x: x2,
26-
y: y2,
27-
v: v2
18+
x: x,
19+
y: y,
20+
v: v
2821
});
2922
return this;
3023
},
31-
_denotify: function (x1, y1, x2, y2) {
32-
var second = x2 !== undefined && y2 !== undefined;
24+
_denotify: function (x, y) {
3325
tm.pushStep(this.capsule, {
3426
type: 'denotify',
35-
x: x1,
36-
y: y1
37-
});
38-
if (second) tm.pushStep(this.capsule, {
39-
type: 'denotify',
40-
x: x2,
41-
y: y2
27+
x: x,
28+
y: y
4229
});
4330
return this;
4431
},

js/module/tracer_manager.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ TracerManager.prototype = {
153153
this.step(this.traceIndex + 1);
154154
},
155155
visualize: function () {
156-
$('#btn_trace').click();
157156
this.traceIndex = -1;
158157
this.resumeStep();
159158
},

js/script.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ dataEditor.on('change', function () {
3939
tm.deallocateAll();
4040
eval(data);
4141
} catch (err) {
42-
console.error(err);
4342
} finally {
4443
tm.visualize();
4544
tm.removeUnallocated();
@@ -215,6 +214,7 @@ var showInfoToast = function (info) {
215214
};
216215

217216
$('#btn_run').click(function () {
217+
$('#btn_trace').click();
218218
try {
219219
tm.deallocateAll();
220220
eval(dataEditor.getValue());

0 commit comments

Comments
 (0)