CS 482 Problem Set 2 Solutions
Dan FitzGerald, dpf7
Question 1:
Describe the set {0 n 10 2n 1∣n≥1}∗intersect 01{0 n 102n 1∣n≥1}∗0∗1 . Are there any strings with an odd number of blocks of zeros?
Solution: Since we are dealing with set intersection, an element is in the set iff it is in both sets being intersected. Let the set we are seeking to descibe be S, the set on the LHS of the intersection be named A, and the set on the RHS be B. We start off by noticing that B begins with 01. Therefore every element in the set S must being with 01. Now we look at A. Since we know we must start with 01, this tells us that n=1. This also requires that we add “001” to “01” to form “01001” to satisfy A's definition. Now we go back to B. We see that if we repeat the piece in the braces 0 times, the 0 outside the brace twice and then finish with 1, then we will satisfy both A and B and hence have an element in S(since it satisfies the definitions on each side of the intersection). Thus, we have 01001 in S. But, also notice that if we have 01001, then for B this could mean taking the 01 outside the braces, then letting n=2 to add on “00100001” to make 0100100001. Then we'd go back to A and see that we have one instance of the expression with n=1 and now half of the expression cocatenated with n=4. Thus, we'd have to add on 8 0's and a 1 to satisfy A. We could go back to B and either use the 0 and 1 outside the braced expression to the right to terminate this string, or we could use those last 8 0's appended to set n=8. Continuing in this pattern of going back and forth “interleaving” the elements of A and B, we see that this set can be described as “an even number blocks of zeros (at least 2) , each separated by a 1 and ending in 1, where, starting at 1, each successive block of zeros has twice as many zeros as the block of 0s preceeding it.”
By this description, no string can have an odd number of blocks of zeros
Question 2 (2.2.6(a))
Give a DFA for the set of all strings starting with 1 that are multiples of 5 in binary.
Solution: The DFA is on the following page. To build this DFA, you must realize what happens upon reading a new character in terms of multiplication. You should recognize that if you see a 0, then you are multiplying your current total by 2 (this is a left shift operation). If you see a 1, you are multiplying your current total and adding 1. To ensure divisibility by 5, we should operate mod 5 and accept s when s mod 5=0 when it is interpreted as a binary number (and begins with 1).
Also, one must take care to construct a separate states for start and 0 mod 5. The reason being, if they are the same states, then you would go to a trap state upon reading a 0. But what if you already read part of a string that started with 1 and was equivalent to 0 mod 5 (i.e. 101) and then read a 0? Your DFA would trap when it shouldn't. Thus, two states should be made (labeled S and 0) in the picture that prevent the aforementioned error from occurring.
0
1
1
3
1
0
1
1
S
0
0
1
trap
4
2
0
0
1
0,1
0
Question 3
This DFA accepts all strings with an odd number of ones. It tells us if we have seen an even number of ones (then we'll be in A) or an odd number of ones (we'll be in state B).
Proof by induction on the length of the input string s that deltahat(A,s) iff s has an even # of 1's
Base case: s is of length 0. Clearly, if s is the empty string, then it has an even number of ones (0). Thus, since we started in state A, delta(A,w)=A. Thus our base case is proven.
Inductive step: Assume the DFA behaves as describes for all strings strictly shorter than s. Prove DFA behaves for strings of length |s|. Then our string is of the form s=xa, where x is 0 or 1.
Let a=0. Thus, s has the same number of 1's as x. If they both have an even number, by the IH, deltahat(A,x)=A. By definition of the states from the table, delta(A,0)=A. If they both have an odd number of 1's, then by the IH, deltahat(A,x)=B and the transition table states that delta(B,0)=B. Therefore, delta(A,s)=A if and only if we have an even number of 1's in the string.
Let a=1. Thus, s has 1 more 1 than x. Assume that x had an even number of ones. Then, by the IH, deltahat(A,x)=A. By the transition table, delta(A,1)=B. Now assume x had an odd number of ones. By the IH, deltahat(A,x)=B. By the transition table, delta(B,1)=A. Therefore, delta(A,s)=A if and only if there are an even number of 1's in the string.
Q.e.d.
Question 4(2.3.1) Convert to a DFA this NFA:
0
1
>p
{p,q}
{p}
q
{r}
{r}
r
{s}
empty set
*s
{s}
{s}
Solution: Use the subset construction. For the states of the DFA, create the power set of the NFA states
0
{p}
1
{p,q}
{p}
{p,q}
{p,q,r}
{p,r}
{p,r}
{p,q,s}
{p}
*{p,s}
{p,q,s}
{p,s}
{p,q,r}
{p,q,r,s}
{p,r}
*{p,q,s}
{p,q,r,s}
{p,r,s}
*{p,r,s}
{p,q,s}
{p,s}
{p,q,r,s}
{p,r,s}
{q}
{r}
*{s}
{q.r}
*{q,s}
*{r,s}
*{q,r,s}
*{p,q,r,s}
empty set
A subset of the states of the NFA (i.e. a state in the DFA) will be a final state if the intersection of it with the final states in the NFA is nonempty (i.e. it contains s). Mark these are your final states. Now, to compute the transition function, look at all the states p in the NFA, see what states the NFA goes to from p when it sees a 0 or 1, and we take the union of all those states. (See pgs 6162). We can rename the states if we wish to convince ourselves that in the DFA is a single state and not a set of states.
Notice that there are unreachable states in the above table. They can be deleted. Here is a table with no unreachable states:
0
1
{p}
{p,q}
{p}
{p,q}
{p,q,r}
{p,r}
{p,r}
{p,q,s}
{p}
*{p,s}
{p,q,s}
{p,s}
{p,q,r}
{p,q,r,s}
{p,r}
*{p,q,s}
{p,q,r,s}
{p,r,s}
*{p,r,s}
{p,q,s}
{p,s}
*{p,q,r,s}
{p,q,r,s}
{p,r,s}
Question 5(2.3.4)
a) Set of strings over {0...9} s.t. the final digit has been seen before
Solution: See the figure. Nondeterministically guess what the final digit will be and transition accordingly. Not much else can be said in terms of wordsstudying the picture should give you the intuition.
b)Set of strings over {0...9} s.t. the final digit has not been seen before.
Solution: See the figure. This problem parallels part (a), but with different transitions out of the start stage. Once again, study the picture if you do not understand.
c)Set of strings of 0's and 1's such that there are two 0's separated by a number of positions that is a multiple of 4.
Solution: We need to keep track of how many positions have followed since we have seen a 0. So, it makes sense that we should go around in a loop mod 4 and then transition from that state to a final state upon seeing another 0. This is precisely what the NFA is doing. It will accept a string with “00” right away, or if two consecutive 0's do not occur, it will keep track of how many 1s separate the 0's (mod 4). Upon seeing the 2nd zero, if it is separated by a number of 1's divisible by 4, the NFA will transition to the accept state and remain there.
NFA's for Question 5:
a)
1...9
0
0...9
0
0
S
0,2..9
1
1
1
........
9
9
9
0...8
b)
1...9
0
0
1...9
0,2..9
0,2..9
1
1
.....
0...8
9
9
c)
0,1
S
0,1
0
0mod4
0,1
0
0,1
1mod4
3mod4
0,1
0,1
2mod4
© Copyright 2026 Paperzz