Example #1
0
def ParkingFunction(pf=None, labelling=None, area_sequence=None,
                    labelled_dyck_word=None):
    r"""
    Return the combinatorial class of Parking Functions.

    A *parking function* of size `n` is a sequence `(a_1, \ldots,a_n)`
    of positive integers such that if `b_1 \leq b_2 \leq \cdots \leq b_n` is
    the increasing rearrangement of `a_1, \ldots, a_n`, then `b_i \leq i`.

    A *parking function* of size `n` is a pair `(L, D)` of two sequences
    `L` and `D` where `L` is a permutation and `D` is an area sequence
    of a Dyck Path of size `n` such that `D[i] \geq 0`, `D[i+1] \leq D[i]+1`
    and if `D[i+1] = D[i]+1` then `L[i+1] > L[i]`.

    The number of parking functions of size `n` is equal to the number
    of rooted forests on `n` vertices and is equal to `(n+1)^{n-1}`.

    INPUT:

    - ``pf`` -- (default: None) a list whose increasing rearrangement satisfies `b_i \leq i`

    - ``labelling`` -- (default: None) a labelling of the Dyck path

    - ``area_sequence`` -- (default: None) an area sequence of a Dyck path

    - ``labelled_dyck_word`` -- (default: None) a Dyck word with 1's replaced by labelling

    OUTPUT:

    - A parking function

    EXAMPLES::

        sage: ParkingFunction([])
        []
        sage: ParkingFunction([1])
        [1]
        sage: ParkingFunction([2])
        Traceback (most recent call last):
        ...
        ValueError: [2] is not a parking function.
        sage: ParkingFunction([1,2])
        [1, 2]
        sage: ParkingFunction([1,1,2])
        [1, 1, 2]
        sage: ParkingFunction([1,4,1])
        Traceback (most recent call last):
        ...
        ValueError: [1, 4, 1] is not a parking function.
        sage: ParkingFunction(labelling=[3,1,2], area_sequence=[0,0,1])
        [2, 2, 1]
        sage: ParkingFunction([2,2,1]).to_labelled_dyck_word()
        [3, 0, 1, 2, 0, 0]
        sage: ParkingFunction(labelled_dyck_word = [3,0,1,2,0,0])
        [2, 2, 1]
        sage: ParkingFunction(labelling=[3,1,2], area_sequence=[0,1,1])
        Traceback (most recent call last):
        ...
        ValueError: [3, 1, 2] is not a valid labeling of area sequence [0, 1, 1]
    """
    if pf is not None:
        return ParkingFunction_class(pf)
    elif labelling is not None:
        if (area_sequence is None):
            raise ValueError("must also provide area sequence along with labelling.")
        if (len(area_sequence) != len(labelling)):
            raise ValueError("%s must be the same size as the labelling %s" % (area_sequence, labelling))
        if any(area_sequence[i] < area_sequence[i+1] and labelling[i] > labelling[i + 1] for i in range(len(labelling) - 1)):
            raise ValueError("%s is not a valid labeling of area sequence %s" % (labelling, area_sequence))
        return from_labelling_and_area_sequence(labelling, area_sequence)
    elif labelled_dyck_word is not None:
        return from_labelled_dyck_word(labelled_dyck_word)
    elif area_sequence is not None:
        DW = DyckWord(area_sequence)
        return ParkingFunction(labelling=range(1, DW.size() + 1),
                               area_sequence=DW)

    raise ValueError("did not manage to make this into a parking function")
def ParkingFunction(pf=None, labelling=None, area_sequence=None, labelled_dyck_word = None):
    r"""
    Returns the combinatorial class of Parking Functions.

    A *parking function* of size `n` is a sequence `(a_1, \ldots,a_n)`
    of positive integers such that if `b_1 \leq b_2 \leq \cdots \leq b_n` is
    the increasing rearrangement of `a_1, \ldots, a_n`, then `b_i \leq i`.

    A *parking function* of size `n` is a pair `(L, D)` of two sequences
    `L` and `D` where `L` is a permutation and `D` is an area sequence
    of a Dyck Path of size `n` such that `D[i] \geq 0`, `D[i+1] \leq D[i]+1`
    and if `D[i+1] = D[i]+1` then `L[i+1] > L[i]`.

    The number of parking functions of size `n` is equal to the number of rooted forests
    on `n` vertices and is equal to `(n+1)^{n-1}`.

    INPUT:

    - ``pf`` -- (default: None) a list whose increasing rearrangement satisfies `b_i \leq i`

    - ``labelling`` -- (default: None) a labelling of the Dyck path

    - ``area_sequence`` -- (default: None) an area sequence of a Dyck path

    - ``labelled_dyck_word`` -- (default: None) a Dyck word with 1's replaced by labelling

    OUTPUT:

    - A parking function

    EXAMPLES::

        sage: ParkingFunction([])
        []
        sage: ParkingFunction([1])
        [1]
        sage: ParkingFunction([2])
        Traceback (most recent call last):
        ...
        ValueError: [2] is not a parking function.
        sage: ParkingFunction([1,2])
        [1, 2]
        sage: ParkingFunction([1,1,2])
        [1, 1, 2]
        sage: ParkingFunction([1,4,1])
        Traceback (most recent call last):
        ...
        ValueError: [1, 4, 1] is not a parking function.
        sage: ParkingFunction(labelling=[3,1,2], area_sequence=[0,0,1])
        [2, 2, 1]
        sage: ParkingFunction([2,2,1]).to_labelled_dyck_word()
        [3, 0, 1, 2, 0, 0]
        sage: ParkingFunction(labelled_dyck_word = [3,0,1,2,0,0])
        [2, 2, 1]
        sage: ParkingFunction(labelling=[3,1,2], area_sequence=[0,1,1])
        Traceback (most recent call last):
        ...
        ValueError: [3, 1, 2] is not a valid labeling of area sequence [0, 1, 1]
    """
    if pf is not None:
        return ParkingFunction_class(pf)
    elif labelling is not None:
        if (area_sequence is None):
            raise ValueError("must also provide area sequence along with labelling.")
        if (len(area_sequence)!=len(labelling)):
            raise ValueError("%s must be the same size as the labelling %s"%(area_sequence,labelling))
        if any(area_sequence[i]<area_sequence[i+1] and labelling[i]>labelling[i+1] for i in range(len(labelling)-1)):
            raise ValueError("%s is not a valid labeling of area sequence %s"%(labelling, area_sequence))
        return from_labelling_and_area_sequence( labelling, area_sequence )
    elif labelled_dyck_word is not None:
        return from_labelled_dyck_word(labelled_dyck_word)
    elif area_sequence is not None:
        DW = DyckWord(area_sequence)
        return ParkingFunction(labelling=range(1,DW.size()+1), area_sequence=DW)
    else:
        raise ValueError("did not manage to make this into a parking function")