Independent sets - Graph Theory

Skip to content
Back to top
View this page
Edit this page

# 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.

  For more information on independent sets, see the Wikipedia article Independent_set_(graph_theory).

  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 slightly 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

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

  Python

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

  Count them:

  Sage

      sage: I.cardinality()
      9

  [Python]

      >>> from sage.all import *
      >>> I.cardinality()
      9

  List only the maximal independent sets:

  Sage

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

  Python

      >>> from sage.all import *
      >>> Im = IndependentSets(g, maximal=True)
      >>> list(Im)
      [[0], [1, 2, 3]]

  And count them:

  Sage

      sage: Im.cardinality()
      2

  [Python]

      >>> from sage.all import *
      >>> Im.cardinality()
      2

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

  Sage

      sage: g = graphs.PetersenGraph()
      sage: number_of = [0] * 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]

  Python

      >>> from sage.all import *
      >>> g = graphs.PetersenGraph()
      >>> number_of = [Integer(0)] * g.order()
      >>> for x in IndependentSets(g):
      ...     number_of[len(x)] += Integer(1)
      >>> 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

      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]]

  [Python]

      >>> from sage.all import *
      >>> is4 = (x for x in IndependentSets(g) if len(x) == Integer(4))
      >>> 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

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

  Python

      >>> from sage.all import *
      >>> g = graphs.DurerGraph()
      >>> I = IndependentSets(g)
      >>> [Integer(0), Integer(2)] in I
      True
      >>> [Integer(0), Integer(3), Integer(5)] in I
      False

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

  Sage

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

  [Python]

      >>> from sage.all import *
      >>> [Integer(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.