Question: d = DisjointForests ( 1 0 ) for i in range ( 1 0 ) : d . make _ set ( i ) for

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.')
AssertionError Traceback (most recent call last)
We will first complete an implemention of a union-find data structure with rank compression.
class DisjointForests:
def (self, n):
assert n >=1,' Empty disjoint forest is disallowed'
self.n = n
self.parents =[None]*n
self.rank =[None]*nConvert the disjoint forest structure into a dictionary dd[i] maps to each elements which belongs to the tree corresponding to idef 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]Implement the find algorithm for a node j in the set.Implement the "rank compression" strategy by making alldef 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. Pleaif (self.parents[j]== j):
return j
return self.find(self.parents[j])Compute union of j1 and j2If they are not the same, thenchild of the higher ranked root.def union(self, j1, j2):
assert 0= j1 self.n
assert 0= j2 self.n
assert self.parents[j1]!= None
assert self.parents[j2]!= Nonej1rep = self.find(self.parents[j1])
j2rep = self.find(self.parents[j2])
j1rank = self.get_rank(j1rep)
j2rank = self.get_rank(j2rep)
if j1rep == j2rep:
return
if(j1rep j2rep):
self.parents[j1rep]= j2rep
elif(j2rep j1rep):
self.parents[j2rep]= j1rep
else:
self.parents[j1rep]= j2rep
d = DisjointForests ( 1 0 ) for i in range ( 1 0

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!