Skip to content

Commit dd4ac22

Browse files
Fikset backward
1 parent 1eccbb2 commit dd4ac22

File tree

8 files changed

+147
-43
lines changed

8 files changed

+147
-43
lines changed

Kruskal/js/KruskalAlgorithm.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ function startKruskal() {
1818
currentEdge = getEdgeId(node1, node2);
1919
writeEdge(currentEdge, node1, node2, weight);
2020
}
21-
var sum = 0;
2221
var j = 0;
2322
while (edgeList.length > 0) {
2423
// Optimization -> Stop algorithm after n-1 edges are in the MST
@@ -33,8 +32,7 @@ function startKruskal() {
3332
controller.highlightEdgeText(currentEdge);
3433
controller.highlightMyEdge(currentEdge);
3534
union(node1, node2);
36-
sum = sum + weight;
37-
controller.addWeightToSum(sum);
35+
controller.addWeightToSum(weight);
3836
j++;
3937
}
4038
else {

Kruskal/js/KruskalAlgorithm.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ function startKruskal() {
2424
writeEdge(currentEdge, node1, node2, weight);
2525
}
2626

27-
let sum = 0;
2827
let j: number = 0;
2928

3029
while (edgeList.length > 0) {
@@ -43,8 +42,7 @@ function startKruskal() {
4342
controller.highlightMyEdge(currentEdge);
4443

4544
union(node1, node2);
46-
sum = sum + weight;
47-
controller.addWeightToSum(sum);
45+
controller.addWeightToSum(weight);
4846

4947
j++;
5048
} else {
@@ -57,7 +55,7 @@ function startKruskal() {
5755
arr = [];
5856
queue = [];
5957
currentEdge = 0;
60-
58+
6159
controller.enableStartButtion();
6260
}
6361

Kruskal/js/Methods.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var randomWeight = 0;
77
var nodes = 0;
88
var MAX_NODES = 10;
99
var edges = 0;
10+
var totalWeight = 0;
1011
function highlightThisEdge(index) {
1112
$("#edge" + index).css({ "stroke": "rgb(16, 130, 219)", "stroke-width": "6" });
1213
}
@@ -21,7 +22,7 @@ function transparentEdge(index) {
2122
$("#edge" + index).css({ "opacity": 0.15 });
2223
$("#edgeWeight" + index).css({ "opacity": 0.15 });
2324
}
24-
function detransparentEdge(index) {
25+
function deTransparentEdge(index) {
2526
$("#edge" + index).css({ "opacity": 1 });
2627
$("#edgeWeight" + index).css({ "opacity": 1 });
2728
}
@@ -31,6 +32,9 @@ function addThisNode(x, y) {
3132
nodes++;
3233
}
3334
}
35+
function removeThisNode() {
36+
removeNode(--nodes);
37+
}
3438
function selectNodes(n1, n2, select) {
3539
if (select == true) {
3640
$("#node" + n1).addClass("selected");
@@ -45,6 +49,9 @@ function connectNodes(node1, node2) {
4549
randomWeight = Math.floor(Math.random() * 10) + 1;
4650
addWeightedEdge(edges++, node1, node2, randomWeight);
4751
}
52+
function removeConnectedNodes() {
53+
removeWeightedEdge(--edges);
54+
}
4855
function disableButton() {
4956
$("#start").attr({ disabled: "true" });
5057
$("#start").css({ "opacity": 0.15 });
@@ -100,8 +107,9 @@ function writeEdge(i, node1, node2, weight) {
100107
}
101108
}
102109
function writeTotalWeight(weight) {
110+
totalWeight += weight;
103111
$("#totalWeight").empty();
104-
$("#totalWeight").append("<p> Total weight: " + weight + " </p>");
112+
$("#totalWeight").append("<p> Total weight: " + totalWeight + " </p>");
105113
}
106114
function clearTotalWeight() {
107115
$("#totalWeight").empty();
@@ -111,6 +119,13 @@ function excludeEdgeText(i) {
111119
$("#edgeContent" + i).css({ "opacity": 0.2 });
112120
$("#edgeContent" + i).css({ "color": "black" });
113121
}
122+
function includeEdgeText(i) {
123+
$("#edgeContent" + i).css({ "opacity": 1 });
124+
$("#edgeContent" + i).css({ "color": "black" });
125+
}
114126
function higlightEdgeText(i) {
115127
$("#edgeContent" + i).css({ "color": "blue" });
116128
}
129+
function deHighlightEdgeText(i) {
130+
$("#edgeContent" + i).css({ "color": "black" });
131+
}

Kruskal/js/Methods.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ let randomWeight = 0;
99
let nodes: number = 0;
1010
let MAX_NODES: number = 10;
1111
let edges: number = 0;
12+
let totalWeight: number = 0;
1213

1314
function highlightThisEdge(index: number) {
1415
$("#edge" + index).css({ "stroke": "rgb(16, 130, 219)", "stroke-width": "6" });
@@ -28,7 +29,7 @@ function transparentEdge(index: number) {
2829
$("#edgeWeight" + index).css({ "opacity": 0.15 });
2930
}
3031

31-
function detransparentEdge(index: number) {
32+
function deTransparentEdge(index: number) {
3233
$("#edge" + index).css({ "opacity": 1 });
3334
$("#edgeWeight" + index).css({ "opacity": 1 });
3435
}
@@ -40,6 +41,10 @@ function addThisNode(x: number, y: number) {
4041
}
4142
}
4243

44+
function removeThisNode() {
45+
removeNode(--nodes);
46+
}
47+
4348
function selectNodes(n1: number, n2: number, select: boolean) {
4449
if (select == true) {
4550
$("#node" + n1).addClass("selected");
@@ -56,6 +61,10 @@ function connectNodes(node1: number, node2: number) {
5661
addWeightedEdge(edges++, node1, node2, randomWeight);
5762
}
5863

64+
function removeConnectedNodes() {
65+
removeWeightedEdge(--edges);
66+
}
67+
5968
function disableButton() {
6069
$("#start").attr({ disabled: "true" });
6170
$("#start").css({ "opacity": 0.15 });
@@ -104,7 +113,6 @@ function drawGraph(n: number) {
104113
graph3();
105114
break;
106115
}
107-
108116
}
109117

110118
function writeEdge(i: number, node1: number, node2: number, weight: number) {
@@ -118,8 +126,9 @@ function writeEdge(i: number, node1: number, node2: number, weight: number) {
118126
}
119127

120128
function writeTotalWeight(weight: number) {
129+
totalWeight += weight;
121130
$("#totalWeight").empty();
122-
$("#totalWeight").append("<p> Total weight: " + weight + " </p>");
131+
$("#totalWeight").append("<p> Total weight: " + totalWeight + " </p>");
123132
}
124133

125134
function clearTotalWeight() {
@@ -133,6 +142,15 @@ function excludeEdgeText(i: number) {
133142
$("#edgeContent" + i).css({ "color": "black" });
134143
}
135144

145+
function includeEdgeText(i: number) {
146+
$("#edgeContent" + i).css({ "opacity": 1 });
147+
$("#edgeContent" + i).css({ "color": "black" });
148+
}
149+
136150
function higlightEdgeText(i: number) {
137151
$("#edgeContent" + i).css({ "color": "blue" });
152+
}
153+
154+
function deHighlightEdgeText(i: number) {
155+
$("#edgeContent" + i).css({ "color": "black" });
138156
}

Kruskal/js/View.js

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ var View = /** @class */ (function () {
3838
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
3939
};
4040
View.prototype.removeEdge = function (edgeId) {
41+
var _a = getEdgeInfo(edgeId), node1 = _a[0], node2 = _a[1], weight = _a[2];
4142
var forward = function (edgeId) {
4243
return function () {
4344
removeEdge(edgeId);
4445
};
4546
}(edgeId);
4647
var backward = function (edgeId) {
4748
return function () {
48-
removeEdge(edgeId);
49+
addWeightedEdge(edgeId, node1, node2, weight);
4950
};
5051
}(edgeId);
5152
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
@@ -58,15 +59,15 @@ var View = /** @class */ (function () {
5859
}(edgeId);
5960
var backward = function (edgeId) {
6061
return function () {
61-
detransparentEdge(edgeId);
62+
deTransparentEdge(edgeId);
6263
};
6364
}(edgeId);
6465
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
6566
};
66-
View.prototype.detransparentEdge = function (edgeId) {
67+
View.prototype.deTransparentEdge = function (edgeId) {
6768
var forward = function (edgeId) {
6869
return function () {
69-
detransparentEdge(edgeId);
70+
deTransparentEdge(edgeId);
7071
};
7172
}(edgeId);
7273
var backward = function (edgeId) {
@@ -82,12 +83,11 @@ var View = /** @class */ (function () {
8283
addThisNode(x, y);
8384
};
8485
}(x, y);
85-
var backward = function (x, y) {
86+
var backward = function () {
8687
return function () {
87-
//Skal være rmeove her
88-
addThisNode(x, y);
88+
removeThisNode();
8989
};
90-
}(x, y);
90+
}();
9191
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
9292
};
9393
View.prototype.connectTheseNodes = function (node1, node2) {
@@ -98,8 +98,7 @@ var View = /** @class */ (function () {
9898
}(node1, node2);
9999
var backward = function (node1, node2) {
100100
return function () {
101-
//Skal være remove her
102-
addThisNode(node1, node2);
101+
removeConnectedNodes();
103102
};
104103
}(node1, node2);
105104
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
@@ -162,23 +161,38 @@ var View = /** @class */ (function () {
162161
excludeEdgeText(i);
163162
};
164163
}(i);
165-
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
164+
var backward = function (i) {
165+
return function () {
166+
includeEdgeText(i);
167+
};
168+
}(i);
169+
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
166170
};
167171
View.prototype.highlighText = function (i) {
168172
var forward = function (i) {
169173
return function () {
170174
higlightEdgeText(i);
171175
};
172176
}(i);
173-
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
177+
var backward = function (i) {
178+
return function () {
179+
deHighlightEdgeText(i);
180+
};
181+
}(i);
182+
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
174183
};
175184
View.prototype.addWeightToSum = function (weight) {
176185
var forward = function (weight) {
177186
return function () {
178187
writeTotalWeight(weight);
179188
};
180189
}(weight);
181-
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
190+
var backward = function (weight) {
191+
return function () {
192+
writeTotalWeight(-weight);
193+
};
194+
}(weight);
195+
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
182196
};
183197
View.prototype.resetAll = function () {
184198
resetGraphUI();
@@ -221,7 +235,17 @@ var View = /** @class */ (function () {
221235
}
222236
};
223237
}(edgeList);
224-
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
238+
var backward = function (edgeList) {
239+
return function () {
240+
for (var index in edgeList) {
241+
var _a = edgeList[index], node1 = _a[0], node2 = _a[1], weight = _a[2];
242+
var currentEdge_2 = getEdgeId(node1, node2);
243+
includeEdgeText(currentEdge_2);
244+
deTransparentEdge(currentEdge_2);
245+
}
246+
};
247+
}(edgeList);
248+
manager.addEvent(new FrontendEvent(forward, backward, this.highlightEventDuration));
225249
};
226250
return View;
227251
}());

0 commit comments

Comments
 (0)