Question: PYTHON: How would I implement this random undirected graph using BFS. I need to create a new function componentsizes(gr) (gr is the graph created from

PYTHON: How would I implement this random undirected graph using BFS. I need to create a new function componentsizes(gr) (gr is the graph created from the code below) that counts the connected components using bfs returns a list containing the sizes of all connected components in the graph, in decreasing order.

###### CODE for undirected random graph takes n and p as input where n is vertices and p is probability of adding a new edge

import random N = int(input("enter the number of vertices:")) #taking the input for number of vertices

P = float(input("enter the probability:")) #taking the input for probability

edges = [] #initially edges list is empty adj = [[] for i in range(N)] #initially the adjancy list is as well empty

#populating edges with all possible edge for i in range(N): for j in range(i+1,N): edges.append([i,j]) #shuffling the edges randomly random.shuffle(edges)

#total no of edges = n*(n-1)/2 and probabbility = p so #so numbe rof eges to take = p*n*(n-1)/2

no_of_edges = int(P*(N-1)*N/2)

for i in range(no_of_edges): adj[edges[i][0]].append(edges[i][1])#appending the vertices to corresponding edges points adj[edges[i][1]].append(edges[i][0]) #printing out the results for i in range(N): print(i,":",adj[i])

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!