Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
755 B
2
Indexable
Never
from queue import queue
adj_list={"a":["b","d"],
          "b":["a","c"],
          "c":["b"],
          "d":["a","e","f"],
          "e":["d","f","g"],
          "f":["d","e","h"],
          "g":["e","h"],
          "h":["g","f"],}
visited={}
parent={}
bfs_traversasl_output=[]
queue=queue()
for node in adj_list.keys():
    visited[node]=False
    parent[node]=None
    
s="a"
visited[s]= True
queue.put(s)
while not queue.empty():
    u=queue.get()
    bfs_travarsial_output.append(u)
    for v in adj_list[u]:
        if visited[v] == False:
            visited[v]=True
            parent[v]=u
            queue.put(v)


d="g"
path=[]
while d is not None:
    path.append(d)
    d=parent[d]
path.reverse()                
print(path)