Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions