(Homework Solution): Question: 6.29 In your class, many students are friends. Let's assume that two students sharing a friend mu…

6.29 In your class, many students are friends. Lets assume that two students sharing a friend must be friends themselves; in other words, if students O and 1 are friends and students 1 and 2 are friends, then students 0 and 2 must be friends. Using this rule, we can partition the students into circles of friends. To do this, implement a function networks() that takes two input arguments. The first is the number n of students in the class. We assume students are identified using integers 0 through n 1. The second input argument is a list of tuple objects that define friends. For example, tuple (0; 2) defines students O and 2 as friends. Function networks() should print the partition of students into circles of friends as illustrated: >>> networks(5, [(0, 1), (1, 2), (3, 4)]) Social network O is (0, 1, 2) Social network 1 is (3, 4)

6.29 In your class, many students are friends. Let’s assume that two students sharing a friend must be friends themselves; in other words, if students O and 1 are friends and students 1 and 2 are friends, then students 0 and 2 must be friends. Using this rule, we can partition the students into circles of friends. To do this, implement a function networks() that takes two input arguments. The first is the number n of students in the class. We assume students are identified using integers 0 through n 1. The second input argument is a list of tuple objects that define friends. For example, tuple (0; 2) defines students O and 2 as friends. Function networks() should print the partition of students into circles of friends as illustrated: >>> networks(5, [(0, 1), (1, 2), (3, 4)]) Social network O is (0, 1, 2) Social network 1 is (3, 4)

Expert Answer

answers

# Well this question is best solved by disjoint set datastructure.

# Think of this as there are different sets which don’t have any common element and

# each set is having some root

# Now if two sets are made to have union operation , then just make root of one

# equal to root of another. Like you have two disjoint sets a single set

# To find root you can use the root finding with path compression

#for finding root

def find_root(i,root):

while i!=root[i]:

root[i] = find_root(root[i],root)

i = root[i]

return i

#for joining two disjoint set

def union(i,j,root):

I = find_root(i,root)

J = find_root(j,root)

if I == J: #belongs to same set , so return

return

root[I] = J #make root of one equal to another

def networks(n,friend_list):

root = [i for i in range(n)] #initially all the friend are root and having no other friend

for (i,j) in friend_list: #by relationships union of disjoint sets

union(i,j,root)

social_networks = {} #to gather the friends under all the roots

for i in range(n):

I = find_root(i,root) #root of the ith friend

#add ith student to social group of root I

if I in social_networks:

social_networks[I].append(i)

else:

social_networks[I] = [i]

i = 0

for value in social_networks.values():

print(‘social network ‘,i,’ is ‘,value)

i += 1

networks(5,[(0,1),(1,2),(3,4)])

Θ l Chegg.com d File Edit Selection View Go Debug Terminal Help program.py- Visual Studio Code Ccode.cpp Most Visited Getting StartedMost Visited # Well this question is best solved by disjoint set datastructure Chegg. Study 3 # Think of this as there are different sets which dont have any common element and # each set is having some root TEX # Now if two sets are made to have union operation , then just make root of one # equal to root of another. Like you have two disjoint sets a single set 6 tuple objects that define friends. For networks() should print the partition >>> networks(5, [(0, 1), (1, 2), (3,4)) Social network O is (0, 1, 2) Social network 1 is (3, 4) 7 # To find root you can use the root finding with path compression 9 1e 12 13 #for finding root 14 def find_root(i,root): 15 16 17 18 19 2e 21 #for joining two disjoint set 22 def union(i,j,root): 23 24 25 26 27 28 while i!-root[i]: Skip & Get next questio root[i]-find_root (root[i],root) -roo returni Your answer I-find root(i,root) ] - find_root(j,root) if I-J: #belongs to same set , so return return root[1] - ] #make root of one equal to another networks(5, [(e,1), (1,2),(3, PROBLEMS OUTPUT DEBUG CONSOLE TERMINAL 1: cmd (base) C:UserslLucky DesktopICPC>python program.py social network θ is [0, 1, 2] social network 1 is [3, 4] (base) C:UsersLucky Desktop ICPC

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents