<?xml version="1.0" encoding="UTF-8"?>

<record version="14" id="11776">
 <title>Dyck language</title>
 <name>DyckLanguage</name>
 <created>2009-05-11 23:55:23</created>
 <modified>2009-10-03 13:59:40</modified>
 <type>Definition</type>
 <creator id="3771" name="CWoo"/>
 <author id="3771" name="CWoo"/>
 <classification>
	<category scheme="msc" code="68Q42"/>
	<category scheme="msc" code="68Q45"/>
 </classification>
 <defines>
	<concept>well-balanced</concept>
 </defines>
 <synonyms>
	<synonym concept="Dyck language" alias="well-nested"/>
	<synonym concept="Dyck language" alias="fully balanced"/>
	<synonym concept="Dyck language" alias="parenthesis language"/>
 </synonyms>
 <related>
	<object name="DyckPaths"/>
	<object name="ExampleOfCatalanNumbers"/>
 </related>
 <preamble>\usepackage{amssymb,amscd}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{mathrsfs}

% used for TeXing text within eps files
%\usepackage{psfrag}
% need this for including graphics (\includegraphics)
%\usepackage{graphicx}
% for neatly defining theorems and propositions
\usepackage{amsthm}
% making logically defined graphics
\usepackage{xypic}
\usepackage{pst-plot}

% define commands here
\newcommand*{\abs}[1]{\left\lvert #1\right\rvert}
\newtheorem{prop}{Proposition}
\newtheorem{thm}{Theorem}
\newtheorem{ex}{Example}
\newcommand{\real}{\mathbb{R}}
\newcommand{\pdiff}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\mpdiff}[3]{\frac{\partial^#1 #2}{\partial #3^#1}}</preamble>
 <content>The importance of using parentheses can be illustrated by looking at the following expression:
$$((2-1)\cdot (-(1+2))\cdot 4)\div((1+2)\cdot 2)$$
There is no ambiguity in computing the result, which is $-2$.  If we remove all the parentheses in the expression, we get 
$$2-1\cdot -1+2 \cdot 4\div 1+2 \cdot 2$$
which does not make much sense, unless we know the order of arithmetic operations in advance.  In addition, without using parentheses, the result will differ depending on how the order of operations is assigned.

Now, if we remove all the symbols in the first expression above except the parentheses, we get 
$$(()(()))(())$$
an expression known as a word of ``well-balanced'' parentheses.

Formally, let $\Sigma_1 = \lbrace (, ) \rbrace$ be an alphabet consisting of the left and right parentheses.  Given word $u$ over $\Sigma_1$, let $D_1(u)$ be the number of occurrences of the left parentheses in $u$ minus the number of occurrences of the right parentheses in $u$.  

\textbf{Definition}.  A word $u$ over $\Sigma_1$ is said to be a word of \emph{well-balanced} parentheses, if 
\begin{enumerate}
\item $D_1(u)=0$, and
\item $D_1(v)\ge 0$ for any prefix $v$ of $u$.
\end{enumerate}
For simplicity, we also say that $u$ is a well-balanced word over $\Sigma_1$.

Given this definition, the word above is well-balanced, but $()(()))$ and $)())(($ are not.

\textbf{Definition}.  The set of well-balanced words over $\Sigma_1$ is called the \emph{parenthesis language} or \emph{Dyck language} over $\Sigma_1$, and is denoted by $\boldsymbol{\operatorname{Paren}_1}$.  

The $1$ in $\Sigma_1$ denotes that only one type of parentheses is used in the language.  

By induction, it is not hard to see that $\boldsymbol{\operatorname{Paren}_1}$ can be generated by the following grammar:
\begin{enumerate}
\item terminal set is $\Sigma_1$, 
\item non-terminal set is the singleton consisting of the start symbol $\sigma$, 
\item productions are $\sigma \to \lambda$ (the empty word), $\sigma \to \sigma\sigma$, and $\sigma \to ( \sigma )$.  
\end{enumerate}
As a result, $\boldsymbol{\operatorname{Paren}_1}$ is context-free.  Furthermore, $\boldsymbol{\operatorname{Paren}_1}$ is a deterministic language, and hence unambiguous.

More generally, one can consider expressions involving more than one type of parentheses, such as [], $\lbrace \rbrace$, and $\langle \rangle$.

\textbf{Definition}.  Let $\Sigma_n = \lbrace (_1, )_1, \ldots, (_n, )_n \rbrace$ be an alphabet consisting of $n$ types of parentheses, a left and a right one for each type.  The \emph{Dyck language} over $\Sigma_n$, written  $\boldsymbol{\operatorname{Paren}_n}$, is the language generated by the following grammar:
\begin{enumerate}
\item terminal set is $\Sigma_n$, 
\item non-terminal set is the singleton consisting of the start symbol $\sigma$, 
\item productions are $\sigma \to \lambda$ (the empty word), $\sigma \to \sigma\sigma$, and $\sigma \to (_i \sigma )_i$ for each $i$ in $\lbrace 1,\ldots, n\rbrace$.  
\end{enumerate}
As before, $\boldsymbol{\operatorname{Paren}_n}$ is context-free, and deterministic in particular, and hence unambiguous.

Words in $\boldsymbol{\operatorname{Paren}_n}$ are also called \emph{well-balanced}.  However, it is a little more complicated to characterize what a well-balanced word is.  The two criteria above for the case $n=1$, while necessary, are not sufficient enough to describe the ``well-nestedness'' of parentheses when $n&gt;1$.  For example, if $n=2$, and the parentheses considered are $\lbrace \rbrace$ and $[]$, then the word $[\lbrace ]\rbrace$ satisfy both criteria above, but fail to be well-nested.

In order to fully characterize a well-balanced word over $\Sigma_n$, we first define, for each $i=1,\ldots, n$, the function $D_i$ much the same way as $D_1$: so that $D_i(u)$ is the number of left parentheses $(_i$, minus the number of right parentheses $)_i$.  Call a word $u$ partially balanced if, for every $i=1,\ldots, n$:
\begin{enumerate}
\item $D_i(u)=0$, and
\item $D_i(v)\ge 0$ for every prefix $v$ of $u$.
\end{enumerate}
Next, write $u=u_1 \cdots u_m$, where each $u_k$ is a symbol in $\Sigma_n$.  Let $u(j)$ be the prefix $u_1 \cdots u_j$.  Given a position $j$ in $u$, if $u_j$ is a left parenthesis, say $(_i$, then there is a corresponding right parenthesis $)_i$ in $u$ to the right of $u_j$, positioned at, say $k$, satisfying the equation $D_i(u(j))=D_i(u(k))+1$.  This is a straightforward result of the two criteria above.  Let $j^+$ be the least such position such that the equation holds.  Now, if $u_j$ is  right parenthesis, then for some position $k&lt;j$, we have $k^+=j$.  This means that, given any position $j$ in $u$, there is a unique pair of positions $(j_0,j_1)$, such that
\begin{itemize}
\item either $j=j_0$ or $j=j_1$, and
\item $j_0^+ = j_1$.
\end{itemize}
Define, for each $j$, the word $u[j]$ to be the subword of $u$ with starting position $j_0$ and ending position $j_1$.  Now, we are ready to state the last criterion in order that $u$ be well-balanced:
\begin{enumerate}
\item[3.] for each position $j$ in $u$, the word $u[j]$ is partially balanced.
\end{enumerate}
It can be shown, the set of words satisfying all three criteria above is $\boldsymbol{\operatorname{Paren}_n}$.  Furthermore, if $n=1$, the third criterion can be derived from the first two criteria.

Other than being deterministic, some basic properties of $\boldsymbol{\operatorname{Paren}_n}$:
\begin{itemize}
\item $\boldsymbol{\operatorname{Paren}_m} \subseteq \boldsymbol{\operatorname{Paren}_n}$, for any $m\le n$.
\item $\boldsymbol{\operatorname{Paren}_n}$ is monoidal (it is a monoid): $\lambda \in \boldsymbol{\operatorname{Paren}_n}$, and if $u,v\in \boldsymbol{\operatorname{Paren}_n}$, then $uv\in \boldsymbol{\operatorname{Paren}_n}$.
\item More generally, $\boldsymbol{\operatorname{Paren}_n}$ is insertion closed: if $u, vw \in \boldsymbol{\operatorname{Paren}_n}$, then $vuw \in \boldsymbol{\operatorname{Paren}_n}$.
\item $\boldsymbol{\operatorname{Paren}_n}$ is also deletion closed: if $u_1vu_2, v \in \boldsymbol{\operatorname{Paren}_n}$, then $u_1u_2 \in \boldsymbol{\operatorname{Paren}_n}$.
\item $\boldsymbol{\operatorname{Paren}_n}$ is not prefix-free.
\item Suppose $f:\Sigma_n \to \Sigma_m^*$ is a function, such that for each $i=1,\ldots, n$, $f$ maps $(_i$ and $)_i$ to some $u_i$ and $v_i$ respectively such that $u_iv_i \in \boldsymbol{\operatorname{Paren}_m}$.  Then the extension $f^*: \Sigma_n^* \to \Sigma_m^*$, when restricted to $\boldsymbol{\operatorname{Paren}_n}$, is a language homomorphism from $\boldsymbol{\operatorname{Paren}_n}$ to $\boldsymbol{\operatorname{Paren}_m}$.
\end{itemize}

\textbf{Remark}.  It can be shown that the number of words of length $2n$ in $\boldsymbol{\operatorname{Paren}_1}$ is the $n$-th Catalan number.  For a proof of this, see \PMlinkname{this entry}{ExampleOfCatalanNumbers}.  The idea is to visualize a word in $\boldsymbol{\operatorname{Paren}_1}$ as a path in a two-dimensional lattice, which can be generated as follows: given a word $u$ of length $2n$, the path $p(u)$ starts from $(0,0)$ (which corresponds to the first symbol of $u$).  If point $(i,j)$ is on the path, then the next point on the path is $(i+1,k)$, where $k=j+1$ if the $i$-th symbol of $u$ is $($, otherwise $k=j-1$.  So the increase from one point to the next is either $(1,1)$, or $(1,-1)$.  As a result, the path $P(u)$ has the property that it never dips below the $x$-axis, and it ends at $(2n,0)$.  Paths defined this way are also known as \emph{Dyck paths}.

\begin{thebibliography}{9}
\bibitem{dk} D. C. Kozen, {\em Automata and Computability}, Springer, New York (1997).
\bibitem{hu} J.E. Hopcroft, J.D. Ullman, {\em Formal Languages and Their Relation to Automata}, Addison-Wesley, (1969).
\end{thebibliography}</content>
</record>
