-
Notifications
You must be signed in to change notification settings - Fork 0
/
cycleValidation.js
59 lines (53 loc) · 1.81 KB
/
cycleValidation.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
// storage
let collectedGraphComponent = [];
let graphComponentMatrix = [];
// for(let r = 0; r < rows; r++){
// let row = [];
// for(let c = 0; c < cols; c++){
// row.push([]);
// }
// graphComponentMatrix.push(row);
// }
// true denotes cycle false vice versa
function isGraphCyclic(graphComponentMatrix){
// dependency -> visited, dfsvisited
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);
}
for(let r = 0; r < rows; r++){
for(let c = 0; c < cols; c++){
if(visited[r][c] === false){
let response = dfsCycleDetection(graphComponentMatrix, r, c, visited, dfsVisited);
if(response === true) return [r, c];
}
}
}
return null;
}
// start -> visited[true] dfsvisited[true]
// end -> dfsvisited[false]
// if visited[i][j] -> backtrack
// cycle condition -> visited[i][j] == true && dfsvisited[i][j] == true
function dfsCycleDetection(graphComponentMatrix, srcr, srcc, visited, dfsVisited){
visited[srcr][srcc] = true;
dfsVisited[srcr][srcc] = true;
for(let children = 0; children < graphComponentMatrix[srcr][srcc].length; children++){
let [crid, ccid] = graphComponentMatrix[srcr][srcc][children];
if(visited[crid][ccid] === false){
let response = dfsCycleDetection(graphComponentMatrix, crid, ccid, visited, dfsVisited);
if(response === true) return true;
}
else if(dfsVisited[crid][ccid] === true) return true;
}
dfsVisited[srcr][srcc] = false;
return false;
}