# Independent sets¶

This module implements the IndependentSets class which can be used to :

• List the independent sets (or cliques) of a graph
• Count them (which is obviously faster)
• Test whether a set of vertices is an independent set

It can also be restricted to focus on (inclusionwise) maximal independent sets. See the documentation of IndependentSets for actual examples.

## Classes and methods¶

class sage.graphs.independent_sets.IndependentSets

Bases: object

The set of independent sets of a graph.

INPUT:

• G – a graph
• maximal – boolean (default: False); whether to only consider (inclusionwise) maximal independent sets.
• complement – boolean (default: False); whether to consider the graph’s complement (i.e. cliques instead of independent sets).

ALGORITHM:

The enumeration of independent sets is done naively : given an independent set, this implementation considers all ways to add a new vertex to it (while keeping it an independent set), and then creates new independent sets from all those that were created this way.

The implementation, however, is not recursive.

Note

This implementation of the enumeration of maximal independent sets is not much faster than NetworkX’, which is surprising as it is written in Cython. This being said, the algorithm from NetworkX appears to be sligthly different from this one, and that would be a good thing to explore if one wants to improve the implementation.

A simple generalization can also be done without too much modifications: iteration through independent sets with given size bounds (minimum and maximum number of vertices allowed).

EXAMPLES:

Listing all independent sets of the Claw graph:

sage: from sage.graphs.independent_sets import IndependentSets
sage: g = graphs.ClawGraph()
sage: I = IndependentSets(g)
sage: list(I)
[, , [1, 2], [1, 2, 3], [1, 3], , [2, 3], , []]


Count them:

sage: I.cardinality()
9


List only the maximal independent sets:

sage: Im = IndependentSets(g, maximal=True)
sage: list(Im)
[, [1, 2, 3]]


And count them:

sage: Im.cardinality()
2


One can easily count the number of independent sets of each cardinality:

sage: g = graphs.PetersenGraph()
sage: number_of =  * g.order()
sage: for x in IndependentSets(g):
....:     number_of[len(x)] += 1
sage: number_of
[1, 10, 30, 30, 5, 0, 0, 0, 0, 0]


It is also possible to define an iterator over all independent sets of a given cardinality. Note, however, that Sage will generate them all, to return only those that satisfy the cardinality constraints. Getting the list of independent sets of size 4 in this way can thus take a very long time:

sage: is4 = (x for x in IndependentSets(g) if len(x) == 4)
sage: list(is4)
[[0, 2, 8, 9], [0, 3, 6, 7], [1, 3, 5, 9], [1, 4, 7, 8], [2, 4, 5, 6]]


Given a subset of the vertices, it is possible to test whether it is an independent set:

sage: g = graphs.DurerGraph()
sage: I = IndependentSets(g)
sage: [0, 2] in I
True
sage: [0, 3, 5] in I
False


If an element of the subset is not a vertex, then an error is raised:

sage: [0, 'a', 'b', 'c'] in I
Traceback (most recent call last):
...
ValueError: a is not a vertex of the graph

cardinality()

Compute and return the number of independent sets.