2

I am looking for a reasonable algorithm in python (well, because I have rather complicated mathematical objects implemented in python, so I cannot change the language) to achieve the following:

I am given a reflexive, symmetric binary relation bin_rel on a set X. The requested function maximal_compatible_subsets(X, bin_rel) should return all containmentwise maximal subsets of X such that the binary relation holds for all pairs a,b of elements in X.

In some more detail: Suppose I am given a binary relation on a set of objects, say

def bin_rel(elt1,elt2):
    # return True if elt1 and elt2 satisfy the relation and False otherwise
    # Example: set intersection, in this case, elt1 and elt2 are sets
    # and the relation picks those pairs that have a nonempty intersection
    return elt1.intersection(elt2)

I can also assume that the relation bin_rel is reflexive (this is, binary_rel(a,a) is True holds) and symmetric (this is, binary_rel(a,b) is binary_rel(b,a) holds).

I am now given a set X and a function bin_rel as above and seek an efficient algorithm to obtain the desired subsets of X

For example, in the case of the set intersection above (with sets replaced by lists for easier reading):

> X = [ [1,2,3], [1,3], [1,6], [3,4], [3,5], [4,5] ]
> maximal_compatible_subsets(X,bin_rel)
[[[1,2,3],[1,3],[1,6]], [[1,2,3],[1,3],[3,4],[3,5]], [[3,4],[3,5],[4,5]]]

This problem doesn't seem to be very exotic, so most welcome would be a pointer to an efficient existing snippet of code.

6
  • The intersection was just a simple example. In general, I am given bin_rel, so I do not worry how this code is written. Also, I can assume it to be reflexive and symmetric, and I do not need to test that. But this assumption will certainly influence the algorithm as I do not ever need to test bin_rel(a,a) or (both bin_rel(a,b) and bin_rel(b,a)). Commented Nov 4, 2016 at 12:21
  • 2
    Note that this is NP-hard, since you can use it to solve max-clique: en.wikipedia.org/wiki/Clique_problem Commented Nov 4, 2016 at 12:23
  • @MattTimmermans: yep, that's right! Commented Nov 4, 2016 at 12:25
  • 1
    You could use NetworkX Commented Nov 4, 2016 at 12:28
  • @MattTimmermans: Now as you write this, indeed what that algorithm searches for is exactly the set of all maximal cliques (or maximal independent sets) in a graph. Commented Nov 4, 2016 at 12:29

2 Answers 2

3

As Matt Timmermans noted this is finding maximal cliques problem that can be solved by Bron–Kerbosch algorithm. NetworkX has implementation that can be used for Python.

Sign up to request clarification or add additional context in comments.

2 Comments

Note that it is not the maximal clique problem, as there only one clique (of globally maximal size) is requested, while I was requesting all maximal cliques. Anyway, thanks, and networkx.find_cliques does exactly that.
@ChristianStump: Sorry if I was unclear, I reworded the answer a bit to make it clearer. Finding maximal cliques is indeed different problem than finding maximum clique.
0

If you want to use python straight out of the box, you could use the following as a starting point:

from itertools import combinations

def maximal_compatible_subsets(X, bin_rel):
    retval = []
    for i in range(len(X) + 1, 1, -1):
        for j in combinations(X, i):
            if all(bin_rel(a, b) for a, b in combinations(j, 2)) and not any(set(j).issubset(a) for a in retval):
                retval.append(tuple(j))
    return tuple(retval)

if __name__ == '__main__':
    x = ( (1,2,3), (1,3), (1,6), (3,4), (3,5), (4,5) )

    def nonempty_intersection(a, b):
        return set(a).intersection(b)

    print x
    print maximal_compatible_subsets(x, nonempty_intersection)

Outputs:

((1, 2, 3), (1, 3), (1, 6), (3, 4), (3, 5), (4, 5))
(((1, 2, 3), (1, 3), (3, 4), (3, 5)), ((1, 2, 3), (1, 3), (1, 6)), ((3, 4), (3, 5), (4, 5)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.