Homework 1 – Solutions

CS138: Automata and Formal Languages
Lecturer: Stefano Tessaro
UC Santa Barbara
Fall 2014
Homework 1 – Solutions
Task 1 – The Power Set
(7 points)
Recall that S is a subset of S 0 , denoted S ⊆ S 0 , if for all x ∈ S we also have x ∈ S 0 . The
power set of a finite set S is 2S = {S 0 : S 0 ⊆ S}, i.e., 2S contains all subsets of S.
a) Compute the following power sets:
- 2{a,b}
{a,b}
- 2∅
- 22
¨¨¨¨¨¨
{a,b}
Clearly, 2{a,b} = {∅, {a}, {b}, {a, b}}. As for 22
, we now have to compute
all subsets of the above. There is one 0-element subset, namely ∅, and one 4element subset, namely {∅, {a}, {b}, {a, b}}. There are four 1-element subsets,
namely
{∅} , {{a}} , {{b}} , {{a, b}} ,
and four 3-element subsets, namely
{{a}, {b}, {a, b}} , {∅, {b}, {a, b}} , {∅, {a}, {a, b}} , {∅, {a}, {b}} .
Finally, we need to find the 2-element subsets, and there are 42 = 6 of them,
namely,
{∅, {a}} , {∅, {b}} , {∅, {a, b}} , {{a}, {b}} , {{a}, {a, b}} , {{b}, {a, b}} .
Therefore, we can put all of these 1 + 1 + 4 + 4 + 6 = 16 subsets together, and
the power set is
{a,b}
22
= {∅, {∅}, {{a}}, {{b}}, {{a, b}}, {∅, {a}}, {∅, {b}}, {∅, {a, b}},
{{a}, {b}}, {{a}, {a, b}}, {{b}, {a, b}}, {{a}, {b}, {a, b}},
{∅, {b}, {a, b}}, {∅, {a}, {a, b}}, {∅, {a}, {b}}, {∅, {a}, {b}, {a, b}}} .
Finally, for 2∅ , note that there exists only one set S 0 such that S 0 ⊆ ∅, namely
∅ itself. Thus, 2∅ = {∅}.
¨¨¨¨¨¨
b) Prove by induction on the size of S that 2S = 2|S| for any finite set S.
Clearly indicate the induction basis and the induction step.
1
¨¨¨¨¨¨
We consider the statement Πn that “For all sets S of size |S| = n, 2S = 2|S| =
2n ”. Proving the above formal is equivalent to proving that Πn is true for all
n ≥ 0, which we now do by induction.
Induction basis: The statement Π0 is true: The only
∅set
of size0 n = 0 is the
∅
empty set, and 2 = {∅} as shown above, and thus 2 = 1 = 2 .
Induction step: Assume that the statement Πn is true for some arbitrary
n ≥ 0. (This is our induction hypothesis.) Then, let S be a set of size |S| = n+1,
and fix an arbitrary element s∗ ∈ S. Then, we can clearly partition subsets of
S depending on whether they contain s∗ or not, i.e.,
2S = {S 0 : S 0 ⊆ S} = {S 0 : S 0 ⊆ S, s∗ ∈ S 0 } ∪ {S 0 : S 0 ⊆ S, s∗ ∈
/ S 0} .
Denote T = S − {s∗ }, i.e., the set T without s∗ , and note that T contains
|T | = n elements. In particular, the set {S 0 : S 0 ⊆ S, s∗ ∈
/ S 0 } is exactly 2T ,
and contains (by the induction hypothesis) 2|T | = 2n elements. Moreover,
every set S 0 in {S 0 : S 0 ⊆ S, s∗ ∈ S 0 } can be seen as T 0 ∪ {s∗ } for some set
T 0 ⊆ T . Therefore,
{S 0 : S 0 ⊆ S, s∗ ∈ S 0 } = {T 0 ∪ {s∗ } : T 0 ⊆ T }
and thus this set contains as many elements as 2T , namely (again by the
induction hypothesis) 2n .
Therefore, the size of 2S is exactly twice 2n , i.e., 2n+1 , which concludes the
proof.
¨¨¨¨¨¨
Task 2 – Strings and Languages
(5 points)
a) Let u = 01011 and v = 101 be strings. Compute:
(i) uv
(ii) u3 v (iii) u(v R )2
¨¨¨¨¨¨
(i) uv = (01011)(101) = 01011101.
(ii) u3 v = (01011)3 (101) = 010110101101011101.
(iii) u(v R )2 = 01011(101R )2 = 01011(101)2 = 01011101101.
¨¨¨¨¨¨
b) Let Σ be an alphabet with k = |Σ| elements, and let ` ≥ 0. How many strings u ∈ Σ∗
are there such that |u| = `?
¨¨¨¨¨¨
2
Recall that a string u ∈ Σ∗ of length ` is a sequence u = a1 a2 · · · a` of symbols a1 , a2 , . . . , a` ∈
Σ. As Σ contains k characters, we have k choices for a1 , k choices for a2 , and so on, and
thus overall k ` possible ways of building u.
¨¨¨¨¨¨
Task 3 – Properties of Reversion
(6 points)
We consider a formal recursive definition of the reversion operation for strings in Σ∗ .
Namely, the reversion operator R is the operation such that
- λR = λ
- (ua)R = auR and (au)R = uR a for all u ∈ Σ∗ , a ∈ Σ.
a) Apply the recursive formula step-by-step to compute (0101)R .
Describe every single step in detail.
¨¨¨¨¨¨
We compute, using repeatedly (ua)R = auR and isolating the right-most
symbol of the string
(0101)R = 1(010)R = 10(01)R = 101(0R )
= 101((λ0)R ) = 1010λR = 1010λ = 1010 .
¨¨¨¨¨¨
b) Prove by induction (over the string length) that (uR )R = u for all u ∈ Σ∗ .
¨¨¨¨¨¨
Define Πn to be the statement that “For all strings u ∈ Σ∗ of length |u| = n,
we have (uR )R = u.” Proving the above is equivalent to proving Πn for all
n ≥ 0, which we now show by induction.
Induction Basis. For n = 0, the only string u with |u| = 0 is the empty string
λ, and Π0 is hence true because
(λR )R = λR = λ ,
where we have used the fact that λR = λ in the recursive definition of reversal.
Induction Step. Assume that Πn is true for some arbitrary n ≥ 0. (This is
our induction hypothesis.) We are now going to prove that Πn+1 is also true.
In particular, let u ∈ Σ∗ be such that |u| = n + 1. Then, we can write u = wa,
where w ∈ Σ∗ , a ∈ Σ, and |w| = n.
3
We can then compute
(uR )R = (wa)R
R
= (awR )R = (wR )R a
where we have used the two versions of the recursive rule in the definition of
reversal given above (first with a on the right, second with a on the left). But
then, by the induction hypothesis that Πn holds, (wR )R = w, since |w| = n,
and thus (uR )R = wa = u, as we wanted to show.
¨¨¨¨¨¨
Task 4 – Palindromes
(6 points)
We define the languages L, L0 ⊆ Σ∗ such that
L = u ∈ Σ∗ : uR = u , L0 = uv ∈ Σ∗ : u = v R .
a) Prove that L0 ⊆ L.
Hint: Use that (uR )R = u and (uv)R = v R uR .
¨¨¨¨¨¨
0
To prove L ⊆ L, we need to prove that for all x ∈ L0 , we also have x ∈ L.
Concretely, let x ∈ L0 . Then, we can write x as x = uv, where u = v R . We
need to prove that x ∈ L, i.e., that xR = x. Indeed,
xR = (uv)R = (v R v)R = v R (v R )R = v R v = x ,
where we have used the two suggested properties of reversion given in the
hint.
¨¨¨¨¨¨
b) Prove that L0 is a proper subset of L whenever |Σ| ≥ 2.
¨¨¨¨¨¨
Note: In fact, the restriction |Σ| ≥ 2 was not necessary, as the statement holds
even for unary alphabets, i.e., Σ’s such that |Σ| = 1. (This of course does not
make the question wrong, but may have been confusing.)
Let Σ be an arbitrary non-empty alphabet, and fix some a ∈ Σ. Then, consider the string w = a, consisting only of the character a. Clearly, wR = aR =
a = w. Thus, w ∈ L. However, there is no way to write w = uv where u = v R ,
and thus w ∈
/ L. In particular, for every x ∈ L0 , the length of x must be even,
since u = v R implies |u| = |v|, and |x| = 2 |u|. In contrast, |w| = 1 is odd.
¨¨¨¨¨¨
4
Task 5 – Concatenation and Closures
(6 points)
Let L ⊆ Σ∗ be a language. Recall that for any k ≥ 1,
Lk = {w1 w2 . . . wk : w1 , . . . , wk ∈ L} ,
and L0 = {λ}. Moreover, the star closure of L is
L∗ = L0 ∪ L1 ∪ L2 ∪ . . . .
a) Let L = {00, 11}. Compute L2 , L4 , and L∗ .
¨¨¨¨¨¨
L2 = {0000, 0011, 1100, 1111}
L4 = {00000000, 00000011, 00001100, 00001111, 00110000, 00110011, 00111100,
00111111, 11000000, 11000011, 11001100, 11001111, 11110000,
11110011, 11111100, 11111111}
The language L∗ contains all strings consisting of consecutive pairs of 0’s
and consecutive pairs of 1’s, including the empty string λ. Formally, we can
define a “doubling” function d : {0, 1}∗ → {0, 1}∗ which takes as input a
string u = a1 a2 . . . an , and returns d(u) = a1 a1 a2 a2 . . . an an , i.e., it doubles
every symbol in the string, and moreover, we define d(λ) = λ. Then,
L∗ = {d(u) : u ∈ {0, 1}∗ } .
¨¨¨¨¨¨
Now consider the following languages Li ⊆ Σ∗ , defined for all integers i ≥ 0,
Li = u : |u| = 2i ∪ {λ} ,
i.e., Li contains all strings of length 2i , as well as the empty string.
b) Give L0 , L1 , and L0 L1 for Σ = {0, 1}.
¨¨¨¨¨¨
The language L0 contains all strings of length 20 = 1, and the empty string,
i.e., L0 = {λ, 0, 1}. Similarly, L1 = {λ, 00, 01, 10, 11}. Then, note that L0 L1
contains all bit-strings of length at most 3 (including λ), as every such string
can be written as a concatenation of strings in L1 and L2 , i.e.,
L1 L2 = {u ∈ {0, 1}∗ : |u| ≤ 3} .
¨¨¨¨¨¨
5
c) Describe the language L0 L1 . . . Lk .
¨¨¨¨¨¨
We show that L0 L1 . . . Lk contains all strings of length at most 2k+1 − 1, including λ, i.e.,
L0 L1 . . . Lk = u ∈ Σ∗ : |u| ≤ 2k+1 − 1 .
First note that all strings in L0 L1 . . . Lk have at most length 2k+1 − 1, as for a
string u = w0 w1 . . . wk , where wi ∈ Li , we have
|u| =
k
X
k
X
|wi | ≤
i=0
2i = 2k+1 − 1 .
i=0
(Note that we have an inequality because wi could be λ, in which case |wi | =
0.) Therefore, L0 L1 . . . Lk cannot contain more than the set of strings of length
at most 2k+1 − 1.
Now, we need to show that all such strings of length at most 2k+1 − 1 are in
L0 L1 · · · Lk . To see this, take any such a string u with |u| ≤ 2k+1 − 1. Now, we
can consider the binary expansion of the length of |u|, i.e.,
|u| =
k
X
bi 2i ,
i=0
where bi is either 0 or 1. Then we can uniquely write u as u = w0 w1 . . . wk
where wi = λ ∈ Li if bi = 0, whereas |wi | = 2i (and thus wi ∈ Li ) if bi = 1. (To
write u like this, one simply starts from the left of u, and whenever bi = 1,
we just set wi to be the next 2i bits of u, whereas if bi = 0, we do not look at u
and set wi = λ.)
¨¨¨¨¨¨
6