Automata Theory: Digital Notes by
Automata Theory: Digital Notes by
Automata Theory: Digital Notes by
Digital Notes By
BIGHNARAJ NAIK
Assistant Professor
Department of Master in Computer Application
VSSUT, Burla
Syllabus
4th SEMESTER MCA
F.M : 70
MCA 207 AUTOMATA THEORY (3-1-0)Cr.-4
Module – I
Introduction to Automata: The Methods Introduction to Finite Automata, Structural
Representations, Automata and Complexity.
Proving Equivalences about Sets, The Contrapositive, Proof by Contradiction,
Inductive Proofs: General Concepts of Automata Theory: Alphabets Strings, Languages,
Applications of Automata Theory.
Module – II
Finite Automata: The Ground Rules, The Protocol, Deterministic Finite Automata: Definition
of a Deterministic Finite Automata, How a DFA Processes Strings, Simpler Notations for
DFA’s, Extending the Transition Function to Strings, The Language of a DFA
Nondeterministic Finite Automata: An Informal View. The Extended Transition Function, The
Languages of an NFA, Equivalence of Deterministic and Nondeterministic Finite Automata.
Finite Automata With Epsilon-Transitions: Uses of ∈-Transitions, The Formal Notation for an
∈-NFA, Epsilon-Closures, Extended Transitions and Languages for ∈-NFA’s, Eliminating ∈-
Transitions.
Module – III
Regular Expressions and Languages: Regular Expressions: The Operators of regular
Expressions, Building Regular Expressions, Precedence of Regular-Expression Operators,
Precedence of Regular-Expression Operators
Finite Automata and Regular Expressions: From DFA’s to Regular Expressions, Converting
DFA’s to Regular Expressions, Converting DFA’s to Regular Expressions by Eliminating States,
Converting Regular Expressions to Automata.
Algebraic Laws for Regular Expressions:
Properties of Regular Languages: The Pumping Lemma for Regular Languages, Applications of
the Pumping Lemma Closure Properties of Regular Languages, Decision Properties of Regular
Languages, Equivalence and Minimization of Automata,
Module – IV
Context-Free Grammars and Languages: Definition of Context-Free Grammars, Derivations
Using a Grammars Leftmost and Rightmost Derivations, The Languages of a Grammar,
Parse Trees: Constructing Parse Trees, The Yield of a Parse Tree, Inference Derivations, and
Parse Trees, From Inferences to Trees, From Trees to Derivations, From Derivation to Recursive
Inferences,
Applications of Context-Free Grammars: Parsers, Ambiguity in Grammars and Languages:
Ambiguous Grammars, Removing Ambiguity From Grammars, Leftmost Derivations as a Way
to Express Ambiguity, Inherent Anbiguity
Module – V
Pushdown Automata: Definition Formal Definition of Pushdown Automata, A Graphical
Notation for PDA’s, Instantaneous Descriptions of a PDA,
The Languages of a PDA: Acceptance by Final State, Acceptance by Empty Stack, From Empty
Stack to Final State, From Final State to Empty Stack
Equivalence of PDA’s and CFG’s: From Grammars to Pushdown Automata, From PDA’s to
Grammars
Module – VI
Properties of Context-Free Languages: Normal Forms for Context-Free Grammars, The
Pumping Lemma for Context-Free Languages, Closure Properties of Context-Free Languages,
Decision Properties of CFL’s
Module - VII
Introduction to Turing Machines: The Turing Machine: The Instantaneous Descriptions for
Turing Machines, Transition Diagrams for Turing Machines, The Language of a Turing
Machine, Turing Machines and Halting
Programming Techniques for Turing Machines, Extensions to the Basic Turing Machine,
Restricted Turing Machines, Turing Machines and Computers,
Module - VIII
Undecidability: A Language That is Not Recursively Enumerable, Enumerating the Binary
Strings, Codes for Turing Machines, The Diagonalization Language
An Undecidable Problem That Is RE: Recursive Languages, Complements of Recursive and RE
languages, The Universal Languages, Undecidability of the Universal Language
Undecidable Problems About Turing Machines: Reductions, Turing Machines That Accept the
Empty Language
Post’s Correspondence Problem: Definition of Post’s Correspondence Problem, The “Modified”
PCP
Other Undecidable Problems: Undecidability of Ambiguity for CFG’s
REFERENCE BOOKS
The alphabet of a formal language is the set of symbols, letters, or tokens from which the strings
of the language may be formed; frequently it is required to be finite. The strings formed from this
alphabet are called words, and the words that belong to a particular formal language are
sometimes called well-formed words or well-formed formulas. A formal language is often
defined by means of a formal grammar such as a regular grammar or context-free grammar, also
called its formation rule.
The field of formal language theory studies the purely syntactical aspects of such languages—
that is, their internal structural patterns. Formal language theory sprang out of linguistics, as a
way of understanding the syntactic regularities of natural languages. In computer science, formal
languages are often used as the basis for defining programming languages and other systems in
which the words of the language are associated with particular meanings or semantics.
A formal languageL over an alphabet Σ is a subset of Σ*, that is, a set of words over that
alphabet.
In computer science and mathematics, which do not usually deal with natural languages, the
adjective "formal" is often omitted as redundant.
While formal language theory usually concerns itself with formal languages that are described by
some syntactical rules, the actual definition of the concept "formal language" is only as above: a
(possibly infinite) set of finite-length strings, no more nor less. In practice, there are many
languages that can be described by rules, such as regular languages or context-free languages.
The notion of a formal grammar may be closer to the intuitive concept of a "language," one
described by syntactic rules.
Formal language
A formal grammar (sometimes simply called a grammar) is a set of formation rules for strings in
a formal language. The rules describe how to form strings from the language's alphabet that are
valid according to the language's syntax. A grammar does not describe the meaning of the strings
or what can be done with them in whatever context—only their form.
A formal grammar is a set of rules for rewriting strings, along with a "start symbol" from which
rewriting must start. Therefore, a grammar is usually thought of as a language generator.
However, it can also sometimes be used as the basis for a "recognizer"—a function in computing
that determines whether a given string belongs to the language or is grammatically incorrect. To
describe such recognizers, formal language theory uses separate formalisms, known as automata
theory. One of the interesting results of automata theory is that it is not possible to design a
recognizer for certain formal languages.
Alphabet
An alphabet, in the context of formal languages, can be any set, although it often makes sense to
use an alphabet in the usual sense of the word, or more generally a character set such as ASCII.
Alphabets can also be infinite; e.g. first-order logic is often expressed using an alphabet which,
besides symbols such as^, ¬, and parentheses, contains infinitely many elements x0, x1, x2, …
that play the role of variables. The elements of an alphabet are called its letters.
word
A word over an alphabet can be any finite sequence, or string, of letters. The set of all words
over an alphabet Σ is usually denoted by Σ* (using the Kleene star). For any alphabet there is
only one word of length 0, the empty word, which is often denoted by e, ε or λ. By concatenation
one can combine two words to form a new word, whose length is the sum of the lengths of the
original words. The result of concatenating a word with the empty word is the original word.
Operations on languages
Certain operations on languages are common. This includes the standard set operations, such as
union, intersection, and complement. Another class of operation is the element-wise application
of string operations.
Examples: suppose L1 and L2 are languages over some common alphabet.
• The concatenation L1L2 consists of all strings of the form vw where v is a string from L1
and w is a string from L2.
• The intersection L1 ∩ L2 of L1 and L2 consists of all strings which are contained in both
languages
• The complement ¬L of a language with respect to a given alphabet consists of all strings
over the alphabet that are not in the language.
• The Kleene star: the language consisting of all words that are concatenations of 0 or more
words in the original language;
• Reversal:
o Let e be the empty word, then eR = e, and
o for each non-empty word w = x1…xn over some alphabet, let wR = xn…x1,
o then for a formal language L, LR = {wR | w L}.
• String homomorphism
Such string operations are used to investigate closure properties of classes of languages. A class
of languages is closed under a particular operation when the operation, applied to languages in
the class, always produces a language in the same class again. For instance, the context-free
languages are known to be closed under union, concatenation, and intersection with regular
languages, but not closed under intersection or complement. The theory of trios and abstract
families of languages studies the most common closure properties of language families in their
own right.
Language
“A language is a collection of sentences of finite length all constructed from a finite alphabet of
symbols.In general, if is an alphabet and L is a subset of *, then L is said to be a languageover
, or simply a language if is understood. Each element of L is said to be a sentenceor a word or a
stringof the language.
Example 1 {0, 11, 001}, { , 10}, and {0, 1}* are subsets of {0, 1}*, and so they are languages
over the alphabet {0, 1}.
The empty set Ø and the set { } are languages over every alphabet. Ø is a language that contains
no string. { } is a language that contains just the empty string.
The unionof two languages L1 and L2, denoted L1 L2, refers to the language that consists of all
the strings that are either in L1 or in L2, that is, to { x | x is in L1 or x is in L2 }. The intersectionof
L1 and L2, denoted L1 L2, refers to the language that consists of all the strings that are both in L1
and L2, that is, to { x | x is in L1 and in L2 }. The complementationof a language L over , or just
the complementation of L when is understood, denoted , refers to the language that consists of
all the strings over that are not in L, that is, to { x | x is in * but not in L }.
Example 2 Consider the languages L1 = { , 0, 1} and L2 = { , 01, 11}. The union of these
languages is L1 L2 = { , 0, 1, 01, 11}, their intersection is L1 L2 = { }, and the complementation
of L1 is = {00, 01, 10, 11, 000, 001, . . . }.
Ø L = L for each language L. Similarly, Ø L = Ø for each language L. On the other hand, =
* and = Ø for each alphabet .
The differenceof L1 and L2, denoted L1 - L2, refers to the language that consists of all the strings
that are in L1 but not in L2, that is, to { x | x is in L1 but not in L2 }. The crossproduct of L1 and
L2, denoted L1 × L2, refers to the set of all the pairs (x, y) of strings such that x is in L1 and y is in
L2, that is, to the relation { (x, y) | x is in L1 and y is in L2 }. The compositionof L1 with L2,
denoted L1L2, refers to the language { xy | x is in L1 and y is in L2 }.
Example 3 If L1 = { , 1, 01, 11} and L2 = {1, 01, 101} then L1 - L2 = { , 11} and L2 - L1 = {101}.
On the other hand, if L1 = { , 0, 1} and L2 = {01, 11}, then the cross product of these languages
is L1 × L2 = {( , 01), ( , 11), (0, 01), (0, 11), (1, 01), (1, 11)}, and their composition is L1L2 = {01,
11, 001, 011, 101, 111}.
Li consists of those strings that can be obtained by concatenating i strings from L. L* consists of
those strings that can be obtained by concatenating an arbitrary number of strings from L.
Example 4 Consider the pair of languages L1 = { , 0, 1} and L2 = {01, 11}. For these
languages L1 2 = { , 0, 1, 00, 01, 10, 11}, and L2 3 = {010101, 010111, 011101, 011111, 110101,
110111, 111101, 111111}. In addition, is in L1*, in L1 +, and in L2* but not in L2 +.
The operations above apply in a similar way to relations in * × *, when and are alphabets.
Specifically, the unionof the relations R1 and R2, denoted R1 R2, is the relation { (x, y) | (x, y) is
in R1 or in R2 }. The intersectionof R1 and R2, denoted R1 R2, is the relation { (x, y) | (x, y) is in
R1 and in R2 }. The compositionof R1 with R2, denoted R1R2, is the relation { (x1x2, y1y2) | (x1,
y1) is in R1 and (x2, y2) is in R2 }.
Grammar
Types of grammars
Prescriptive: prescribes authoritative norms for a language
Descriptive: attempts to describe actual usage rather thanenforce arbitrary rules
Formal: a precisely defined grammar, such as context-free
context
Generative: a formal grammar that can generate naturallanguage expressions
Examples:
1. The language consists of all strings begin with 0.
{0}{0, 1}*
2. The language consists of alll strings begin with 0, and end with 1.
{0}{0, 1}*{1}
3. The language consists of all strings with odd lengths.
{0, 1}2n−1, n = 1, 2, . . .
4. The language consists of all strings with substring of three consecutive 0.
{0, 1}*000{0, 1}*
5. The language consists of all strings without substring of three consecutive 0.
{001, 01, 1}*
Regular grammar
A regular grammar is any right-linear or left-linear grammar.
A right regular grammar (also called right linear grammar) is a formal grammar (N, Σ, P, S) such
that all the production rules in P are of one of the following forms:
B → a - where B is a non-terminal in N and a is a terminal in Σ
B → aC - where B and C are in N and a is in Σ
B → ε - where B is in N and ε denotes the empty string, i.e. the string of length 0.
In a left regular grammar (also called left linear grammar), all rules obey the forms
A → a - where A is a non-terminal in N and a is a terminal in Σ
A → Ba - where A and B are in N and a is in Σ
A → ε - where A is in N and ε is the empty string.
An example of a right regular grammar G with N = {S, A}, Σ = {a, b, c}, P consists of the
following rules
S → aS
S → bA
A→ε
A → cA
andS is the start symbol. This grammar describes the same language as the regular expression
a*bc*.
Regular expression
A regular expression (or regexp, or pattern, or RE) is a text string that describes some
(mathematical) set of strings. A RE r matches a string s if s is in the set of strings described by r.
Regular Expressions have their own notation. Characters are single letters for example ‘a’, ‘
’(single blank space), ‘1’ and ‘-’ (hyphen). Operators are entries in a RE that match one or more
characters.
Regular expressions consist of constants and operator symbols that denote sets of strings and
operations over these sets, respectively. The following definition is standard, and found as such
in most textbooks on formal language theory. Given a finite alphabet Σ, the following constants
are defined as regular expressions:
Given regular expressions R and S, the following operations over them are defined to produce
regular expressions:
To avoid parentheses it is assumed that the Kleene star has the highest priority, then
concatenation and then alternation. If there is no ambiguity then parentheses may be omitted. For
example, (ab)c can be written as abc, and a|(b(c*)) can be written as a|bc*.
Examples:
A deterministic finite automaton without accept states and without a starting state is known as a
transition system or semi automaton.
automaton
Given an input symbol , one may write the transition function as , using
the simple trick of currying,, that is, writing for all . This way, the
transition
nsition function can be seen in simpler terms: it's just something that "acts" on a state in Q,
yielding another state. One may then consider the result of function composition
omposition repeatedly
applied to the various functions , , and so on. Using this notion we define
. Given a pair of letters , one may define a new function , by
insisting that , where denotes function composition. Clearly, this process can be
recursively
ively continued. So, we have following recursive definition
where and .
• DFAs were invented to model real world finite state machines in contrast to the concept
of a Turing machine,, which was too general to study properties of real world machines.
• DFAs are one of the most practical models of computation, since there is a trivial linear
time, constant-space, online algorithm to simulate a DFA on a stream of input. Also,
there are efficient algorithms to find a DFA recognizing:
• Here, P(Q) denotes the power set of Q. Let w = a1a2 ... an be a word over the alphabet Σ.
The automaton
ton M accepts the word w if a sequence of states, r0,r1, ..., rn, exists in Q with
the following conditions:
1. r0 = q0
2. ri+1 ∈ ∆(ri,
(ri, ai+1), for i = 0, ..., nn−1
3. rn∈ F.
Implementation
• Create multiple copies. For each n way decision, the NFA creates up to copies of
the machine. Each will enter a separate state.
• If,, upon consuming the last input symbol, at least one copy of the NFA is in the accepting
state, the NFA will accept. (This, too, requires linear storage with respect to the number
of NFA states, as there can be one machine for every NFA state.)
• Explicitly propagate tokens through the transition structure of the NFA and match
whenever a token reaches the final state. This is sometimes useful when the NFA should
encode additional context about the events that triggered the transition.
Decision property of Regular Language
A decision property for a class of languages is an algorithm that takes a formal description of a
language (e.g., a DFA) and tells whether or not some property holds.
Closure Properties
• A closure property of a language classsays that given languages in the class,an operator
(e.g., union) producesanother language in the same class.
• Example: the regular languages areobviously closed under union,concatenation, and
(Kleene) closure.Use the RE representation of languages.
• The principal closure properties of regular languages are:
1.The union of two regular languages is regular.
If L and M are regular languages, then so is L ∪ M.
2. The intersection of two regular languages is regular.
If L and M are regular languages, then so is L ∩ M.
3. The compliment of two regular languages is regular.
If L is a regular language over alphabet Σ, then Σ*-L is also regular language.
4. The difference of two regular languages is regular.
If L and M are regular languages, then so is L - M.
5. The reversal of a regular language is regular.
The reversal of a string means that the string is written backward, i.e. reversal of abcde is
edcba.
The reversal of a language is the language consisting of reversal of all its strings, i.e. if
L={001,110} then
L = {100,011}.
6.The closure of a regular language is regular.
If L is a regular language, then so is L*.
7. The concatenation of regular languages is regular.
If L and M are regular languages, then so is L M.
8.The homomorphism of a regular language is regular.
A homomorphism is a substitution of strings for symbol. Let the function h be defined by
h(0) = a and h(1) = b then h applied to 0011 is simply aabb.
If h is a homomorphism on alphabet Σ and a string of symbols w = abcd…z then
h (w) = h (a) h (b) h(c) h (d)…h (z)
The mathematical definition for homomorphism is
h: Σ*→Γ* such that ∀ x, y ∈Σ*
A homomorphism can also be applied to a language by applying it to each of strings in
the language. Let L be a language over alphabet Σ, and h is a homomorphism on Σ, then
h (L) = { h(w) | w is in L }
The theorem can be stated as “ If L is a regular language over alphabet Σ, and h is a
homomorphism on Σ, then h(L) is also regular ” .
9. The inverse homomorphism of two regular languages is regular.
Suppose h be a homomorphism from some alphabet Σ to strings in another alphabet Τ
and L be a language over Τ then h inverse of L, h′ (L) is set of strings w inΣ* such that
h(w) is in L.
The theorem states that “ If h is a homomorphism from alphabet Σ to alphabet T , and L
is aregular language on T , then h′(L) is also a regular language.
• The pumping lemma for regular languages describes an essential property of all regular
languages.
• Informally, it says that all sufficiently long words in a regular language may be pumped
— that is, have a middle section of the word repeated an arbitrary number of times — to
produce a new word which also lies within the same language.
• Specifically, the pumping lemma says that for any regular language L there exists a
constant p such that any word w in L with length at least p can be split into three
substrings, w = xyz, where the middle portion y must not be empty, such that the words
xz, xyz, xyyz, xyyyz, … constructed by repeating y an arbitrary number of times
(including zero times) are still in L. This process of repetition is known as "pumping".
• Moreover, the pumping lemma guarantees that the length of xy will be at most p,
imposing a limit on the ways in which w may be split.
• Finite languages trivially satisfy the pumping lemma by having p equal to the maximum
string length in L plus one.
Let L be a regular language. Then there exists an integer p ≥ 1 depending only on L such
that every string w in L of length at least p (p is called the "pumping length") can be
written as w = xyz (i.e., w can be divided into three substrings), satisfying the following
conditions:
1. |y| ≥ 1
2. |xy| ≤ p
i
3. for all i ≥ 0, xy z∈ L
y is the substring that can be pumped (removed or repeated any number of times, and the
resulting string is always in L). (1) means the loop y to be pumped must be of length at
least one; (2) means the loop must occur within the first p characters. There is no
restriction on x and z.
In simple words, For any regular language L, any sufficiently long word w(in L) can be
k
split into 3 parts. i.e w = xyz , such that all the strings xy z for k≥0 are also in L.
For every regular language there is a finite state automaton (FSA) that accepts the language. The
number of states in such an FSA are counted and that count is used as the pumping length p. For
a string of length at least p, let s0 be the start state and let s1, ...,spbe the sequence of the next p
states visited as the string is emitted. Because the FSA has only p states, within this sequence of
p + 1 visited states there must be at least one state that is repeated. Write S for such a state. The
transitions that take the machine from the first encounter of state S to the second encounter of
state S match some string. This string is called y in the lemma, and since the machine will match
a string without the y portion, or the string y can be repeated any number of times, the conditions
of the lemma are satisfied.
For example, the following image shows an FSA.
DFA minimization
Example
A simple linear grammar is G with N = {S}, Σ = {a, b}, P with start symbol S and rules
S → aSb
S→ε
• the left-linear or left regular grammars, in which all nonterminals in right hand sides are
at the left ends;
• the right-linear or right regular grammars, in which all nonterminals in right hand sides
are at the right ends.
Collectively, these two special types of linear grammars are known as the regular grammars;
both can describe exactly the regular languages.
• linear grammars in which all nonterminals in right hand sides are at the left or right ends,
but not necessarily all at the same end.
By inserting new nonterminals, every linear grammar can be brought into this form without
affecting the language generated. For instance, the rules of G above can be replaced with
S → aA
A → Sb
S→ε
Hence, linear grammars of this special form can generate all linear languages.
Right linearGrammar
A -> a B or A -> a,
Example:
S -> a T T -> 1
S -> b T T
S -> a T -> 2
S -> b T
T -> a T T -> a
T -> b T T -> b
T -> 1
T -> 2
S => a
S => a T => a 1
S => a T => a 1 T
Constructing a Nondeterministic Finite State Automaton from a
Right Linear Grammar
S -> a T T -> 1 T
S -> b T T -> 2 T
S -> a T -> a
S -> b T -> b
T -> a T T -> 1
T -> b T T -> 2
d(S,a) = {T, X}
d(S,b) = {T,X}
d(S,1) = F
d(S,2) = F
d(T,a) = {T,X}
d(T,b) = {T,X}
d(T,1) = {T,X}
d(T,2) = {T,X}
A Left Linear Grammar for Identifiers
Example:
S -> S a
S -> S b S => a
S -> S 1 S => S 1 => a 1
S -> S 2 S => S 2 => S b 2
S -> a => S 1 b 2 => a 1 b 2
S -> b
G=(V,T,P,S) where
Example:
1. S → x
2. S → y
3. S → z
4. S → S + S
5. S → S - S
6. S → S * S
7. S → S / S
8. S → ( S )
(x+y)*x-z*y/(x+x)
as follows:
for X.
2) a, a ->for each a ∈A. When terminal a is read as input and a is also on top of the stack, pop
the stack.
Rule 1 reflects the following fact: one way to meet the task of finding an instance of X as a
prefix of the inputstring not yet read, is to solve all the tasks, in the correct order, present in the
right-hand side w of the productionX -> w. M can be considered to be a non-deterministic parser
for G. A formal proof that M accepts precisely Lcan be done by induction on the length of the
derivation of any w ∈L.
Ambiguous Grammar;
A grammar is said to be ambiguous if more than two parse trees can be constructed from it.
Example 1:
Example 2 :Show that the following grammar is ambigious
Answer :
As you can see we can have two corresponding parse trees for the above grammar, so the
grammar is ambiguous.
Removal of Ambiguity
For compiling applications we need to design unambiguous grammar, or to use ambiguous
grammar with additional rules to resolve the ambiguity.
1. Associativity of operators.
2. Precedence of operators.
3. Separate rules or Productions.
1. Associativity of Operators:
If operand has operators on both side then by connection, operand should be associated with
the operator on the left.
• Token string: 9 - 5 + 2
• Production rules
list → list - digit | digit
digit → 0 | 1 | 2 | . . . | 9
In the C programming language the assignment operator, =, is right associative. That is, token
string a = b = c should be treated as a = (b = c).
(9 + 5) * 2 and 9 + (5 * L)
The associativity of '+' and '*' do not resolve this ambiguity. For this reason, we need to know the
relative precedence of operators.
The convention is to give multiplication and division higher precedence than addition and
subtraction.
Only when we have the operations of equal precedence, we apply the rules of associative.
So, in the example expression: 9 + 5 * 2.
We perform operation of higher precedence i.e., * before operations of lower precedence i.e., +.
Therefore, the correct interpretation is 9 + (5 *).
3. Separate Rule:
Consider the following grammar and language again.
S → IF b THEN S ELSE S
| IF b THEN S
| a
An ambiguity can be removed if we arbitrary decide that an ELSE should be attached to the last
preceding THEN.
We can revise the grammar to have two nonterminals S1 and S2. We insist that S2 generates IF-
THEN-ELSE, while S1 is free to generate either kind of statements.
S1 → IF b THEN S1
| IF b THEN S2 THEN S1
| a
S2 → IF b THEN S2 ELSE S2
| a
Although there is no general algorithm that can be used to determine if a given grammar is
ambiguous, it is certainly possible to isolate rules which leads to ambiguity or ambiguous
grammar.
Example:
Show that the given grammar is ambiguous and also remove the ambiguity.
Answer :
E.
As two parse trees can be possible so the above given grammar is ambigious.
Now the same parse tree can be drawn as follows.
Inherent Ambiguity
A context-free
free language L is said to be inherently ambiguous if all its grammars are ambiguous.
If even one grammar for L is unambiguous, then L is an unambiguous language.
Example :
Pumping Lemma For CFL
For every CFL L there is a constant n such that every z ∈ L of length |z| ≥ n can be written as
z = u v w x y such that the following holds:
1) v x ≠
2) |v w x| ≤ n, and
3) uvk w xk y ∈ L for all k ≥ 0.
Proof:
Given CFL L, choose any G = G(L) in Chomsky NF. This implies that the parse tree of any z ∈
L is a binary tree, as shown in the figure below at left. The length n of the string at the leaves and
the height h of a binary tree are related by h ≥ log n, i.e. a long string requires a tall parse tree.
By choosing the critical length n = 2 |V | + 1 we force the height of the parse trees considered to
be h ≥ |V| + 1. On a root-to-leaf path of length ≥ |V| + 1 we encounter at least |V| + 1 nodes
labeled by non-terminals. Since G has only |V| distinct non-terminals, this implies that on some
long root-to-leaf path we must encounter 2 nodes labeled with the same non-terminal, say W, as
shown at right.
For two such occurrences of W (in particular, the two lowest ones), and for some u, v, y, x, w
∈A*, we have: S ->* u W y, W ->* v W x and W ->* w. But then we also have W ->* v2 W x2,
and in general, W ->* vkWxk, and S ->* u vk W xk y and S ->* u vk w xk y for all k ≥ 0,
Example-1 :Let G be a CFG in Chomsky normal form that contains b variables. Show that, if G
generates some string with a derivation having at least 2b steps, L(G) is infinite.
Answer:
Since G is a CFG in Chomsky normal form, every derivation can generate at most two non-
terminals, so that in any parse tree using G, an internal node can have at most two children. This
implies that every parse tree with height k has at most 2k–1 internal nodes.If G generates some
string with a derivation having at least 2b steps, the parse tree of that string will have at least 2b
internal nodes. Based on the above argument, this parse tree has height is at least b + 1, so that
there exists a path from root to leaf containing b + 1 variables. By pigeonhole principle, there is
one variable occurring at least twice. So, we can use the technique in the proof of the pumping
lemma to construct infinitely many strings which are all in L(G).
Example-2 :
Let C = {xy | x, y 2 {0, 1}_, |x| = |y|, and x 6= y}. Show that C is a context-free language.
Answer:
We observe that a string is in C if and only if it can be written as xywith |x| = |y| such that for
some i, the ith character of x is different from the ith character of y. To obtain such a string, we
start generating the corresponding ithcharacters, and fill up the remaining characters. Based on
the above idea, we define the CFG for C is as follows:
S → AB | BA
A →XAX | 0
B → XBX | 1
X→0|1
Let A =
Example-3 :
Let A= {wtwR| w, t ∈{0, 1}*and |w| = |t| }Prove that A is not a context-free language.
Answer:
Suppose on the contrary that A is context-free. Then, let p be the pumping
length for A, such that any string in A of length at least p will satisfy the pumping lemma.
Now, we select a string s in A with s = 02p0p1p02p. For s to satisfy the pumping lemma,
there is a way that s can be written as uvxyz, with |vxy| ≤ p and |vy| ≥1, and for any i,
uvixyiz is a string in A. There are only three cases to write s with the above conditions:
Case 1: vy contains only 0s and these 0s are chosen from the last 02p of s. Let i be a
number with 7p > |vy| × (i + 1) ≥ 6p. Then, either the length of uvixyizis not a multiple of 3, or
this string is of the form wtw0 such that |w| = |t| = |w′| with w′is all 0s and w is not all 0s (this is,
w′ = wR).
Case 2: vy does not contain any 0s in the last 02p of s. Then, either the length of uv2xy2z
is not a multiple of 3, or this string is of the form wtw′ such that |w| = |t| = |w′| with
w is all 0s and w′ is not all 0s (that is, w′ = wR).
Case 3: vy is not all 0s, and some 0s are from the last 02p of s. As |vxy| _ p, vxy in this
case must be a substring in 1p′p. Then, either the length of uv2xy2z is not a multiple
of 3, or this string is of the form wtw′such that |w| = |t| = |w′| with w is all 0s and
w′ is not all 0s (that is, w′ 6= wR).
In summary, we observe that there is no way s can satisfy the pumping lemma. Thus, a
contradiction occurs (where?), and we conclude that A is not a context-free language.
Theorem : L1 = { 0k 1k 2k / k ≥ 0 } is not context free.
Pf (by contradiction): Assume L is CF, let n be the constant asserted by the pumping lemma.
Consider z = 0n 1n 2n = u v w x y. Although we don’t know where vwx is positioned within z,
the assertion |vw x| ≤ n implies that v w x contains at most two distinct letters among 0, 1, 2. In
other words, one or two of thethree letters 0, 1, 2 is missing in vwx. Now consider u v2 w x2 y.
By the pumping lemma, it must be in L. Theassertion |v x| ≥ 1 implies that u v2 w x2 y is longer
than u v w x y. But u v w x y had an equal number of 0s, 1s,and 2s, whereas u v2 w x2 y cannot,
since only one or two of the three distinct symbols increased in number. Thiscontradiction
proves the theorem.
Theorem : L2 = { w w / w ∈{0, 1} } is not context free.
Proof (by contradiction): Assume L is CF, let n be the constant asserted by the pumping lemma.
Consider z = 0n+1 1n+1 0n+1 1n+1 = u v w x y. Using k = 0, the lemma asserts z0 = u w y ∈ L,
but we showthat z0 cannot have the form t t, for any string t, and thus that z0 ∈ L, leading to a
contradiction. Recall that |v wx| ≤ n, and thus, when we delete v and x, we delete symbols that
are within a distance of at most n from eachother. By analyzing three cases we show that, under
this restriction, it is impossible to delete symbols in such away as to retain the property that the
shortened string z0 = u w x has the form t t. We illustrate this using theexample n = 3, but the
argument holds for any n.Given z = 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1, slide a window of length n = 3
across z, and delete any characters youwant from within the window. Observe that the blocks of
0s and of 1s within z are so long that the truncated z,call it z’, still has the form “0s 1s 0s 1s”.
This implies that if z’ can be written as z’ = t t, then t must have theform t = “0s 1s”. Checking
the three cases: the window of length 3 lies entirely within the left half of z; thewindow straddles
the center of z; and the window lies entirely within the right half of z, we observe that in noneof
these cases z’ has the form z’ = t t, and thus that z0 = u w y ∉L.
Context sensitive grammars and languages
The rewriting rules B -> w of a CFG imply that a non-terminal B can be replaced by a word w ∈
(V A)* “in any context”. In contrast, a context sensitive grammar (CSG) has rules of the form:
u B v -> u w v, where u, v, w ∈ (V A)*, implying that B can be replaced by w only in the
context “u on the left, v on the right”. It turns out that this definition is equivalent (apart from the
nullstring) to requiring that any CSG rule be of the form v -> w, where v, w ∈ (V A)*, and
|v| ≤ |w|. This monotonicity property (in any derivation, the current string never gets shorter)
implies that the word problem for CSLs: “given CSG G and given w, is w ∈L(G)?” is decidable.
An exhaustive enumeration of all derivations up to the length |w| settles the issue. As an example
of the greater power of CSGs over CFGs, recall that we used the pumping lemma to prove that
the language 0k 1k 2k is not CF.
Parikh's theorem
Parikh's theorem in theoretical computer science says that if we look only at the relative number of
occurrences of terminal symbols in a context-free language, without regard to their order, then the
language is indistinguishable from a regular language. It is useful for deciding whether or not a string
with given number of some terminals is accepted by a context-free grammar. It was first proved by Rohit
Parikh in 1961 and republished in 1966.
Cantor's theorem
Cantor's theorem states that, for any setA, the set of all subsets of A (the power set of A) has a
strictly greater cardinality than A itself. For finite sets, Cantor's theorem can be seen to be true by
a much simpler proof than that given below, since in addition to subsets of A with just one
member, there are others as well, and since n < 2n for all natural numbers n. But the theorem is
true of infinite sets as well. In particular, the power set of a countably infinite set is uncountably
infinite.
Proof:
Godel Numbering:
In mathematical logic, a Godel numbering is a function that assigns to each symbol and well-
formed formula of some formal language a unique natural number, called its Gödel number. The
concept was famously used by Kurt Godel for the proof of his incompleteness theorems. (Gödel
1931)
Since Godel's paper was published in 1931, the term "Godel numbering" or "Godel code" has
been used to refer to more general assignments of natural numbers to mathematical objects.
Example:
• Pushdown automata can also manipulate the stack, as part of performing a transition.
Finite state machines choose a new state, the result of following the transition. The
manipulation can be to push a particular symbol to the top of the stack, or to pop off the
top of the stack. The automaton can alternatively ignore the stack, and leave it as it is.
The choice of manipulation (or no manipulation) is determined by the transition table.
• Put together: Given an input signal, current state, and stack symbol, the automaton can
follow a transition to another state, and optionally manipulate (push or pop) the stack.
• In general, pushdown automata may have several computations on a given input string,
some of which may be halting in accepting configurations while others are not. Thus we
have a model which is technically known as a "nondeterministic pushdown automaton"
(NDPDA or NPDA).
• Nondeterminism means that there may be more than just one transition available to
follow, given an input signal, state, and stack symbol. If in every situation only one
transition is available as continuation of the computation, then the result is a deterministic
pushdown automaton (DPDA), a strictly weaker device. Unlike finite-state machines,
there is no mechanical way to turn a NDPDA into an equivalent DPDA.
• If we allow a finite automaton access to two stacks instead of just one, we obtain a more
powerful device, equivalent in power to a Turing machine. A linear bounded automaton
is a device which is more powerful than a pushdown automaton but less so than a Turing
machine.
• Nondeterministic pushdown automata are equivalent to context-free grammars: for every
context-free grammar, there exists a pushdown automaton such that the language
generated by the grammar is identical with the language generated by the automaton,
which is easy to prove. The reverse is true, though harder to prove: for every pushdown
automaton there exists a context-free grammar such that the language generated by the
automaton is identical with the language generated by the grammar.
PDA Transitions:
Second:
δ(q, ε, z) = {(p1,γ1), (p2,γ2),…, (pm,γm)}
– Current state is q
– Current input symbol is not considered
– Symbol currently on top of the stack z
– Move to state pi from q
– Replace z with γi on the stack (leftmost symbol on top)
– No input symbol is read
:
Example 4 :
(b)
(c)
Answer:
(a)
(b)
(c)
Parsing:
• In computer science and linguistics, parsing, or, more formally, syntactic analysis, is the
process of analyzing a text, made of a sequence of tokens (for example, words), to
determine its grammatical structure with respect to a given (more or less) formal
grammar.
• Parsing is also an earlier term for the diagramming of sentences of natural languages, and
is still used for the diagramming of inflected languages, such as the Romance languages
or Latin.
• Parsing is a common term used in psycholinguistics when describing language
comprehension.
• In this context, parsing refers to the way that human beings, rather than computers,
analyze a sentence or phrase (in spoken language or text) "in terms of grammatical
constituents, identifying the parts of speech, syntactic relations, etc." This term is
especially common when discussing what linguistic cues help speakers to parse garden-
path sentences.
Types Of Parsing
• Top-down parsing is a parsing strategy where one first looks at the highest level of the
parse tree and works down the parse tree by using the rewriting rules of a formal
grammar. LL parsers are a type of parser that uses a top-down parsing strategy.
• Top-down parsing is a strategy of analyzing unknown data relationships by hypothesizing
general parse tree structures and then considering whether the known fundamental
structures are compatible with the hypothesis. It occurs in the analysis of both natural
languages and computer languages.
• Top-down parsing can be viewed as an attempt to find left-most derivations of an input-
stream by searching for parse-trees using a top-down expansion of the given formal
grammar rules. Tokens are consumed from left to right. Inclusive choice is used to
accommodate ambiguity by expanding all alternative right-hand-sides of grammar rules.
• In top-down parsing, you start with the start symbol and apply the productions until you
arrive at the desired string.
• As an example, let’s trace through the two approaches on this simple grammar that
recognizes strings consisting of any number of a’s followed by at least one (and possibly
more) b’s:
S –> AB
A –>aA | ε
B –> b | bB
Here is a top-down parse of aaab. We begin with the start symbol and at each step, expand one of
the remaining nonterminals by replacing it with the right side of one of its productions. We
repeat until only terminals remain. The top-down parse produces a leftmost derivation of the
sentence.
S
AB S –> AB
aAB A –>aA
aaAB A –>aA
aaaAB A –>aA
aaaεB A –>ε
aaab B –> b
Bottom Up Parsing
A bottom-up parse works in reverse. We begin with the sentence of terminals and each step
applies a production in reverse, replacing a substring that matches the right side with the
nonterminal on the left. We continue until we have substituted our way back to the start symbol.
If you read from the bottom to top, the bottom-up parse prints out a rightmost derivation of the
sentence.
aaab
aaaεb (insert ε)
aaaAb A –>ε
aaAb A –>aA
aAb A –>aA
Ab A –>aA
AB B –> b
S S –> AB
Ogden's lemma
In the theory of formal languages, Ogden's lemma (named after William F. Ogden) provides an
extension of flexibility over the pumping lemma for context-free languages.
Ogden's lemma states that if a language L is context-free, then there exists some number p > 0
(where p may or may not be a pumping length) such that for any string w of length at least p in L
and every way of "marking" p or more of the positions in w, w can be written as
w = uxyzv
Ogden's lemma can be used to show that certain languages are not context-free, in cases where
the pumping lemma for context-free languages is not sufficient. An example is the language
i j k l
{a b c d : i = 0 or j = k = l}. It is also useful to prove the inherent ambiguity of some languages.
Proof:
Let b be the maximum number of symbols in the right-hand side of a rule. For any parse tree T
of string z, and z has at least p marked positions. We say a leaf in T is marked if its
corresponding position in z is marked. We say an internal node of T is marked if the subtrees
rooted at two or more of its children each contains a marked leaf. We claim that if every (root-to-
leaf) path in T contains at most i marked internal nodes, T has at most bi marked leaves. Assume
that this claim is true (which will be proved shortly by induction). To prove Ogden’s lemma, we
|V |+1
set p = b . Then, the minimum marked internal nodes in a path is |V | + 1. By pigeonhole
principle, there exists some variable appearing at least twice on that path. Then, by a similar way
of proving the original pumping lemma, we can show that z can be written as uvwxy satisfying
Ogden’s lemma. We now go back to prove the claim. The claim is true for i = 0, since if every
path inT has at most 0 marked nodes, T has no marked nodes. Thus, there must be at most
b0 = 1 marked leaves (why?). For i ≥1, let q be the unique marked internal node whose
ancestors (if exist) are not marked. Then, the number of marked leaves in T is equal to
the total number of marked leaves under the children of q. Also, as q is a marked node,
in the subtree rooted at any child of q, every path has at most i − 1 marked nodes. By
i−1
induction, every subtree has at most b marked leaves. As q has at most b children, T
thus has at most bi marked leaves.
C(Cocke)Y(Younger)K(Kasami) Algorithm:
A Turing machine is a device that manipulates symbols on a strip of tape according to a table of
rules. Despite its simplicity, a Turing machine can be adapted to simulate the logic of any
computeralgorithm, and is particularly useful in explaining the functions of a CPU inside a
computer.
The "Turing" machine was described in 1936 by Alan Turingwho called it an "a-machine"
(automatic machine). The Turing machine is not intended as practical computing technology, but
rather as a hypothetical device representing a computing machine. Turing machines help
computer scientists understand the limits of mechanical computation.
A Turing machine that is able to simulate any other Turing machine is called a universal Turing
machine (UTM, or simply a universal machine). A more mathematically oriented definition with
a similar "universal" nature was introduced by Alonzo Church, whose work on lambda calculus
intertwined with Turing's in a formal theory of computation known as the Church–Turing thesis.
The thesis states that Turing machines indeed capture the informal notion of effective method in
logic and mathematics, and provide a precise definition of an algorithm or 'mechanical
procedure'.
• A tape which is divided into cells, one next to the other. Each cell contains a symbol
from some finite alphabet. The alphabet contains a special blank symbol (here written as
'B') and one or more other symbols. The tape is assumed to be arbitrarily extendable to
the left and to the right, i.e., the Turing machine is always supplied with as much tape as
it needs for its computation. Cells that have not been written to before are assumed to be
filled with the blank symbol. In some models the tape has a left end marked with a
special symbol; the tape extends or is indefinitely extensible to the right.
• A head that can read and write symbols on the tape and move the tape left and right one
(and only one) cell at a time. In some models the head moves and the tape is stationary.
• A state register that stores the state of the Turing machine, one of finitely many. There is
one special start state with which the state register is initialized. These states, writes
Turing, replace the "state of mind" a person performing computations would ordinarily
be in.
• A finite table (occasionally called an action table or transition function) of instructions
(usually quintuples [5-tuples] : qiaj→qi1aj1dk, but sometimes 4-tuples) that, given the
state(qi) the machine is currently in and the symbol(aj) it is reading on the tape (symbol
currently under the head) tells the machine to do the following in sequence (for the 5-
tuple models):
• Either erase or write a symbol (replacing aj with aj1), and then
• Move the head (which is described by dk and can have values: 'L' for one step left or 'R'
for one step right or 'N' for staying in the same place), and then
• Assume the same or a new state as prescribed (go to state qi1).
Hamiltonian cycle in a graph G = (V, E): a cycle that contains each of the vertices in V (exactly
once)
Traveling salesman problem (TSP):
Weighted graph G = (V, E, w: E ->Reals), V = {1, .., n}, E = { (i, j) | i < j } (often the complete
graph Kn).Weight (or length) of a path or cycle = sum of the weights of its edges.
Given G= (V, E, w) find a shortest Hamiltonian cycles, if any exist.
A clique in a graph G = (V, E): a subset V’ V such that for all u, v V’, (u, v) E.
|V’| is the size of the clique. A clique of size k is called a k-clique.
Clique problems: Given G= (V, E), answer questions about the existence of cliques, find
maximumclique, enumerate all cliques.
Examples:
Ex: the graph a) shown at left has exactly 1 Hamiltonian cycle, highlighted in b). The graph c)
has none. Thecomplete graph K4 has 3 Hamiltonian cycles. d) The complete graph Kn has (n-1)!
/ 2 Hamiltonian cycles
Ex: Graph a) has three maximum 3-cliques. The only cliques in graph c) are the vertices and the
edges, i.e. the1-cliques and the 2-cliques. Graph d) is a 4-clique, and every subset of its vertices
is a clique.
Decision problems:
• Given G, does G have a Hamiltonian cycle? Given G and k, does G have a k-clique?
Given G = (V, E, w) and a real number B, does G have a Hamiltonian cycle of length ≤ B?
Finding the answer to a decision problem is often hard, whereas verifying a positive answer is
often easy: weare shown an object and merely have to verify that it meets the specifications (e.g.
trace the cycle shown in b).Decision problems are naturally formalized in terms of machines
accepting languages, as follows: probleminstances (e.g. graphs) are coded as strings, and the
code words of all instances that have the answer YES (e.g.have a Hamiltonian cycle) form the
language to be accepted.
Optimization problems:
• Given G, construct a maximum clique.
• TSP: Given Kn = (V, E, w) find a Hamiltonian cycle of minimal total length.
Both problems, of finding the answer and verifying it, are usually hard. If I claim to show you a
maximumclique, and it contains k vertices, how do you verify that I haven’t missed a bigger
one? Do you have toenumerate all the subsets of k+1 vertices to be sure that there is no (k+1)-
clique? Nevertheless, verifying isusually easier than finding a solution, because the claimed
solution provides a bound that eliminates manysuboptimal candidates.
Enumeration problems:
• Given G, construct all Hamiltonian cycles, or all cliques, or all maximum cliques.
Enumeration problems are solved by exhaustive search techniques such as backtrack. They are
time consumingbut often conceptually simple, except when the objects must be enumerated in a
prescribed order. Enumerationis the technique of last resort for solving decision problems or
optimization problems that admit no efficientalgorithms, or for which no efficient algorithm is
known. It is an expensive technique, since the number ofobjects to be examined often grows
exponentially with the length of their description.
Theorem
The class P of problems solvable in polynomial time
Practically all standard combinatorial algorithms presented in a course on Algorithms and Data
Structures runin polynomial time. They are sequential algorithms that terminate after a number
of computational steps that isbounded by some polynomial p(n) in the size n of the input data, as
measured by the number of data items thatdefine the problem. A computational step is any
operation that takes constant time, i.e. time independent of n.
In practical algorithm analysis there is a fair amount of leeway in the definition of
“computational step” and“data item”. For example, an integer may be considered a single data
item, regardless of its magnitude, and anyarithmetic operation on integers as a single step. This is
reasonable when we know a priori that all numbersgenerated are bounded by some integer
“maxint”, and is unreasonable for computations that generate numbersof unbounded magnitude.
In complexity theory based on Turing machines the definition is clear: a computational step is a
transitionexecuted, a data item is a character of the alphabet, read or written on a square of tape.
The alphabet is usuallychosen to be {0, 1} and the size of data is measured in bits. When
studying the class P of problems solvable inpolynomial time, we only consider deterministic
TMs that halt on all inputs.
Let tM: A* -> Integers be the number of steps executed by M on input x A*.
This chapter deals with TMs whose running time is bounded by some polynomial in the length of
the input.
TM M is or runs in polynomial time iff polynomial p such x A*: tM( x ) ≤ p( |x| )
P = { L A* | TM M, polynomial p such that L = L(M) and x A*: tM ( x ) ≤ p( |x| ) }
Notice that we do not specify the precise version of TM to be used, in particular the number of
tapes of M isleft open. This may be surprising in view of the fact that a multi-tape TM is much
faster than a single-tape TM.
A detailed analysis shows that “much faster” is polynomially bounded: a single-tape TM S can
simulate anymulti-tape TM M with at most a polynomial slow-down: for any multi-tape TM M
there is a single-tape TM Sand a polynomial p such that for all x A*, tS ( x ) ≤ p( tM ( x ) ).
This simulation property, and the fact thata polynomial of a polynomial is again a polynomial,
makes the definition of the class P extremely robust.
The question arises whether the generous accounting that ignores polynomial speed-ups or slow-
downs is ofpractical relevance. After all, these are much greater differences than ignoring
constant factors as one doesroutinely in asymptotics. The answer is a definite YES, based on
several considerations:
1) Practical computing uses random access memory, not sequential storage such as tapes. Thus,
the issue ofhow many tapes are used does not even arise. The theory is formulated in terms of
TMs, rather than morerealistic models of computation such as conventional programming
languages, for the sake of mathematicalprecision. And it turns out that the slowness of tape
manipulation gets absorbed, in comparison with a RAM model, by the polynomial
transformations we ignore so generously.
2) Most practical algorithms are of low degree, such as O( n), O( n log n), O( n2 ), or O( n3 ).
Low-degreepolynomials grow slowly enough that the corresponding algorithms are
computationally feasible for manyvalues of n that occur in practice. E.g. for n = 1000, n3 = 109
is a number of moderate size when compared toprocessor clock rates of 1 GHz and memories of
1 GByte. Polynomial growth rates are exceedingly slowcompared to exponential growth
(consider 21000 ).
3) This complexity theory, like any theory at all, is a model that mirrors some aspects of reality
well, and otherspoorly. It is the responsibility of the programmer or algorithm designer to
determine in each specific case,whether or not an algoritm “in P” is practical or not.
Examples of problems (perhaps?) in P:
1) Every context-free language is in P. In Ch5 we saw an O( n3 ) parsing algorithm that solves
the wordproblem for CFLs, where n is the length of the input string.
2) The complexity of problems that involve integers depends on the representation. Fortunately,
with the usualradix representation, the choice of radix r > 1 is immaterial (why?). But if we
choose an exotic notation,everything might change. For example, if integers were given as a list
of their prime factors, many arithmeticproblems would become easier. Paradoxically, if integers
were given in the unwieldy unary notation, somethings might also become “easier” according to
the measure of this chapter. This is because the length of theunary representation < k >1 of k is
exponential in the length of the radix r ≥ 2 representation < k > r. Given anexponentially longer
input, a polynomial-time TM is allowed to use an exponentially longer computation timeas
compared to the “same” problem given in the form of a concise input.
The following example illustrates the fact that the complexity of arithmetic problems depends on
the numberrepresentation chosen. The assumed difficulty of factoring an integer lies at the core
of modern cryptography.Factoring algorithms known today require, in the worst case, time
exponential in the length, i.e. number of bits,of the radix representation of the number to be
factored. But there is no proof that factoring is NP-hard -according to today’s knowledge, there
might exist polynomial-time factoring algorithms. This possibilitygained plausibility when it was
proven that primality, i.e. the problem of determining whether a natural number(represented in
radix notation) is prime or composite.
Theorem :
The class NP of problems solvable in non-deterministic polynomial time
“NP” stands for “non-deterministic polynomial”. It is instructive to introduce two different but
equivalentdefinitions of NP, because each definition highlights a different key aspect of NP. The
original definitionexplicitly introduces non-deterministic TMs:
NP = { L A* | NTM N, polynomial p such that L = L(N) and x A*: tN ( x ) ≤ p( |x| )
}
Notice that this differs from the definition of P only in the single letter “N” in the phrase “
NTM N ..”,indicating that we mean non-deterministic Turing machines. We had seen that
deterministic and nondeterministicTMs are equally powerful in the presence of unbounded
resources of time and memory. But thereis a huge difference in terms of the time they take for
certain computations. A NTM pursues simultaneously anumber of computation paths that can
grow exponentially with the length of the computation.Because of many computation paths
pursued simultaneously we must redefine the function tN: A* -> Integersthat measures the
number of steps executed. An input x A* may be accepted along a short path as well as
along a long path, and some other paths may not terminate. Therefore we define tN(x) as the
minimumnumber of steps executed along any accepting path for x.
Whereas the original definition of NP in terms of non-deterministic TMs has the intuitive
interpretation viaparallel computation, an equivalent definition based on deterministic TMs is
perhaps technically simpler tohandle. The fact that these two definitions are equivalent provides
two different ways of looking at NP.The motivation for this second definition of NP comes from
the observation that it may be difficult to decidewhether a string meeting certain specifications
exists; but that it is often easier to decide whether or not a givenstring meets the specifications. In
other words, finding a solution, or merely determining whether a solution
exists, is harder than checking a proposed solution’s correctness. The fact that this intuitive
argument leads to arigorous definition of NP is surprising and useful!
In the following definition, the language L describes a problem class, e.g. all graphs with a
desired property ;the string w describes a problem instance, e.g. a specific graph G; the string c =
c(w), called a “certificate forw” or a witness, plays the role of a key that “unlocks w”: the pair w,
c is easier to check than w alone!
Example: for the problems of Hamiltonian cycles and cliques, w = <G> is the representationof
graph G, and the certificate c is the representation of a cycle or a clique, respectively. Given c, it
is easy toverify that c represents a cycle or a clique in G.
a verifier for L A* is a deterministic TM V with the property thatL = { w | c(w) A* such
that V accepts < w, c> }The string c = c(w) is called a certificate for w’s membership in L. <w,
c> denotes a representation of the pair(w, c) as a single string, e.g. w#c, where a reserved symbol
# separates w from its certificate. The idea behindthis concept of certificate is that it is easy to
verify w L if you are given w’s certificate c. If not, you wouldhave to try all strings in the
hope of finding the right vertificate, a process that may not terminate. We formalizethe phrase
“easy to verify” by requiring that a verifier V is (or runs in) polynomial-time.