Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #17804: Add cardinality function to know the number of orderings
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanncohen committed Feb 18, 2015
1 parent 707124c commit 6a1b563
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/sage/graphs/pq_trees.py
Expand Up @@ -685,6 +685,28 @@ def set_contiguous(self, v):

return PARTIAL, False

def cardinality(self):
r"""
Return the number of orderings allowed by the structure.
EXAMPLE::
sage: from sage.graphs.pq_trees import P, Q
sage: p = P([[0,3], [1,2], [2,3], [2,4], [4,0],[2,8], [2,9]])
sage: p.cardinality()
5040
sage: p.set_contiguous(3)
(1, True)
sage: p.cardinality()
1440
"""
from math import factorial
n = factorial(self.number_of_children())
for c in self._children:
if isinstance(c,PQ):
n = n*c.cardinality()
return n

class Q(PQ):
r"""
A Q-Tree is a PQ-Tree whose children are
Expand Down Expand Up @@ -976,3 +998,20 @@ def set_contiguous(self, v):

return (PARTIAL, not seen_right_end)

def cardinality(self):
r"""
Return the number of orderings allowed by the structure.
EXAMPLE::
sage: from sage.graphs.pq_trees import P, Q
sage: q = Q([[0,3], [1,2], [2,3], [2,4], [4,0],[2,8], [2,9]])
sage: q.cardinality()
5040
"""
n = 1
for c in self._children:
if isinstance(c,PQ):
n = n*c.cardinality()

return n if (self.number_of_children() == 1) else 2*n

0 comments on commit 6a1b563

Please sign in to comment.