Post on 19-Feb-2022
Properties ofContext-Free Languages
COMP 455 โ 002, Spring 2019
Jim Anderson (modified by Nathan Otterness) 1
Simplification of CFGs
We can simplify CFGs by removing:
Useless symbols.
โ๐ is generating if ๐ ึโ๐ค, where ๐ค โ ๐โ.
โ๐ is reachable if ๐ ึโ๐ผ๐๐ฝ (๐ is the start symbol).
โ๐ is useful only if it is both reachable and generating.
๐บ-productions, of the form ๐ด โ ํ.
โIf ํ is in the language, we will still need oneํ-production.
Unit productions, of the form ๐ด โ ๐ต.
Jim Anderson (modified by Nathan Otterness) 2
Finding Generating Variables
Theorem 7.4: The following algorithm correctly finds all generating variables.
Jim Anderson (modified by Nathan Otterness) 3
old_vars := โ ;new_vars := ๐ด | ๐ด โ ๐ค exists, and ๐ค โ ๐โ ;while old_vars โ new_vars:
old_vars = new_vars;new_vars = old_vars โช ๐ด | ๐ด โ ๐ผ, ๐ผ โ ๐ โช old_vars โ ;
return new_vars;
Finding Generating Variables
Proof of Theorem 7.4:
We want to show that ๐ is added to new_vars if and only
if ๐ ึโ๐ค for some ๐ค โ ๐โ.
โOnly ifโ: We must show that if ๐ is added to new_vars
then ๐ ึโ๐ค.
This can be proven by induction on the number of iterations of the algorithm (specifics are left as an exercise).
Jim Anderson (modified by Nathan Otterness) 4
old_vars := โ ;new_vars := ๐ด | ๐ด โ ๐ค exists, and ๐ค โ ๐โ ;while old_vars โ new_vars:
old_vars = new_vars;new_vars = old_vars โช ๐ด | ๐ด โ ๐ผ, ๐ผ โ ๐ โช old_vars โ ;
return new_vars;
Finding Generating Variables
Proof of Theorem 7.4:
We want to show that ๐ is added to new_vars if and only
if ๐ ึโ๐ค for some ๐ค โ ๐โ.
โIfโ: We must show that if ๐ ึโ๐ค, then ๐ is eventually
added to new_vars.
This can be proven by induction on the length of the derivation (specifics are left as an exercise).
Jim Anderson (modified by Nathan Otterness) 5
old_vars := โ ;new_vars := ๐ด | ๐ด โ ๐ค exists, and ๐ค โ ๐โ ;while old_vars โ new_vars:
old_vars = new_vars;new_vars = old_vars โช ๐ด | ๐ด โ ๐ผ, ๐ผ โ ๐ โช old_vars โ ;
return new_vars;
Finding Reachable Variables
Theorem 7.5: There exists an iterative algorithm that will correctly find all reachable symbols.
This is similar to the previous algorithm, except this time youโll start with a set containing the start symbol and look for new reachable symbols in each iteration.
Jim Anderson (modified by Nathan Otterness) 6
old_vars := โ ;new_vars := ๐ ;while old_vars โ new_vars:
old_vars = new_vars;new_vars = old_vars โช๐ด | ๐ด is produced by something in old_vars ;
return new_vars;
Eliminating Useless Symbols
Theorem 7.2: (Abbreviated) Every nonempty CFL is generated by a CFG with no useless symbols.
Proof:
Let ๐ฟ be the language of some CFG ๐บ, where ๐ฟ โ โ .
Define:
๐บ ๐บ1 ๐บ2
Jim Anderson (modified by Nathan Otterness) 7
RemoveNon-generating
using Theorem 7.4
RemoveNon-reachable
using Theorem 7.5
This order matters!
Eliminating Useless SymbolsProof, continued:
Assume ๐บ2 contains a useless variable, ๐.
By Theorem 7.5, ๐ึ๐บ2
โ๐ผ๐๐ฝ.
โ In other words, we know that ๐ is reachable in ๐บ2.
Any production in ๐บ2 must be a production in ๐บ1, so ๐ึ๐บ1
โ๐ผ๐๐ฝ must be a production in ๐บ1.
By Theorem 7.4, ๐ึ๐บ1
โ๐ผ๐๐ฝึ
๐บ1
โ๐ค.
โ In other words, we know that ๐ is producing in ๐บ1.
Every symbol in this derivation is reachable from ๐, so none will be eliminated by Theorem 7.5. So, ๐ึ
๐บ2
โ๐ผ๐๐ฝึ
๐บ2
โ๐ค.
This contradicts the assumption that ๐ was useless in ๐บ2.
Jim Anderson (modified by Nathan Otterness) 8
๐บ
Removenonโgenerating
๐บ1
Removeunreachable
๐บ2
It should be intuitively clear that removing useless
symbols wonโt change the language of a grammar.
Eliminating Useless Symbols: Example
Jim Anderson (modified by Nathan Otterness) 9
๐ โ ๐ด๐ต | ๐๐ด โ ๐
๐ โ ๐ด๐ต | ๐๐ด โ ๐
๐ โ ๐๐ด โ ๐
Remove Unreachable
Remove Non-generating
๐ โ ๐ด๐ต | ๐๐ด โ ๐
๐ โ ๐๐ด โ ๐
๐ โ ๐
Remove Non-generating
Remove Unreachable
Incorrect order: Correct order:
Note that these are not the same!
Removing Nullable Symbols
A symbol ๐ด is nullable if ๐ด ึโํ.
Theorem 7.7: There exists an algorithm that will correct identify all nullable symbols.
We will not prove thisโit should be intuitively similar to what weโve done before.
Jim Anderson (modified by Nathan Otterness) 10
old_vars := โ ;new_vars := ๐ด | ๐ด โ ํ exists ;while old_vars โ new_vars:
old_vars = new_vars;new_vars = old_vars โช ๐ด | ๐ด โ ๐ผ, ๐ผ โ old_varsโ ;
return new_vars;
Removing ํ-Productions
Theorem 7.9, reworded: If ๐ฟ = ๐ฟ ๐บ for a CFG ๐บ, then there exists a CFG ๐บ1 with no ํ-Productions such that ๐ฟ ๐บ1 = ๐ฟ ๐บ โ ํ .
Proof:
To construct ๐ฎ๐: If ๐ด โ ๐1โฆ๐๐ is in ๐, then add all productions of the form ๐ด โ ๐ผ1โฆ๐ผ๐ to ๐1, where:
1. If ๐๐ is not nullable, then ๐ผ๐ = ๐๐,
2. If ๐๐ is nullable, then ๐ผ๐ is either ๐๐ or ํ, and
3. Not all ๐ผ๐โs are ํ.
Jim Anderson (modified by Nathan Otterness) 11
This requires adding two production rules for each nullable ๐๐.
Removing ํ-Productions: Example
CFG ๐:
๐ โ ๐ด๐ต๐ถ
๐ด โ ํ
๐ต โ ๐ | ํ
๐ถ โ ๐
๐ด and ๐ต are nullable. CFG ๐1:
๐ โ ๐ด๐ต๐ถ ๐ต๐ถ ๐ด๐ถ | ๐ถ
๐ต โ ๐
๐ถ โ ๐
Jim Anderson (modified by Nathan Otterness) 12
๐ด is now useless in ๐1. (If you want to eliminate both ํ-productions and useless symbols, you must remove
ํ-productions first.
Removing ํ-Productions: Proof
Proof, continued:
Claim: For all ๐ด โ ๐ and ๐ค โ ๐โ, ๐ดึ๐บ1
โ๐ค if and only if
๐ค โ ํ and ๐ดึ๐บ
โ๐ค.
โIfโ: Assume ๐ดึ๐บ
๐๐ค and ๐ค โ ํ. We prove by
induction on ๐ that ๐ดึ๐บ1
โ๐ค.
Base case: ๐ = 1 (one derivation step)
๐ด โ ๐ค must be a production in ๐. Because ๐ค โ ํ,๐ด โ ๐ค is also a production in ๐บ1.
Jim Anderson (modified by Nathan Otterness) 13
๐บ1: The CFG without ํ-productions producing
๐ฟ ๐บ โ ํ .
Removing ํ-Productions: ProofInductive step: ๐ > 1 (more than one derivation step)
Assume ๐ดึ๐บ๐1โฆ๐๐ ึ
๐บ
๐โ1๐ค. Then ๐๐ึ
๐บ
โ๐ค๐ and
๐ค = ๐ค1โฆ๐ค๐.
If ๐ค๐ โ ํ, then ๐๐ึ๐บ1
โ๐ค๐, by the induction hypothesis.
If ๐ค๐ = ํ, then ๐๐ is nullable.
Therefore, ๐ด โ ๐ฝ1โฆ๐ฝ๐ is in the productions of ๐บ1, where
๐ฝ๐ = ๐๐ if ๐ค๐ โ ํ,
๐ฝ๐ = ํ if ๐ค๐ = ํ.
And we have the following derivation in ๐บ1:
๐ด ึ ๐ฝ1๐ฝ2โฆ๐ฝ๐ ึโ๐ค1๐ฝ2โฆ๐ฝ๐ ึ
โ๐ค1๐ค2โฆ๐ค๐ = ๐ค.
Jim Anderson (modified by Nathan Otterness) 14
Claim: For all ๐ด โ ๐ and ๐ค โ ๐โ,
๐ดึ๐บ1
โ๐ค if and only if ๐ค โ ํ and ๐ดึ
๐บ
โ๐ค.
Removing ํ-Productions: Proof
โOnly ifโ: Assume ๐ดึ๐บ1
๐๐ค. Then, ๐ค โ ํ.
We will prove by induction on ๐ that ๐ดึ๐บ
โ๐ค.
Base case: ๐ = 1.
๐ด โ ๐ค is in the productions of ๐บ1. Therefore, ๐ด โ ๐ผis in the productions of ๐บ where ๐ค = ๐ผ with nullable symbols replaced by ํ.
Jim Anderson (modified by Nathan Otterness) 15
Claim: For all ๐ด โ ๐ and ๐ค โ ๐โ,
๐ดึ๐บ1
โ๐ค if and only if ๐ค โ ํ and ๐ดึ
๐บ
โ๐ค.
Removing ํ-Productions: Proof
โOnly ifโ, continued:
We must show that the derivation ๐ดึ๐บ๐ผึ
๐บ
โ๐ค exists in ๐บ.
Inductive step: Suppose that ๐ดึ๐บ1๐1โฆ๐๐ ึ
๐บ1
๐โ1๐ค.
Then, ๐ด โ ๐ฝ is in the productions of ๐บ, where๐1โฆ๐๐ = ๐ฝ with some nullable symbols removed.
As in the base case, ๐ดึ๐บ
โ๐1โฆ๐๐. And, by the inductive
hypothesis, we can show that ๐1โฆ๐๐ึ๐บ
โ๐ค.
Jim Anderson (modified by Nathan Otterness) 16
This is where nullable symbols are replaced by ํ.
โAssume the derivation with ๐ โ 1 steps is correctโฆโ etc.
Removing Unit Productions
Theorem 7.13 (reworded): If ๐ฟ = ๐ฟ ๐บ for a CFG ๐บ, then there exists a CFG ๐บ1 with no unit productions such that ๐ฟ = ๐ฟ ๐บ1 .
Proof: Let ๐บ = ๐, ๐, ๐, ๐
To construction ๐บ1, first add all non-unit productions in ๐ to ๐1.
Next, if ๐ด ึโ
๐บ๐ต and ๐ต โ ๐ผ is a non-unit production in
๐, then add ๐ด โ ๐ผ to ๐1.
Jim Anderson (modified by Nathan Otterness) 17
We can find all pairs of ๐ด, ๐ต where
๐ดึ๐บ
โ๐ต using an iterative algorithm like
before (see Section 7.1.4 of the book).
Removing Unit Productions: Example
Consider the CFG ๐บ with the following productions:
๐ โ ๐ด | ๐
๐ด โ ๐ด๐ด๐
After removing the unit production ๐ โ ๐ด, this becomes:
๐ โ ๐ด๐ด๐ | ๐
๐ด โ ๐ด๐ด๐
Jim Anderson (modified by Nathan Otterness) 18
Removing Unit Productions: Proof
Claim: ๐ฟ ๐บ1 โ ๐ฟ ๐บ
Proof:
If ๐ด โ ๐ผ is in ๐1, then ๐ดึ๐บ
โ๐ผ. Therefore, ๐ดึ
๐บ1
โ๐ผ implies
๐ดึ๐บ
โ๐ผ.
Jim Anderson (modified by Nathan Otterness) 19
๐บ: The grammar containing unit productions.๐บ1: The grammar with unit productions removed.
Removing Unit Productions: Proof
Claim: ๐ฟ ๐บ โ ๐ฟ ๐บ1
Proof:
Suppose ๐ค โ ๐ฟ ๐บ .
Let ๐ = ๐ผ0ึ๐บ๐ผ1ึ
๐บโฆึ
๐บ๐ผ๐ = ๐ค be a leftmost derivation.
If ๐ผ๐ึ๐บ๐ผ๐+1 is due to a non-unit production, then ๐ผ๐ึ
๐บ1๐ผ๐+1.
Jim Anderson (modified by Nathan Otterness) 20
๐บ: The grammar containing unit productions.๐บ1: The grammar with unit productions removed.
Removing Unit Productions: Proof
Claim: ๐ฟ ๐บ โ ๐ฟ ๐บ1
Consider the following leftmost derivation with unit productions in ๐บ: ๐ผ๐โ1ึ
๐บ๐ผ๐ึ
๐บ๐ผ๐+1ึ
๐บโฆึ
๐บ๐ผ๐ึ
๐บ๐ผ๐+1.
Unit productions just replace the symbol at the same (leftmost) position, so ๐ผ๐โ1 ึ ๐ผ๐+1 will also hold by
some production in ๐1 โ ๐.
Jim Anderson (modified by Nathan Otterness) 21
๐บ: The grammar containing unit productions.๐บ1: The grammar with unit productions removed.
Non-unit production
Non-unit production
Unit productions
Putting it all Together
Theorem 7.14: If ๐ฟ is the language of a CFG ๐บ, and ๐ฟcontains at least one string other than ํ, then there exists a CFG ๐บ1 with no ํ-productions, unit productions, or useless symbols such that ๐ฟ ๐บ1 = ๐ฟ โ ํ .
(See the book for a formal proof.)
The order in which we apply the previous results is important.
Jim Anderson (modified by Nathan Otterness) 22
Putting it all Together
The order of previous results:
We saw on slide 12 that eliminating ํ-productions may cause some symbols to become useless, so we must eliminate ํ-productions before removing useless symbols.
In the same example on slide 12, removing ํ-productions also introduced a unit production (๐ โ๐ถ), so ํ-productions must be eliminated before unit productions.
Jim Anderson (modified by Nathan Otterness) 23
Putting it all Together
Finally, unit productions must be eliminated before useless symbols. Consider this example:
So, the only viable order is: 1) Eliminate ํ-productions, 2) Eliminate unit productions, and 3) Eliminate useless symbols.
Jim Anderson (modified by Nathan Otterness) 24
๐ โ ๐ด๐ต๐ด โ ๐ต๐ต โ ๐ถ๐ถ โ ๐
๐ โ ๐ด๐ต๐ด โ ๐๐ต โ ๐๐ถ โ ๐
Remove useless symbols
Remove unit productions
๐ถ is now useless, but is still in the
grammar
Chomsky Normal Form (CNF)
A CFG is in Chomsky Normal Form if all of its productions are of the form ๐ด โ ๐ต๐ถ or ๐ด โ ๐, and it contains no useless symbols.
Theorem 7.16 (reworded): Any CFL that doesnโt include ํ can be generated by a CFG in CNF.
Proof: Let ๐ฟ = ๐ฟ ๐บ for some CFG ๐บ, where ํ โ ๐ฟ. Use Theorem 7.14 to convert ๐บ into ๐บ1 = ๐, ๐, ๐, ๐ , where ๐บ1 contains no ํ-productions, unit productions, or useless symbols.
Jim Anderson (modified by Nathan Otterness) 25
Converting to Chomsky Normal Form
Proof (continued):
If ๐ด โ ๐ is a production, then ๐ โ ๐ (which is already in the correct form).
Otherwise, consider ๐ด โ ๐1๐2โฆ๐๐, where ๐ โฅ 2.
If ๐๐ is a terminal, introduce a new variable ๐ถ๐ and a new production ๐ถ๐ โ ๐, then replace ๐๐ by ๐ถ๐.
Call the resulting grammar ๐บ2 (after making all such replacements).
Claim: ๐ฟ ๐บ1 = ๐ฟ ๐บ2 . (The proof is left as an exercise.)
Jim Anderson (modified by Nathan Otterness) 26
Otherwise ๐ด โ ๐ would be a unit production, and have
been eliminated already.
Converting to Chomsky Normal Form
The remaining problem is that we need to replace productions of the form ๐ด โ ๐ต1๐ต2โฆ๐ต๐ (where๐ โฅ 3).
Replace such a production by:
๐ด โ ๐ต1๐ท1, ๐ท1 โ ๐ต2๐ท2, โฆ, ๐ท๐โ1 โ ๐ต๐โ1๐ต๐, using newly added ๐ท๐ variables.
Call the resulting grammar ๐บ3.
Claim: ๐ฟ ๐บ3 = ๐ฟ ๐บ2 . (The proof is left as an exercise.)
Jim Anderson (modified by Nathan Otterness) 27
Conversion to CNF: Example
Jim Anderson (modified by Nathan Otterness) 28
๐ โ ๐๐ด | ๐๐ต๐ด โ ๐๐ด๐ด | ๐๐ | ๐๐ต โ ๐๐ต๐ต | ๐๐ | ๐
๐ โ ๐ถ๐๐ด | ๐ถ๐๐ต๐ด โ ๐ถ๐๐ด๐ด | ๐ถ๐๐ | ๐๐ต โ ๐ถ๐๐ต๐ต | ๐ถ๐๐ | ๐๐ถ๐ โ ๐๐ถ๐ โ ๐
Replace ๐ด โ ๐ถ๐๐ด๐ด by:๐ด โ ๐ถ๐๐ท1, ๐ท1 โ ๐ด๐ด
Replace ๐ต โ ๐ถ๐๐ต๐ต by:๐ต โ ๐ถ๐๐ท2, ๐ท2 โ ๐ต๐ต
๐ โ ๐ถ๐๐ด | ๐ถ๐๐ต๐ด โ ๐ถ๐๐ท1 | ๐ถ๐๐ | ๐๐ต โ ๐ถ๐๐ท2 | ๐ถ๐๐ | ๐๐ท1 โ ๐ด๐ด๐ท2 โ ๐ต๐ต๐ถ๐ โ ๐๐ถ๐ โ ๐
Starting CFG with no ํ-productions, unit productions, or useless symbols.
CFG in CNF.
The Pumping Lemma for CFLs
Theorem 7.18 (the Pumping Lemma for CFLs):Let ๐ฟ be any CFL. Then there exists a value ๐ such that for all ๐ง โ ๐ฟ, where ๐ง โฅ ๐, there exist strings๐ข, ๐ฃ, ๐ค, ๐ฅ, ๐ฆ such that:
1. ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆ,
2. ๐ฃ๐ฅ โฅ 1,
3. ๐ฃ๐ค๐ฅ โค ๐, and
4. For all ๐ โฅ 0, ๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.
Jim Anderson (modified by Nathan Otterness) 29
Proof of the Pumping Lemma for CFLs
If ๐ฟ is a CFL, let ๐บ be a CFG generating ๐ฟ โ ํ .
Claim: Let ๐ง โ ๐ฟ โ ํ . If a parse tree for ๐ง in ๐บ hasno path longer than ๐, then ๐ง โค 2๐โ1. (This is Theorem 7.17 in the book.)
Proof, by induction on ๐:
Base case: ๐ = 1. String ๐ง = ๐. Tree: ๐ ๐ง = 1 = 2๐โ1
๐Jim Anderson (modified by Nathan Otterness) 30
The Pumping Lemma only applies to strings longer
than ๐, so removing ํdoesnโt matter.
Proof of the Pumping Lemma for CFLs
Inductive step: ๐ > 1.
Suppose that a tree exists with some path of length ๐, but no path exceeding a length ๐. It looks like this:
Jim Anderson (modified by Nathan Otterness) 31
๐
๐ด ๐ต
โค 2๐โ2 โค 2๐โ2
๐1 ๐2
โค 2๐โ1
Proof of the Pumping Lemma for CFLs
Let ๐ equal the number of variables in the CFG ๐บ.
Let ๐ = 2๐.
Suppose ๐ง โ ๐ฟ ๐บ , where ๐ง โฅ ๐.
โNote: ๐ง > 2๐โ1
We claim that any parse tree for ๐ง has a path of length โฅ ๐ + 1.
โTo see this, suppose all paths in a tree are shorter than ๐ + 1 (no path is > ๐). Then, ๐ง โค 2๐โ1, contradicting the claim that ๐ง > 2๐โ1.
Jim Anderson (modified by Nathan Otterness) 32
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.
Proof of the Pumping Lemma for CFLs
As stated on the previous slide, any parse tree for ๐งhas a path of length โฅ ๐ + 1.
Such a path has at least ๐ + 2 nodes, ๐ + 1 of which are variables.
Jim Anderson (modified by Nathan Otterness) 33
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.
๐
๐ง
Some path from the start symbol to a terminal in ๐ง
must contain ๐ + 1 variables.
The CNF grammar requires replacing variables with a
terminal at the end of the path.
Proof of the Pumping Lemma for CFLs
Since the CFG contains ๐ variables, but the path in ๐งโs parse tree contains ๐ + 1 variables, at least one variable must be repeated in the path.
If ๐ด is the repeated variable, the path looks like this:
Jim Anderson (modified by Nathan Otterness) 34
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.
๐
๐ง
๐ด
๐ด
Proof of the Pumping Lemma for CFLs
Consider the subtrees rooted at each occurrence of ๐ด:
Jim Anderson (modified by Nathan Otterness) 35
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.
๐
๐ง
๐ด
๐ด
๐ข ๐ฃ ๐ค ๐ฅ ๐ฆ
Proof of the Pumping Lemma for CFLs
A subtree rooted at ๐ด has (at least) two possible yields: ๐ค๐ค๐ฅ and ๐ค.
Jim Anderson (modified by Nathan Otterness) 36
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.๐
๐ด
๐ด
๐ข ๐ฃ ๐ค ๐ฅ ๐ฆ
๐ฃ๐ค๐ฅ = yield of first subtree of ๐ด๐ค = yield of second subtree of ๐ด
Proof of the Pumping Lemma for CFLs
We can replace the possible subtrees rooted at ๐ด with each other to generate different strings.
Jim Anderson (modified by Nathan Otterness) 37
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.๐
๐ข
๐ค
๐ฆ
This tree has a yield ๐ข๐ค๐ฆ = ๐ข๐ฃ0๐ค๐ฅ0๐ฆThis string must also be in ๐ฟ.
๐ด
Proof of the Pumping Lemma for CFLs
Jim Anderson (modified by Nathan Otterness) 38
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.๐
๐ด
๐ด
๐ข ๐ฃ ๐ฅ ๐ฆ
We can replace the possible subtrees rooted at ๐ด with each other to generate different strings.
๐ฃ ๐ค ๐ฅ
This tree has a yield ๐ข๐ฃ๐ฃ๐ค๐ฅ๐ฅ๐ฆ = ๐ข๐ฃ2๐ค๐ฅ2๐ฆThis string must also be in ๐ฟ.
๐ดNote that this introduces another occurrence of ๐ด.
Proof of the Pumping Lemma for CFLs
Jim Anderson (modified by Nathan Otterness) 39
โข ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆโข ๐ฃ๐ฅ โฅ 1โข ๐ฃ๐ค๐ฅ โค ๐โข For all ๐ โฅ 0,
๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ.
๐
๐ด
๐ด
๐ข ๐ฃ ๐ฅ ๐ฆ
๐ฃ ๐ค ๐ฅ
๐ด
๐ด
๐ด
๐ฃ ๐ฅ
๐ฃ ๐ฅ
โฆ
We can repeat this process indefinitely to keep generating strings in ๐ฟ of the form ๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ.
So, ๐ข๐ฃ๐๐ค๐ฅ๐๐ฆ โ ๐ฟ for all ๐ โฅ 0.
Application of the CFL Pumping Lemma
๐ฟ = ๐๐๐๐๐๐ | ๐ โฅ 1 .
Let the string ๐ง = ๐๐๐๐๐๐, for an arbitrary ๐ > 0. Clearly, ๐ง โ ๐ฟ for any ๐.
Let ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆ, ๐ฃ๐ฅ โฅ 1, and ๐ฃ๐ค๐ฅ โค ๐. This means:
โ ๐ฃ๐ฅ is all ๐โs, ๐โs, or ๐โs, or
โ ๐ฃ๐ฅ is all ๐โs and ๐โs or all ๐โs and ๐โs
โ (The key point is that ๐ฃ๐ฅ can not possibly contain ๐โs, ๐โs, and ๐โs all at the same time.)
If ๐ฃ๐ฅ contains only one type of symbol, ๐ข๐ฃ0๐ค๐ฅ0๐ฆ will contain too few of that symbol.
If ๐ฃ๐ฅ contains two types of symbols, ๐ข๐ฃ0๐ค๐ฅ0๐ฆ will contain too many of the symbol not in ๐ฃ๐ฅ.
Therefore, ๐ข๐ฃ0๐ค๐ฅ0๐ฆ โ ๐ฟ, and the Pumping Lemma for CFLs does not hold for ๐ฟ.
Jim Anderson (modified by Nathan Otterness) 40
Application of the CFL Pumping Lemma ๐ฟ = ๐ค๐ค| ๐ค โ ๐ + ๐ โ .
Let ๐ง = 0๐1๐0๐1๐. Clearly, ๐ง โ ๐ฟ for any ๐.
Let ๐ง = ๐ข๐ฃ๐ค๐ฅ๐ฆ, ๐ฃ๐ฅ โฅ 1, and ๐ฃ๐ค๐ฅ โค ๐. This means:
โ ๐ฃ๐ฅ is all 0s or all 1s
โ ๐ฃ๐ฅ is some 0s followed by some 1s
โ ๐ฃ๐ฅ is some 1s followed by some 0s.
If ๐ฃ๐ฅ contains only 0s or only 1s, ๐ข๐ฃ0๐ค๐ฅ0๐ฆ will contain too few 0s or 1s in one half of the string.
If ๐ฃ๐ฅ contained 0s followed by 1s, then either the first or second half of ๐ข๐ฃ0๐ค๐ฅ0๐ฆ will contain fewer 0s and 1s than the other half.
If ๐ฃ๐ฅ contained 1s followed by 0s (๐ฃ๐ฅ is in the middle of ๐ง), then ๐ข๐ฃ0๐ค๐ฅ0๐ฆwill have more 0s in the first group of 0s than the second group of 0s. (The number of 1s will also be similarly imbalanced.)
In any of these cases, ๐ข๐ฃ0๐ค๐ฅ0๐ฆ โ ๐ฟ.
Jim Anderson (modified by Nathan Otterness) 41
Closure of CFLs under Substitution
Recall that a homomorphism maps characters in some alphabet ฮฃ to strings over another alphabet ฮ.
A homomorphism is actually a special case of substitution, which maps characters in one alphabet to any string in a language over another alphabet.
Consider this example substitution ๐:
โฮฃ = 0,1 , ฮ = ๐, ๐ , ๐ 0 = ๐ + ๐โ, ๐ 1 = ๐โ๐.
โ๐ ๐โ๐โ = ๐ + ๐โ โ ๐โ๐ โ.
โ(Note that this particular example uses regular languages.)
Jim Anderson (modified by Nathan Otterness) 42
Closure of CFLs under Substitution
Theorem 7.23 (reworded): The CFLs are closed under substitution and, by extension, homomorphism.
Proof:
The main idea is to replace all terminals in a CFG with start symbols of another CFG.
Let ๐ฟ be a CFL, and ๐ฟ โ ฮฃโ. For all ๐ โ ฮฃ, let ๐ฟ๐ be a CFL.
Let ๐ฟ = ๐ฟ ๐บ . For all ๐ โ ฮฃ, let ๐ฟ๐ = ๐ฟ ๐บ๐ .
Assume these grammars have distinct variables.
Jim Anderson (modified by Nathan Otterness) 43
Closure under Substitution, Proof contd.
Let ๐บ = ๐, ๐, ๐, ๐ , and for all ๐ โ ฮฃ, ๐บ๐ = ๐๐, ๐๐, ๐๐ , ๐๐
Define ๐บโฒ = ๐โฒ, ๐โฒ, ๐โฒ, ๐โฒ , where:
๐โฒ = ๐โฮฃ๐๐ฺ โช ๐
๐โฒ = ๐โฮฃ๐๐ฺ
๐โฒ = ๐
๐โฒ = ๐โฮฃ๐๐ฺ โช {๐ด โ ๐ผโฒ | ๐ด โ ๐ผ is in ๐, and๐ผโฒ = ๐ผ with each ๐ โ ฮฃ replaced by ๐๐}.
The language defined by substitution equals ๐ฟ ๐บโฒ . (The proof is left as an exerciseโor see Theorem 7.23 in the book.)
Jim Anderson (modified by Nathan Otterness) 44
Union, Concatenation, and Closure
Theorem 7.24: CFLs are closed under Union, Concatenation, โ-closure, and +-closure.
Proof:
Union: Let ๐ฟ1 and ๐ฟ2 be CFLs. ๐ฟ1 โช ๐ฟ2 = ๐ ๐ฟ , where ๐ฟ = 1, 2 (which is clearly a CFL), and ๐ is the substitution defined by ๐ 1 = ๐ฟ1 and ๐ 2 = ๐ฟ2.
The proofs for the others are similar.
Jim Anderson (modified by Nathan Otterness) 45
Notation similar to โ, but meaning โ1 or more repetitionsโ.
Closure under Reversal
Theorem 7.25: CFLs are closed under reversal.
Proof:
If ๐ฟ = ๐ฟ ๐บ , where ๐บ = ๐, ๐, ๐, ๐ , then ๐ฟ๐ = ๐ฟ ๐บ๐ , where ๐บ๐ = ๐, ๐, ๐๐ , ๐ , and ๐๐ = {๐ด โ ๐ผ๐ | A โ ๐ผ is a production in ๐}.
(The full formal proof is left as an exercise.)
Jim Anderson (modified by Nathan Otterness) 46
The productions in ๐บ๐
are just the productions in ๐บ written backwards.
(Lack of) Closure under Intersection
Theorem: CFLs are not closed under intersection.
Proof:
๐ฟ1 = ๐๐๐๐๐๐ | ๐ โฅ 1 . We know ๐ฟ1 isnโt a CFL from earlier slides.
๐ฟ2 = ๐๐๐๐๐๐ | ๐ โฅ 1, ๐ โฅ 1 . This is a CFL.
๐ฟ3 = ๐๐๐๐๐๐ | ๐ โฅ 1, ๐ โฅ 1 . This is also a CFL.
However, ๐ฟ1 = ๐ฟ2 โฉ ๐ฟ3.
Jim Anderson (modified by Nathan Otterness) 47
See example 7.26 in the book for CFGs.
(Lack of) Closure under Complementation
Corollary to the previous theorem: CFLs are notclosed under complementation.
Proof:
CFLs are closed under union.
๐ฟ1 โฉ ๐ฟ2 โก ๐ฟ1 โช ๐ฟ2.
So, if CFLs are closed under complementation, they would be closed under intersection, too.
Jim Anderson (modified by Nathan Otterness) 48
Intersection with Regular Languages
Theorem 7.26: If ๐ฟ is a CFL and ๐ is a regular language, then ๐ฟ โฉ ๐ is a CFL.
Proof:
Let ๐ฟ be the language of some PDA ๐ = ๐๐, ฮฃ, ฮ, ๐ฟ๐, ๐๐ , ๐0, ๐น๐ .
Let ๐ be the language of some DFA ๐ด = ๐๐ด, ฮฃ, ๐ฟ๐ด, ๐๐ด, ๐น๐ด .
Idea: Create a new PDA combining the states of ๐ and ๐ด, similar to combining two DFAs.
Jim Anderson (modified by Nathan Otterness) 49
Proof: Intersection with Reg. Languages
Let ๐โฒ = ๐๐ ร ๐๐ด, ฮฃ, ฮ, ๐ฟ, ๐๐ , ๐๐ด , ๐0, ๐น๐ ร ๐น๐ด , where:
๐ฟ ๐, ๐ , ๐, ๐ contains ๐, ๐ , ๐พ if and only if๐ฟ๐ด ๐, ๐ = ๐ and ๐ฟ๐ ๐, ๐, ๐ contains ๐, ๐พ .
Claim: ๐๐ , ๐๐ด , ๐ค, ๐0 โ๐
๐โฒ๐, ๐ , ํ, ๐พ if and only if
๐๐ , ๐ค, ๐0 โ๐
๐๐, ํ, Y and ๐ฟ ๐๐ด, ๐ค = ๐.
(This can be proven by induction on ๐.)
Jim Anderson (modified by Nathan Otterness) 50
Closure under Inverse Homomorphism
Theorem 7.30: CFLs are closed under inverse homomorphism.
Proof:
Consider ๐ฟ, where ๐ฟ is the language of some PDA ๐.
๐ = ๐, ฮ, ฮ, ๐ฟ, ๐0, ๐0, ๐น .
Let โ: ฮฃ โ ฮโ.
Construct a PDA ๐โฒ that accepts โโ1 ๐ฟ .
Jim Anderson (modified by Nathan Otterness) 51
Closure under Inverse Homomorphism
Proof, continued:
The key idea is the same as for regular languages: When processing an input ๐, ๐โฒ simulates ๐ on the input โ ๐ .
In A DFA, simulation required just a single state transition.
However, ๐, being a PDA, does more than just change state on input โ ๐ --it may change the stack contents or make nondeterministic choices.
Solution: Use a buffer to hold the symbols of โ ๐ .
Jim Anderson (modified by Nathan Otterness) 52
This buffer will really be part of ๐โฒโs (finite!) state.
Closure under Inverse Homomorphism
Conceptually:
Jim Anderson (modified by Nathan Otterness) 53
โโ ๐
Buffer
Input๐
Original PDA state
Stack
Accept/Reject
The buffer must be large enough to hold the longest
string produced by โ.
Closure under Inverse Homomorphism
Short example: โ ๐ = 01, and โ ๐ = 111
Jim Anderson (modified by Nathan Otterness) 54
โ
BufferInput string:๐
Original PDA state
Stack
Accept/Reject
๐๐11011 Etc.
Closure under Inverse Homomorphism
We have a homomorphism โ: ฮฃ โ ฮโ.
Recall that ๐ = ๐, ฮ, ฮ, ๐ฟ, ๐0, ๐0, ๐น
Let ๐โฒ = ๐โฒ, ฮฃ, ฮ, ๐ฟโฒ, ๐0, ํ , ๐0, ๐น ร ํ .
States in ๐โฒ is a set of pairs ๐, ๐ฅ such that
โ๐ is a state in ๐, and
โ๐ฅ is the finite โbufferโโโ ๐ or a suffix of โ ๐for some symbol ๐ โ ฮฃ.
Jim Anderson (modified by Nathan Otterness) 55
Start in the original start state, with an empty buffer.
Accept in any of the original accepting states, if the buffer is empty.
Closure under Inverse Homomorphism
Transitions in ๐โฒ:
๐ฟโฒ ๐, ๐ฅ , ํ, ๐ contains all ๐, ๐ฅ , ๐พ such that ๐ฟ ๐, ํ, ๐
contains ๐, ๐พ .
โ โSimulate ํ-transitionsโ
๐ฟโฒ ๐, ๐๐ฅ , ํ, ๐ contains all ๐, ๐ฅ , ๐พ such that ๐ฟ ๐, ๐, ๐contains ๐, ๐พ .
โ โSimulate non-ํ transitionsโ
๐ฟโฒ ๐, ํ , ๐, ๐ contains ๐, โ ๐ , ๐ for all ๐ โ ฮฃ and ๐ โ ฮ.
โ โLoad the bufferโJim Anderson (modified by Nathan Otterness) 56
Closure under Inverse Homomorphism
Claim: If ๐0, โ ๐ค , ๐0 โ๐
โ๐, ํ, ๐พ , then
๐0, ํ , ๐ค, ๐0 โ๐โฒ
โ๐, ํ , ํ, ๐พ .
Proof sketch:
For each ๐ โ ฮฃ, whatever sequence of moves ๐ makes on โ ๐ , ๐โฒ can make a corresponding sequence of moves on input ๐.
Jim Anderson (modified by Nathan Otterness) 57
Closure under Inverse Homomorphism
Claim: If ๐0, ํ , ๐ค, ๐0 โ๐โฒ
โ๐, ํ , ํ, ๐พ , then
๐0, โ ๐ค , ๐0 โ๐
โ๐, ํ, ๐พ .
Proof sketch:
๐โฒ can only process ๐ค one character at a time. For each character ๐, ๐โฒ does what ๐ does on โ ๐ .
The first claim implied that ๐ฟ ๐โฒ โ โโ1 ๐ฟ ๐ .
This claim implies that ๐ฟ ๐โฒ โ โโ1 ๐ฟ ๐ .
Jim Anderson (modified by Nathan Otterness) 58
Decision Properties of CFLs
As with regular languages, weโll focus less on efficiency and more on simplicity than whatโs done in the book.
Theorem: There exist algorithms to determine if a CFL is (a) empty, (b) finite, or (c) infinite.
Jim Anderson (modified by Nathan Otterness) 59
Detecting if a CFL is Empty
Nonemptiness:
Use the iterative algorithm for detecting useless
symbols to test if ๐ด ึโ๐ค for all ๐ด โ ๐.
The CFL is nonempty if and only if ๐ ึโ๐ค for some
terminal string ๐ค.
Jim Anderson (modified by Nathan Otterness) 60
Detecting if a CFL is Finite or Infinite
Assume ๐ฟ does not contain ํ. (If ํ โ ๐ฟ, then consider ๐ฟ โ ํ instead.)
Suppose ๐ฟ = ๐ฟ ๐บ , where ๐บ = ๐, ๐, ๐, ๐ is in CNF (and therefore has no useless symbols).
Consider the directed graph ๐, ๐ธ , where ๐ด, ๐ต โ ๐ธ if ๐ด โ ๐ต๐ถ or ๐ด โ ๐ถ๐ต is in ๐ for some variable ๐ถ.
Claim: ๐ฟ is finite if and only if ๐, ๐ธ has no cycles.Jim Anderson (modified by Nathan Otterness) 61
๐ฟ โ ํ will be finite if and only if ๐ฟ is finite.
Detecting if a CFL is Finite or Infinite
Claim (again): ๐ฟ is finite if and only if ๐, ๐ธ has no cycles.
โOnly ifโ: A cycle has the form ๐ด0, ๐ด1, โฆ, ๐ด๐, ๐ด0.
Therefore, we have ๐ด0 ึ ๐ผ1๐ด1๐ฝ1 ึ ๐ผ2๐ด2๐ฝ2 ึ โฏ ึ ๐ผ๐๐ด๐๐ฝ๐ ึ๐ผ๐+1๐ด0๐ฝ๐+1, where ๐ผ๐๐ฝ๐ = ๐.
Since ๐บ has no useless symbols:
๐ผ๐+1 ึโ๐ค,
๐ฝ๐+1 ึโ๐ฅ,
๐ ึโ๐ฆ๐ด0๐ง, and
๐ด0 ึโ๐ฃ,
where ๐ค, ๐ฅ, ๐ฆ, ๐ง, and ๐ฃ are terminal strings.Jim Anderson (modified by Nathan Otterness) 62
This is due to the grammar being in CNFโwe can
only produce ๐ symbols in ๐ derivation steps.
Detecting if a CFL is Finite or Infinite
โOnly ifโ proof, continued:
From:
๐ผ๐+1 ึโ๐ค
๐ฝ๐+1 ึโ๐ฅ
๐ ึโ๐ฆ๐ด0๐ง
๐ด0 ึโ๐ฃ
we have:
๐ ึโ๐ฆ๐ด0๐ฅ ึ
โ๐ฆ๐ค๐ด0๐ฅ๐ง ึ
โ๐ฆ๐ค2๐ด0๐ฅ
2๐ง ึโโฆึ
โ๐ฆ๐ค๐๐ด0๐ฅ
๐๐ง ึโ๐ฆ๐ค๐๐ฃ๐ฅ๐๐ง.
Therefore, ๐ฟ is infinite.
Jim Anderson (modified by Nathan Otterness) 63
Detecting if a CFL is Finite or Infinite
Claim (again): ๐ฟ is finite if and only if ๐, ๐ธ has no cycles.
โIfโ: Suppose ๐, ๐ธ has no cycles.
Definition: The rank of ๐ด โ ๐ is the longest path beginning from ๐ด. If a graph has no cycles, then all ranks are finite.
Claim: If ๐ด has a rank ๐, then ๐ด derives no terminal string of length exceeding 2๐.
โThis can be proven by induction on ๐, similarly to some claims prior to the Pumping Lemma.
Since ๐, ๐ธ has no cycles, ๐ has a finite rank. Therefore, the longest string derivable from ๐ has a finite length, so ๐ฟ must be finite.
Jim Anderson (modified by Nathan Otterness) 64
Membership: CYK Algorithm
We want to know if a string ๐ฅ is in ๐ฟ.
Let ๐ฟ โ ํ = ๐ฟ ๐บ for a CFG ๐บ in CNF.
Let ๐ฅ = ๐.
Let ๐ฅ๐๐ be a substring of ๐ฅ of length ๐ beginning at
position ๐.
Inductively determine all variables ๐ด such that
๐ด ึโ๐ฅ๐๐.
๐ฅ โ ๐ฟ if and only if ๐ ึโ๐ฅ1๐.
Jim Anderson (modified by Nathan Otterness) 65
Membership: CYK Algorithm
Base case: ๐ = 1. ๐ด ึโ๐ฅ๐๐ if and only if ๐ด โ ๐ฅ๐๐.
Inductive step: ๐ > 1. ๐ด ึโ๐ฅ๐๐ if and only if there
exists ๐ด โ ๐ต๐ถ and ๐, 1 โค ๐ < ๐, such that ๐ต ึโ๐ฅ๐๐ and
๐ถ ึโ๐ฅ๐+๐ ๐โ๐.
Jim Anderson (modified by Nathan Otterness) 66
๐ = 1, so ๐ฅ๐๐ = ๐ฅ๐1. ๐ฅ๐1has a length of 1, so
itโs a terminal symbol.
Membership: CYK Algorithm
Jim Anderson (modified by Nathan Otterness) 67
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
The time complexity is ๐ ๐3 .
Pseudocode for determining the sets of variables, ๐๐๐,
that can produce the substring ๐ฅ๐๐:
CYK Algorithm: Example
Jim Anderson (modified by Nathan Otterness) 68
๐ โ ๐ด๐ต | ๐ต๐ถ๐ด โ ๐ต๐ด | ๐๐ต โ ๐ถ๐ถ | ๐๐ถ โ ๐ด๐ต | ๐๐ฅ = ๐๐๐๐๐ (๐ = 5)
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
1 2 3 4 512345
i โ
j
b a a b a
CYK Algorithm: Example
Jim Anderson (modified by Nathan Otterness) 69
๐ โ ๐ด๐ต | ๐ต๐ถ๐ด โ ๐ต๐ด | ๐๐ต โ ๐ถ๐ถ | ๐๐ถ โ ๐ด๐ต | ๐๐ฅ = ๐๐๐๐๐ (๐ = 5)
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
1 2 3 4 51 B A, C A, C B A,C2345
i โ
j
b a a b a
CYK Algorithm: Example
Jim Anderson (modified by Nathan Otterness) 70
๐ โ ๐ด๐ต | ๐ต๐ถ๐ด โ ๐ต๐ด | ๐๐ต โ ๐ถ๐ถ | ๐๐ถ โ ๐ด๐ต | ๐๐ฅ = ๐๐๐๐๐ (๐ = 5)
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
1 2 3 4 51 B A, C A, C B A,C2 S, A B S, C S, A345
i โ
j
b a a b a
CYK Algorithm: Example
Jim Anderson (modified by Nathan Otterness) 71
๐ โ ๐ด๐ต | ๐ต๐ถ๐ด โ ๐ต๐ด | ๐๐ต โ ๐ถ๐ถ | ๐๐ถ โ ๐ด๐ต | ๐๐ฅ = ๐๐๐๐๐ (๐ = 5)
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
1 2 3 4 51 B A, C A, C B A,C2 S, A B S, C S, A3 B B45
i โ
j
b a a b a
CYK Algorithm: Example
Jim Anderson (modified by Nathan Otterness) 72
๐ โ ๐ด๐ต | ๐ต๐ถ๐ด โ ๐ต๐ด | ๐๐ต โ ๐ถ๐ถ | ๐๐ถ โ ๐ด๐ต | ๐๐ฅ = ๐๐๐๐๐ (๐ = 5)
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
1 2 3 4 51 B A, C A, C B A,C2 S, A B S, C S, A3 B B4 S, A, C5
i โ
j
b a a b a
CYK Algorithm: Example
Jim Anderson (modified by Nathan Otterness) 73
๐ โ ๐ด๐ต | ๐ต๐ถ๐ด โ ๐ต๐ด | ๐๐ต โ ๐ถ๐ถ | ๐๐ถ โ ๐ด๐ต | ๐๐ฅ = ๐๐๐๐๐ (๐ = 5)
for i := 1 to n:Vi1 := {A | A โ xi1 }
for j := 2 to n:for i := 1 to n โ j + 1:
Vij := ;for k := 1 to j โ 1:
Vij := Vij {A | A โ BC, B Vik, C Vi+k j-k}
1 2 3 4 51 B A, C A, C B A,C2 S, A B S, C S, A3 B B4 S, A, C5 S, A, C
i โ
j
b a a b a
๐ โ ๐1๐, so ๐ฅ โ ๐ฟ.