// if vertex 0 and 1 are connected then, graph = [ [1], [0] ]
const graph = [
[1, 4],
[0, 4, 2, 3],
[1, 3],
[1, 4, 2],
[3, 0, 1]
]; // cycle
const graph = [
[1, 2],
[0],
[0],
]; // no cycle
const State = {
'WHITE': 0, // not visisted
'GRAY': 1, // current checking
'BLACK': 2 // visited
}
function dfsAll(adjs) {
// console.log(adjs);
const vertexNum = adjs.length;
const visited = new Array(vertexNum).fill(State.WHITE);
let componentCnt = 0;
for (let vertex = 0; vertex < adjs.length; vertex++) {
if (visited[vertex] !== State.WHITE) continue;
componentCnt += 1; // check component count
console.log(`:: vertex ${vertex} is traversing, :: components = ${componentCnt}`);
const flag = dfs(adjs, visited, vertex, vertex);
console.log(`vertex ${vertex} form a cycle? ${flag}`)
if (flag === true) {
console.log('cycle detected!');
return true;
}
}
return false;
}
function dfs(adjs, visited, here, parent) {
visited[here] = State.BLACK; // 방문 순간 양방향 소통이 일어나 black
const adj = adjs[here];
for (const str of adj) {
const there = Number(str);
// if (visited[there] === State.WHITE) {
// const flag = dfs(adjs, visited, there, here);
// if (flag === true)
// return true;
// } else if (parent !== there) {
// return true;
// }
if (parent === there) continue;
if (visited[there] === State.GRAY) return true; // different
if (visited[there] === State.BLACK) return true;
const flag = dfs(adjs, visited, there, here);
if (flag === true)
return true;
}
visited[here] = BLACK;
return false;
}
dfsAll(graph);