from collections import deque

def bfs(G, s):
    P, Q = {s: None}, deque([s])
while Q:
    u = Q.popleft()
for v in G[u]:
    if v in P:
        continue
P[v] = u
Q.append(v)
return P
Обход дерева в ширину, Python