Complexity and Computability Theory I

Download Report

Transcript Complexity and Computability Theory I

Complexity and Computability
Theory I
Lecture #12
Instructor: Rina Zviel-Girshin
Lea Epstein
Overview
•
•
•
•
Push-down automata - examples
The pumping lemma for CFL
Examples
Interesting facts
Rina Zviel-Girshin @ASC
2
L = {aibj | j>i0}
Construct a PDA to recognize:
L = {aibj | j>i0}
We will use the final states model.
The basic idea:
• write each a to the stack
• for each b pop from the stack and check that
the number of b’s greater than the number of
a’s
Rina Zviel-Girshin @ASC
3
L = {aibj | j>i0}
Rina Zviel-Girshin @ASC
4
L = {aibj | j>i0}
Construct a PDA to recognize:
L = {aibj | j>i0}
We will use the empty stack model.
The basic idea:
• write each a to the stack
• for each b, pop from the stack until reaching Z
and then for each b give two possibilities
– to pop Z and stop
– or to do nothing
Rina Zviel-Girshin @ASC
5
Example
Rina Zviel-Girshin @ASC
6
L = {aibicjdj |i,j0}
Construct a PDA to recognize:
L = {aibicjdj |i,j0}
We will use the empty stack model.
The basic idea:
• write each a to the stack and for each b pop
• write each c to the stack and for each d pop
Rina Zviel-Girshin @ASC
7
L = {aibicjdj |i,j0}
Rina Zviel-Girshin @ASC
8
Context-Free Languages
• Languages decided by PDA called
– context-free languages.
• Not all languages are context-free.
Rina Zviel-Girshin @ASC
9
Non-Context-Free Languages
• Consider L={aibici |i0}.
• We try to build a push-down automata that
recognizes (decides) it.
• If we manage to build a PDA for L then
the language is context-free.
• We cannot build a push-down automaton
that recognizes L.
Rina Zviel-Girshin @ASC
10
L={aibici |i0}
• WHY?
• We can compare a number of a-'s with b-'s
or c-'s but not both.
• If we compared the number of a-'s to the
number of b-'s then we can't compare c-'s
with any of them.
• Stack (or counter) is empty.
Rina Zviel-Girshin @ASC
11
Non-Context-Free Languages
• So some languages are not CFL.
• The question is which?
• We will answer this question later –
pumping lemma for context-free languages.
Rina Zviel-Girshin @ASC
12
Recursive grammar
• Back to grammars.
• Grammar is recursive if it has:
- direct recursion (AwAu)
or
- indirect recursion (AwBu,BzA)
Rina Zviel-Girshin @ASC
13
Direct recursion
• The following rule is called a direct recursion
rule:
AwAu
where
– w,u(VU)* and
– A derives A in one step and
– either w or u is not empty
• Why?
– Unite rule.
Rina Zviel-Girshin @ASC
14
Indirect recursion
• A grammar having the following rules is
called an indirect recursion grammar:
AwBu
BzA
where
–
–
–
–
w,u,z(VU)*
either w or u is not empty
z is not empty
A derives A in two steps
Rina Zviel-Girshin @ASC
15
Recursive grammar
• Grammar is recursive if it has direct or
indirect recursion.
Example:
SaS | b
SaA
AcS
Rina Zviel-Girshin @ASC
16
Infinite Language
• If a context-free language (CFL) has an infinite
number of words then its grammar is recursive
one.
• This means that there must be at least one
production rule that is recursive (directly or
indirectly).
• Recursive rules are used to generate strings of
arbitrary length. (infinite)
Rina Zviel-Girshin @ASC
17
Formal proof
• Proof by contradiction:
• We assume that L(G) is infinite and G is a
grammar with no recursive rules.
• Let n be a number of rules in G.
• The longest string in G can be parsed in at
most n steps
- (no recursion means there is no reuse
of the same production rule, so after
using n rules all rules are used).
Rina Zviel-Girshin @ASC
1
n
18
Formal proof (cont)
•
•
•
•
Each step should be a different rule.
Now we can “play” with sequences of rules.
We have n! different sequences.
That means – number of strings produced
by non recursive grammar is bounded by n!.
• The language L(G) is finite.
• Contradiction to the assumption L(G) is
infinite.
Rina Zviel-Girshin @ASC
1
n
19
Infinite Language Example
SuAz
AvAy | x
where either v or y is not empty.
• If we do not use the recursive rule we derived a
language that consist of only one string - uxz.
• If we use the recursive rule than the grammar
allows us to derive infinitely many strings having
the same construction (pattern).
Rina Zviel-Girshin @ASC
20
Example (cont.)
• For example w = uv2xy2z can be derived as
follows:
SuAzuvAyzuvvAyyz uvvxyyz
• The same pattern can be used to derive all
words of the form
w = uvkxykz for all k0.
Rina Zviel-Girshin @ASC
21
Pumping length
• That means that every CFL has a special value
called the pumping length such that all strings
longer than the pumping length can be
"pumped".
• The string can be divided into 5 parts w=uvxyz.
• The second and fourth can be pumped.
w = uvkxykz for all k0
Rina Zviel-Girshin @ASC
22
Pumping Lemma for CFL
• Let L be an infinite context-free language.
• There is a positive integer p (the pumping
length) such that for all strings wL with
|w|p, w can be divided into five pieces
w=uvxyz satisfying the following conditions:
1. |vy|>0
2. |vxy|p
3. for each i0, uvixyizL
Rina Zviel-Girshin @ASC
23
Proof - value of p
• Let G be a CFG for CFL L.
• First we find out the value of p.
• Let n be the maximum number of symbols
in the right side of some rule in G.
• So we know that in any parse tree of G a
node can't have more than n children.
• So if the height of the tree is h then the
length of the string its generate is at most
n h.
Rina Zviel-Girshin @ASC
24
Proof - value of p
• Let |V| be the number of variables in G.
• We set p to be n|V|+2 .
• p= n|V|+2
• Because n2 (no unite rules) then for any
string of length p the parse tree requires
height at least |V|+2.
• Lets call this string w: |w|p .
Rina Zviel-Girshin @ASC
25
Proof
• Since G has only |V| variables that means
that at least one of the production rules have
to be repeated (height |V|+2).
• Lets call this variable R.
• For convenience later, we select R to be a
variable that repeats among the lowest
|V|+1 variables.
Rina Zviel-Girshin @ASC
26
Proof
S
•We divide w into
uvxyz accordingly
to the parse tree.
R
R
u
v
x
y
z
•Each occurrence
of R has a subtree
under it.
Rina Zviel-Girshin @ASC
27
Proof
• The upper occurrence of R has a larger
subtree and generates vxy.
• The lower occurrence of R has a smaller
subtree and generates only x.
• Both subtrees are generated by the same
variable R.
• That means if we substitute one for the
other we will still obtain valid parse trees.
Rina Zviel-Girshin @ASC
28
S
S
That
establishes
condition 3
of lemma.
R
R
u
v
R
v
x
y
•Replacing the smaller
by the larger repeatedly
generates the string
uvixyiz at each i>0.
x
u
z
y
R
z
•Replacing the larger by
the smaller generates the
string uxz or uvixyiz
where i=0.
Rina Zviel-Girshin @ASC
29
Proof
• To prove condition 1 (|vy|>0) we must be
sure that both v and y are not .
• We use a grammar without union rules.
• So if v and y are both  we have a rule R 
R – unite rule. Contradiction.
• So condition 1 is satisfied.
Rina Zviel-Girshin @ASC
30
Proof
• To prove condition 2 (|vxy|p) we will
check the height of the subtree rooted in
first R - subtree that generates vxy.
• It’s height is at most |V|+2.
• So it can generate a string of length at most
n|V|+2. But p= n|V|+2 .
• Condition 2 is satisfied.
Q.E.D
Rina Zviel-Girshin @ASC
31
Usage of lemma
• We use pumping lemma to prove that a
language is not context-free.
• To prove that language L is context-free you
have:
– to build a CFG for L
or
– to build PDA that recognizes the words
of L.
Rina Zviel-Girshin @ASC
32
L={aibici |i0}
We will use the pumping lemma to prove that
L={aibici |i0} is not CFL.
Proof:
• We assume that L is CFL and obtain a
contradiction.
• Let p be the pumping length for L.
• We select a string w= apbpcp . wL and
|w|>p, so it can be pumped.
Rina Zviel-Girshin @ASC
33
L={aibici |i0}
• Lets divide w into five parts uvxyz such that
|vy|>0 and |vxy|p.
• We have two cases:
1. v and y contain only one type of alphabet
symbol. So if we choose i=2 then we have
uv2xy2z and it cannot contain the same
number of a's, b's and c's. So wL.
Rina Zviel-Girshin @ASC
34
L={aibici |i0}
2. v and y contain only two type of
alphabet symbol. (Can't have three because
condition 2 holds). So if we choose i=2 then
we have uv2xy2z and it cannot contain the
same number of a's, b's and c's, because we
pumped only two alphabet symbols.
• So wL.
Rina Zviel-Girshin @ASC
35
L={aibici |i0}
• One of these cases must occur.
• In both cases we have a contradiction to
lemma.
• So the assumption that L is CFL must be
false.
• L={aibici |i0} is not CFL.
Rina Zviel-Girshin @ASC
36
Intersection
• CFL are not closed under intersection.
Example:
L1={anbnck |n,k>0}
L2={akbncn | n,k>0}
Both L1 and L2 are CFL
L1L2 = {ajbjcj | j>0} -not CFL
Rina Zviel-Girshin @ASC
37
Complement
• CFL are not closed under complement.
Proof by contradiction:
• Lets take two CFL L1 and L2 and assume
that L1' and L2‘ are CFL.
• By union of L1' and L2' we obtain another
CFL. (Since CFL closed under union
operation).
Rina Zviel-Girshin @ASC
38
Complement
• If we take the compliment of this union we
again obtain CFL.
• (L1'L2')'=L1L2 - is CFL.
• But we know that
{anbnck |n,k>0} {akbncn | n,k>0} = {anbncn | n>0}
-not CFL
Rina Zviel-Girshin @ASC
39
Any Questions?
Rina Zviel-Girshin @ASC
40