Skip to content

Commit

Permalink
Trac #14126: Count Number of Linear Extensions of a Poset
Browse files Browse the repository at this point in the history
Posets appear to have no mechanism to count the number of linear
extensions other than computing all the linear extensions and finding
the length of that list. Using recursion, one can count the number of
linear extensions without having to generate all the linear extensions.

URL: https://trac.sagemath.org/14126
Reported by: csar
Ticket author(s): Jori Mäntysalo
Reviewer(s): Frédéric Chapoton
  • Loading branch information
Release Manager authored and vbraun committed Aug 5, 2016
2 parents 9a354aa + 24bc331 commit 6e41860
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/sage/combinat/posets/linear_extensions.py
Expand Up @@ -432,6 +432,35 @@ def poset(self):
"""
return self._poset

def cardinality(self):
"""
Return the number of linear extensions.
EXAMPLES::
sage: N = Poset({0: [2, 3], 1: [3]})
sage: N.linear_extensions().cardinality()
5
TESTS::
sage: Poset().linear_extensions().cardinality()
1
sage: Posets.ChainPoset(1).linear_extensions().cardinality()
1
"""
from sage.rings.integer import Integer

H = self._poset.order_ideals_lattice(as_ideals=False)._hasse_diagram
L = H.level_sets()
c = [0] * H.order()
for l in L[0]:
c[l] = 1
for lev in L[1:]:
for l in lev:
c[l] = sum(c[i] for i in H.lower_covers_iterator(l))
return Integer(sum(c[i] for i in H.sinks()))

def __iter__(self):
r"""
Iterates through the linear extensions of the underlying poset.
Expand Down

0 comments on commit 6e41860

Please sign in to comment.