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