Skip to content

Commit 5dd4d7e

Browse files
author
Kenneth Apeland
committed
fikset noen bugs og knapper, men backwards fungerer ikke i dfs
1 parent 473a76e commit 5dd4d7e

File tree

4 files changed

+73
-58
lines changed

4 files changed

+73
-58
lines changed

Graphs/js/dfsBfsController.js

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var highlightEventDuration = 0;
1010
var visitEventDuration = 2000;
1111
var paused = true;
1212
var algoRunning = "";
13+
var color = [];
1314
function helpStartBfs() {
1415
if (algoRunning != "BFS") {
1516
resetForNewAlgo();
@@ -40,11 +41,13 @@ function startBfs(startIndex) {
4041
resetVisited(nodes);
4142
bfsQueue = [];
4243
currentNode = startIndex;
44+
var lastElemtent = 0;
4345
bfsQueue.push(startIndex);
4446
while (bfsQueue.length != 0) {
4547
var v = bfsQueue.shift();
4648
popFromBfsQueue(v);
4749
visit(v);
50+
lastElemtent = v;
4851
var adjacent = adjacencyList[v];
4952
for (var i = 0; i < adjacent.length; i++) {
5053
var w = adjacent[i];
@@ -54,7 +57,7 @@ function startBfs(startIndex) {
5457
}
5558
}
5659
}
57-
resetAfterSearch();
60+
removeHighlighting(lastElemtent);
5861
}
5962
function addToBfsQueue(v) {
6063
var forward = function (id) {
@@ -112,21 +115,25 @@ function startDfs(startIndex) {
112115
resetVisited(nodes);
113116
currentNode = startIndex;
114117
dfs(startIndex);
115-
resetAfterSearch();
118+
removeHighlighting(startIndex);
116119
}
117120
function dfs(v) {
118121
visit(v);
119122
var adjacent = adjacencyList[v];
120123
for (var i = 0; i < adjacent.length; i++) {
121124
if (visited[adjacent[i]])
122125
continue;
123-
setHighlightEdge(getEdgeId(v, adjacent[i]), true);
126+
setHighlightEdge(getEdgeId(v, adjacent[i]), true, color[i]);
127+
console.log(color);
128+
color[i] = true;
124129
dfs(adjacent[i]);
125-
setHighlightEdge(getEdgeId(v, adjacent[i]), false);
130+
setHighlightEdge(getEdgeId(v, adjacent[i]), false, color[i]);
131+
color[i] = false;
132+
console.log(color);
126133
visit(v);
127134
}
128135
}
129-
function setHighlightEdge(edgeId, highlight) {
136+
function setHighlightEdge(edgeId, highlight, lastColor) {
130137
var forward = function (id, h) {
131138
return function () {
132139
if (h) {
@@ -137,16 +144,16 @@ function setHighlightEdge(edgeId, highlight) {
137144
} //remove highlight
138145
};
139146
}(edgeId, highlight);
140-
var backwards = function (id, h) {
147+
var backwards = function (id, lastColor) {
141148
return function () {
142-
if (h) {
149+
if (lastColor == true) {
143150
$("#edge" + id).css({ "stroke": "rgb(0, 0, 0)", "stroke-width": "4" });
144-
} //remove highlight
151+
}
145152
else {
146153
$("#edge" + id).css({ "stroke": "rgb(16, 130, 219)", "stroke-width": "6" });
147-
} // add highlight
154+
}
148155
};
149-
}(edgeId, highlight);
156+
}(edgeId, lastColor);
150157
manager.addEvent(new FrontendEvent(forward, backwards, highlightEventDuration));
151158
}
152159
function visit(id) {
@@ -172,23 +179,17 @@ function visit(id) {
172179
manager.addEvent(new FrontendEvent(forward, backwards, visitEventDuration));
173180
currentNode = id;
174181
}
175-
function resetAfterSearch() {
176-
var forward = function (curr) {
182+
function removeHighlighting(v) {
183+
var forward = function (v) {
177184
return function () {
178-
$("#node" + curr).css("border", "6px solid black");
179-
for (var i = 0; i < nodes; i++) {
180-
$("#node" + i).css("background-color", "white");
181-
}
185+
$("#node" + v).css({ "border-color": "black" });
182186
};
183-
}(currentNode);
184-
var backwards = function (curr) {
187+
}(v);
188+
var backwards = function (v) {
185189
return function () {
186-
$("#node" + curr).css("border", "6px solid rgb(16, 130, 219)");
187-
for (var i = 0; i < nodes; i++) {
188-
$("#node" + i).css("background-color", "rgb(80, 250, 80)");
189-
}
190+
$("#node" + v).css("border", "6px solid rgb(16, 130, 219)");
190191
};
191-
}(currentNode);
192+
}(v);
192193
manager.addEvent(new FrontendEvent(forward, backwards, visitEventDuration));
193194
}
194195
function resetVisited(numNodes) {

Graphs/js/dfsBfsController.ts

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ let highlightEventDuration = 0;
1111
let visitEventDuration = 2000;
1212
let paused = true;
1313
let algoRunning = "";
14+
let color: boolean[] = [];
1415

1516
function helpStartBfs() {
1617
if (algoRunning != "BFS") {
@@ -44,13 +45,14 @@ function startBfs(startIndex: number) {
4445
resetVisited(nodes);
4546
bfsQueue = [];
4647
currentNode = startIndex;
47-
48+
let lastElemtent: number = 0;
4849
bfsQueue.push(startIndex);
4950

5051
while (bfsQueue.length != 0) {
5152
let v = bfsQueue.shift();
5253
popFromBfsQueue(v);
5354
visit(v);
55+
lastElemtent = v;
5456

5557
let adjacent = adjacencyList[v];
5658
for (let i = 0; i < adjacent.length; i++) {
@@ -61,8 +63,7 @@ function startBfs(startIndex: number) {
6163
}
6264
}
6365
}
64-
65-
resetAfterSearch();
66+
removeHighlighting(lastElemtent);
6667
}
6768

6869
function addToBfsQueue(v: number) {
@@ -128,7 +129,7 @@ function startDfs(startIndex: number) {
128129
resetVisited(nodes);
129130
currentNode = startIndex;
130131
dfs(startIndex);
131-
resetAfterSearch();
132+
removeHighlighting(startIndex);
132133
}
133134

134135
function dfs(v: number) {
@@ -137,14 +138,16 @@ function dfs(v: number) {
137138
let adjacent = adjacencyList[v];
138139
for (let i = 0; i < adjacent.length; i++) {
139140
if (visited[adjacent[i]]) continue;
140-
setHighlightEdge(getEdgeId(v, adjacent[i]), true);
141+
setHighlightEdge(getEdgeId(v, adjacent[i]), true, color[i]);
142+
color[i] = true;
141143
dfs(adjacent[i]);
142-
setHighlightEdge(getEdgeId(v, adjacent[i]), false);
144+
setHighlightEdge(getEdgeId(v, adjacent[i]), false, color[i]);
145+
color[i] = false;
143146
visit(v);
144147
}
145148
}
146149

147-
function setHighlightEdge(edgeId: number, highlight: boolean) {
150+
function setHighlightEdge(edgeId: number, highlight: boolean, lastColor: boolean) {
148151
let forward = function (id, h) {
149152
return function () {
150153
if (h) {
@@ -156,16 +159,15 @@ function setHighlightEdge(edgeId: number, highlight: boolean) {
156159
};
157160
}(edgeId, highlight);
158161

159-
let backwards = function (id, h) {
162+
let backwards = function (id, lastColor) {
160163
return function () {
161-
if (h) {
164+
if (lastColor == true) {
162165
$("#edge" + id).css({"stroke": "rgb(0, 0, 0)", "stroke-width": "4"});
163-
} //remove highlight
164-
else {
166+
} else {
165167
$("#edge" + id).css({"stroke": "rgb(16, 130, 219)", "stroke-width": "6"});
166-
} // add highlight
168+
}
167169
};
168-
}(edgeId, highlight);
170+
}(edgeId, lastColor);
169171

170172
manager.addEvent(new FrontendEvent(forward, backwards, highlightEventDuration))
171173
}
@@ -196,26 +198,20 @@ function visit(id: number) {
196198
currentNode = id;
197199
}
198200

199-
function resetAfterSearch() {
200-
let forward = function (curr) {
201+
function removeHighlighting(v: number) {
202+
let forward = function (v) {
201203
return function () {
202-
$("#node" + curr).css("border", "6px solid black");
203-
for (let i = 0; i < nodes; i++) {
204-
$("#node" + i).css("background-color", "white");
205-
}
204+
$("#node" + v).css({"border-color": "black"});
206205
};
207-
}(currentNode);
206+
}(v);
208207

209-
let backwards = function (curr) {
208+
let backwards = function (v) {
210209
return function () {
211-
$("#node" + curr).css("border", "6px solid rgb(16, 130, 219)");
212-
for (let i = 0; i < nodes; i++) {
213-
$("#node" + i).css("background-color", "rgb(80, 250, 80)");
214-
}
210+
$("#node" + v).css("border", "6px solid rgb(16, 130, 219)");
215211
};
216-
}(currentNode);
212+
}(v);
217213

218-
manager.addEvent(new FrontendEvent(forward, backwards, visitEventDuration));
214+
manager.addEvent(new FrontendEvent(forward, backwards, visitEventDuration))
219215
}
220216

221217
function resetVisited(numNodes: number) {

Graphs/js/graphStructureController.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,26 @@ function resetAll() {
2323
manager.clear();
2424
nodes = 0;
2525
edges = 0;
26+
$("#dfs").html("DFS");
27+
$("#bfs").html("BFS");
28+
algoRunning = "";
2629
}
2730
function resetForNewAlgo() {
2831
manager.clear();
2932
$("#queueUI").find("div.nodeUI").each(function () {
3033
$(this).remove();
3134
});
32-
$("#edge").each(function () {
33-
$(this).css({ "stroke": "rgb(0, 0, 0)", "stroke-width": "4" });
34-
});
35+
for (var i = 0; i < nodes; i++) {
36+
$("#edge" + i).css({ "stroke": "rgb(0, 0, 0)", "stroke-width": "4" });
37+
}
38+
for (var i = 0; i < nodes; i++) {
39+
$("#node" + i).css({ "background-color": "white", "border-color": "black" });
40+
}
3541
deselectAllNodes();
3642
removeVisitedArray();
37-
resetAfterSearch();
43+
for (var i = 0; i < nodes; i++) {
44+
color.push(false);
45+
}
3846
}
3947
function checkOverlap(x, y) {
4048
var overlap = false;

Graphs/js/graphStructureController.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,29 @@ function resetAll() {
3030
manager.clear();
3131
nodes = 0;
3232
edges = 0;
33+
$("#dfs").html("DFS");
34+
$("#bfs").html("BFS");
35+
algoRunning = "";
3336
}
3437

3538
function resetForNewAlgo() {
3639
manager.clear();
3740
$("#queueUI").find("div.nodeUI").each(function () {
3841
$(this).remove();
3942
});
40-
$("#edge").each(function () {
41-
$(this).css({"stroke": "rgb(0, 0, 0)", "stroke-width": "4"});
42-
});
43+
for (let i = 0; i < nodes; i++) {
44+
$("#edge" + i).css({"stroke": "rgb(0, 0, 0)", "stroke-width": "4"});
45+
}
46+
for (let i = 0; i < nodes; i++) {
47+
$("#node" + i).css({"background-color" : "white", "border-color" : "black"});
48+
}
49+
4350
deselectAllNodes();
4451
removeVisitedArray();
45-
resetAfterSearch();
52+
53+
for (let i = 0; i < nodes; i++) {
54+
color.push(false);
55+
}
4656
}
4757

4858
function checkOverlap(x: number, y: number) {

0 commit comments

Comments
 (0)