Ask Question
14 May, 00:53

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)

+5
Answers (1)
  1. 14 May, 03:20
    0
    See explaination

    Explanation:

    #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) ])
Know the Answer?
Not Sure About the Answer?
Find an answer to your 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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers