Uploaded by siddhantshukla47

APznzaaY27BPQtQ1cpfvR mSn5hrSuyDu4-4v5dZe2g2mewJ8BWxZ2eFR65EVJpg8pabUxSamY-JtW107qxuBzJtOfoeXSHk6n1XG2R0udyub1kcpoyStDYExPLW91V2KmfiR7pOOoyP7yMttl8POZVv-ljIJ8Gdw-a9 87Mk4kTk7fY45TcFSHS3xcNn26co

advertisement
Theory of Computation
(CCT 207)
Presented by : Prof. V. Bongirwar
Course Objectives
1. To provide students an understanding of basic
concepts in the theory of computation.
2. To teach formal languages and various models of
computation.
3. To exhibit fundamental concepts related with
computability theory.
The Aim of the Course is
1. To familiarize students with the foundation &
principles of computer science
2. To teach material useful in subsequent courses
( compiler design, algorithm analysis)
3. To strengthen students ability to carry out formal &
rigorous mathematical arguments.
Syllabus
UNIT I
Basics of Sets and Relation, Countability and Diagonalization, Principle
of mathematical induction, Pigeon-hole principle. Fundamentals of
formal languages and grammars, Chomsky hierarchy of languages.
UNIT II
Finite automata: Deterministic finite automata (DFA), Nondeterministic
finite automata (NFA) and equivalence with DFA, Minimization of finite
automata, NFA with Epsilon Transitions, Finite Automata with output.
UNIT III
Regular expressions and Regular languages, Regular grammars and
equivalence with finite automata, properties of regular languages,
pumping lemma for regular languages, Context-free grammars (CFG)
and language(CFL), parse trees, ambiguity in CFG, Reduction of CFGs,
Chomsky and Greibach normal forms.
UNIT IV
Push Down Automata: Deterministic pushdown automata and NonDeterministic pushdown automata, Acceptance by two methods:
Empty stack and Final State, Equivalence of PDA with CFG, closure
properties of CFLs.
UNIT V
Turing machines: The basic model for Turing machines (TM), Turing
recognizable (recursively enumerable) and Turing-decidable
(recursive) languages, variants of Turing machines, unrestricted
grammars and equivalence with Turing machines, TMs as
enumerators.
UNIT VI
Undecidability: Church-Turing thesis, Universal Turing machine,
Undecidable problems about languages, Recursive Function Theory
• Course Outcomes
On successful completion of the course, students will be able to
demonstrate
1. Describe the formal relationships among machines, languages
and grammars.
2. Design and Optimize finite automata for given regular language.
3. Design Push Down Automata, Turing Machine for given
languages.
4. Demonstrate use of computability, decidability, recursive
function theory through problem solving
Text & Reference Books
Text Books:
John E. Hopcroft, Rajeev Motwani and Jeffrey D. Ullman, Introduction to
Automata Theory, Languages, and Computation, Pearson Education Asia.
•
Reference Books:
1.
Harry R. Lewis and Christos H. Papadimitriou, Elements of the Theory
Computation, Pearson Education Asia.
2. Dexter C. Kozen, Automata and Computability, Undergraduate Texts in
Computer Science, Springer.
3. Michael Sipser, Introduction to the Theory of Computation, PWS
Publishing.
4. John Martin, Introduction to Languages and The Theory of Computation,
Tata McGraw Hill.
Marks Distribution (100 Marks)
Internal Exam (40 Marks)
• Test Exam :
Test 1 - 15 M
Test 2 - 15 M
• Internal Assessment- 10 Marks
End Semester Exam (60 Marks)
Theory of Computation: A Historical
Perspective
1930s
1940-1950s
1969
1970-
• Alan Turing studies Turing machines
• Decidability
• Halting problem
• “Finite automata” machines studied
• Noam Chomsky proposes the
“Chomsky Hierarchy” for formal
languages
Cook introduces “intractable” problems
or “NP-Hard” problems
Modern computer science: compilers,
computational & complexity theory evolve
9
Alan Turing (1912-1954)
•
•
•
•
(A pioneer of automata theory)
Father of Modern Computer Science
English mathematician
Studied abstract machines called Turing machines even
before computers existed
Heard of the Turing test?
10
Mathematical Notation & Techniques
Sets
Definition 1. A set is a group of objects.The objects in a set
are called the elements, or members, of the set.
Example 1
The set of positive integers less than 100 can be denoted as 1,2,3,...,99.
Example 2
A set can also consists of seemingly unrelated elements: a,2, Fred , New Jersey .
Definition 2. Two sets are equal if and only if they have the
same elements.
Example 3
Set {1,3,3,3,5,5,5,5} is the same as set {1,3,5}.
• A set can be described by using a set builder notation.
Example 3
O = {x | x is an odd positive integer less than 10}
Example 4
N = {x | natural numbers} = {0,1,2,3,...}
Z = {x | integers} = {...,-2,-1,0 ,1,...}
Z = {x | positive integers} = {1,2,3,...}
R = {x | real numbers}
• A set can be described by using a Venn diagram.
Example 5
Draw a Venn diagram that represents V, the set of vowels in English alphabet.
U
a,e,i,o,u
V
• The set that has no elements is called empty set, denoted by .
Definition 3. The set A is said to be a subset of B if and
only if every element of A is also an element of B. We use
the notation A  B to indicate that A is a subset of the set B.
• A  B if and only if x(x  A → x  B)
• For any set P,   P and P  P.
• If A  B and B  A, then A = B.
Example 6
{ , {a},{b},{a, b}} = {x| x is a subset of the set {a, b}}.
• If a set A is a subset of set B but that A  B, A is called to be a proper subset of B,
denoted as A  B.
U
A
B
Definition 4. The power set of a set S is the set of all subsets of S,
denoted by P(S) or 2 S.
Example 7
What is the power set of the set S = {0,1,2}?
Solution : P({0,1,2}) (or 2S ) = { , {0},{1}, | {2},{0,1},{0,2},{1,2},{0,1,2}}.
• Set Operations:
Complement A = {x | x U and x  A}, where set U is called " universal"
that contains all the elements we might ever consider}
Union
A  B = {x | x  A or x  B}
Intersecti on A  B = {x | x  A and x  B}
Difference
A − B = {x | x  A and x  B}
Example 8
{1,3,5}  {1,2,3} = {1,2,3,5}.
{1,3,5}  {1,2,3} = {1,3}.
{1,3,5} - {1,2,3} = {5}.
U
A
U
U
B
A
B
B
Example 9
Let A = {a,e,i,o,u} and the universal set is the set of the letters of the English
alphabet.Then A = {b, c, d , f , g , h, j , k , l , m, n, p, q, r , s, t , v, w, x, y, z}.
U
A
A
• Set Identities
Table 1
Identity
A  = A
Set Identities
A U = A
A U = U
A  = 
A A = A
Name
Identity laws
Domination laws
A A = A
Idempotent laws
( A) = A
A B = B  A
Complementation laws
A B = B  A
A  ( B  C ) = ( A  B)  C
A  ( B  C ) = ( A  B)  C
A  ( B  C ) = ( A  B)  ( A  C )
A  ( B  C ) = ( A  B)  ( A  C )
A B = A B
A B = A B
Commutative laws
Associative laws
Distributive laws
De Morgan’s law
• Cartisian product of A and B, denoted by A  B, is the set of all ordered paris (a,b),
where a  A and b  B. Hence, A  B = {(a,b)| a  A  b  B}.
Example 10
Let A = { 1,2 } and B = {a,b,c}.
A  B = {( 1,a),( 1,b),( 1,c),( 2 ,a),( 2 ,b),( 2 ,c)}
B  A = {(a,1 ),(a,2 ),(b,1 ),(b,2 ),(c,1 ),(c,2 )}
A B  B  A
Functions and Relations
Definition 1. Let A and B be sets. A function f from A to B is an assignment of
exactly one element of B to each element of A. We write f(a)=b if b is the
unique element of B assigned by the function f to the element a of A. If f is a
function from A to B, we write f : A →B.
x
x
y
z
A function
Not a function
Example 1
Let set A={Adams, Chou, Goodfriend, Rodriguez, Stevens} and B={A,B,C,D,F}.
Let G be the function that assigns a grade to a student in our theory of computation.
G
Adames
A
The domain of G is the set A={Adams,
Chou, Goodfriend, Rodriguez, Stevens},
Chou
B
and the range of G is the set {A,B,C,F}.
Goodfriend
C
Rodriguez
D
Stevens
F
Definition 3
Let A and B be the sets. A relation R from A to B is a subset of A B .
From the definition, relation R is a set of pairs.
If (a, b)  R, we say a has a relation R with b, denoted as aRb.
• Functions can be consider as relations, but relations are
more general than functions.
Example 3
Let A={1,2,3,4}. R={(1,2), (2,3),(3,1),(4,4)} is a relation from A to A
Example 4
R={(1,2), (2,3),(1,3),(1,4)} is a relation but not a function.
Example 5
Let A be the set of students. Let B the set of courses. The set R that
consists of those pairs (a, b), where a is a student enrolled in course b,
is a relation from A to B.
Definition 4
R is an equivalence relation if for any pair (x,y) of R
xRx for all x
(reflexivity)
If xRy then yRx
(symmetry)
If xRy and yRz, then xRz.
(transitivity)
We usually use

to denote equivalence relation.
Definition 4
R is an equivalence relation if for any pair (x,y) of R
xRx for all x
(reflexivity)
If xRy then yRx
(symmetry)
If xRy and yRz, then xRz.
(transitivity)
We usually use

to denote equivalence relation.
Languages & Grammars
Or “words”
• Languages: “A language is a
collection of sentences of finite
length all constructed from a
finite alphabet of symbols”
• Grammars: “A grammar can be
regarded as a device that
enumerates the sentences of a
language” -
22
Alphabet
An alphabet is a finite, non-empty set of symbols
• We use the symbol ∑ (sigma) to denote an
alphabet
• Examples:
–
–
–
–
Binary: ∑ = {0,1}
All lower case letters: ∑ = {a,b,c,..z}
Alphanumeric: ∑ = {a-z, A-Z, 0-9}
DNA molecule letters: ∑ = {a,c,g,t}
23
Strings
A string or word is a finite sequence of symbols chosen
from ∑
• Empty string is  (or “epsilon”)
• Length of a string w, denoted by “|w|”, is equal to
the number of (non- ) characters in the string
– E.g., x = 010100
– x = 01  0  1  00 
–
|x| = 6
|x| = ?
xy = concatentation of two strings x and y
24
Powers of an alphabet
Let ∑ be an alphabet.
– ∑k = the set of all strings of length k
– ∑* = ∑0 U ∑1 U ∑2 U …
– ∑+ = ∑1 U ∑2 U ∑3 U …
Example if ∑ = {0, 1}
Then ∑0 = { }
∑1= { 0, 1}
∑2= { 00, 01, 10, 11}
∑3= {000, 001, 010, 011, 100, 101, 110, 111}
So ∑* = ∑0 U ∑1 U ∑2 U … =
∑+ = ∑1 U ∑2 U ∑3 U …
25
Languages
L is a said to be a language over alphabet ∑, only if L  ∑*
➔ this is because ∑* is the set of all strings (of all possible length
including 0) over the given alphabet ∑
Examples:
1. Let L be the language of all strings consisting of n 0’s followed by n
1’s:
L = {, 01, 0011, 000111,…}
2. Let L be the language of all strings of with equal number of 0’s and
1’s:
L = {, 01, 10, 0011, 1100, 0101, 1010, 1001,…}
Canonical ordering of strings in the language
Definition:
Ø denotes the Empty language
• Let L = {}; Is L=Ø?
NO
26
The Membership Problem
Given a string w  ∑*and a language L over ∑,
decide whether or not w L.
Example:
Let w = 100011
Q) Is w  the language of strings with equal
number of 0s and 1s?
27
Intro to Languages
• English grammar tells us if a given combination of words is
a valid sentence.
• The syntax of a sentence concerns its form while the
semantics concerns its meaning.
e.g. the mouse wrote a poem
• From a syntax point of view this is a valid sentence.
• From a semantics point of view not so fast…perhaps in
Disney land
• Natural languages (English, French, Portguese, etc) have
very complex rules of syntax and not necessarily welldefined.
29
Formal Language
• Formal language – is specified by well-defined set of rules of
syntax
• We describe the sentences of a formal language using a
grammar.
• Two key questions:
•
1 - Is a combination of words a valid sentence in a
formal language?
•
2 – How can we generate the valid sentences of a
formal language?
• Formal languages provide models for both natural
languages and programming languages.
31
Grammars (Semi-formal)
• Example: A grammar that generates a
subset of the English language
sentence → noun _ phrase
noun _ phrase → article
predicate → verb
predicate
noun
33
article → a
article → the
noun → boy
noun → dog
verb → runs
verb → sleeps
35
• A derivation of “the boy sleeps”:
sentence  noun _ phrase
predicate
 noun _ phrase
verb
 article
verb
 the noun
noun
verb
 the boy verb
 the boy sleeps
37
• A derivation of “a dog runs”:
sentence  noun _ phrase
predicate
 noun _ phrase
verb
 article
noun
verb
 a noun
verb
 a dog verb
 a dog runs
• Language of the grammar:
L = { “a boy runs”,
“a boy sleeps”,
“the boy runs”,
“the boy sleeps”,
“a dog runs”,
“a dog sleeps”,
“the dog runs”,
“the dog sleeps” }
41
Notation
noun → boy
noun → dog
Variable
or
Non-terminal
Production
rule
Terminal
Symbols of
the vocabulary
Symbols of
the vocabulary
43
Grammar Notation (Formal definition):
A grammar is define with the help of 4-tuple (V, T, P, S) where;
1.
V is the finite set of nonterminal symbols, Nonterminals in grammar are
also known as variables. It represents by capital letters of alphabets, for
example; A, B, …. X, Y etc.
2.
T is a finite set of terminal symbols, which are the characters of the
alphabet that appear in the strings generated by the grammar. It
represents by small letters of alphabets (like; a, b, c, .. .. .., x, y, z), binary
digits (0, 1) and digits as wells (like; 1, 2, 3, .. .. ..), for example; a, b, c, 0, 1
etc.
3.
P is a finite set of productions rules,
In CFG every production rule is of the form A→α, where A is a
nonterminal and α is a sequence of non terminals and terminals.
4.
S is a start symbol, which is a special nonterminal symbol that appears in
the initial string generated by the grammar.
The Chomsky Hierarchy
• A containment hierarchy of classes of formal languages
Regular
(DFA)
Contextfree
(PDA)
Contextsensitive
(LBA)
Recursivelyenumerable
(TM)
46
According to Chomsky hierarchy, grammars are divided of 4
types:
Type 0 known as unrestricted grammar.
Type 1 known as context sensitive grammar.
Type 2 known as context free grammar.
Type 3 Regular Grammar.
Type 0: Unrestricted Grammar:
Type-0 grammars include all formal grammars. It is any phase
structured grammar without any restriction. Type 0 grammar
language are recognized by Turing machine. These languages are
also known as the Recursively Enumerable languages.
Grammar Production in the form of
α-> β
where α ≠ ε & α is ( V + T)* V ( V + T)*
V : Variables , T : Terminals, β is ( V + T )*.
In type 0 there must be at least one variable on Left side of
production.
Example
For example,
S → ACaB
Sab –> ba
Bc → acB
A –> S.
CB → DB
Here, Variables are S, A and Terminals a, b.
aD → Db
Type 1: Context Sensitive Grammar:
Type-1 grammars generate the context-sensitive languages.
The language generated by the grammar are recognized by
the Linear Bound Automata
In Type 1
I. First of all Type 1 grammar should be Type 0.
II. Grammar Production in the form of α-> β
|α|<= |β |
i.e. count of symbol in α is less than or equal to β
For Example,
S –> AB
AB –> abc
B –> b
Type 2: Context Free Grammar:
Type-2 grammars generate the context-free languages. The
language generated by the grammar is recognized by a Pushdown
automata. Type-2 grammars generate the context-free languages.
In Type 2,
1. First of all it should be Type 1.
2. Left hand side of production can have only one variable.
| α | = 1.
Their is no restriction on β .
For example,
S –> AB
A –> a
B –> b
CFG is the theoretical basis for syntax of most programming
language
Type 3: Regular Grammar
Type-3 grammars generate regular languages. These
languages are exactly all languages that can be accepted by a
finite state automaton.
Type 3 is most restricted form of grammar.
Type 3 should be in the given form only :
V –> VT* / T*.
(or)
V –> T*V /T*
for example :
S –> ab.
Question : Classify the following grammars to identify its
type & hence write the automata used to recognize the
language of grammar.
S→ACaB
Ca→aaC
S→AAa
A→c | Ba
CBD→DB
Ba→ bca
S→ASB | d
A→ aA
D→abb
Type 0
Unristricted
Grammer
M/C-Turing
Machine
Type 1 Context
Sensitive
Grammer
M/C- Linear
Bounded
Automata
Type 2 Context
Free Grammer
M/C-Push
Down
Automata
Language Generated by Grammar
Given a grammar G, its corresponding language L(G)
represents the set of all strings generated from G.
Key Points –
• For a given grammar G, its corresponding language
L(G) is unique.
• The language L(G) corresponding to grammar G must
contain all strings which can be generated from G.
• The language L(G) corresponding to grammar G must
not contain any string which can not be generated
from G.
Example 1
Let G = (V, T, S, P), where V = { A, S}, T = {a, b}, S is a
start symbol and
P = { S → aA,
S → b,
A → aa }
• The language of this grammar is given by L (G) =
{b, aaa};
1. we can derive aA from S using S → aA, and then
derive aaa using A → aa.
2. We can also derive b using S → b.
Example 2
• Grammar:
G=(V,T,S,P)
T={a,b}
V={S}
• Derivation of sentence
P=
S -> aSb
S -> ε
ab
:
S  aSb  ab
S → aSb
S →
55
• Grammar:
S → aSb
S →
• Derivation of sentence
:
aabb
S  aSb  aaSbb  aabb
S → aSb
S →
56
• Other derivations:
S  aSb  aaSbb  aaaSbbb  aaabbb
S  aSb  aaSbb  aaaSbbb
 aaaaSbbbb  aaaabbbb
So, what’s the language of the
grammar with the productions?
S → aSb
S →
Language of the grammar with the productions:
S → aSb
S →
L = {a b : n  0}
n n
57
Example 3
• Suppose we have the following grammar
G: N = {S, A, B}
T = {a, b}
P = {S → AB, A → aA | a, B → bB | b}
• The language generated by this grammar
−
•
•
•
•
Example 3
Suppose we have the following grammar
G: N = {S, A, B}
T = {a, b}
P = {S → AB, A → aA | a, B → bB | b}
The language generated by this grammar
−
L(G) = {ab, a2b, ab2, a2b2, ………}
= {am bn | m ≥ 1 and n ≥ 1}
Construct the language generated by
the given grammar
i) S->Ab
A-> aAb | ɛ
ii) S-> Aa
A-> B
B-> Aa
iii) S → aSbb | ɛ
iv) S->aA | ɛ
A-> bS
v) S-> 0S1S1S | 1S1S0S | 1S0S1S | ɛ
vi)S → aaA, A → aAb | ab
vii)S → aSa
S → bSb
S→c
Ans:
Construct the language generated by
the given grammar
i) S->Ab
A-> aAb | ɛ
ii) S-> Aa
A-> B
B-> Aa
iii) S → aSbb | ɛ
iv) S->aA | ɛ
A-> bS
v) S-> 0S1S1S | 1S1S0S | 1S0S1S | ɛ
vi)S → aaA, A → aAb | ab
vii)S → aSa
S → bSb
S→c
Ans: {WcWR : w ∈ {a, b}*}
Consider the grammar: (GATE-CS)
S -> aSa|bSb|a|b
The language generated by the above grammar over the alphabet
{a,b} is the set of:
(A) All palindromes
(B) All odd length palindromes.
(C) All even length palindromes
HW: S→aSc | B
B →bBc | ɛ
Consider the grammar: (GATE-CS)
S -> aSa|bSb|a|b
The language generated by the above grammar over the alphabet
{a,b} is the set of:
(A) All palindromes
(B) All odd length palindromes.
(C) All even length palindromes
Solution: Using S->a and S->b, a and b can be generated.
Similarly using S=>aSa=>aba, aba can be generated.
Other strings which can be generated from grammar are:
a, b, aba, bab, aaa, bbb, ababa, …
Therefore, option (B) is correct.
HW: S→aSc | B
B →bBc | ɛ
Construction of grammar generating
the language
Key Points –
• For a given language L(G), there can be more
than one grammar which can produce L(G).
• The grammar G corresponding to language
L(G) must generate all possible strings of L(G).
• The grammar G corresponding to language
L(G) must not generate any string which is not
part of L(G).
ε
L
Examples
• Given a language L(G), its corresponding
grammar G represents the production rules
which produces L(G). Consider the language
L(G): = {anbn, n>=0}
Solution: The language L(G) is set of strings
{ε, ab, aabb, aaabbb…. }
ε
L
Examples
• Given a language L(G), its corresponding
grammar G represents the production rules
which produces L(G). Consider the language
L(G): = {anbn, n>=0}
Solution: The language L(G) is set of strings
{ε, ab, aabb, aaabbb…. }
For ε string in L(G), the production rule can be S->ε.
For other strings in L(G), the production rule can be
S->aSb|ε.
Therefore, grammar G corresponding to L(G) is:
S->aSb| ε.
Examples:
Problem − Suppose, L (G) = {am bn | m ≥ 0 and n > 0}. We
have to find out the grammar G which produces L(G).
Solution: Since L(G) = {am bn | m ≥ 0 and n > 0}
the set of strings accepted can be rewritten as −
L(G) = {b, ab, bb, aab, abb, …….}
Here, the start symbol has to take at least one ‘b’ preceded
by any number of ‘a’ including null.
To accept the string set {b, ab, bb, aab, abb, …….}, we
have taken the productions −
Examples:
Problem − Suppose, L (G) = {am bn | m ≥ 0 and n > 0}. We have to find
out the grammar G which produces L(G).
Solution: Since L(G) = {am bn | m ≥ 0 and n > 0}
the set of strings accepted can be rewritten as −
L(G) = {b, ab, bb, aab, abb, …….}
Here, the start symbol has to take at least one ‘b’ preceded by any
number of ‘a’ including null.
To accept the string set {b, ab, bb, aab, abb, …….}, we have taken the
productions −
S → aS , S → B, B → b and B → bB
•
•
•
•
•
•
•
•
S → B → b (Accepted)
S → B → bB → bb (Accepted)
S → aS → aB → ab (Accepted)
S → aS → aaS → aaB → aab(Accepted)
S → aS → aB → abB → abb (Accepted)
Thus, we can prove every single string in L(G) is accepted by the
language generated by the production set.
Hence the grammar −
G: ({S, A, B}, {a, b}, S, { S → aS | B , B → b | bB })
Problem − Suppose, L (G) = {am bn | m > 0 and n ≥ 0}. We have to find
out the grammar G which produces L(G).
Solution −
Since L(G) = {am bn | m > 0 and n ≥ 0}, the set of strings accepted can
be rewritten as −
L(G) = {a, aa, ab, aaa, aab ,abb, …….}
Here, the start symbol has to take at least one ‘a’ followed by any
number of ‘b’ including null.
To accept the string set {a, aa, ab, aaa, aab, abb, …….}, we have taken
the productions −
S → aA, A → aA , A → B, B → bB ,B → ε
S → aA → aB → aλ → a (Accepted)
S → aA → aaA → aaB → aaλ → aa (Accepted)
S → aA → aB → abB → abλ → ab (Accepted)
S → aA → aaA → aaaA → aaaB → aaaλ → aaa (Accepted)
S → aA → aaA → aaB → aabB → aabλ → aab (Accepted)
S → aA → aB → abB → abbB → abbλ → abb (Accepted)
Thus, we can prove every single string in L(G) is accepted by the
language generated by the production set.
Hence the grammar −
G: ({S, A, B}, {a, b}, S, {S → aA, A → aA | B, B → ε | bB })
Examples
• L={ anbmcmd2n | n>=0; m >0 }
Solution:
• L(G) = {WcWR : w ∈ {a, b}*}
Solution->
• L={w | w ɛ (a,b)* and w starts and ends with
the same symbol }
Solution:
• All strings in the language L : {an bma2n |n, m
≥ 0}
Solution:
Examples
• L={ anbmcmd2n | n>=0; m >0 }
Solution: S-> aSdd | A A-> bAc | bc
• L(G) = {WcWR : w ∈ {a, b}*}
Solution-> S → aSa | bSb | c
• L={w | w ɛ (a,b)* and w starts and ends with the
same symbol }
Solution: S-> aAa | bAb
A-> aA | bA | ɛ
• All strings in the language L : {an bma2n |n, m ≥ 0}
Solution: S → aSaa | B
B → bB | ɛ
1) All nonempty strings that start and end with the same
symbol
Solution:
2) Design a grammar for the language {0 n1 n | n ≥ 0} ∪ {1 n0 n
| n ≥ 0}
Solution:
1. Construct the grammar S1 → ________ for the language {0 n 1n | n ≥ 0}
2. Construct the grammar S2 → ________for the language {1 n 0 n | n ≥ 0}
3. Put them together adding the rule S → S1 | S2,
3) Let the set of alphabet A = {a, b, c} Then, the language of
palindromes over the alphabet A has the grammar
• All nonempty strings that start and end with the same symbol
Solution:
S → aAa | bAb | a | b
A → aA | bA | Є
• Design a grammar for the language {0 n1 n | n ≥ 0} ∪ {1 n0 n | n ≥ 0}
Solution:
1. Construct the grammar S1 → 0S11 |Є for the language {0 n 1 n | n ≥ 0}
2. Construct the grammar S2 → 1S20 | Є for the language {1 n 0 n | n ≥ 0}
3. Put them together adding the rule S → S1 | S2,
obtaining
S → S1 | S2
S1 → 0S11 | Є
S2 → 1S20 | Є
• Let the set of alphabet A = {a, b, c} Then, the language of palindromes
over the alphabet A has the grammar
Solution; S → aSa| bSb |cSc| a|b|c| Є
Equivalent Grammars
• Two grammars are said to be equivalent if they
generate the same languages.
• Consider the following two grammarsGrammar G1: S → aSb | ∈
Grammar G2: S → aAb | ∈
A → aAb | ∈
Both these grammars generate the same language
given as- L = { anbn | n>=0 }
Thus, L(G1) = L(G2)
Since both the grammars generate the same
language, therefore they are equivalent.
Formal Proof Proving Techniques
• By contradiction
– Start with the statement contradictory to the given
statement
– E.g., To prove (A => B), we start with:
• (A and ~B)
• … and then show that could never happen
• By induction
– (3 steps) Basis, inductive hypothesis, inductive step
• By contrapositive statement
– If A then B
≡
If ~B then ~A
75
A Proof Technique—Mathematical Induction
Definition 1
Mathematical induction is a mathod we use to prove a sequence of statements
P1,P2 ,... to be true.
(i) Basis : Proving that for some k = 1, P1 is true.
(ii) Inductive Assumption : Sopposing for any n  k, Pn is true.
(iii) Inductive Step :
Proving that Pn +1 is true.
Example
Let Sum(n) = 0 + 1 + 2 +-------------+ n for all natural numbers n,
giva an inductive prrof that the following equation is true for all
natural numbers m & n:
Sum ( m + n ) = sum (m) + sum (n) + mn
Summary
•
•
•
•
•
•
Historical perspective
Chomsky hierarchy of Languages
Alphabets, strings/words/sentences, languages
Formal definition of grammar
Membership problem
Proof Techniques
80
Download