Page 1 of 5
Cairo University
Faculty of Computers and Information
Postgraduate Final Exam (Pre-masters)
Department : Computer science
Lecturer : Dr. Hussien Sharaf
Course : Theory of computation
Course Code : CS611 Marks : 60 marks
Date : 1-June-2013 Time : 2 hours
Solve only five questions, given that questions one and two are
mandatory:
Question 1[mandatory] [15 marks]
For each of the following languages do the following:
i. Understand the languages without any assistance;
ii. Write sample words for the language where you show the smallest possible
word.
iii. Construct a regular expression that represents the language.
Note: Draw and fill the following table in your answer sheet:
Sample Regular Expression
a. Σ ={a, b} Only words with exactly two consecutive b's [1 marks]
Solution:
Sample = {bb, bba, abb, abba, aabbaaa …..}
a*bba*
b. Σ ={a, b} Empty words or words that do not end with ab. [2 marks]
Solution:
Sample= { Λ, a,b, abb, aa, ba, bbb,….. }
Λ + b+ ((a + b)*(a + bb))
c. Σ ={a, b} Only words such that { an
bm
| n is even and m is odd} where n and m indicate number
of “a”s and “b”s respectively. [2 marks]
Solution:
Sample = {b,aab,aabbb,..}
(aa)* b(bb)*
(aa+bb)* b
d. Σ ={a, b} Only words such that { an
bm
| (n+m) is even }; where n and m indicate number of “a”s
and “b”s respectively. Note that an even number could be sum of two even numbers or of two
odd numbers. [3 marks]
Solution:
Sample = {Λ, ab, aabb, aaabbb...}
(aa)* (bb)* + (aa)* a (bb)* b
e. Σ ={a, b} Words where “a” appears ONLY in even positions or doesn’t appear at all.
[3 marks]
Solution:
Sample = { Λ b ba bbb bbba baba …..}
- b+(bb+ba)*b*
- (ba+b)*
- (b(bb)* a)* + ( b(bb)*)*
f. Σ ={a, b} Construct a regular expression for all strings that have exactly one double letter in
them. “One double letter” implies two equal touching letters; triples or more are excluded.
[4 marks]
Solution:
Sample = { aa, baa, …..}
(b + Λ)(ab)*aa(ba)*(b + Λ) + (a + Λ)(ba)*bb(ab)*(a + Λ)
Page 2 of 5
Question 2[mandatory] [10 marks]
For each of the following languages do the following:
i. Understand the languages without any assistance;
ii. Write sample words for the language where you show the smallest possible
word.
iv. Draw a deterministic finite automaton that represents the language and
handles incorrect inputs.
a. Σ = {a, b} words where “a” appears ONLY in the second position or doesn’t appear at all.
[2 marks]
Solution:
Sample = { Λ b ba bbb bab babbb …..}
b. Σ = {a, b} Words that begin and ending with the same letter. [4 marks]
Solution:
Sample = { Λ b ba bbb bab babbb …..}
a
b
b
-+1 2
a,b
+3
a
a
-+1
b
4
a
b
3
a
+2
b
b
a
a
5
b
+4
Page 3 of 5
c. Prove that the language an
bn
is not regular using pumping lemma. [4 marks]
Solution:
If L is regular, then by Pumping Lemma (P.L.) ∃ n such that . . .
Now let x = 0n
1n
s ∈ L and |s| ≥ n, so by P.L. ∃x, y , z
then x = 0n
1n
= xyz with |xy| ≤ n and |y| ≥ 1.
But then (4) fails for i = 0:
So, x = 0s
, y = 0t
, z = 0p
1n
with
s + t ≤ n, t ≥ 1, p≥ 0, s+ t + p = n.
fails for i = 0: mean that t = 0 , then s+p not = n and number of 0 will be greater than
number of 1 and language will be not regular
Solve three of the next four questions
Question 3 [12 marks]
a. Σ ={a, b}. Describe “anything aa anything” “(a+b)*aa(a+b)*” using CFG. [6 marks]
Solution:-
(a+b)*aa(a+b)*
1 S à XaaX
2) X à aX
3) X à bX
4) X à Λ
Textbook Reference: Page 234
b. Derive “baabaab” from the above grammar. [6 marks]
Note: Draw and fill the following table in your answer sheet:
Derivation Rule Number
Solution:-
S à XaaX [by 1]
X à bXaaX [by 3]
X à bᴧaaX [by 4]
X à baabX [by 3]
X à baabaX [by 2]
X à baabaaX [by 2]
X à baabaabX [by 3]
X à baabaabΛ [by 4]
Textbook Reference: Page 234
Question 4 [12 marks]
PDA superceed FA by having a memory in form of stack hence it can handle
recursive structures.
a. Create a diagram for a PDA that can parse the CFG: [6 marks]
S→(S)|a
b. Trace the PDA on input “(a)” [6 marks]
Page 4 of 5
State Stack Tape
Start Δ….. (a)Δ…..
READ1 Δ…… (a)Δ…..
PUSH ( (Δ……. (a)Δ…..
READ1 (Δ……. (a)Δ…..
POP ) (Δ……. (a)Δ…..
Question 5 [12 marks]
a. Construct a TM that accepts all words with ‘b’ as a second letter. [6 marks]
Solution:
b. Write down the tracing table of aba for the above TM. [6 marks]
Note: Draw and fill the following table in your answer sheet:
Input string
(underline the letter that the head is reading)
State Number
2Start 1
Reject 5
Accept 4
(a, a, R)
(b, b, R)
(b, b, R)
(a, a, R)
PUSH ( READ2READ1
POP3
POP2
START
ACCEPT
( a )
(
(
(a, a, R)
(b, b, R)
)
Page 5 of 5
Solution:
Input string
(underline the letter the the head is reading)
State Number
aba 1
aba 2
aba 4 Accept
Question 6 [12 marks]
a) Given the following table for some problems, write down the class of the problem [Class
P, Class NP]: [6 marks]
Problem complexity Class
heap sort n log n
linear sort n3
Linear search n2
quick sort n log n
Creating a schedule for m football matches for n teams. 2m*n
Traveling sales man visiting n cities n! (n factorial)
3 Cars need to visit n customers only once with the least cost. 3n
check if a number is prime for a number n n/4
Composite Sum for a number n n-1
Does m clauses with n boolean variables has an assignment that makes
the whole expression TRUE
2m*n
Solution:
Problem complexity Class
heap sort n log n N
linear sort n3
N
Linear search n2
N
quick sort n log n N
Creating a schedule for m football matches for n teams. 2m*n
NP
Traveling sales man visiting n cities n! (n factorial) NP
3 Cars need to visit n customers only once with the least cost. 3n
NP
check if a number is prime for a number n n/4 N
Composite Sum for a number n n-1 N
Does m clauses with n boolean variables has an assignment that makes
the whole expression TRUE
2m*n
NP
b) Given the following instance of SAT: [6 marks]
SAT= (a ν	
  b)	
  Λ	
  (¬b ν	
  c)	
  Λ	
  (¬a ν¬b)	
  
i) Construct a table that solves the problem.
ii) Does this problem have a valid solution?
Solution:
a b c a∨b ¬b∨c ¬a∨¬b SAT
T T T T T F F
T T F T F F F
T F T T T T T
T F F T T T T
F T T T T T T
F T F T F T F
F F T F T T F
F F F F T T F
SignatureNameExaminers
Examiner 1
Examiner 2

Model answer of exam TC_spring 2013

  • 1.
    Page 1 of5 Cairo University Faculty of Computers and Information Postgraduate Final Exam (Pre-masters) Department : Computer science Lecturer : Dr. Hussien Sharaf Course : Theory of computation Course Code : CS611 Marks : 60 marks Date : 1-June-2013 Time : 2 hours Solve only five questions, given that questions one and two are mandatory: Question 1[mandatory] [15 marks] For each of the following languages do the following: i. Understand the languages without any assistance; ii. Write sample words for the language where you show the smallest possible word. iii. Construct a regular expression that represents the language. Note: Draw and fill the following table in your answer sheet: Sample Regular Expression a. Σ ={a, b} Only words with exactly two consecutive b's [1 marks] Solution: Sample = {bb, bba, abb, abba, aabbaaa …..} a*bba* b. Σ ={a, b} Empty words or words that do not end with ab. [2 marks] Solution: Sample= { Λ, a,b, abb, aa, ba, bbb,….. } Λ + b+ ((a + b)*(a + bb)) c. Σ ={a, b} Only words such that { an bm | n is even and m is odd} where n and m indicate number of “a”s and “b”s respectively. [2 marks] Solution: Sample = {b,aab,aabbb,..} (aa)* b(bb)* (aa+bb)* b d. Σ ={a, b} Only words such that { an bm | (n+m) is even }; where n and m indicate number of “a”s and “b”s respectively. Note that an even number could be sum of two even numbers or of two odd numbers. [3 marks] Solution: Sample = {Λ, ab, aabb, aaabbb...} (aa)* (bb)* + (aa)* a (bb)* b e. Σ ={a, b} Words where “a” appears ONLY in even positions or doesn’t appear at all. [3 marks] Solution: Sample = { Λ b ba bbb bbba baba …..} - b+(bb+ba)*b* - (ba+b)* - (b(bb)* a)* + ( b(bb)*)* f. Σ ={a, b} Construct a regular expression for all strings that have exactly one double letter in them. “One double letter” implies two equal touching letters; triples or more are excluded. [4 marks] Solution: Sample = { aa, baa, …..} (b + Λ)(ab)*aa(ba)*(b + Λ) + (a + Λ)(ba)*bb(ab)*(a + Λ)
  • 2.
    Page 2 of5 Question 2[mandatory] [10 marks] For each of the following languages do the following: i. Understand the languages without any assistance; ii. Write sample words for the language where you show the smallest possible word. iv. Draw a deterministic finite automaton that represents the language and handles incorrect inputs. a. Σ = {a, b} words where “a” appears ONLY in the second position or doesn’t appear at all. [2 marks] Solution: Sample = { Λ b ba bbb bab babbb …..} b. Σ = {a, b} Words that begin and ending with the same letter. [4 marks] Solution: Sample = { Λ b ba bbb bab babbb …..} a b b -+1 2 a,b +3 a a -+1 b 4 a b 3 a +2 b b a a 5 b +4
  • 3.
    Page 3 of5 c. Prove that the language an bn is not regular using pumping lemma. [4 marks] Solution: If L is regular, then by Pumping Lemma (P.L.) ∃ n such that . . . Now let x = 0n 1n s ∈ L and |s| ≥ n, so by P.L. ∃x, y , z then x = 0n 1n = xyz with |xy| ≤ n and |y| ≥ 1. But then (4) fails for i = 0: So, x = 0s , y = 0t , z = 0p 1n with s + t ≤ n, t ≥ 1, p≥ 0, s+ t + p = n. fails for i = 0: mean that t = 0 , then s+p not = n and number of 0 will be greater than number of 1 and language will be not regular Solve three of the next four questions Question 3 [12 marks] a. Σ ={a, b}. Describe “anything aa anything” “(a+b)*aa(a+b)*” using CFG. [6 marks] Solution:- (a+b)*aa(a+b)* 1 S à XaaX 2) X à aX 3) X à bX 4) X à Λ Textbook Reference: Page 234 b. Derive “baabaab” from the above grammar. [6 marks] Note: Draw and fill the following table in your answer sheet: Derivation Rule Number Solution:- S à XaaX [by 1] X à bXaaX [by 3] X à bᴧaaX [by 4] X à baabX [by 3] X à baabaX [by 2] X à baabaaX [by 2] X à baabaabX [by 3] X à baabaabΛ [by 4] Textbook Reference: Page 234 Question 4 [12 marks] PDA superceed FA by having a memory in form of stack hence it can handle recursive structures. a. Create a diagram for a PDA that can parse the CFG: [6 marks] S→(S)|a b. Trace the PDA on input “(a)” [6 marks]
  • 4.
    Page 4 of5 State Stack Tape Start Δ….. (a)Δ….. READ1 Δ…… (a)Δ….. PUSH ( (Δ……. (a)Δ….. READ1 (Δ……. (a)Δ….. POP ) (Δ……. (a)Δ….. Question 5 [12 marks] a. Construct a TM that accepts all words with ‘b’ as a second letter. [6 marks] Solution: b. Write down the tracing table of aba for the above TM. [6 marks] Note: Draw and fill the following table in your answer sheet: Input string (underline the letter that the head is reading) State Number 2Start 1 Reject 5 Accept 4 (a, a, R) (b, b, R) (b, b, R) (a, a, R) PUSH ( READ2READ1 POP3 POP2 START ACCEPT ( a ) ( ( (a, a, R) (b, b, R) )
  • 5.
    Page 5 of5 Solution: Input string (underline the letter the the head is reading) State Number aba 1 aba 2 aba 4 Accept Question 6 [12 marks] a) Given the following table for some problems, write down the class of the problem [Class P, Class NP]: [6 marks] Problem complexity Class heap sort n log n linear sort n3 Linear search n2 quick sort n log n Creating a schedule for m football matches for n teams. 2m*n Traveling sales man visiting n cities n! (n factorial) 3 Cars need to visit n customers only once with the least cost. 3n check if a number is prime for a number n n/4 Composite Sum for a number n n-1 Does m clauses with n boolean variables has an assignment that makes the whole expression TRUE 2m*n Solution: Problem complexity Class heap sort n log n N linear sort n3 N Linear search n2 N quick sort n log n N Creating a schedule for m football matches for n teams. 2m*n NP Traveling sales man visiting n cities n! (n factorial) NP 3 Cars need to visit n customers only once with the least cost. 3n NP check if a number is prime for a number n n/4 N Composite Sum for a number n n-1 N Does m clauses with n boolean variables has an assignment that makes the whole expression TRUE 2m*n NP b) Given the following instance of SAT: [6 marks] SAT= (a ν  b)  Λ  (¬b ν  c)  Λ  (¬a ν¬b)   i) Construct a table that solves the problem. ii) Does this problem have a valid solution? Solution: a b c a∨b ¬b∨c ¬a∨¬b SAT T T T T T F F T T F T F F F T F T T T T T T F F T T T T F T T T T T T F T F T F T F F F T F T T F F F F F T T F SignatureNameExaminers Examiner 1 Examiner 2