class partition """union-find structure for maintaining disjoint sets "" nested position class class positionslots _container _element _size _parent def init (selfcontainere) """create new position that is the leader of its own group ""reference to partition instance self container container self element self size convention for group leader self parent self def element(self) """return element stored at this position "" return self element public partition methods def make group(selfe) """makes new group containing element eand returns its position "" return self position(selfe def find(selfp) """finds the group containging and return the position of its leader "" if parent ! parent self find( parentoverwrite parent after recursion return parent def union(selfpq) """merges the groups containg elements and (if distinct"" self find( self find( if is not bonly merge if different groups if size size parent size + size else parent size + size code fragment python implementation of partition class using union-bysize and path compression