Depth First Search
function dfs(adj) { var s = 0; var V = adj.length; var visited = Array(V).fill(false); var ans = []; var stack = []; stack.push(s); while(stack.length != 0) { s = stack[stack.length - 1]; stack.pop(); if(visited[s] === false) { ans.push(s); visited[s] = true; } for(var i = 0; i != adj[s].length; ++i) if(visited[adj[s][i]] === false) stack.push(adj[s][i]); } return ans; }