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. 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)

# 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

Don't use plagiarized sources. Get Your Custom Essay on
Question: 6.29 In your class, many students are friends. Let's assume that two students sharing a friend mu……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!

# 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)])

Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

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 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.

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?

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

Proceed with the payment

Choose the payment system that suits you most.

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.

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.