-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyclicPathTrace.js
68 lines (56 loc) · 2.28 KB
/
cyclicPathTrace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// For delay and wait
function colorPromise() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 1000);
})
}
async function isGraphCylicTracePath(graphComponentMatrix, cycleResponse) {
let [srcr, srcc] = cycleResponse;
let visited = []; // Node visit trace
let dfsVisited = []; // Stack visit trace
for (let i = 0; i < rows; i++) {
let visitedRow = [];
let dfsVisitedRow = [];
for (let j = 0; j < cols; j++) {
visitedRow.push(false);
dfsVisitedRow.push(false);
}
visited.push(visitedRow);
dfsVisited.push(dfsVisitedRow);
}
let response = await dfsCycleDetectionTracePath(graphComponentMatrix, srcr, srcc, visited, dfsVisited);
if (response === true) return Promise.resolve(true);
return Promise.resolve(false);
}
// Coloring cell for tracking
async function dfsCycleDetectionTracePath(graphComponentMatrix, srcr, srcc, visited, dfsVisited) {
visited[srcr][srcc] = true;
dfsVisited[srcr][srcc] = true;
let cell = document.querySelector(`.cell[rid="${srcr}"][cid="${srcc}"]`);
cell.style.backgroundColor = "lightblue";
await colorPromise(); // 1sec finished
for (let children = 0; children < graphComponentMatrix[srcr][srcc].length; children++) {
let [nbrr, nbrc] = graphComponentMatrix[srcr][srcc][children];
if (visited[nbrr][nbrc] === false) {
let response = await dfsCycleDetectionTracePath(graphComponentMatrix, nbrr, nbrc, visited, dfsVisited);
if (response === true) {
cell.style.backgroundColor = "transparent";
await colorPromise();
return Promise.resolve(true);
}
}
else if (visited[nbrr][nbrc] === true && dfsVisited[nbrr][nbrc] === true) {
let cyclicCell = document.querySelector(`.cell[rid="${nbrr}"][cid="${nbrc}"]`);
cyclicCell.style.backgroundColor = "lightsalmon";
await colorPromise();
cyclicCell.style.backgroundColor = "transparent";
cell.style.backgroundColor = "transparent";
await colorPromise();
return Promise.resolve(true);
}
}
dfsVisited[srcr][srcc] = false;
return Promise.resolve(false);
}