Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
755 B
1
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)