Вы находитесь на странице: 1из 3

CMPS 364 (Theory of Computation) Solution to Exercise 1.

4 (page 11) in Hopcroft and Ullman


First we establish some notation: For a string x and a symbol a , dene #a (x) to be the number of occurrences of a in x. As this problem deals with balanced strings of parentheses, let us also dene (x) = #( (x) #) (x) (i.e., the dierence between the numbers of left and right parentheses, respectively, in x). Now we reword the problem. The strings of balanced parentheses can be dened in at least two ways: Denition 1: A string w { (, ) } is balanced if and only if both (a) (w) = 0 and (b) For every prex w of w, (w ) 0. Denition 2: A string w { (, ) } is balanced if and only if either (a) w = , or (b) w = (u), where u is balanced, or (c) w = uv, where each of u and v is balanced and nonempty. Let Li be the set {w | w satises Denition i}, i = 1, 2. Prove by induction on the length of a string that L1 = L2 . Solution: We begin by stating a few properties of the functions # and . Lemma 1: Let y, z and a . Then #a (yz) = #a (y) + #a (z). Proof: Because # was dened informally, we cannot prove this. But it is intuitively obvious. If we had a more rigorous denition of #, we could prove Lemma 1. Here is such a denition: For all a, b and x , #a ( ) = 0 and #a (xb) = #a (x) if a = b #a (x) + 1 if a = b

The reader is invited to use this denition to provide a proof of Lemma 1. Lemma 2: For all y and z, (yz) = (y) + (z). Proof: Details are left to the reader. (Use Lemma 1, the denition of , and a little algebra.) QED Now we state and prove several lemmas regarding L1 . Lemma 3: If y L1 and yz L1 , then z L1 . Proof: Assume that y, yz L1 . By denition of L1 , (y) = 0 = (yz), from which it follows by Lemma 2 (and the slightest bit of algebra) that (z) = 0. Hence, z satises (a) of Denition 1. Suppose, contrary to what we wish to show, that some proper prex z of z satises (z ) < 0. But then yz is a proper prex of yz and, by Lemma 2, (yz ) < 0, contradicting the assumption that yz L1 . Hence, no such prex of z exists, which means that z satises (b) of Denition 1. 1

Having shown that z satises both (a) and (b) of Denition 1, we conclude z L1 . QED Lemma 4: Let x = and x L1 . Then x = (z) for some z. That is, the rst symbol of x is ( and the last symbol is ). Proof: If the rst symbol of x were ), it would fail to satisfy (a) of Denition 1. Hence, x begins with (. If the last symbol of x were (, take x to be the prex of x of length |x|1. Then x = x (, which, by Lemma 2 (and our assumption that (x) = 0) implies that (x ) = 1, contradicting our assumption that x satises (b) of Denition 1. Hence, x ends with ). QED Lemma 5: Let x = and x L1 . If no nonempty proper prex of x is in L1 , then x = (z) for some z L1 . Proof: From Lemma 4 we know that x = (z) for some z. It remains to show that z L1 . From (x) = 0 and Lemma 2 we get (z) = 0, which means that z satises (a) of Denition 1. Now suppose, contrary to what is to be proved, that z fails to satisfy (b) of Denition 1, which is to say that (z ) < 0 for some prex z of z. Taking z to be the shortest such prex, we have (z ) = 1, from which we get that (z L1 , contradicting our assumption that no nonempty proper prex of x is in L1 . QED Lemma 6: Let x L1 . Then (x) L1 . Proof: From (x) = 0 and Lemma 2, it follows that ((x)) = 0, so (x) satises (a) of Denition 1. Suppose, contrary to what is to be proved, that (x) fails to satisfy (b) of Denition 2, which is to say that (u) < 0 for some prex u of (x). Clearly, it must be that u = (x for some prex x of x. But 0 > ((x ) = 1 + (x ) implies that x fails to satisfy (b) of Denition 1, contradicting our assumption. QED Lemma 7: Let u, v L1 . Then uv L1 . Proof: From (u) = 0 = (v) and Lemma 2, we have (uv) = 0, so uv satises (a) of Denition 1. Let w be any prex of uv. Then either w is a prex of u, in which case (w) 0 by virtue of u satisfying (b) of Denition 1, or else w = uv , where v is a prex of v, in which case (w) = (uv ) = (u) + (v ) = (v ) 0 the four steps of which are justied by w = uv , Lemma 2, (u) = 0 (by u satisfying (a) of Denition 1), and the fact that v satises (b) of Denition 1. QED Now we prove the main result: Theorem 1: L1 L2 . Proof: Let x L1 . We show by induction on |x| that x L2 . Basis: |x| = 0. We have x = , which, by (a) of Denition 2, is in L2 . Induction Step: |x| > 0. As an induction hypothesis, assume that L1 {z | |z| < |x|} L2 . (That is, assume that every member of L1 shorter than x is in L2 .)

Let x = uv, where u is the shortest nonempty prex of x that is in L1 . If v = , by Lemma 5 we have that x = (z) for some z L1 . By the induction hypothesis, z L2 . But then x L2 by (b) of Denition 2. If, on the other hand, v = , we have u L1 and uv L1 , from which it follows, by Lemma 3, that v L1 . As both u and v are shorter than x, the induction hypothesis tells us that both u and v are in L2 . But then x = uv is in L2 by (c) of Denition 2. QED Theorem 2: L2 L1 Proof: Let x L2 . We show by induction on |x| that x L1 . Basis: |x| = 0, which is to say x = . Clearly, so L1 . satises both (a) and (b) of Denition 1, and

Induction Step: |x| > 0. As an induction hypothesis, assume that L2 {z | |z| < |x|} L1 . (That is, assume that every member of L2 shorter than x is in L1 .) For x to be in L2 is to say that x satises either (b) or (c) of Denition 2. In the former case, we have x = (z) for some z L2 . By the induction hypothesis, z L1 . Then by Lemma 6, x L1 . In the latter case, we have x = yz for some nonempty y, z L2 . As each of y and z is shorter than x, the induction hypothesis says that both y and z are in L1 . By Lemma 7, yz L1 . QED From Theorems 1 and 2 it follows that L1 = L2 , as was to be proved.

Вам также может понравиться