def DFS(G): stack = [] visited = [ 0 for i in range(len(G)) ] stack.append(0) while len(stack) > 0: current = stack.pop() visited[current] = 1 print(current, end=" ") for j in reversed(G[current]): if visited[j] == 0: stack.append(j) DFS([ [2,4,5], [2,3,5], [0,1,3,4,5], [1,2], [0,2,6], [0,1,2], [4] ])