Ticket #14126: trac_14126-count_linear_extensions_of_a_poset-csar.patch

File trac_14126-count_linear_extensions_of_a_poset-csar.patch, 2.5 KB (added by csar, 7 years ago)
  • sage/categories/finite_posets.py

    # HG changeset patch
    # User Sebastian Csar <sacsar@gmail.com>
    # Date 1360949553 18000
    # Node ID 8d3ba5344778bb2beb688d885eefc25e63239b92
    # Parent  ec1fb07db6e23f9fbd4c34f0d48198d08ec76473
    #14126: Add a function to count the number of linear extensions of a poset
    
    diff --git a/sage/categories/finite_posets.py b/sage/categories/finite_posets.py
    a b  
    1313from sage.categories.category import Category
    1414from sage.categories.posets import Posets
    1515from sage.categories.finite_enumerated_sets import FiniteEnumeratedSets
     16from sage.misc.cachefunc import cached_function
    1617
    1718class FinitePosets(Category):
    1819    r"""
     
    400401                sage: list(A)
    401402                [[], [0], [1], [1, 2], [1, 3], [2], [3], [4]]
    402403            """
     404
     405
     406        @cached_method
     407        def number_of_linear_extensions(self):
     408            """
     409            Returns the number of linear extensions of ``self``, that is the number of ways the partial order can be extended to a total order.
     410
     411            This function works by recursing through the order ideals with a cache. As a result, its complexity in time and memory is `O(n2^n)`. An alternative way to compute the number of linear extensions is to use the `cardinality` method of the `LinearExtensionsOfPoset` class, which uses `_cardinality_from_iterator` and is `O(n)` in memory and `O(n!)` in time.
     412
     413            EXAMPLES::
     414
     415                sage: P = Posets.BooleanLattice(3)
     416                sage: P.number_of_linear_extensions()
     417                48
     418                sage: P.linear_extensions().cardinality()
     419                48
     420
     421                sage: P = Posets.AntichainPoset(4)
     422                sage: P.number_of_linear_extensions()
     423                24
     424
     425                sage: P = Posets.ChainPoset(4)
     426                sage: P.number_of_linear_extensions()
     427                1
     428                sage: P.number_of_linear_extensions() == len(P.linear_extensions())
     429                True
     430            """
     431            from sage.combinat.posets.posets import Poset
     432            @cached_function
     433            def extensions_rec( P ):
     434                if P.cardinality() == 1:
     435                    return 1
     436                exts = 0
     437                for x in P.maximal_elements():
     438                    H = P.hasse_diagram()
     439                    H.delete_vertex( x )
     440                    exts += extensions_rec( Poset( H ) )
     441                return exts
     442            return extensions_rec( self )