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
python If a number num1 divides another number num2 evenly then num1 is a divisor of...
python If a number num1 divides another number num2 evenly then num1 is a divisor of num2. For example, 2 is a divisor of 2, 4, 6, 8, but 2 is not a divisor of 1, 3, 5, 7, 9, 11, 13. Write a function named count_divisors(m,n) that works as follows. Input: the function takes two integer arguments m and n Process: the function asks the user to enter numbers (positive or negative), and counts the total number of inputs...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
12.6 Can a pretest on mathematics skills predict success in a statistics course?  Assume students in an...
12.6 Can a pretest on mathematics skills predict success in a statistics course?  Assume students in an introductory stats class can be considered a random sample from a larger population of statistics students.  These 62 students took a pretest at the beginning of the semester, and their score from the pre-test and their score from the final exam in the course were recorded.  The least squares regression line was found to be y = 13.8 + 0.81x, and the standard error of b1...
-Data Structure in C++ (Review for C++) -using vector and class In this assignment you’re going...
-Data Structure in C++ (Review for C++) -using vector and class In this assignment you’re going to build a simple “register machine”, a kind of minimal computer that only supports storing a fixed number of values (i.e., no randomly-accessible “main memory”). Your machine will consist of an input loop that reads lines of input from the user and then executes them, stopping when the user quits (by executing the stop command). Each line of input consists of a command followed...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):...
I NEED TASK 3 ONLY TASK 1 country.py class Country:     def __init__(self, name, pop, area, continent):         self.name = name         self.pop = pop         self.area = area         self.continent = continent     def getName(self):         return self.name     def getPopulation(self):         return self.pop     def getArea(self):         return self.area     def getContinent(self):         return self.continent     def setPopulation(self, pop):         self.pop = pop     def setArea(self, area):         self.area = area     def setContinent(self, continent):         self.continent = continent     def __repr__(self):         return (f'{self.name} (pop:{self.pop}, size: {self.area}) in {self.continent} ') TASK 2 Python Program: File: catalogue.py from Country...
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...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
3) Now you will plan for your retirement. To do this we need to first determine...
3) Now you will plan for your retirement. To do this we need to first determine a couple of values. a. How much will you invest each year? $1,000 a year is what i will invest. State what you will use for P, r, and n to earn credit. ( The typical example of a retirement investment is an I.R.A., an Individual Retirement Account, although other options are available. However, for this example, we will assume that you are investing...
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...
Plagiarism Certification Tests for Undergraduate College Students and Advanced High School Students These tests are intended...
Plagiarism Certification Tests for Undergraduate College Students and Advanced High School Students These tests are intended for undergraduate students in college or those under 18 years of age. Read these directions carefully! The below test includes 10 questions, randomly selected from a large inventory. Most questions will be different each time you take the test, You must answer at least 9 out of 10 questions correctly to receive your Certificate. You have 40 minutes to complete each test, and you...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT