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