Question: * * Please only answer the #your code here * * class DisjointForests: def _ _ init _ _ ( self , n ) :

**Please only answer the #your code here**
class DisjointForests:
def __init__(self, n):
assert n >=1,' Empty disjoint forest is disallowed'
self.n = n
self.parents =[None]*n
self.rank =[None]*n
# Function: dictionary_of_sets
# Convert the disjoint forest structure into a dictionary d
# wherein d has an entry for each representative i
# d[i] maps to each elements which belongs to the tree corresponding to i
# in the disjoint forest.
def dictionary_of_sets(self):
d ={}
for i in range(self.n):
if self.is_representative(i):
d[i]= set([i])
for j in range(self.n):
if self.parents[j]!= None:
root = self.find(j)
assert root in d
d[root].add(j)
return d
def make_set(self, j):
assert 0<= j < self.n
assert self.parents[j]== None, 'You are calling make_set on an element multiple times -- not allowed.'
self.parents[j]= j
self.rank[j]=1
def is_representative(self, j):
return self.parents[j]== j
def get_rank(self, j):
return self.rank[j]
# Function: find
# Implement the find algorithm for a node j in the set.
# Repeatedly traverse the parent pointer until we reach a root.
# Implement the "rank compression" strategy by making all
# nodes along path from j to the root point directly to the root.
def find(self, j):
assert 0<= j < self.n
assert self.parents[j]!= None, 'You are calling find on an element that is not part of the family yet. Please call make_set first.'
# your code here
# Function : union
# Compute union of j1 and j2
# First do a find to get to the representatives of j1 and j2.
# If they are not the same, then
# implement union using the rank strategy I.e, lower rank root becomes
# child of the higher ranked root.
# break ties by making the first argument j1's root the parent.
def union(self, j1, j2):
assert 0<= j1< self.n
assert 0<= j2< self.n
assert self.parents[j1]!= None
assert self.parents[j2]!= None
# your code here
Test:
d = DisjointForests(10)
for i in range(10):
d.make_set(i)
for i in range(10):
assert d.find(i)== i, f'Failed: Find on {i} must return {i} back'
d.union(0,1)
d.union(2,3)
assert(d.find(0)== d.find(1)),'0 and 1 have been union-ed together'
assert(d.find(2)== d.find(3)),'2 and 3 have been union-ed together'
assert(d.find(0)!= d.find(3)),'0 and 3 should be in different trees'
assert((d.get_rank(0)==2 and d.get_rank(1)==1) or
(d.get_rank(1)==2 and d.get_rank(0)==1)), 'one of the nodes 0 or 1 must have rank 2'
assert((d.get_rank(2)==2 and d.get_rank(3)==1) or
(d.get_rank(3)==2 and d.get_rank(2)==1)), 'one of the nodes 2 or 3 must have rank 2'
d.union(3,4)
assert(d.find(2)== d.find(4)),'2 and 4 must be in the same set in the family.'
d.union(5,7)
d.union(6,8)
d.union(3,7)
d.union(0,6)
assert(d.find(6)== d.find(1)),'1 and 6 must be in the same set in the family'
assert(d.find(7)== d.find(4)),'7 and 4 must be in the same set in the family'
print('All tests passed: 10 points.')

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!