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

St.

Paul University Manila


(St. Paul University System)
680 Pedro Gil St., Malate, Manila

College of Business and Management


Computer Science and Information Technology Programs

LABORATORY MANUAL
in
Theory of Computation

Maria Regina G. Lim Cachero


October, 2010

Preface
This laboratory manual includes a collection of experiments and exercises
suitable for learning the topics contained in Theory of Computation. It starts with a
discussion and application of Mathematical Notations and Terminologies, an essential
part to being introduced to the Computational Theory.
The concepts discussed in this course focuses on Automata and Languages, as
well as, Computability Theory. Hence, most, if not all, experiments are directed towards
applying the theories of these concepts in various kinds of computing problems.
The manual intends to make learning Theory of Computation more meaningful.
Thus, a learners success in accomplishing the experiments and exercises in this
manual will lead to better results in the quizzes and examinations for this course.
Any error or modification in this manual may be reported to my email address so
that the material can be improved accordingly.

Special Note to Students

To better understand the different domains of Theory of Computation, familiarity


to mathematical notations and terminologies will work in your favor. Hence, please do
your own refresher of basic mathematical and algebraic concepts, such as Sets,
Algebraic expressions. This will facilitate your absorption of the concepts in this course.

Maria Regina G. Lim-Cachero


rhejlim@yahoo.com

Table of Contents

MATHEMATICAL NOTATIONS AND TERMINOLOGIES


Sets ..

Functions ..

Relations ...

AUTOMATA AND LANGUAGES


Deterministic Finite Automata .. 7
Regular Expressions 8
Context Free Grammars .. 10
Pushdown Automata.. .. 12
COMPUTABILITY THEORY
Turing Machine. . 14

MATHEMATICAL NOTIONS AND TERMINOLOGY


OBJECTIVES
1. Develop a good understanding of the basic mathematical objects, tools, and
notations used in learning theory of computation;
2. Solve problems on set operations, Venn diagramming, and relations and
functions;
3. Be familiar with how these notions and terminologies are used in
computational theory.

TOPIC : SETS

The branch of mathematics that studies sets, which are collections of objects.
It is used in the definitions of nearly all mathematical objects.
Set theory deals with operations between, relations among, and statements
about sets.

x is a member of A is written xA
A member of a set is also known as its element
The basic binary relation between two sets is the subset relation, also called
set inclusion. Subset means every element of S is also an element of T.
Subset Principles
o A B if and only if whenever xA it follows that xB
o AB if and only if AB and AB
o For any set A, AU
o AA
o If AB and BA then A=B
o If AB and BC then AC
o ABA and AAB
Basic Set Operations
o Union - of the sets A and B, denoted , is the set whose members are
members of at least one of A or B.
o Intersection - of the sets A and B, denoted , is the set whose members
are members of both A and B.
o Complement - of set A relative to set U, denoted Ac, is the set of all
members of U that are not members of A. This is also known as set
difference.
o Symmetric Difference - of sets A and B is the set whose members are
members of exactly one of A and B.

o Cartesian Product - of A and B, denoted X, is the set whose members are


all possible ordered pairs (a,b) where a is a member of A and b is a
member of B.
o Power Set - of a set A is the set whose members are all possible subsets
of A.

EXPERIMENTS:
I. True or False?
_________1.

{a,a,b} {a,b}

_________2.

{c, a, b} = {c, b, a}

_________3.

{x,y,z} =

_________4.

{1,2,3,4} = {1,2,3}

_________5.

{e,f,g} {h,i,j} = {j,h,i} {g,f,e}

_________6.

{a,b,c,c,d} {a,b,c,d,e,f,g}

_________7.

{a,b,b} {a,b,c}

II. What is the resulting set?


__________1.

{2,3,5} {3,5,7}

__________2.

{a,b,c} {2,3}

__________3.

{a,b,c} {2,3}

__________4.

{2,4,6} {3,4,5}

__________5.

{1,2,3,4,5,6} {2,3,5,7,9,11}

__________6.

P({1,2})

III. Answer the following:


__________1.

Let A={a, b, {c, d}, e}. How many elements does A contain?

__________2.

Let A = {2, {4, 5}, 4}. Which statement is correct?


a)
b)
c)
d)

5 is an element of A.
{5} is an element of A.
{4, 5} is an element of A.
{5} is a subset of A.

__________3.

Which of these sets is finite?


a)
b)
c)
d)

{x | x is even}
{x | x < 5}
{1, 2, 3,...}
{1, 2, 3,...,999,1000}

__________4.

Let S={1, 2, 3}. How many subsets does S contain?

__________5.

Let D E. Suppose a D and b


statements must be true?
a) c
b) b
c) a
d) a

__________6.

E. Which of the following

D
D
E
D

Let A = {x | x is even}, B = {1, 2, 3,..., 99, 100}, C = {3, 5, 7, 9}, D =


{101, 102} and E = {101, 103, 105}. Which of these sets can equal
S if S A and S and B are disjoint?
a) A

b) B

c) C

d) D

e) E

__________7.

Let S = {a, b}. How many elements does the power set 2S contain?

__________8.

Which set S does the power set 2S = { , {1}, {2}, {3}, {1, 2}, {1, 3},
{2, 3}, {1, 2, 3}} come from?
a) {{1},{2},{3}}
b) {1, 2, 3}
c) {{1, 2}, {2, 3}, {1, 3}}
d) {{1, 2, 3}}

__________9.

Let A = {x, y, z}, B = {v, w, x}. Which of the following statements is


correct?
a) A
b) A
c) A
d) A

B = {v, w, x, y, z}
B = {v, w, y, z}
B = {v, w, x, y}
B = {x, w, x, y, z}

__________10. Let A = {1, 2, 3, ..., 8, 9} and B = {3, 5, 7, 9}. Which of the following
statements is correct?.
a) A
b) A
c) A
d) A

B = {2, 4, 6}
B = {1, 2, 3, 4, 5, 6, 7, 8, 9}
B = {1, 2, 4, 6, 8}
B = {2, 4, 6, 8}

__________11. Let C = {1, 2, 3, 4} and D = {1, 3, 5, 7, 9}. How many elements does
the set C D contain?
How many elements does the set C

D contain?

__________12. Let A = {2, 3, 4}, B = {3} and C = {x | x is even}. Which statement is


correct?
a) C A = B
b) C B = A
c) A C
d) C / A = B
__________13. Let A

B, B

a) B D
b) A C
c) A = B
b) B D =

C and D

and B

A = C. Which statement is always false?

__________14. Let U = {1, 2, 3, ..., 8, 9} and A = {1, 3, 5, 7}. Find A'.


a) A' = {2, 4, 6, 8}
b) A' = {2, 4, 6, 8, 9}
c) A' = {2, 4, 6}
d) A' = {9}
__________15. Let U = {1, 2, 3,..., 8, 9}, B = {1, 3, 5, 7} and C = {2, 3, 4, 5, 6}. How
many elements does the set (B C)' contain?
How many elements does the set (C - B)' contain?
IV. Draw the Venn Diagram for the following:
1.

A = {2, 4, 5, 3, 1, 6, 8}
B = {3, 5, 4, 7, 9, 12}
C = {4, 7, 10}
D = {11,14, 9, 7}
E = {1, 23, 25}

2.

In an interview of 50 students,
12 liked Proposition 8 and Proposition 13
18 liked Proposition 8, but not Proposition 5
4 like Proposition 8, Proposition 13 and Proposition 5
25 liked Proposition 8
15 liked Proposition 13
10 liked Proposition 5, but not Proposition 8 or Proposition 13
1 liked Proposition 13 and Proposition 5, but not Proposition 8

3.

In a class of 50 college students, 30 are studying VB .Net, 25 studying Java,


and 10 are studying both. How many students are not studying either
computer language?
4

TOPIC : FUNCTIONS

it is an object that sets up an input-output relationship.


A function takes an input and produces an output; in every function, the same
input always produces the same output.
also called mapping, i.e. f(a) b can be read as f maps a to b.
Parts of a Function
o DOMAIN - the set of possible inputs to the function.
o RANGE the set of outputs of a function
o The notation for saying that f is a function with domain D and range R is:
f : D R.

EXPERIMENT:
Answer the following:
1.

B (x) = x2 x3 Evaluate B(5)

2.

s (x) = 2x2 2x Evaluate s(3)

3.

f (x) = 5x + 2 Evaluate f(2)

4.

h(x) = 3x3 Evaluate h(2)

5.

k(x) = 3x 1 Evaluate k(-1)

6.

Let f 1, f 2, f 3, f 4, f 5 be functions of R into R and


let f 1(x) = x2 + 3x - 4. Which of these functions are equal to f 1?
a) f 2(x) = x 2
b) f 3(y) = y 2 - 4
c) f 4(z) = z 2 + 3z - 4
d) f 5(x) = x 2 + 3x

7.

Let the functions f, g, and h be defined by :


(a) f (x) = x 3 where x R
(b) g (y) = y 3 where 2 < y < 9
(c) h (z) = z 3 where 1 < z < 10
W hich of these functions are equal?

TOPIC : RELATIONS

A set of ordered pairs


The domain is the set of all the first numbers of the ordered pairs; all of the xvalues.
The range is the set of the second numbers in each pair; or the y-values.
e.g. in a relation {(1,3), (4,7), (2,8)}
The domain is {1,4,2} and the range is {3,7,9}

EXPERIMENT:
1. What is the domain and range of the following relation?
{ (-1,2), (2, 51), (1, 3), (8, 22), (9, 51) }
2. The relation R is given by the set of ordered pairs, R = {(2, 4), (3, 4), (1,3),
(3, 5), (2, 3) }. Which of the following is the domain of R?
a) {2, 3, 1, 5}
b) {1, 2, 3}
c) {1, 2, 3, 4, 5}
d) {2, 4, 3, 5}
3. The relation R is given by the set of ordered pairs, R = {(2, 4), (3, 4), (1,3),
(3, 5), (2, 3) }. What should the range of R be?
a) {3, 4, 5}
b) {1, 2, 3}
c) {1, 2, 3, 4, 5}
d) {4, 4, 2, 5}
4. Find the inverse of the relation
R = {(2, 4), (3, 4), (1,3), (3, 5), (2, 3)}
a) {(2, 4), (3, 4), (1, 3), (3, 5), (2, 3)}
b) {(4, 2), (4, 3), (3, 1), (5, 3), (3, 2)}
c) {5, 4, 3, 2, 1}
d) {(4, 4), (3, 3), (1, 1), (5, 5), (2, 2)}
5. Two sets are given by E = {2, 4, 5, 6} and F = {1, 3, 6}. Let R be a relation
from set E to set F, which is defined by the open sentence "x is greater than
y". How many elements does the solution set of R contain?
6. Which relations below are functions?
Relation #1 { (3,4), (4,5), (6,7), (8,9) }
Relation #2 { (3,4), (4,5), (6,7), (3,9) }
Relation #3 { (-3,4), (4,-5), (0,0), (8,9) }
Relation #4 { (8, 11), (34,5), (6,17), (8,19) }

AUTOMATA and LANGUAGES


OBJECTIVES
1. Be familiar with the study of abstract computing devices or machines;
2. Appreciate automata theory as useful models for designing computing
hardware and software.
3. Create automata that will implement specific problems;
4. Evaluate regular expressions and context free grammars.

TOPIC : FINITE AUTOMATA

Finite automata is an important way to describe certain simple, but highly


useful languages called regular languages."
A graph with a finite number of nodes, called states.
Arcs are labeled with one or more symbols from some alphabet.
One state is designated the start state or initial state.
Some states are final states or accepting states.
The language of the FA is the set of strings that label paths that go from the
start state to some accepting state.
A finite (deterministic) acceptor A is specified by five (5) pieces of information
or tuples:
A = (S, A, i, , T) ;
Where:
S is a finite set called the set of states
A is the finite input alphabet
i is a fixed element of S called the initial state
is a function : S X A S, called the transition function
T is a subset of S (possibly empty!) called the set of terminal or final
states.
There are two ways of providing the 5 tuples to specify an acceptor: transition
diagrams and transition tables.
A transition diagram is a special kind of directed labeled graph: the vertices
are labeled by the states S of A; there is an arrow labeled a from the vertex
labeled s to the vertex labeled t precisely when (s, a) = t in A.
That is to say, the input a causes the acceptor A to change from state s to
state t.
Example:

EXPERIMENT:
Create a Deterministic Finite Automata (DFA) for the following problems. State
the 5 Tuples of the DFA. Construct the Transition Diagram for the DFA as well.
Create DFAs accepting 0 and 1 as input and recognizes all strings that:
1. ends in a 1
2. have odd number of 1s
3. contain even number of 0s and any number of 1s
4. have a sequence 11 somewhere in the string
5. contain at least one 1 and an even number of 0s follow the last 1
6. contain even number of zeroes and even number of ones
7. that start and end with the same number (either 1 or 0).
8. starts with 101 and succeeded by any number of 1s and 0s
9. is preceded by any number of 1s and 0s but ends with 101
10. contain two consecutive 0s followed by two consecutive 1s.

TOPIC : REGULAR EXPRESSIONS

Symbols and alphabet


o A symbol is a valid character in a language
o Alphabet is set of legal symbols; Typically denoted as
Metacharacters/metasymbols that have special meanings
o Defining reg-ex operations (e.g. |, (, ), *, +, etc.)
o Escape character (\) to turn off special meanings
o Empty string and empty set ={}
Basic regular expressions
o Single characters (a={a} for any character a )
o Empty string ={}
o Empty set
8

Basic regular expression operations


o Union/Alternation,
denoted by a | b or a + b
o Concatenation, denoted by ab
o Repetition (Kleen closure, 0 or more times) a*

EXPERIMENTS
Simplify the following Regular Expressions:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.

(A + B + B) + + 1
N + M + K + N + M +
(AB) (B+B)
C(A+A+A+B)
C(A+A+A+)
( + D) + D
(L + M) + N
B + B + B +
C+C+D+D+
R + L
Z + Z + X
S + S + R
(B + B)C
(E + D + E + D)
(AB)C+C

Evaluate the following Regular Expressions based on their elements:


A = {2, 4, 6}, B = {3, 8, 10}, C = {1, 9}, D = { }, E = { }
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.

D+C+B=?
BE = ?
DC = ?
AB = ?
AD = ?
B+B=?
(CD) B =?
(C + D) + (C + D) =?
D(C + B) = ?
E(A + B) = ?
9

TOPIC : CONTEXT FREE GRAMMARS

Contest free grammars (CFGs) are formal notations for expressing recursive
definition of languages.
There is a finite set of symbols that form the strings of the language being
defined. The alphabet are called TERMINALS or TERMINAL SYMBOLS.
There is a finite set of variables also called NONTERMINALS or SYNTACTIC
CATEGORIES.
One of the variables represents the language being defined, it is called the
START SYMBOL. Other variables represent auxiliary classes of strings that are
used to help define the language of the start symbol.
There is a finite set of productions or rules that represent the recursive definition
of a language.
Production Composition: Each production consists of:
A. a variable that is being defined by the production, called the HEAD.
B. the production symbol
C. a string of zero or more terminals called the BODY.
CFG = (V,T,P,S) where
V = set of variables
T = the terminals
P = set of productions
S = start symbol

EXPERIMENT:
Using the grammar, show the derivations of the following sentences
EI
I Ib
EE+E
I I0
EE*E
I I1
E(E)
Ia
Ib
I Ia
1.
2.
3.
4.
5.

(ab * aa1) + bb0


aaa + bbb + a
ab0 + bb1 * b1
a*b+a
11 + ab
10

Using the grammar, construct the parse tree of the following sentences
E E+E | EE |
E*E | E/E |
-E
E (E)
E id
id a | b | c
1.
2.
3.
4.
5.

(a + b) * (b c)
c * (a + b)
a+b*ca/c+b
(c / a) + a + b
a + c / a b + (c * a + b)

Using the grammar, do a bottom-up parsing of the following sentences:


RR/R
R RR
RR*
R(R)
Ra
Rb
1. a/b*a
2. (a/bb/a)
3. a/b/(c*a)
4. a*(a/c)*c
5. (ab)/(ab)

11

TOPIC : PUSHDOWN AUTOMATA

a pushdown automaton (PDA) is a finite automaton that can make use of a stack
containing data.
They can use the top of the stack to decide which transition to take.
They can manipulate the stack as part of performing a transition.
A PDA is formally defined as a 7-tuple:
Where :
Q is a finite set of states
is a finite set known as input alphabet
is a finite set known as stack alphabet
is the transition function, that denotes the power set of S.
qo Q is the start state
Z is the initial stack symbol
F Q is the set of accepting states
Transitions:
Let ((p, a, ), (q, )) be a transition.
It means that we
o Move from state p.
o Read a from the tape,
o Pop the string from the stack,
o Move to state q,
o Push string onto the stack.
The first three (p, a, ), are input.
The last two (q, ) are output.

((p, a, ), (q, )) is drawn as:

a, ;

When we push , we push the symbols of as we read them right to left.


o When we push the string abc, we push c, then push b, then push a.
When we pop , we pop the symbols of as we read them from left to right
(reverse order).
o When we pop the string abc, we pop a, then pop b, then pop c.

12

EXPERIMENT:
State the language that is accepted by the following PDAs. Give at least five strings
that can be accepted and five strings that are not.
PDA 1:

PDA 2:

PDA 3

13

COMPUTABILITY THEORY
OBJECTIVES
1. Be familiar with the application of Turing Machine in the design of computing
resources;
2. Appreciate computability theory as part of the foundations of computer
science.
3. Create Turing machines that will implement specific problems;

TOPIC: TURING MACHINE

First proposed by Alan Turing in 1936


Turing machines are not physical objects but mathematical ones.
It is similar to a finite automaton but with an unlimited and unrestricted
memory.
A Turing machine is a much more accurate model of a general purpose
computer. A Turing machine can do everything that a real computer can do.
The Turing machine model uses an infinite tape as its unlimited memory.
It has a tape head that can read and write symbols and move around on the
tape.
Initially the tape contains only the input string and is blank everywhere else.
If the machine needs to store information, it may write this information on the
tape.
To read the information that it has written, the machine can move its head
back over it. The machine continues computing until it decides to produce an
output.
The outputs accept and reject are obtained by entering designated accepting
and rejecting states.
If it doesn't enter an accepting or a rejecting state, it will go on forever, never
halting.
A TM consists of
o A TAPE which is divided into cells, one next to the other.
o A HEAD that can read and write symbols on the tape and move the
tape left and right one (and only one) cell at a time.
o A finite TABLE ("action table", or transition function) of instructions
(usually quintuples [5-tuples]
o A STATE REGISTER that stores the state of the Turing table, one of
finitely many.

14

EXPERIMENT:
Using the following Turing Machines, show the configuration of the following strings.
Likewise, state the language that is accepted by the Turing machine.:

1.

2.

00

3.

000

4.

0000

5.

000000

15

Consider the following Turing Machines, then state the language that they accept:
TM 1:

TM 2:

(q0,0) = (q0,B,R)

(q0,0) = (q1, 1, R)

(q0,1) = (q1,B,R)

(q1,1) = (q0, 0, R)

(q1,1) = (q1,B,R)

(q1,B) = (qf, B, R)

(q1,B) = (qf,B,R)
TM 3:
(q0,0) = (q1,1,R)
(q1,1) = (q2,0,L)
(q2,1) = (q0,1,R)
(q1,B) = (qf,B,R)

Using the following transition table, construct the transition diagram for the following
Turing Machines:
Table No. 1
State

q0

(q1, B, R)

(q5, B, R)

q1

(q1, 0, R)

(q2,1, R)

q2

(q3, 1, L)

(q2, 1, R)

(q4, B, L)

q3

(q3, 0, L)

(q3, 1, L)

(q0, B, R)

q4

(q4, 0, L)

(q4, B, L)

(q6, 0, R)

q5

(q5, B, R)

(q5, B, R)

(q6, B, R)

q6

16

Table No. 2
State

q0

(q1, X,R)

(q3,Y,R)

q1

(q1, 0,R) (q2,Y,L)

(q1,Y,R)

q2

(q2, 0, L)

(q0,X,R)

(q2,Y,L)

q3

q4

17

(q3, Y,R) (q4,B,)


-