-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcyclicPathTrace.js
71 lines (57 loc) · 2.24 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
69
70
71
function colorPromise() {
return new Promise((resolve, reject) =>{
setTimeout(()=>{
resolve();
},1000);
});
}
async function isGraphCyclicTracePath(graphComponentMatrix,cycleResonse) {
// Dependenvy -> visited, dfsVisited (2D Array)
let visited = [];
let dfsVisited = [];
let [srcr,srcc] = cycleResonse;
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 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 = "transparent";
await colorPromise();
return Promise.resolve(true);
}
}
else if(dfsVisited[crid][ccid] === true){
let cyclicCell = document.querySelector(`.cell[rid="${crid}"][cid="${ccid}"]`);
cyclicCell.style.backgroundColor = "lightsalmon";
await colorPromise();
cyclicCell.style.backgroundColor = "transparent";
await colorPromise();
cell.style.backgroundColor = "transparent";
await colorPromise();
return Promise.resolve(true);
}
}
dfsVisited[srcr][srcc] = false;
return Promise.resolve(false);
}