Breadth First Search
function bfs(adj) { var V = adj.length; var s = 0; var visited = Array(V).fill(false); var ans = []; var queue = []; visited[s] = true; queue.push(s); while(queue.length != 0) { s = queue[0]; ans.push(s); queue.shift(); for(var i = 0; i != adj[s].length; ++i) { if (visited[adj[s][i]] === false) { visited[adj[s][i]] = true; queue.push(adj[s][i]); } } } return ans; }