def bfs(graph,start): visited=[] q=[start] while q: print(f"q=",q) node=q.pop(0) print(f"visited",visited) if node not in visited: visited.append(node) neighbors=graph[node] for neigh in neighbors: q.append(neigh) return visited graph={ 1:[2,3], 2:[4,5,6], 3:[7], 4:[], 5:[], 6:[], 7:[] } print(bfs(graph,1))
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more