Wolfram Research

Function Repository Resource:

DyckWords

Source Notebook

Give all possible ways to form proper brackets

Contributed by: Wolfram Staff

ResourceFunction["DyckWords"][n]

gives all possible ways to form proper sets of n pairs of brackets.

Details and Options

Three conditions define a proper set of brackets: (1) [ ] is a proper set; (2) two proper sets can be concatenated; and (3) a proper set can be placed anywhere inside another proper set.

Examples

Basic Examples

Here is the simplest nontrivial example with two pairs of brackets:

In[1]:=
ResourceFunction["DyckWords"]@2
Out[1]=

Here are the ways to nest three pairs of brackets:

In[2]:=
ResourceFunction["DyckWords"]@3
Out[2]=

There are five ways to do that:

In[3]:=
Length@%
Out[3]=

Dyck words are counted by the Catalan numbers:

In[4]:=
CatalanNumber@Range@10
Out[4]=

Scope

Define the helper function:

In[5]:=
DyckPointsFromDyckWord@word_ :=
 Prepend[
  Accumulate[
   StringSplit@StringReplace[
      word,
      {"[" -> "[ ", "]" -> "] "}
      ] /. {"[" -> {1, 1}, "]" -> {1, -1}}
   ],
  {0, 0}
  ]

A Dyck path of length 2n units starts at the origin, goes NE or SE one unit and finishes on the x axis:

In[6]:=
Map[ListLinePlot@*DyckPointsFromDyckWord, ResourceFunction["DyckWords"][3]]
Out[6]=

Resource History

Source Metadata

License Information