-
Notifications
You must be signed in to change notification settings - Fork 0
/
cyclicPathTrace.js
68 lines (55 loc) · 2.13 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
function colorPromise(){
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve();
}, 1000);
})
}
async function isGraphCyclicTracePath(graphComponentMatrix, cycleResponse){
let [srcr, srcc] = cycleResponse;
let visited = [];
let dfsVisited = [];
for(let r = 0; r < rows; r++){
let visitedRow = [];
let dfsVisitedRow = [];
for(let c = 0; c < cols; c++){
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 cells 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();
for(let children = 0; children < graphComponentMatrix[srcr][srcc].length; children++){
let [crid, ccid] = graphComponentMatrix[srcr][srcc][children];
if(visited[crid][ccid] === false){
let response = await dfsCycleDetectionTracePath(graphComponentMatrix, crid, ccid, visited, dfsVisited);
if(response === true){
cell.style.backgroundColor = "#ecf0f1";
await colorPromise();
return Promise.resolve(true);
}
}
else if(dfsVisited[crid][ccid] === true){
let cyclicCell = document.querySelector(`.cell[rid="${crid}"][cid="${ccid}"]`);
cell.style.backgroundColor = "lightsalmon";
await colorPromise();
cyclicCell.style.backgroundColor = "#ecf0f1";
await colorPromise();
cell.style.backgroundColor = "#ecf0f1";
return Promise.resolve(true);
}
}
dfsVisited[srcr][srcc] = false;
return false;
}