EQUAL language - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

EQUAL language

Description:

abba. Strings in {a,b}* not in EQUAL include. aaa. bbb. aab. ababa. 4. Designing a CFG for EQUAL ... Now consider a longer string x in EQUAL. Since x has ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 29
Provided by: erict9
Learn more at: http://www.cse.msu.edu
Category:
Tags: equal | abba | language

less

Transcript and Presenter's Notes

Title: EQUAL language


1
Lecture 28
  • EQUAL language
  • Designing a CFG
  • Proving the CFG is correct

2
EQUAL language
  • Designing a CFG

3
EQUAL
  • EQUAL is the set of strings with an equal number
    of as and bs
  • Strings in EQUAL include
  • aabbab
  • bbbaaa
  • abba
  • Strings in a,b not in EQUAL include
  • aaa
  • bbb
  • aab
  • ababa

4
Designing a CFG for EQUAL
  • Think recursively
  • Base Case
  • What is the shortest possible string in EQUAL?
  • Answer l
  • Production Rule
  • S -- l

5
Recursive Case
  • Recursive Case
  • Now consider a longer string x in EQUAL
  • Since x has length 0, x must have a first
    character
  • This must be a or b
  • Two possibilities for what x looks like
  • x ay
  • What must be true about relative number of as
    and bs in y?
  • y must have one extra b
  • x bz
  • What must be true about relative number of as
    and bs in z?
  • z must have one extra a

6
Case 1 xay
  • x ay where y has one extra b
  • What must y look like?
  • Some examples
  • b
  • babba
  • aabbbab
  • aaabbbb
  • Is there a general pattern?
  • In particular, are there substrings in y which
    belong to EQUAL?

7
Decomposing y
  • y has one extra b
  • Possible examples
  • b, babba, aabbbab, aaabbbb
  • Common pattern
  • In each string, there is some prefix with exactly
    one more b than a
  • That is, y ubv where
  • ub is the prefix with an extra b
  • u and v both have an equal number of as and bs
  • Decompose the 4 strings above into u, b, v
  • lbl, aabbbab, lbabba, aaabbbbl

8
Implication
  • Case 1 xay
  • y has one extra b
  • Case 1 refined xaubv
  • u, v belong to EQUAL
  • Production rule for this case
  • S -- aSbS
  • The first S can derive the string u
  • The second S can derive the string v

9
Case 2 xbz
  • Case 2 xbz
  • z has one extra a
  • Case 2 refined xbuav
  • u, v belong to EQUAL
  • Production rule for this case
  • S -- bSaS
  • The first S can derive the string u
  • The second S can derive the string v

10
Final Grammar
  • EG (V, S, S, P)
  • V S
  • S a,b
  • S S
  • P S -- l aSbS bSaS

11
EQUAL language
  • Proving CFG is correct

12
Is our grammar correct?
  • How do we prove our grammar is correct?
  • Informal
  • Test some strings
  • Review logic behind program (CFG) design
  • Formal
  • First, show every string derived by EG belongs to
    EQUAL
  • That is, show L(EG) is a subset of EQUAL
  • Second, show every string in EQUAL can be derived
    by EG
  • That is, show EQUAL is a subset of L(EG)
  • Both proofs will be inductive proofs
  • Inductive proofs and recursive algorithms go well
    together

13
L(EG) subset of EQUAL
  • Let x be an arbitrary string in L(EG)
  • What does this mean?
  • S EG x
  • Follows from definition of x in L(EG)
  • We will prove the following
  • If S 1EG x, then x is in EQUAL
  • If S 2EG x, then x is in EQUAL
  • If S 3EG x, then x is in EQUAL
  • If S 4EG x, then x is in EQUAL
  • ...

14
L(EG) subset of EQUAL
  • Statement to be proven
  • For all n 1, if S nEG x, then x is in EQUAL
  • Prove this by induction on n
  • Base Case
  • n 1
  • The only string x such that S 1EG x is the
    string l
  • Follows from inspection of EG
  • The string l belongs to EQUAL

15
Inductive Case
  • Inductive Hypothesis
  • For 1 jEG x, then x is in
    EQUAL
  • Note, this is a strong induction hypothesis
  • Traditional inductive hypothesis would take form
  • For some n 1, if S nEG x, then x is in
    EQUAL
  • The difference is we assume the basic hypothesis
    for all integers between 1 and n, not just n
  • Statement to be Proven in Inductive Case
  • If S n1EG x, then x is in EQUAL

16
Proving Inductive Case
  • If S n1EG x, then x is in EQUAL
  • Let x be an arbitrary string such that S n1EG
    x
  • There are three possible first derivation steps
  • Case 1 S l nEG x
  • Case 2 S aSbS nEG x
  • Case 3 S bSaS nEG x
  • These 3 cases follow from looking at grammar EG
  • Case 1 is not possible
  • n was assumed to be at least 1 which means x is
    derived in at least 2 steps

17
Case 2 S aSbS nEG x
  • This means x has the form aubv where
  • S
  • S
  • This follows because in n steps, we go from aSbS
    to x
  • Thus, the first S can take at most n-1 steps to
    generate u
  • Likewise for the second S generating v
  • Apply the inductive hypothesis
  • u and v belong to EQUAL
  • Note we needed the strong inductive hypothesis
  • Conclude x belongs to EQUAL
  • x aubv where u and v belong to EQUAL
  • Clearly the number of as in x equals the number
    of bs in x

18
Case 3 S bSaS nEG x
  • This means x has the form buav where
  • S
  • S
  • This follows because in n steps, we go from bSaS
    to x
  • Thus, the first S can take at most n-1 steps to
    generate u
  • Likewise for the second S generating v
  • Apply the inductive hypothesis
  • u and v belong to EQUAL
  • Note we needed the strong inductive hypothesis
  • Conclude x belongs to EQUAL
  • x buav where u and v belong to EQUAL
  • Clearly the number of as in x equals the number
    of bs in x

19
L(EG) subset of EQUAL
  • Wrapping up inductive case
  • In all possible derivations of x, we have shown
    that x belongs to EQUAL
  • Thus, we have proven the inductive case
  • Conclusion
  • By the principle of mathematical induction, we
    have shown that L(EG) is a subset of EQUAL

20
EQUAL subset of L(EG)
  • Let x be an arbitrary string in EQUAL
  • What does this mean?
  • x has an equal number of as and bs
  • Follows from definition of x in EQUAL
  • We will prove the following
  • If x 0 and x is in EQUAL, then x is in L(G)
  • If x 1 and x is in EQUAL, then x is in L(G)
  • If x 2 and x is in EQUAL, then x is in L(G)
  • If x 3 and x is in EQUAL, then x is in L(G)
  • ...

21
EQUAL subset of L(EG)
  • Statement to be proven
  • For all n 0, if x n and x is in EQUAL,
    then x is in L(EG)
  • Prove this by induction on n
  • Base Case
  • n 0
  • The only string x such that x0 and x is in
    EQUAL is the string l
  • Follows from definition of EQUAL
  • The string l belongs to L(EG)
  • Follows from production S -- l

22
Inductive Case
  • Inductive Hypothesis
  • For 0 then x is in L(EG)
  • Again, this is a strong induction hypothesis
  • Statement to be Proven in Inductive Case
  • If x n1 and x is in EQUAL, then x is in
    L(EG)
  • note n 0

23
Proving Inductive Case
  • If xn1 and x is in EQUAL, then x is in L(EG)
  • Let x be an arbitrary string such that xn1
    and x is in L(EG)
  • There are two possibilities for the first
    character in x
  • Case 1 first character in x is a
  • Case 2 first character in x is b
  • These 2 cases follow from looking at alphabet S
  • Case 1 x ay where y has one extra b
  • Case 2 x bz where z has one extra a

24
Case 1 x ay
  • This means x has the form aubv where
  • u is in EQUAL and has length
  • v is in EQUAL and has length
  • Proving this statement true
  • Consider all the prefixes of string y
  • length 0 l
  • length 1 y1
  • length 2 y1y2
  • length n y1y2 yn y

25
Case 1 x ay
  • Consider all the prefixes of string y
  • length 0 l
  • length 1 y1
  • length 2 y1y2
  • length n y1y2 yn y
  • The first prefix l has the same number of as as
    bs
  • The last prefix y has one extra b
  • The relative number of as and bs changes in the
    length i prefix differs by only one from the
    length i-1 prefix
  • Thus, there must be a first prefix t of y where t
    has one extra b
  • Furthermore, the last character of t must be b
  • Otherwise, t would not be the FIRST prefix of y
    with one extra b
  • Break t into u and b and let the remainder of y
    be v
  • The statement follows

26
Case 1 x aubv
  • x aubv
  • u is in EQUAL and has length
  • v is in EQUAL and has length
  • Apply the induction hypothesis
  • u and v belong to L(EG)
  • This means S EG u and S EG v.
  • Conclude x is in L(EG) by constructing a
    derivation
  • S aSbS EG aubS EG aubv
  • The first derivation step follows from production
    S -- aSbS
  • The second series of derivation steps follows
    from S EG u
  • The third series of derivation steps follows from
    S EG v

27
Case 2 x buav
  • x buav
  • u is in EQUAL and has length
  • v is in EQUAL and has length
  • Follows from similar proof as in case 1
  • Apply the induction hypothesis
  • u and v belong to L(EG)
  • This means S EG u and S EG v.
  • Conclude x is in L(EG) by constructing a
    derivation
  • S bSaS EG buaS EG buav
  • The first derivation step follows from production
    S -- bSaS
  • The second series of derivation steps follows
    from S EG u
  • The third series of derivation steps follows from
    S EG v

28
EQUAL subset of L(EG)
  • Wrapping up inductive case
  • For all possible first characters of x, we have
    shown that x belongs to L(EG)
  • Thus, we have proven the inductive case
  • Conclusion
  • By the principle of mathematical induction, we
    have shown that EQUAL is a subset of L(EG)
Write a Comment
User Comments (0)
About PowerShow.com