



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




while queue: # Creating loop to visit each node h = queue.pop(0) print (h, end = " ") for neighbour in SocialMedia[h]: if neighbour not in visited: visited.append(neighbour) queue.append(neighbour)
print("Following is the Breadth-First Search") bfs(visited, SocialMedia, 'SocialMedia') Question # 2 SocialMedia = { 'SocialMedia' : ['Insta', 'FB','Whatsapp'], 'Insta' : ['Chat', 'Calls','Video Calls', 'Follow', 'Marketing'], 'FB' : ['Chat', 'Calls','Video Calls','Making Friends','Marketing'], 'Whatsapp':['Chat', 'Calls', 'Video Calls'], 'Chat':[], 'Calls':[], 'Video Calls':[], 'Follow':[], 'Marketing':[], 'Chat':[],
'Calls':[], 'Video Calls':[], 'Making Friends':[], 'Marketing':[], 'Chat':[], 'Calls':[], 'Video Calls':[], } visited = set() # Set to keep track of visited nodes of graph. def dfs(visited, SocialMedia, node): #function for dfs if node not in visited: print (node) visited.add(node) for neighbour in SocialMedia[node]: dfs(visited, SocialMedia , neighbour)
print("Following is the Depth-First Search") dfs(visited, SocialMedia, 'SocialMedia')