CPSC 121: Models of Computation
Assignment #0, due NONE
NOTES
These are some worked examples of problems similar to the assignment problems that might help you learn
about and complete the assignment problems.
Questions
[0] 1. Describe in English, and as simply as possible, the strings for which the following machine ends up in
an accepting state, i.e., the language the DFA accepts. Briefly explain your answer (but you need not
prove your answer correct).
For the alphabet 𝐼 = {𝑀, 𝐴}:
Solution : To start with, let’s just find a sequence of letters that gets us to each state. That’s not too
hard; it’s a bit like solving a maze.
The empty string gets us to state 1, the start state (and right away, we can notice that any number of As
will keep us there).
The letter “M” gets us to state 2 (and right away, we can notice that any number of Ms will keep us
there).
An “MA” gets us to state 3.
An “MAM” gets us to state 4.
An “MAMA” gets us to state 5 and is accepted.
So, I’ll start by labelling each of the states by that text. 1 is just the start state. 2 is “M”, 3 is “MA”, 4 is
“MAM”, 5 is “MAMA”.
Now, what takes us back to the start state? Only three arcs. An “A” from each of states 1, 3, and 5. So,
any time we get to the start state, the string we saw must have ended with “A”. Given my labels for 3
and 5, it looks like it even ends with “AA”, except when we got there with JUST the empty string or the
string “A” (both of which leave us in the start state). So, I’m going to relabel the start state “...AA”, with
the understanding that “” and “A” also get me there.
What about state 2? Only “M” arcs lead there. Given states 2 and 4’s labels, that must be something
ending in “MM” unless we got there from the start state. So, maybe I’ll label this “...MM”, again with
the understanding that it might be just a single “M” coming from the start state. (And, that “exception”
may not be near the start of the string, since we can get back to the start state from the middle of the
DFA.)
How about state 3? The only way to get to it is from state 2 on an “A”. So, it looks like it should be
labelled “...MA” or something similar.
How about state 4? We can get there from state 3 (“...MAM”) or state 5 (“...MAMAM”). So, let’s label
it “...MAM”.
State 5? We can only get there from state 4, which makes it look like “...MAMA”. So, that’s a string
ending in “MAMA”.
Is that what the DFA is designed to accept? Let’s check. If we were in state 5 and we knew the string
ended in “MAMA” and we got an “M”, we’d have a string ending in “MAM”. If we got another “A”,
we’d want to be back in state 5. Sure enough, that’s what the DFA sets us up to do. If we were in 5 and
got an “A”, we’d have “...MAMAA”, which doesn’t get us close to ending in “MAMA”, and the DFA
does, in fact, kick us all the way back to the start state.
In state 2, we have just the first “M” of “MAMA”. If we get another “M”, we still have the first “M” of
“MAMA”. If we get an “A”, however, we have the first “MA” and move on to state 3.
State 3 works similarly. An “M” advances us to state 4. An “A” blows our work so far and sends us back
to the start state.
Excellent! So, our answer is “This DFA recognizes exactly those strings that end with the four letters
MAMA.”
Notice how useful labelling the states as we go along can be. You should label states! You may find
your labels changing as you go, but it will help you design the DFAs. (Why don’t we label the states
in the problems we give you? Because with the labels the problems become too easy! Figuring out the
labels is the heart of understanding the DFA.)
[0] 2. Design a DFA that will accept exactly the strings over the alphabet {𝐴, 𝐵, . . . , 𝑍} that contain the substring “AB”, but not the substring “BBA”. For instance, your DFA should accept the strings “ABRACADABRA”, “ABSOLUTE” and “BAOBAB”, but not “ABBA”, “BBBBBAD”, or “COMPUTER”.
(Note: you can label an arc “𝐼” to indicate that every input letter received while at the arc’s initial
state follows the arc or label it “else” to indicate that every input letter not otherwise labelled from the
arc’s initial state follows the arc.)
Solution : We only really care about A and B. So, for now, I’m going to pay attention to only those
letters. I’ll figure out what to do with everything else when I’m done with an “A and B only” DFA.
Our goal is to reject any string that contains “BBA” and otherwise accept any string that contains “AB”.
We’ll build up the DFA from the start state. First, is the start state accepting? That is, does the DFA
accept the empty string?
The empty string does not contain “AB”; so, the start state should be rejecting:
It has an “A” and a “B” arc coming off. The “A” could start an “AB” and the “B” could start a “BBA”; so,
both seem distinct from the start state. So, we’ll send each to a new state. We start working on the “A”
state but could start on the “B”. (I choose “A” to be positive. . . after all, it might lead right to an accept!)
Now, “AB” should be accepted. So, the “B” arc from “A” should definitely lead to an accepting state.
“AA” should be rejected. It doesn’t make any progress towards “BBA”, however. So, perhaps it should
just lead right back to this state. In that case, we could think of the state as being “any string without an
AB or a BBA that ends in A” instead of just “A”. On a “B”, it would still lead to an accepting state. So,
that sounds good. Our accepting state then becomes “anything ending in AB that doesn’t contain BBA”:
Let’s work on the accepting state next. If we get an “A”, the string ends in “ABA”, which is fine (still
contains “AB”, doesn’t contain “BBA”). If we get a “B”, we get “ABB”. That’s fine so far (still contains
“AB”, doesn’t contain “BBA”), but it’s on its way to containing “BBA”. So, that’s definitely a different
state:
It’s too tempting now to be “negative”! Let’s work on the “. . . ABB” state. If we get an “A” there, we
should reject and never again accept. That’s because the string will contain “BBA”, and we reject any
such string. So, that should lead to a garbage state. If we get a “B”, we get something like “. . . ABBB”,
which should still be accepted but is still perilously close to being rejected (on another “A”). So, we’ll
loop that back where it came from:
Now, let’s go back up to the accepting state ending in “ABA”. On a “B”, we’re back to a string ending in
“AB”, which should be accepting but has the makings of a “BBA” at its end. So, let’s go back to the first
accepting state. On an “A”, we should still accept and aren’t in any imminent danger of a “BBA”, just as
we already are in this state. So, let’s loop back:
We have just one state left to work on. That’s the “B” state in the lower left. On a “B”, that string
ends with “BB” and is in imminent danger of being garbage. That’s a lot like our “. . . AB. . . BB” state,
except that we haven’t seen an “AB” yet; so, we don’t accept. So, let’s make a new state that leads to the
garbage on an “A”, just like the “. . . AB. . . BB” state:
What should that state do on a “B”? Then, it ends in a bunch of Bs and is still in imminent danger of
being rejected. So, just like the “. . . AB. . . BB” state, it’ll loop back on itself:
That’s the whole DFA! We should now try out some test strings. We’ll make up some of our own that
don’t have other letters in them like “B”, “BBBB”, “ABBA” (accept!), “BABABABB” (accept!), and
“BABABBAB”. Not only are the whole strings good test cases, but as we work through the string, we
can make sure we’re in the right state up to that point (since a shorter version of the same string should
still be processed correctly, if not necessarily with the same accept/reject outcome). So, for example, “B”
and “BA” are rejected; “BAB”, “BABA”, “BABAB”, and “BABABB” are accepted; and “BABABBA”
and “BABABBAB” are rejected.
Finally, we add in other letters. Mostly, other letters “break up” any patterns we had and leave us in
the “safer” states. We’re no longer on our way to “AB”, but we’re also no longer on our way to “BBA”.
We’ve put those arcs in below using “else” to indicate any letter but “A” or “B”. We also used a big ugly
I to indicate an arc that every input letter follows (the one that loops back on the garbage state).
[0] 3. For each function listed below, determine whether it is one-to-one, whether it is onto, and whether it is
a 1-1 correspondence. Your answers must be well justified (a simple “yes” or “no” is not sufficient), but
you need not formally prove them.
[0] a. Addition over the positive integers.
Solution : We consider a function that takes two arguments to take a two-tuple. In this case,
then, the domain is Z+ × Z+ and the codomain is Z+ .
1-1: A function is 1-1 exactly when the following is true: if 𝑓 (𝑥) = 𝑓 (𝑦) then 𝑥 = 𝑦. So, consider
a particular value in the co-domain. Can we find two different inputs that yield that value?
Let’s pick some “unusual” values and a “normal” value. How about 1? Can we get 1 in two
different ways? Well, 0 + 1 = 1, but that’s not even legal because 0 is not positive. 1 + 1 ∕= 1.
So, in fact, we cannot even get 1 as an output. That’ll be important below!
OK, how about 2? Well, 1 + 1 = 2, and there’s no other way to get 2 (since 1 is the smallest
positive integer, and anything larger than 1+1 is bigger than 2). So, if 𝑓 (𝑥) and 𝑓 (𝑦) are both 2, 𝑥
and 𝑦 really do have to be the same pairs of integers. Still, that doesn’t prove that the conditional
is always true!
Now, let’s try a more “normal” value. How about 5? Well, 4 + 1 = 5, but so does 3 + 2. Aha!
That’s two different pairs of integers. So, that’s a counterexample which shows that this statement
is not always true: if 𝑓 (𝑥) = 𝑓 (𝑦) then 𝑥 = 𝑦. So, this function is not 1-1.
Our short answer could be: “No. Consider that 4 + 1 and 3 + 2 both equal 5.”
onto: We’ve already established that this function is not onto because there’s no way to get 1.
Our short answer could be “No. There’s no pair of positive integers smaller than (1, 1), but even
𝑓 (1, 1) > 1. So, nothing maps to 1.”
1-1 correspondence: No. A function must be both 1-1 and onto to be a 1-1 correspondence, and
this one is not both 1-1 and onto.
Note that if we considered addition over the non-negative integers instead of the positive integers,
it still wouldn’t be 1-1 but it would be onto. I’ll leave the 1-1 part to you to justify, but it’s much
like above! Why would it be onto? If you pick an arbitrary 𝑦, I can pick an 𝑥 such that 𝑓 (𝑥) = 𝑦.
My 𝑥 will be the pair (𝑦, 0), and 𝑦 + 0 = 𝑦. The short answer would be “Yes this is onto because
0 + 𝑦 = 𝑦.”
[0] b. 𝑓 : Z → Z defined by 𝑓 (𝑥) = −𝑥 − 1.
Solution : 1-1: A function is 1-1 exactly when the following is true: if 𝑓 (𝑥) = 𝑓 (𝑦) then 𝑥 = 𝑦.
So, consider a particular value in the co-domain. Can we find two different inputs that yield that
value?
Let’s pick some “unusual” values and a “normal” value. How about 0? Can we get 0 in two
different ways? Well, if 𝑓 (𝑥) = 0, then −𝑥 − 1 = 0. So, solving for 𝑥: 𝑥 = −1.
There’s not really any other especially unusual values. So, let’s try a “normal” one. 5 worked
before: if 𝑓 (𝑥) = 5, then −𝑥 − 1 = 5. So, 𝑥 = −6.
Let’s try the other direction. We’ll try to prove the conditional by antecedent assumption. Assume
𝑓 (𝑥) = 𝑓 (𝑦). Then, −𝑥 − 1 = −𝑦 − 1. So, −𝑥 = −𝑦, and therefore 𝑥 = 𝑦. That’s it! We’ve
proven that 𝑓 is 1-1 over this domain/co-domain.
The short answer might be: “Yes. If 𝑓 (𝑥) = 𝑓 (𝑦), then −𝑥 − 1 = −𝑦 − 1, −𝑥 = −𝑦, and 𝑥 = 𝑦.”
onto: Can we find an 𝑥 for every 𝑓 (𝑥)? We’ve already found a couple above. Can we “solve for
𝑥” in general?
𝑓 (𝑥) = −𝑥 − 1. So, 𝑓 (𝑥) + 1 = −𝑥 and 𝑥 = −𝑓 (𝑥) − 1.
Now, we’re ready to prove this function onto. For an arbitrary, 𝑦, we select 𝑥 = −𝑦 − 1. Then,
𝑓 (𝑥) = −(−𝑦 − 1) − 1 = 𝑦 + 1 − 1 = 𝑦, and we’re done.
Our short answer could be “Yes. My 𝑥 will be −𝑦 − 1, and −(−𝑦 − 1) − 1 = 𝑦.”
© Copyright 2026 Paperzz