Exemple #1
0
 def equivalenceClasses(input, output):
     """
     (File, File) -> None
     Computes equivalence classes using a partition.
     First reads an integer from the input stream that
     specifies the size of the universal set.
     Then reads pairs of integers from the input stream
     that denote equivalent items in the universal set.
     Prints the partition on end-of-file.
     """
     line = input.readline()
     p = PartitionAsForest(int(line))
     for line in input.readlines():
         words = line.split()
         i = int(words[0])
         j = int(words[1])
         s = p.find(i)
         t = p.find(j)
         if s is not t:
             p.join(s, t)
         else:
             output.write("redundant pair: %d, %d\n" % (i, j))
     output.write(str(p) + "\n")