KeyError: 'f'
Closed this issue · 0 comments
from queue import Queue
#creating graph with 12 nodes and 17 edges
adj_list={"A":["B","D"],
"B":["A","C"],
"C":["B","I"],
"D":["A","E","f"],
"E":["D","F","G"],
"F":["A","E","H"],
"G":["E","H","L"],
"H":["F","G","L","K"],
"I":["C","J"],
"J":["I","K","F"],
"K":["J","H","L"],
"L":["G","H","K"] }
#create bfs
visited={} #create empty dictionary for keep track of visited node
level={} #create empty dictionary for keep track of level of each node
parent={} #create empty dictionary for keep track of parent of each node
bfs_travesal_output=[] #create empty lis for store of travesar order
queue=Queue() #empty queue
for node in adj_list.keys():
visited[node]=False
parent[node]=None
level[node]=-1
s="A"
visited[s]=True
level[s]=0
queue.put(s)
while not queue.empty():
u= queue.get()
bfs_travesal_output.append(u)
for v in adj_list[u]:
if not visited[v]:
visited[v]= True
parent[v]= u
level[v]= level[u]+1
queue.put(v)
print(bfs_travesal_output)
KeyError Traceback (most recent call last)
in
39
40 for v in adj_list[u]:
---> 41 if not visited[v]:
42 visited[v]= True
43 parent[v]= u
KeyError: 'f'