简体   繁体   中英

Subset of mutually exclusive items from sets

I have sets S_0, ..., S_N . How do I find the largest subset T , such that the intersection I_i of T and S_i (for each 0 <= i <= N) contains at most one element.

I have a solution for this, but I'm guessing that it's unnecessarily slow (essentially several nested for-loops trying out all combinations). So my question is:

  • Is there an efficient algorithm for this problem?
  • If not, is there an efficient algorithm that finds large subsets T ?

I think you are unlikely to find an efficient general algorithm for this problem because I believe it is NP complete.

If you had an efficient algorithm to solve this, then you could solve the maximum independent set problem .

Sketch of Proof

Suppose you have a graph, then for each edge construct a set containing {i,j} where i and j are the vertices connected by the edge.

Then the largest subset T for these sets will be the maximal independent set for your graph.

Conversion to Maximal independent set

More usefully, you can also express your problem in terms of finding a maximal independent set for the graph where there is an edge between a and b if and only if there is a set that contains both a and b.

You may then be able to use some standard solvers for the maximal independent set problem such as the one in Pythons NetworkX .

You could speed up your program by caching whether the intersection of sets S_a and S_b are empty or not. Rather than building the set T which I think is the union of some of the sets in S . You keep a list T of the indexes of sets and to check if a set S_n intersects with T you check if in the table for the set in T that S_n intersects with one of them.

I did this in one of my Python programs as the set intersection test was the slow spot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM