Aaaa
let g = new Map();
g.set(1, [2,3]);
g.set(2, [3, 8]);
g.set(3, []);
let path = [];
let visited = new Set();
let step = 0;
const ip = (g, v) => {
step++;
console.log("------\nstep " + step);
visited.add(v);
path.push(v);
console.log("Visiting and pushing " + v);
const adj = g.get(v);
console.log("Looking at subtree " + adj);
if (adj.length > 0) {
for (var w of adj) {
if (path.includes(w)) {
return true;
}
return ip(g, w);
}
}
path.pop();
console.log("Popping at path=" + path);
return false;
}
const result = ip(g, 1);
console.log("---------\nAnd the result is: " + result);