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(range(len(G))): if G[current][j] == 1 and visited[j] == 0: stack.append(j) DFS([ [0,0,1,0,1,1,0], [0,0,1,1,0,1,0], [1,1,0,1,1,1,0], [0,1,1,0,0,0,0], [1,0,1,0,0,0,1], [1,1,1,0,0,0,0], [0,0,0,0,1,0,0] ])