sol1 by wuzhengqin


									Homework set 1 with solutions

        February 22, 2010

1     Some DFAs (C)
Give DFAs that recognize the following languages, where the alphabet is {a, b, c}:

    1. L1 consists of all strings that start with ab.
    2. L2 consists of all strings that contain cca.
    3. L3 consists of all strings that contain at least 3 b’s (not necessarily con-
    4. L4 consists of all strings that contain exactly 3 b’s (not necessarily con-

                         Figure 1: Solutions for problem 1

2     Counting with DFAs (C)
The alphabet is {a, b, c}. Give a DFA that recognizes the language L which
consists of the strings where the number of b’s is a multiple of 3.

                         Figure 2: Solution for problem 2

                       Figure 3: Solution for problem 3

                         Figure 4: NFA for problem 4

3     Comments in HTML (C)
In HTML, comments are delimited by <!-- and -->. Suppose for simplicity
that the only characters that appear in an HTML file are Σ = {a, b} and the
characters needed to delimit a comment. Give a DFA that recognizes a string
which consists of a single properly delimited comment and nothing else. In other
words, your DFA must recognize strings that begin with <!--, end with -->
and do not contain another of these two patterns in the middle (i.e. no nested

4     Understanding NFAs (C)
Consider that NFA depicted in Figure 4. The alphabet is {a, b}

    • Give two examples of strings that the NFA accepts and two examples of
      strings that it rejects.

    • Informally describe the language that the NFA accepts.

    Answer: The NFA accepts abba and bbaab. It rejects baa and bbba. Generally,
it accepts strings that begin with an even number of b’s.

                         Figure 5: NFA for problem 5

Figure 6: DFA for problem 5. The states in the bottome right also have c
transitions to the E state, but they are not depicted to ease presentation.

5     NFA to DFA (B)
For the NFA of Figure 5 and the alphabet {a, b, c}

    1. Give an informal description of the language the NFA accepts.

    2. Give an equivalent DFA.

    Answer: This NFA accepts strings that begin with a non-empty sequence of
c’s. This is followed by a sequence of a’s and b’s such that we either have an
odd number of a’s or a number of b’s that is a multiple of 3. The equivalent
DFA can be seen in Figure 6.

6     Alarm system (B)
You decide to protect your property with a sophisticated alarm system. In order
to enter, someone has to punch in a secret 4-digit number. For simplicity we will
assume that the only allowed digits are {1, 2, 3, 4} and that the secret number
is 4321 (not a very safe choice!).

                           Figure 7: DFA for problem 6

   Your goal for this problem is to design a DFA that works in the same way as
the alarm system (where accepting an input is equivalent to opening the door).

    • The input, in the alphabet {1, 2, 3, 4}, tells you the digits that were pressed
      in the order that this happened.
    • Obviously, your DFA must accept the string 4321, even if more buttons
      are pressed afterwards.
    • If a wrong digit is entered, the alarm system should restart, i.e. go back
      to expecting 4321.
    • If the system restarts three times, the door should lock permanently.

    For example:

    • 4321234 should be accepted.
    • 4334321, 334321, 43443224321 should be accepted.
    • 434321 should not be accepted (the second 4 causes a restart, so it’s not
      considered part of the secret) but 4344321 should be accepted.
    • 2224321 should not be accepted (three restarts happen).

7     Another DFA (B)
Our alphabet is {a, b}. Construct a DFA that accepts all strings that contain
an even number of a’s, an odd number of b’s and do not contain the substring
ba. Your DFA should have 5 states.

                            Figure 8: DFA for problem 7

8     Reversing (A)
Let wR denote the reverse of a string, for example abbabR = babba. If L is a
language, we denote by LR the language LR = {wR | w ∈ L}. Show that the
class of regular languages is closed under this operation, that is, show that if L
is regular then so is LR . (Hint: take the DFA that decides L and make it work
“backwards”. How can you do this?)
    Answer: Following the hint we take the DFA that decides L. Now, informally
we reverse the direction of every arc and a string will be accepted if, when we
start from a final state we can reach the initial state.
    More formally, suppose we have a DFA D = (Q, Σ, δ, q0 , F ) that decides L.
Construct the NFA N = (Q ∪ {q ′ }, Σ, δ ′ , q ′ , {q0 }), where we have added a new
state q ′ to Q. The only accepting state is the previously initial state and q ′ is
the new initial state. We let δ ′ (q ′ , ǫ) = F and for all qi , qj ∈ Q, x ∈ Σ such that
δ(qi , x) = qj we set that qi ∈ δ ′ (qj , x) (this is the formal way of saying that we
reversed all arcs).
    The NFA we have constructed can of course be made deterministic. So the
only thing that we still have to argue about is that L accepts a string w iff N
accepts it. Let q0 , q1 , . . . , ql−1 , ql be the sequence of states that D goes through
when deciding w. If D accepts, then ql ∈ F . So, in N we can use an ǫ transition
to ql from q ′ and then follow the reverse path to q0 . Similarly, if there exists a
path from q ′ to q0 in N for a given string, then its first state is a state in F , so
D will take the reverse path starting from q0 and end up in an accepting state.

9     Unary languages (A)
For this problem our alphabet consists only of a single symbol {a} and the
languages over this alphabet are called unary. If w is a string, we denote by |w|
its length.
    We say that an infinite set of natural numbers S is semi-linear if there
exist two natural numbers p, n0 and two finite sets of natural numbers F, S ′
such that S = S ′ ∪ {kp + m | k ∈ N, k ≥ n0 , m ∈ F }. For example the
set {2, 3, 7, 8, 12, 13, 17, 18, . . .} is semi-linear (why?). The following set is also
semi-linear: {1, 2, 4, 7, 8, 12, 13, 17, 18, 22, 23, . . .} (why?)

                               Figure 9: Problem 9

    Prove that if a unary language L is regular then the set S = {|w| | w ∈ L}
is semi-linear. (Hint: what does a DFA for such a language look like?)
    Answer: The graph that corresponds to a unary DFA is actually very simple:
every vertex has exactly one outgoing arc. Since we are only interested in
states reachable from the initial state, our graph can only look like the graph
of Figure 9. First we (may) have a path and then we (may) have a cycle. The
length of the cycle gives us p and the length of the path gives us n0 · p. The
positions of final states in the path give us S ′ , while the positions of final states
in the cycle give us F .
    For example, suppose that the diagram for our DFA is a cycle of length 5
(i.e. the initial state is part of the cycle and the path has length 0). Then the
set of accepted string is semi-linear with S ′ = ∅, p = 5, k0 = 0 and the set F is
the set of distances of accepting states from the initial state.

10     Division (A)
It is not hard to see how we can decide whether the length of a string is a
multiple of a fixed number using a DFA. What if the string is actually a number
encoded in binary?
    Construct a DFA which accepts all binary numbers which are multiples of
5. For example 101, 1010, 1111, 10100 and so on should be accepted. (Hint: to
count the length of a string, we have to remember how many symbols we have
seen so far, mod 5. Now we should do the same but with binary numbers)
    Answer: Intuitively, regular languages are languages we can decide while
keeping track of a limited amount of information. For example, to decide if the
length of a string is a multiple of 5 we keep track of the remainder of the length
of the string read so far if we divide it by 5. We use the same idea with binary
numbers, only that now, every time we read a new character the remainder mod
5 changes in a more complicated way.
    For example, suppose that we have read so far the string 100 (i.e. the

                       Figure 10: DFA for problem 10

number 4). The remainder mod 5 is 4. What happens if we read a 0 next?
Since the number we have read so far doubles when we append a 0 to its binary
representation, its remainder mod 5 will also double and become 8 mod 5 or
equivalently 3. What if we read a 1? The number will be double plus one and
the same happens to the remainder, which therefore remains 4.
   The full DFA can be seen in Figure 10


To top