Question: Hi, I'm having problems building a FindSet method and Union method for building a disjoint-set data structure using dictionaries. I have attached my code below
Hi, I'm having problems building a FindSet method and Union method for building a disjoint-set data structure using dictionaries. I have attached my code below the. I need the FindSet method to be given a value and then find the key through the dictionary. I have attached the assignment help sheet and the supposed output below. I need help designing it feel free to change that piece of code as much as you need.

class Diset(object):
def __init__(self):
self.rep = {}
def Makeset(self, rep):
try:
int(rep)
if rep in self.rep.keys():
print("There is already a set with representative" + str(rep))
else:
self.rep.setdefault(rep, [])
except ValueError:
print(str(rep) + " is not a number, try again")
def Addtoset(self,rep,num):
try:
n = int(num)
if rep in self.rep.keys():
if num in [x for v in self.rep.values() for x in v]:
print(str(n) + " is already in the Diset")
else:
self.rep.setdefault(rep, []).append(num)
else:
print(str(rep) + " is not a rep in dictionary, create representative first")
except ValueError:
print(str(num) + " is not a number, try again")
def Findset(self,num):
for num in [x for v in self.rep.values() for x in v]:
list(self.rep.keys())[list(self.rep.values()).index(num)]
A dynamic set is a set that can change over time A disjoint-set data structure (diset) is a collection of disjoint dynamic sets. Each set has a designated representative member. A diset supports the following operations MAKE-SET(x) creates a new set whose representative and only member is x FIND-SET(x) returns the representative of the set containing x UNION(x) unites the dynamic sets that contain x and y into a new set that is the union of these two sets. The representative of the resulting set is any member of the union. The two uniting sets are deleted from the diset and replaced by their union. ADD-TO-SET(xy) adds the value y to the set whose representative is x if y is not already a member of some set in the diset. For example, suppose S-1,2,3,43,(5,6),17,8,9,11,12,14, (10,13,15)} be a diset where the bold elements are the representatives. The following operations taken in the order shown in the table would produce the indicated results Value returned Operation NDSET0) 131,2,3,4),(5,6),17,8,91112,14),10,13,15)) MAKE-SET (16) ADD-TO SET(16,17) ADD-TO SET(3,11) (1,2,3,4),(5,6), (7,8,9,11,12,14),10,13,15),(16)) (1,2,3,4),(5,6),(7,8,9,11,12,14),(10,13,15, 16,17 Duplicate1,2,3,4,(5,6), (7,8,9,11,1214), (10,13,15, 16,17)) entry message UNION(3,13) FIND-SET (10) (1,2,3,4,10,13,15, (5,6,17,8,9,1112,14, 16,17)) 3(1,2,3,4,10,13,15, (5,6,(7,8,9,11,12,14),(16,17)) Assignment: Implement a DISET class with the methods FIND-SET, MAKE-SET, UNION, and ADD- TO-SET as described. Upload your file as diset.py. Here is an interactive session with the DISET class using the previous example
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
