set partition question

pball.benetech at gmail.com pball.benetech at gmail.com
Mon May 26 21:12:18 EDT 2008


Partial solution: the complements idea sparked a slightly new
direction. S is defined as above (all sets except s0, the target).

fset = frozenset
s0 = fset([1])
s1 = fset([1,2])
s2 = fset([2])
s3 = fset([2,3,4])
s4 = fset([3])
s5 = fset([1,2,3,4,5])
s6 = fset([5])
s7 = fset([2,3,4,5])
S = set([s1,s2,s3,s4,s5,s6,s7])

# solutions for s0:
# s1 - s2
# s5 - s7
# s5 - s3 - s6

def find_complement(S, s0):
    for s in set(s for s in S if s & s0):
        symm_diff = s ^ s0
        if symm_diff in S:
            yield s0, s, symm_diff

for c in find_complement(S, s0):
    print c

(frozenset([1]), frozenset([1, 2]), frozenset([2]))
(frozenset([1]), frozenset([1, 2, 3, 4, 5]), frozenset([2, 3, 4, 5]))

So this algorithm can find partitions with 2 parts. It's helpless for
more complicated relationships -- in this case the combination s0 = s5-
s3-s6. Some kludgy ideas occur to me, and I'll work on them now. All
ideas welcome -- PB.





More information about the Python-list mailing list