-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcycleValidation.js
53 lines (45 loc) · 1.24 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
//storage of parent child relation
let collectedGraphComponent=[];
let graphComponentMatrix=[];
// for(let i=0;i<rows;i++){
// let row = [];
// for(let j=0;j<cols;j++){
// row.push([]);
// }
// graphComponentMatrix.push(row);
// }
// checking whether graph is cyclic or not
function isGraphCyclic(){
let visited =[];
let dfsVisited=[];
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(dfsCycleDetection(i,j,visited,dfsVisited))return [i,j];
}
}
return null;
}
function dfsCycleDetection(i,j,visited,dfsVisited){
if(visited[i][j]){
if(dfsVisited[i][j])return true;
return false;
}
visited[i][j]=true;
dfsVisited[i][j]=true;
for(let k=0;k<graphComponentMatrix[i][j].length;k++){
let child= graphComponentMatrix[i][j][k];
if(dfsCycleDetection(child[0],child[1],visited,dfsVisited))return true;
}
dfsVisited[i][j]=false;
return false;
}