Function Repository Resource:

ParkingFunctionQ

Source Notebook

Determine whether a list of positive integers is a valid parking function

Contributed by: Shenghui Yang

ResourceFunction["ParkingFunctionQ"][l]

returns True if a given list of nonnegative integers l satisfies the defining condition of a parking function, and False otherwise.

Details

Let α=(a1,a2,a3,,an-1,an). Let b1<=b2<=b3<=<=bn-1<=bn be the increasing rearrangement of α. Then α is a parking function if and only if bi<=i.
Every permutation of the entries of a parking function is also a parking function.
Parking functions represent preferences for parking spots wherein each car can successfully park in its preferred spot or later.
Let f(n) be the number of parking functions of length n. Then f(n)=(n+1)n-1.
The parking functions can also be placed in bijection with the spanning trees on a complete graph with n+1 vertices, one of which is designated as the root.

Examples

Basic Examples (2) 

Test if a list is a valid parking function:

In[1]:=
ResourceFunction["ParkingFunctionQ"][{ 6, 1, 5, 2, 2, 1, 5}]
Out[2]=

The following list is not a valid parking function because the second car parks at the third spot so there is no valid spot for the last car:

In[3]:=
seq = {1, 3, 3};
ResourceFunction["ParkingFunctionQ"][seq]
Out[4]=

Scope (3) 

Enumerate all parking functions with length three:

In[5]:=
tuples = Tuples[{1, 2, 3}, {3}];
valid = Select[tuples, ResourceFunction["ParkingFunctionQ"]]
Out[6]=

Verify the result with the length formula:

In[7]:=
Length[valid] == (3 + 1)^(3 - 1)
Out[7]=

Show which tuples are not valid parking functions:

In[8]:=
Complement[tuples, valid]
Out[8]=

Applications (6) 

The code below takes a parking function and returns the actual order in which the cars will park:

In[9]:=
carPerm[l_?ResourceFunction["ParkingFunctionQ"]] := Module[{used = {}}, AppendTo[
     used, # + First@Complement[Range[0, Length[l] - #], used - #]] & /@ l; Ordering@used]

Give preferred parking spots for cars labelled from 1 to 7:

In[10]:=
preferences = { 6, 1, 5, 2, 2, 1, 5};
carLen = Length[preferences];

Calculate where the cars actually park, starting with car number one in the sixth spot:

In[11]:=
cp = carPerm[preferences]
Out[11]=

Find the permutation after all cars have parked (spot 6 is taken by car 1, spot 1 by car 2, etc.):

In[12]:=
psPerm = (Range[carLen] /. MapIndexed[#1 -> #2[[1]] &, cp])
Out[12]=

The jump list shows that car 1 through 4 parked in their preferred spots, car 5 had to jump one space further, car 6 had to jump 3 spots and car 7 had to jump two spots:

In[13]:=
jl = With[{len = Length[preferences]}, psPerm - preferences]
Out[13]=

The lucky cars are the ones parked at their preferred spot, which is the position of zeros in the jump list above:

In[14]:=
Position[jl, 0][[All, 1]]
Out[14]=

Properties and Relations (2) 

Test if the following list is a valid parking function:

In[15]:=
seq = { 6, 1, 5, 2, 2, 1, 5};
ResourceFunction["ParkingFunctionQ"][seq]
Out[16]=

All permutations of the input are valid as well:

In[17]:=
AllTrue[Union[
  seq[[#]] & /@ Permutations[{1, 2, 3, 4, 5, 6, 7}]], ResourceFunction[
 "ParkingFunctionQ"]]
Out[17]=

Possible Issues (1) 

The input must be a list of nonnegative integers. Otherwise the function returns unevaluated:

In[18]:=
ResourceFunction["ParkingFunctionQ"][{3, 4, 1, \[Pi]}]
Out[18]=
In[19]:=
ResourceFunction["ParkingFunctionQ"][{3, 4, 1, -1}]
Out[19]=

Publisher

Shenghui Yang

Version History

  • 1.0.0 – 23 January 2026

Source Metadata

Related Resources

Author Notes

For the relationship between parking functions, rooted forests, non-crossing partitions of set,and Dyck paths, see the referenced note by R. Stanley.

License Information