Question

A python question... In your class, many students are friends. Let’s assume that two students sharing...

A python question...

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 0 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 0 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 0 is {0, 1, 2} Social network 1 is {3, 4}

Homework Answers

Answer #1

Screenshot of the code:

Code to copy:

# Find by path compression
def find(i):
   global arr
   while(arr[i]!=i):
       arr[i] = arr[arr[i]]
       i = arr[i]
   return i

# Union by size
def union(friend1, friend2):
   global arr, size
   if size[friend1]<size[friend2]:
       arr[friend1] = arr[friend2]
       size[friend2] += size[friend1]
   else:
       arr[friend2] = arr[friend1]
       size[friend1] += size[friend2]

def networks(n, friends_list):
   global arr, size
   # arr stores the one of friend val, arr[0] = 1 i.e. 1 is a friend of 0
   arr = [None]*n
   # Size of network
   size = [None]*n
   for i in range(n):
       # Everyone is a friend to himself
       arr[i] = i
       # Size of each group is 1
       size[i] = 1
   # For each tuple make union
   for friends in friends_list:
       union(friends[0], friends[1])
  
   network = []
   # Add unique values of arr to network as a single group
   for head in set(arr):
       network.append({head})

   for i in range(n):
       for group in network:
           # If friend of i is in current group then add i to that group
           if arr[i] in group:
               group.add(i)
   return network

# Function call
network = networks(5, [(0, 1), (1, 2), (3, 4)])
# Output
for i in range(len(network)):
   print('Social network ',i+1,': ',network[i])

output screenshot:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following commands do: dd, y3, p, :set cindent (1 pt) VIM exercises These exercises on the computer need to be repeated by each student in the pair. This is to ensure that both students understand how to get around in Linux!!! For this part of the lab, you will create a .vimrc file that will help you develop your C++ programs using VIM. First, we...
There are two reflective essays from MED students during their third year internal medicine clerkship. One...
There are two reflective essays from MED students during their third year internal medicine clerkship. One student sees each connection to a patient as like the individual brush strokes of an artist and the other sees gratitude in a patient with an incurable illness and is moved to gratitude in her own life. (WORD COUNT 500) Reflect on both essays and then choose one and describe how the student grew from the experience. Then explain what you learned as a...
QUESTION 1 1. Brianna is trying to increase her chances of being promoted to vice president...
QUESTION 1 1. Brianna is trying to increase her chances of being promoted to vice president by working to build good work relationships with other managers outside her own department. Brianna's behavior should be viewed as dysfunctional politics. functional politics. coercive power. functional influence. 2 points QUESTION 2 1. The Gingerbread Factory has a separate unit that makes their chocolate crunch cookies and another unit that is completely responsible for all operations in producing their ginger snap cookies. The Gingerbread...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT