Dyck language

From testwiki
Revision as of 20:28, 26 February 2025 by 109.243.0.15 (talk)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Template:Short description Template:One source In the theory of formal languages of computer science, mathematics, and linguistics, a Dyck word is a balanced string of brackets. The set of Dyck words forms a Dyck language. The simplest, Dyck-1, uses just two matching brackets, e.g. ( and ).

Dyck words and language are named after the mathematician Walther von Dyck. They have applications in the parsing of expressions that must have a correctly nested sequence of brackets, such as arithmetic or algebraic expressions.

Formal definition

Let Σ={[,]} be the alphabet consisting of the symbols [ and ]. Let Σ* denote its Kleene closure. The Dyck language is defined as:

{uΣ*| all prefixes of u contain no more ]'s than ['s and the number of ['s in u equals the number of ]'s}.

Context-free grammar

It may be helpful to define the Dyck language via a context-free grammar in some situations. The Dyck language is generated by the context-free grammar with a single non-terminal Template:Math, and the production:

Template:Math

That is, S is either the empty string (Template:Math) or is "[", an element of the Dyck language, the matching "]", and an element of the Dyck language.

An alternative context-free grammar for the Dyck language is given by the production:

Template:Math

That is, S is zero or more occurrences of the combination of "[", an element of the Dyck language, and a matching "]", where multiple elements of the Dyck language on the right side of the production are free to differ from each other.

Alternative definition

In yet other contexts it may instead be helpful to define the Dyck language by splitting Σ* into equivalence classes, as follows. For any element uΣ* of length |u|, we define partial functions insert:Σ*×Σ* and delete:Σ*×Σ* by

insert(u,j) is u with "[]" inserted into the jth position
delete(u,j) is u with "[]" deleted from the jth position

with the understanding that insert(u,j) is undefined for j>|u| and delete(u,j) is undefined if j>|u|2. We define an equivalence relation R on Σ* as follows: for elements a,bΣ* we have (a,b)R if and only if there exists a sequence of zero or more applications of the insert and delete functions starting with a and ending with b. That the sequence of zero operations is allowed accounts for the reflexivity of R. Symmetry follows from the observation that any finite sequence of applications of insert to a string can be undone with a finite sequence of applications of delete. Transitivity is clear from the definition.

The equivalence relation partitions the language Σ* into equivalence classes. If we take ϵ to denote the empty string, then the language corresponding to the equivalence class Cl(ϵ) is called the Dyck language.

Generalizations

Typed Dyck language

There exist variants of the Dyck language with multiple delimiters, e.g., Dyck-2 on the alphabet "(", ")", "[", and "]". The words of such a language are the ones which are well-parenthesized for all delimiters, i.e., one can read the word from left to right, push every opening delimiter on the stack, and whenever we reach a closing delimiter then we must be able to pop the matching opening delimiter from the top of the stack. (The counting algorithm above does not generalise).

For example, the following is a valid sentence in Dyck-3 (with matching delimiters colored the same):

( [ [ ] { } ] ( ) { ( ) } ) [ ]

Finite depth

A Dyck language sentence can be pictured as a descent and ascent through the levels of nested brackets. As one reads along a Dyck sentence, each opening bracket increases the nesting depth by 1, and each closing bracket decreases by 1. The depth of a sentence is the maximal depth reached within the sentence.

For example, we can annotate the following sentence with the depth at each step:

0 ( 1 [ 2 [ 3 ] 2 { 3 } 2 ] 1 ( 2 ) 1 { 2 ( 3 ) 2 } 1 ) 0 [ 1 ] 0

and the entire sentence has depth 3.

We define Dyck-(k, m) as the language with k types of brackets and maximal depth m. This has applications in the formal theory of recurrent neural networks.[1]

Properties

Cn=k=1nN(n,k)

Examples

Lattice of the 14 Dyck words of length 8 - [ and ] interpreted as up and down

We can define an equivalence relation L on the Dyck language 𝒟. For u,v𝒟 we have (u,v)L if and only if |u|=|v|, i.e. u and v have the same length. This relation partitions the Dyck language: 𝒟/L={𝒟0,𝒟1,}. We have 𝒟=𝒟0𝒟2𝒟4=n=0𝒟n where 𝒟n={u𝒟|u|=n}. Note that 𝒟n is empty for odd n.

Having introduced the Dyck words of length n, we can introduce a relationship on them. For every n we define a relation Sn on 𝒟n; for u,v𝒟n we have (u,v)Sn if and only if v can be reached from u by a series of proper swaps. A proper swap in a word u𝒟n swaps an occurrence of '][' with '[]'. For each n the relation Sn makes 𝒟n into a partially ordered set. The relation Sn is reflexive because an empty sequence of proper swaps takes u to u. Transitivity follows because we can extend a sequence of proper swaps that takes u to v by concatenating it with a sequence of proper swaps that takes v to w forming a sequence that takes u into w. To see that Sn is also antisymmetric we introduce an auxiliary function σn:𝒟n defined as a sum over all prefixes v of u:

σn(u)=vw=u((count of ['s in v)(count of ]'s in v))

The following table illustrates that σn is strictly monotonic with respect to proper swaps.

Strict monotonicity of σn
partial sums of σn(u) P P1 P Q
u ] [
u [ ]
partial sums of σn(u) P P+1 P Q
Difference of partial sums 0 2 0 0

Hence σn(u)σn(u)=2>0 so σn(u)<σn(u) when there is a proper swap that takes u into u. Now if we assume that both (u,v),(v,u)Sn and uv, then there are non-empty sequences of proper swaps such u is taken into v and vice versa. But then σn(u)<σn(v)<σn(u) which is nonsensical. Therefore, whenever both (u,v) and (v,u) are in Sn, we have u=v, hence Sn is antisymmetric.

The partial ordered set D8 is shown in the illustration accompanying the introduction if we interpret a [ as going up and ] as going down.

See also

Notes

Template:Reflist

References

  1. Template:Cite arXiv
  2. Kambites, Communications in Algebra Volume 37 Issue 1 (2009) 193-208
  3. Barrington and Corbett, Information Processing Letters 32 (1989) 251-256