Docsity

Breadth-First and Depth-First Search Algorithms on Social Media Graph, Assignments of Artificial Intelligence

Partial preview of the text

Download Breadth-First and Depth-First Search Algorithms on Social Media Graph and more Assignments Artificial Intelligence in PDF only on Docsity!

Artificial Intelligence

]

[Year]

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)

Driver Code

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)

Driver Code

print("Following is the Depth-First Search") dfs(visited, SocialMedia, 'SocialMedia')