set partition question

Discussion in 'Python' started by pball.benetech@gmail.com, May 25, 2008.

  1. Guest

    dear pythonistas,

    So imagine that we have a set of sets S. If we pick arbitrarily one
    set, s0, what are all the combinations of other sets in S which when
    combined by set operations result in s0?

    s0 = set([1])
    s1 = set([1,2])
    s2 = set([2])
    S = set([s0,s1,s2])
    one answer we're searching for is s0 = s1 - s2

    There may be arbitrarily many set elements (denoted by integers
    1,2,3,...) and arbitrarily many combinations of the elements composing
    the sets s_i (s0, s1, ...). We can use any operation or function which
    takes and returns sets.

    I think this problem is related to integer partitioning, but it's not
    quite the same. The range of answers has a little combinatorial
    explosion problem as S gains new members. In my problem, len(S) is
    usually on the order of 1M, and in the worst case, 10M, and there are
    on the order of 10k elements.

    My attempts to come up with an algorithm have not succeeded. Any ideas
    occur to you folks? -- PB.

    --
    Patrick Ball, Ph.D.
    Chief Scientist
    & Director, Human Rights Program
    http://www.benetech.org
    http://www.hrdag.org
    http://www.martus.org
    , May 25, 2008
    #1
    1. Advertising

  2. > s0 = set([1])
    > s1 = set([1,2])
    > s2 = set([2])
    > S = set([s0,s1,s2])
    > one answer we're searching for is s0 = s1 - s2
    >
    > There may be arbitrarily many set elements (denoted by integers
    > 1,2,3,...) and arbitrarily many combinations of the elements composing
    > the sets s_i (s0, s1, ...). We can use any operation or function which
    > takes and returns sets.


    In that case, there is always a trivial answer. As I can use any
    function which takes and returns sets, and as I shall come up with
    a function that returns s0, I just use the following function

    def f(s):
    return s0

    To compute s0, just invoke f with any other of the sets, and you
    will - get s0.

    > I think this problem is related to integer partitioning, but it's not
    > quite the same.


    I think the problem is significantly underspecified. It would be a more
    interesting problem if there was a restriction to a few selected set
    operations, e.g. union, intersection, difference, and combinations
    thereof.

    Regards,
    Martin
    Martin v. Löwis, May 25, 2008
    #2
    1. Advertising

  3. Guest

    On May 25, 1:13 pm, "Martin v. Löwis" <> wrote:
    > > We can use any operation or function which
    > > takes and returns sets.

    >
    > I think the problem is significantly underspecified. It would be a more
    > interesting problem if there was a restriction to a few selected set
    > operations, e.g. union, intersection, difference, and combinations
    > thereof.


    Ok, that's quite right -- when I just tried to define "any function,"
    I found that all the solutions I came up with were combinations of the
    set operations defined for immutable sets. Let me improve the spec as
    the following:

    There may be arbitrarily many set elements (denoted by integers
    1,2,3,...), arbitrarily many combinations of the elements composing
    the sets s_i (s0, s1, ...). We can use any of python's set operations
    or combination of those operations.

    Thanks for the clarification -- PB.
    , May 25, 2008
    #3
  4. writes:

    > dear pythonistas,
    >
    > So imagine that we have a set of sets S. If we pick arbitrarily one
    > set, s0, what are all the combinations of other sets in S which when
    > combined by set operations result in s0?
    >
    > s0 = set([1])
    > s1 = set([1,2])
    > s2 = set([2])
    > S = set([s0,s1,s2])
    > one answer we're searching for is s0 = s1 - s2
    >
    > There may be arbitrarily many set elements (denoted by integers
    > 1,2,3,...) and arbitrarily many combinations of the elements composing
    > the sets s_i (s0, s1, ...). We can use any operation or function which
    > takes and returns sets.
    >
    > I think this problem is related to integer partitioning, but it's not
    > quite the same. The range of answers has a little combinatorial
    > explosion problem as S gains new members. In my problem, len(S) is
    > usually on the order of 1M, and in the worst case, 10M, and there are
    > on the order of 10k elements.
    >
    > My attempts to come up with an algorithm have not succeeded. Any ideas
    > occur to you folks? -- PB.


    Unless your sets have some sort of pattern, it sounds to me like this
    problem is at least exponential... Good luck!

    --
    Arnaud
    Arnaud Delobelle, May 25, 2008
    #4
  5. Guest

    On May 25, 3:51 pm, wrote:
    > dear pythonistas,
    >
    > I think this problem is related to integer partitioning, but it's not
    > quite the same. The range of answers has a little combinatorial
    > explosion problem as S gains new members. In my problem, len(S) is
    > usually on the order of 1M, and in the worst case, 10M, and there are
    > on the order of 10k elements.


    If, by "integer partitioning," you mean the "subset sum
    problem" (given a finite set S of integers, does S contain a subset
    which sums up to some given integer k?), then you are right. I'm
    reasonably sure there's a polynomial reduction from your problem to
    the subset sum problem. That would make your problem NP-complete.

    As for algorithms, I don't think there's an exact algorithm any better
    than trying all the possibilities and stopping when one comes close.
    Say, for example, we specify the problem to be: given sets s_0, s_1,
    s_2, ..., s_n, with S defined to be the union of all the s_i's, return
    all functions f which, using the sets s_1, s_2, ..., s_n and the
    elementary set operations (union, intersection, difference), returns
    the set s_0.

    Now, you need to be a little more careful, because, given a function f
    which satisfies the problem, I can always define f' = f(S) + s_1 - s_1
    and get another function which satisfies the definition.

    Like I said, because this looks related to the subset sum problem
    (though harder, because you're asking for "all" such functions, for
    some rigorous definition of "all," as per the previous sentence), I
    suspect it's NP-complete. However, there are probably some good
    heuristics, such as if s1 has a large intersection with s0, it's
    probably a good idea to use s1 in whatever formula you come up with.

    Other than that, I don't really have an idea. Can you say what the
    application of this algorithm would be? Knowing the use case might
    suggest a better approach.
    , May 26, 2008
    #5
  6. > There may be arbitrarily many set elements (denoted by integers
    > 1,2,3,...), arbitrarily many combinations of the elements composing
    > the sets s_i (s0, s1, ...). We can use any of python's set operations
    > or combination of those operations.


    That still allows for trivial solutions:

    Given s0, and s1..sn, the following Python code outputs a Python
    fragment that, when run, returns s0:

    print "set()",
    for e in s0:
    print ".union(set([%s]))" % repr(e),

    For s0=set([1,2,3]), I get

    set() .union(set([1])) .union(set([2])) .union(set([3]))

    Or, more trivially,

    print repr(s0)

    which gives me

    set([1,2,3])

    In either case, s0 is generated through "any of python's set
    operations".

    Regards,
    Martin
    Martin v. Löwis, May 26, 2008
    #6
  7. Guest

    On May 25, 7:46 pm, wrote:
    > > I think this problem is related to integer partitioning, but it's not

    <snip>
    >
    > If, by "integer partitioning," you mean the "subset sum
    > problem" (given a finite set S of integers, does S contain a subset
    > which sums up to some given integer k?), then you are right.  I'm
    > reasonably sure there's a polynomial reduction from your problem to
    > the subset sum problem.  That would make your problem NP-complete.


    Wow, that's helpful. http://en.wikipedia.org/wiki/Subset_sum clarifies
    what I'm trying to do. Yes, it probably is NP-complete. Fortunately my
    use case doesn't require that the process finish, just that it produce
    a lot. I can exclude trivial cases (e.g., f' = f(S) + s_1 - s_1) with
    some sort of simplifier.

    > Like I said, because this looks related to the subset sum problem
    > (though harder, because you're asking for "all" such functions, for
    > some rigorous definition of "all," as per the previous sentence), I
    > suspect it's NP-complete.  However, there are probably some good
    > heuristics, such as if s1 has a large intersection with s0, it's
    > probably a good idea to use s1 in whatever formula you come up with.


    There are a few real-world constraints which make the problem easier,
    but only by linear factors, I suspect. More on this below.


    > Other than that, I don't really have an idea.  Can you say what the
    > application of this algorithm would be?  Knowing the use case might
    > suggest a better approach.


    This is a problem in statistical estimation. Given n records made up
    of k variables, define a cell as the point in the cartesian product of
    v_1 * v_2 * ... * v_k. I want to apply an estimator on the data in
    each cell.

    However, many cells are empty, and others are too sparse to support
    estimation. One solution is to build lots of valid aggregations,
    called "strata," for which estimates can be made for chunks of cells.
    A "valid aggregation" is a sequence of *adjacent* cells (see below)
    whose data are pooled (by whatever mechanism is relevant to the
    estimator).

    The cells are "elements" in my initial problem statement, and the
    strata are the sets.

    The constraints I mentioned are on the composition of strata: strata
    can only contain sequences of adjacent cells, where adjacency is
    defined for each variable. Two cells are adjacent if they are equal on
    every variable except one, and on that one and by its definition, they
    are adjacent.

    Does this make it easier to understand, or more difficult? thanks --
    PB.
    , May 26, 2008
    #7
  8. Guest

    On May 25, 11:40 pm, wrote:

    > This is a problem in statistical estimation. Given n records made up
    > of k variables, define a cell as the point in the cartesian product of
    > v_1 * v_2 * ... * v_k. I want to apply an estimator on the data in
    > each cell.


    So, basically, V = (v_1, v_2, ... , v_{k-1}, v_k) can be regarded as
    an abstract, k-dimensional vector, right?

    If I understand your revised problem statement correctly, what you
    really want to do is build a graph of these vectors, where graph
    adjacency is equivalent to adjacency in your sense. That is, imagine
    you have V_1, V_2, ... , V_n all sitting out in front of you,
    represented abstractly as simple points. Draw a line between V_i and
    V_j if they are "adjacent" in your sense of the word. What you have
    then is a graph structure where your version of adjacency exactly
    corresponds to graph adjacency. Then, in your language, a stratum is
    simply a path in this graph, and finding those is easy.

    That is, unless I've got this all wrong. :)
    , May 26, 2008
    #8
  9. Guest

    On May 25, 10:41 pm, wrote:
    > So, basically, V = (v_1, v_2, ... , v_{k-1}, v_k) can be regarded as
    > an abstract, k-dimensional vector, right?


    Yes.


    > If I understand your revised problem statement correctly, what you
    > really want to do is build a graph of these vectors, where graph
    > adjacency is equivalent to adjacency in your sense.  That is, imagine
    > you have V_1, V_2, ... , V_n all sitting out in front of you,
    > represented abstractly as simple points.  Draw a line between V_i and
    > V_j if they are "adjacent" in your sense of the word.  What you have
    > then is a graph structure where your version of adjacency exactly
    > corresponds to graph adjacency.  Then, in your language, a stratum is
    > simply a path in this graph, and finding those is easy.


    You're solving an earlier part of the problem which I call stratum
    generation. I've never thought to use a graph representation for
    stratum generation -- very interesting, and I'll pursue it. Would you
    be willing to outline how you'd do it here?

    I've had fun "breeding" strata using genetic algorithms, and a lot of
    interesting ideas came out of that experiment, in particular the
    utility of building randomly shaped strata to do indirect estimates
    (the objective of the set arithmetic described here). I used GA's
    because I think that the space of possible strata is too big to search
    by brute force.

    I'd still like to have the graph representation for the brute force
    solution. Or, in another random algorithm, the graph could be a
    sampling frame from which random paths could be pulled.

    In a real dataset, some of the strata will contain adequate data to
    make an estimate, and these are called valid strata. Other legal
    strata (as shown by a path through the graph) may not have adequate
    data to make an estimate (thus, legal but invalid).

    We've done this problem by hand: observing that we can estimate a
    stratum (1,2,3) and another (2,3) but not (1). So
    E(1) = E(1,2,3) - E(2,3)
    There are issues with the variance of E(1), but that's a very
    different problem. Using the valid strata E(1,2,3) and E(2,3) we've
    made an indirect estimate of E(1).

    I'm looking for an algorithm which automates the search for
    combinations like the one above. Given a set of valid strata[1], yield
    combinations of valid strata which, when combined via union or
    difference[2], evaluate to a stratum (adjacent but possibly not
    valid). The combination of valid strata is called an indirect
    estimate

    [1] strata can be generated via many methods; they all end up in the
    same database
    [2] Note that the set of operations is shrinking :)

    Thanks in advance for your thoughts -- PB.
    , May 26, 2008
    #9
  10. writes:

    > On May 25, 1:13 pm, "Martin v. Löwis" <> wrote:
    >> > We can use any operation or function which
    >> > takes and returns sets.

    >>
    >> I think the problem is significantly underspecified. It would be a more
    >> interesting problem if there was a restriction to a few selected set
    >> operations, e.g. union, intersection, difference, and combinations
    >> thereof.

    >
    > Ok, that's quite right -- when I just tried to define "any function,"
    > I found that all the solutions I came up with were combinations of the
    > set operations defined for immutable sets. Let me improve the spec as
    > the following:
    >
    > There may be arbitrarily many set elements (denoted by integers
    > 1,2,3,...), arbitrarily many combinations of the elements composing
    > the sets s_i (s0, s1, ...). We can use any of python's set operations
    > or combination of those operations.


    OK then, if you only allow union, intersection, difference, symmetric
    difference then I think it would be easy to prove (I haven't done it!)
    that if there is a solution to you problem, then the method below
    yields a solution:

    *** warning: all this is untested AND written in a rush ***

    let S be the set of all your sets except s0 (i.e. s1, s2, s3, ...)


    # X is the "domain", C is the set of complements in X of elements of S

    X = reduce(set.union, S)
    C = set(X - s for s in S)


    # Find the "fibers" at each element x of s0. A fiber at x is the set
    # of all s in S or C that contain x

    from collections import defaultdict
    fibers = defaultdict(list)

    for s in S | C:
    for x in s & s0:
    fibers[x].append(s)


    # Find the "seeds" at each x in s0. A seed at x is the set of all
    # elements contained in all s in the fiber at x.

    # Intutively, a seed at x is the smallest set containing x that can be
    # built from S

    seeds = [reduce(set.intersection, F) for F in fibers.itervalues()]


    # Now we know if there is a solution:

    sol = reduce(set.union, seeds)

    if sol != s0:
    print "No solution"
    else:
    print "Solution: union(intersection(fibers[x]) for x in s0)"


    I think this solution will be found in O(m*n**2) time, where:

    * n is the size of X (i.e. the total number of elements)
    * m is the size of S (i.e. the total number of sets)

    and assuming that set operations are linear.

    --
    Arnaud
    Arnaud Delobelle, May 26, 2008
    #10
  11. Arnaud Delobelle <> writes:

    > writes:
    >
    >> On May 25, 1:13 pm, "Martin v. Löwis" <> wrote:
    >>> > We can use any operation or function which
    >>> > takes and returns sets.
    >>>
    >>> I think the problem is significantly underspecified. It would be a more
    >>> interesting problem if there was a restriction to a few selected set
    >>> operations, e.g. union, intersection, difference, and combinations
    >>> thereof.

    >>
    >> Ok, that's quite right -- when I just tried to define "any function,"
    >> I found that all the solutions I came up with were combinations of the
    >> set operations defined for immutable sets. Let me improve the spec as
    >> the following:
    >>
    >> There may be arbitrarily many set elements (denoted by integers
    >> 1,2,3,...), arbitrarily many combinations of the elements composing
    >> the sets s_i (s0, s1, ...). We can use any of python's set operations
    >> or combination of those operations.

    >
    > OK then, if you only allow union, intersection, difference, symmetric
    > difference then I think it would be easy to prove (I haven't done it!)
    > that if there is a solution to you problem, then the method below
    > yields a solution:
    >
    > *** warning: all this is untested AND written in a rush ***
    >
    > let S be the set of all your sets except s0 (i.e. s1, s2, s3, ...)
    >
    >
    > # X is the "domain", C is the set of complements in X of elements of S
    >
    > X = reduce(set.union, S)
    > C = set(X - s for s in S)
    >
    >
    > # Find the "fibers" at each element x of s0. A fiber at x is the set
    > # of all s in S or C that contain x
    >
    > from collections import defaultdict
    > fibers = defaultdict(list)
    >
    > for s in S | C:
    > for x in s & s0:
    > fibers[x].append(s)
    >
    >
    > # Find the "seeds" at each x in s0. A seed at x is the set of all
    > # elements contained in all s in the fiber at x.
    >
    > # Intutively, a seed at x is the smallest set containing x that can be
    > # built from S
    >
    > seeds = [reduce(set.intersection, F) for F in fibers.itervalues()]
    >
    >
    > # Now we know if there is a solution:
    >
    > sol = reduce(set.union, seeds)
    >
    > if sol != s0:
    > print "No solution"
    > else:
    > print "Solution: union(intersection(fibers[x]) for x in s0)"
    >
    >
    > I think this solution will be found in O(m*n**2) time, where:
    >
    > * n is the size of X (i.e. the total number of elements)
    > * m is the size of S (i.e. the total number of sets)
    >
    > and assuming that set operations are linear.


    I forgot to add, using reduce() is probably not a great idea but it
    allowed me to write down my idea quickly!

    --
    Arnaud
    Arnaud Delobelle, May 26, 2008
    #11
  12. Guest

    I used Arnaud's suggestion with a few minor tweaks, as follows:

    #-----
    fset = frozenset
    s0 = fset([1])
    s1 = fset([1,2])
    s2 = fset([2])

    # let S be the set of all your sets except s0 (i.e. s1, s2, s3, ...)
    S = set([s1,s2])

    # X is the "domain", C is the set of complements in X of elements of
    S
    X = set()
    for s in S:
    X |= s
    print "s0 is ", s0
    print "S is ", S
    print "X is ", X

    C = set(fset(X - s) for s in S if fset(X-s))
    print "C is ", C

    # Find the "fibers" at each element x of s0.
    # A fiber at x is the set of all s in S or C that contain x
    from collections import defaultdict
    fibers = defaultdict(list)
    for s in S | C:
    for x in s & s0:
    fibers[x].append(s)
    print "fibers are ", fibers

    # Find the "seeds" at each x in s0.
    # A seed at x is the set of all elements contained in all s in the
    fiber at x.
    # Intutively, a seed at x is the smallest set containing x that can
    be
    # built from S
    seeds = set()
    for F in fibers.itervalues():
    seeds &= set(F)
    print 'seeds are ', seeds

    # Now we know if there is a solution:
    sol = set()
    for s in seeds:
    sol |= s
    if sol != s0:
    print "No solution"
    else:
    print "Solution: union(intersection(fibers[x]) for x in s0)"
    #-----

    yields this:

    s0 is frozenset([1])
    S is set([frozenset([1, 2]), frozenset([2])])
    X is set([1, 2])
    C is set([frozenset([1])])
    fibers are defaultdict(<type 'list'>, {1: [frozenset([1, 2]),
    frozenset([1])]})
    seeds are set([])
    No solution


    I follow your logic up to the point of the "seeds," where I get a bit
    confused. Thanks much! for the idea, I'm going to keep thinking about
    it. Any other ideas are *most* welcome. -- PB.
    , May 26, 2008
    #12
  13. Guest

    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.
    , May 27, 2008
    #13
  14. writes:

    > I used Arnaud's suggestion with a few minor tweaks, as follows:
    >
    > #-----
    > fset = frozenset
    > s0 = fset([1])
    > s1 = fset([1,2])
    > s2 = fset([2])
    >
    > # let S be the set of all your sets except s0 (i.e. s1, s2, s3, ...)
    > S = set([s1,s2])
    >
    > # X is the "domain", C is the set of complements in X of elements of
    > S
    > X = set()
    > for s in S:
    > X |= s
    > print "s0 is ", s0
    > print "S is ", S
    > print "X is ", X
    >
    > C = set(fset(X - s) for s in S if fset(X-s))
    > print "C is ", C
    >
    > # Find the "fibers" at each element x of s0.
    > # A fiber at x is the set of all s in S or C that contain x
    > from collections import defaultdict
    > fibers = defaultdict(list)
    > for s in S | C:
    > for x in s & s0:
    > fibers[x].append(s)
    > print "fibers are ", fibers
    >
    > # Find the "seeds" at each x in s0.
    > # A seed at x is the set of all elements contained in all s in the
    > fiber at x.
    > # Intutively, a seed at x is the smallest set containing x that can
    > be
    > # built from S
    > seeds = set()
    > for F in fibers.itervalues():
    > seeds &= set(F)

    Change this to:

    seeds = []
    for F in fibers.itervalues():
    seed = X
    for f in F:
    seed &= f
    seeds.append(seed)

    And the whole thing should work

    HTH

    > print 'seeds are ', seeds
    >
    > # Now we know if there is a solution:
    > sol = set()
    > for s in seeds:
    > sol |= s
    > if sol != s0:
    > print "No solution"
    > else:
    > print "Solution: union(intersection(fibers[x]) for x in s0)"
    > #-----
    >
    > yields this:
    >
    > s0 is frozenset([1])
    > S is set([frozenset([1, 2]), frozenset([2])])
    > X is set([1, 2])
    > C is set([frozenset([1])])
    > fibers are defaultdict(<type 'list'>, {1: [frozenset([1, 2]),
    > frozenset([1])]})
    > seeds are set([])
    > No solution
    >
    >
    > I follow your logic up to the point of the "seeds," where I get a bit
    > confused. Thanks much! for the idea, I'm going to keep thinking about
    > it. Any other ideas are *most* welcome. -- PB.


    --
    Arnaud
    Arnaud Delobelle, May 27, 2008
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Thee_Psycho

    OT: Java from 2nd Partition.

    Thee_Psycho, Nov 2, 2003, in forum: Java
    Replies:
    2
    Views:
    318
    Thee_Psycho
    Nov 2, 2003
  2. news
    Replies:
    0
    Views:
    355
  3. shakeel-ur-rehman

    sector , file and partition reading in C++

    shakeel-ur-rehman, Jan 24, 2004, in forum: C++
    Replies:
    18
    Views:
    9,472
    Alf P. Steinbach
    Feb 1, 2004
  4. jota

    std::partition

    jota, Jul 5, 2004, in forum: C++
    Replies:
    6
    Views:
    1,873
    Dietmar Kuehl
    Jul 6, 2004
  5. bolega
    Replies:
    1
    Views:
    673
    Stan Bischof
    Mar 28, 2011
Loading...

Share This Page