Answer:
See explaination
Step-by-step 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)])