-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcycleValidation.js
66 lines (57 loc) · 2.42 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
60
61
62
63
64
65
66
let collectedGraphComponent = [];
// Storage -> 2D matrix
let graphComponentMatrix = [];
// for (let i = 0; i < rows; i++) {
// let row = [];
// for (let j = 0; j < cols; j++) {
// // why array & not obj?? bec there can be more than 1 child of a cell
// row.push([]);
// }
// graphComponentMatrix.push(row);
// }
// returns bool val if graph is cyclic or not
function isGraphCyclic(graphComponentMatrix) {
// dependency -> visited, dfsVisited (2d array)
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);
}
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (!visited[i][j]) {
let response = dfsCycleDetection(graphComponentMatrix, i, j, visited, dfsVisited);
if (response) return [i, j];
}
}
}
return null;
}
// start -> visited(True) dfsVisited(True)
// end -> dfsVisited(False)
// Cycle condition -> if (visited[i][j] == true && dfsVisited[i][j] == true)
function dfsCycleDetection(graphComponentMatrix, srcRow, srcCol, visited, dfsVisited) {
visited[srcRow][srcCol] = true;
dfsVisited[srcRow][srcCol] = true;
// A1 -> [ [0,1], [1,0], [5,10], ... ]
for (let children = 0; children < graphComponentMatrix[srcRow][srcCol].length; children++) {
let [childRid, childCid] = graphComponentMatrix[srcRow][srcCol][children];
if (!visited[childRid][childCid]) {
let response = dfsCycleDetection(graphComponentMatrix, childRid, childCid, visited, dfsVisited);
if (response) return true; // is cycle is found, return immediately, no need to explore more
}
// else if (visited[childRid][childCid] && dfsVisited[childRid][childCid]) not using this condition because, from our first condition, else if block will only run if visited[childRid][childCid] is NOT FALSE i.e. TRUE so no need to check that condition
else if (dfsVisited[childRid][childCid]) {
return true;
}
}
dfsVisited[srcRow][srcCol] = false;
return false;
}