0

I have some part of codes about disjoint sets.

import set
import copy
"""
Importing set.py should provide the following functions:

set.Initialize(Values)       -- return a new set system with one element
                                per set.

                                Values is a list of values for the
                                sets

set.Find(set, value)         -- Return the name of the set in which value lives.
                                Do path compression.

                                Exactly what you return isn't
                                important, as long as Find(set,
                                val1) and Find(set, val2) are
                                equal if and only if val1 and val2
                                are in the same set.

set.Merge(set, value1, value2)  -- Merge operation.  Make sure you parent
                                   the smaller set to the larger.

"""

import random
import time


random.seed(time.time())
A = []                                  
passed=True
# First make a list of stuff for the set system
for i in range(1000):
    val = random.randrange(-1000,1000)
    if(val not in A):
        A.append(val)
MyA = copy.deepcopy(A)

my_set = set.Initialize(A)

# Now randomly do finds and unions until everything is in the same set.
print "Doing lots of finds and unions..."
while(MyA.count(MyA[0]) != len(A)):
    # raw_input("Press any key...")
    length = len(A)
    index_1 = random.randrange(0, length)
    val_1 = A[index_1]
    index_2 = random.randrange(0, length)
    val_2 = A[index_2]
    if(random.randrange(1,6) < 3):

    # With probability 2/5, do a find
    set_says = (set.Find(my_set, val_1) == set.Find(my_set, val_2))
    A_says = (MyA[index_1] == MyA[index_2])
    # print "Find ",val_1,"=",set.Find(my_set, val_1)
    # print "Find ",val_2,"=",set.Find(my_set, val_2)
    if(set_says != A_says):
        passed=False
        print "Oops: I said Find(", val_1, ") == Find(", val_2, ") and got ", set_says
    else:
        # Otherwise (with probability 3/5), do a union
        set1=MyA[index_1]
        set2=MyA[index_2]
        for i in range(len(MyA)):
            if(MyA[i] == set1):
                MyA[i] = set2
        set.Merge(my_set, val_1, val_2)
        # print "Merging ",val_1, "and", val_2
# print A
# print MyA
# Now verify that everything is in the same set.  Start with set.py.
print "Checking that everything is in one set now..."
val = set.Find(my_set, A[0])
for i in range(len(A)):
val2 = set.Find(my_set, A[i])
if(val != val2):
  passed=False
  print "Oops, ", val, " and ", val2, " should be in the same set, but aren't."

print "Verifying the test harness..."
val = MyA[0]
for i in range(len(MyA)):
    if(MyA[i] != val):
        print "Uh, oh, it looks like the test harness itself has a bug."
if passed:
    print "All tests passed!"

I want to add find, merge and initialization function. In the top of the code, it tells what each functions have to do. I already did find and merge and I hope they are right. However I can't get initialization function. I have no idea how can I make it return a new set system with one element per set. Could you guys please help me with that?

Here is my find and merge functions below:

def Find(set,x):
    if set[x] == set[x].parent:
        return set[x]
    set[x].parent = Find(set,set[x].parent)
    return set[x].parent

def Merge(set, labelA, labelB):
    pa = Find(set, labelA)
    pb = Find(set, labelB)
    if pa == pb: 
        return #They are already joined
    parent = pa
    child = pb
    if pa.rank < pb.rank:
        parent = pb
        child = pa
    child.parent = parent
    parent.rank = max(parent.rank, child.rank+1)
Prune
  • 76,765
  • 14
  • 60
  • 81
gcaur
  • 29
  • 4
  • 2
    Can you describe the input and output you're expecting? It's not clear what you're asking from your description. – munk Feb 22 '16 at 20:19
  • "set system" is not a Python data structure; please clarify what result you need. Also, the **set** class does not have an **Initialize** method. Are you planning to extend the class? If so, you need more infrastructure. – Prune Feb 22 '16 at 20:31
  • @Prune: The questioner is trying to implement a [disjoint-set data structure](https://en.wikipedia.org/wiki/Disjoint-set_data_structure), also known as a union-find data structure. It has nothing to do with Python's built-in `set` type, and its operations don't resemble those of any Python built-in type. – user2357112 Feb 23 '16 at 22:40
  • Ah; thanks. It's been a long time since I dealt with such constructs. I deleted my (useless) answer. – Prune Feb 23 '16 at 22:54
  • I've switched mind-set, so I'm on the same page ... I think. Where is the rest of the code for class **set**? We need something that supports at least **parent** and **child** attributes, since the **set** operations depend on them. – Prune Feb 23 '16 at 23:03
  • the set class will include find, merge, and initialization functions in it and first main code I have on the top is just a test class – gcaur Feb 23 '16 at 23:15

0 Answers0