Skip to content

Commit bf3cacd

Browse files
committed
Implementert 'ferdig' tabell, endringer må nok gjøres
1 parent a0ac294 commit bf3cacd

File tree

8 files changed

+185
-6
lines changed

8 files changed

+185
-6
lines changed

Kruskal/js/Controller.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ var Controller = /** @class */ (function () {
4646
Controller.prototype.enableStartButtion = function () {
4747
viewer.enableThisButton();
4848
};
49+
Controller.prototype.excludeEdgeText = function (index) {
50+
viewer.excludeText(index);
51+
};
52+
Controller.prototype.highlightEdgeText = function (index) {
53+
viewer.highlighText(index);
54+
};
55+
Controller.prototype.addWeightToSum = function (weight) {
56+
viewer.addWeightToSum(weight);
57+
};
4958
return Controller;
5059
}());
5160
var controller = new Controller();

Kruskal/js/Controller.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ class Controller {
5858
enableStartButtion() {
5959
viewer.enableThisButton();
6060
}
61+
62+
excludeEdgeText(index: number) {
63+
viewer.excludeText(index);
64+
}
65+
66+
highlightEdgeText(index :number) {
67+
viewer.highlighText(index);
68+
}
69+
70+
addWeightToSum(weight: number) {
71+
viewer.addWeightToSum(weight);
72+
}
6173
}
6274

6375
var controller: Controller = new Controller();

Kruskal/js/KruskalAlgorithm.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,33 @@ function startKruskal() {
1414
for (var i = 0; i < edgeList.length; i++) {
1515
arr[i] = i;
1616
}
17+
var sum = 0;
18+
var j = 0;
1719
while (edgeList.length > 0) {
1820
var _a = edgeList.pop(), node1 = _a[0], node2 = _a[1], weight = _a[2];
1921
currentEdge = getEdgeId(node1, node2);
22+
writeEdge(currentEdge, node1, node2, weight);
2023
controller.selectTwoNodes(node1, node2);
24+
controller.highlightEdgeText(currentEdge);
2125
controller.highlightMyEdge(currentEdge);
26+
console.log("Edge: " + currentEdge);
2227
if (connected(node1, node2) == false) {
2328
union(node1, node2);
29+
sum = sum + weight;
30+
controller.addWeightToSum(sum);
2431
}
2532
else {
2633
controller.dehighlightMyEdge(currentEdge);
2734
controller.transparentMyEdge(currentEdge);
35+
controller.excludeEdgeText(currentEdge);
2836
}
2937
controller.deselectTwoNodes(node1, node2);
38+
j++;
3039
}
40+
/*
41+
swapTwoElements(0, 1);
42+
swapTwoElements(1,2);
43+
*/
3144
arr = [];
3245
queue = [];
3346
currentEdge = 0;

Kruskal/js/KruskalAlgorithm.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,38 @@ function startKruskal() {
2222
arr[i] = i;
2323
}
2424

25+
let sum = 0;
26+
let j: number = 0;
27+
2528
while (edgeList.length > 0) {
2629
let [node1, node2, weight] = edgeList.pop();
30+
2731
currentEdge = getEdgeId(node1, node2);
32+
writeEdge(currentEdge, node1, node2, weight);
33+
2834
controller.selectTwoNodes(node1, node2);
35+
controller.highlightEdgeText(currentEdge);
2936
controller.highlightMyEdge(currentEdge);
37+
console.log("Edge: " + currentEdge);
38+
3039
if (connected(node1, node2) == false) {
3140
union(node1, node2);
41+
sum = sum + weight;
42+
controller.addWeightToSum(sum);
3243
}
3344
else {
3445
controller.dehighlightMyEdge(currentEdge);
3546
controller.transparentMyEdge(currentEdge);
47+
controller.excludeEdgeText(currentEdge);
3648
}
3749
controller.deselectTwoNodes(node1, node2);
50+
j++;
3851
}
52+
/*
53+
swapTwoElements(0, 1);
54+
swapTwoElements(1,2);
55+
*/
56+
3957

4058
arr = [];
4159
queue = [];

Kruskal/js/Methods.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ function numberOfNodes(value) {
7272
}
7373
function drawGraph(n) {
7474
console.log("number" + n);
75+
$("#edgeTable").empty();
76+
viewer.resetAll(); //Not working, button must reset
7577
switch (+n) {
7678
case 3:
77-
console.log("hello");
7879
graph3();
7980
break;
8081
case 4:
81-
console.log(n);
8282
graph4();
8383
break;
8484
case 5:
@@ -100,6 +100,36 @@ function drawGraph(n) {
100100
graph10();
101101
break;
102102
default:
103-
console.log("wassup");
103+
graph3();
104+
break;
104105
}
105106
}
107+
function writeEdge(i, node1, node2, weight) {
108+
if (i != undefined && node1 != undefined && node2 != undefined && weight != undefined) {
109+
$("#edgeTable").append("<li id='edgeElem" + i + "'>" +
110+
"<div class='content' id='edgeContent" + i + "'>" +
111+
"Node [" + node1 + "] " +
112+
"Node [" + node2 + "] " +
113+
" Weight: " + weight + "</div></li>");
114+
}
115+
}
116+
function writeTotalWeight(weight) {
117+
$("#totalWeight").empty();
118+
$("#totalWeight").append("<p> Total weight: " + weight + " </p>");
119+
}
120+
function clearTotalWeight() {
121+
$("#totalWeight").empty();
122+
$("#totalWeight").append("<p> Total weight: 0 </p>");
123+
}
124+
function excludeEdgeText(i) {
125+
$("#edgeContent" + i).css({ "opacity": 0.2 });
126+
$("#edgeContent" + i).css({ "color": "black" });
127+
}
128+
function higlightEdgeText(i) {
129+
$("#edgeContent" + i).css({ "color": "blue" });
130+
}
131+
function swapTwoElements(i, j) {
132+
var a = document.getElementById("edgeContent" + i);
133+
var b = document.getElementById("edgeContent" + j);
134+
a.parentNode.insertBefore(b, a);
135+
}

Kruskal/js/Methods.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@ function numberOfNodes(value: number) {
8888

8989
function drawGraph(n: number) {
9090
console.log("number" + n);
91+
$("#edgeTable").empty();
92+
viewer.resetAll(); //Not working, button must reset
9193

9294
switch (+n) {
9395
case 3:
94-
console.log("hello");
9596
graph3();
9697
break;
9798
case 4:
98-
console.log(n);
9999
graph4();
100100
break;
101101
case 5:
@@ -117,7 +117,48 @@ function drawGraph(n: number) {
117117
graph10();
118118
break;
119119
default:
120-
console.log("wassup");
120+
graph3();
121+
break;
122+
}
123+
124+
}
125+
126+
function writeEdge(i: number, node1: number, node2: number, weight: number) {
127+
if (i != undefined && node1 != undefined && node2 != undefined && weight != undefined) {
128+
$("#edgeTable").append("<li id='edgeElem" + i + "'>" +
129+
"<div class='content' id='edgeContent" + i + "'>" +
130+
"Node [" + node1 + "] " +
131+
"Node [" + node2 + "] " +
132+
" Weight: " + weight + "</div></li>");
121133
}
134+
}
135+
136+
function writeTotalWeight(weight: number) {
137+
$("#totalWeight").empty();
138+
$("#totalWeight").append("<p> Total weight: " + weight + " </p>");
139+
}
140+
141+
function clearTotalWeight() {
142+
$("#totalWeight").empty();
143+
$("#totalWeight").append("<p> Total weight: 0 </p>");
144+
}
145+
146+
147+
function excludeEdgeText(i: number) {
148+
$("#edgeContent" + i).css({"opacity": 0.2});
149+
$("#edgeContent" + i).css({"color": "black"});
150+
151+
152+
}
153+
154+
function higlightEdgeText(i: number) {
155+
$("#edgeContent" + i).css({"color": "blue"});
156+
}
157+
158+
function swapTwoElements(i: number, j: number) {
159+
var a = document.getElementById("edgeContent" + i);
160+
var b = document.getElementById("edgeContent" + j);
161+
a.parentNode.insertBefore(b, a);
162+
122163

123164
}

Kruskal/js/View.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,37 @@ var View = /** @class */ (function () {
156156
}();
157157
manager.addEvent(new FrontendEvent(forward, backward, 10));
158158
};
159+
View.prototype.excludeText = function (i) {
160+
var forward = function (i) {
161+
return function () {
162+
excludeEdgeText(i);
163+
};
164+
}(i);
165+
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
166+
};
167+
View.prototype.highlighText = function (i) {
168+
var forward = function (i) {
169+
return function () {
170+
higlightEdgeText(i);
171+
};
172+
}(i);
173+
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
174+
};
175+
View.prototype.addWeightToSum = function (weight) {
176+
var forward = function (weight) {
177+
return function () {
178+
writeTotalWeight(weight);
179+
};
180+
}(weight);
181+
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
182+
};
159183
View.prototype.resetAll = function () {
160184
resetGraphUI();
161185
manager = new EventManager();
162186
manager.pause();
163187
manager.nextEvents = new Array;
164188
manager.previousEvents = new Array;
189+
clearTotalWeight();
165190
arr = [];
166191
nodes = 0;
167192
edges = 0;

Kruskal/js/View.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,43 @@ class View {
198198
manager.addEvent(new FrontendEvent(forward, backward, 10));
199199
}
200200

201+
excludeText(i: number) {
202+
var forward = function(i) {
203+
return function() {
204+
excludeEdgeText(i);
205+
}
206+
}(i);
207+
208+
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
209+
}
210+
211+
highlighText(i: number) {
212+
var forward = function(i) {
213+
return function() {
214+
higlightEdgeText(i);
215+
}
216+
}(i);
217+
218+
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
219+
}
220+
221+
addWeightToSum(weight: number) {
222+
var forward = function(weight) {
223+
return function() {
224+
writeTotalWeight(weight);
225+
}
226+
} (weight);
227+
228+
manager.addEvent(new FrontendEvent(forward, forward, this.highlightEventDuration));
229+
}
230+
201231
resetAll() {
202232
resetGraphUI();
203233
manager = new EventManager();
204234
manager.pause();
205235
manager.nextEvents = new Array;
206236
manager.previousEvents = new Array;
237+
clearTotalWeight();
207238
arr = [];
208239
nodes = 0;
209240
edges = 0;

0 commit comments

Comments
 (0)