Professional Documents
Culture Documents
Advanced Data Structures & Algorithms: Assignment No: 1
Advanced Data Structures & Algorithms: Assignment No: 1
Advanced Data Structures & Algorithms: Assignment No: 1
Assignment no: 1
Group Members
(Group #14)
Date: 02/02/2021
Question 1)
Design a TM for the language L = {a^i b^j c^k : i × j = k and i, j, k ≥ 1}
Sol)
The logic for this T.M is very similar to multiplication logic in TM.
Let’s analyze the Time Complexity of this approach with an example & try to generalize the
result:
Consider aaabbcccccc:
Iteration 1:
● 3 steps- flip a and reach b ● i steps
● 2 steps - flip b and reach first c ● j steps
● 3 steps- backward to reach next b ● j+1 steps
● j steps
● 2 steps- flip 2nd b & reach 2nd c ● j+1 steps
● 3 steps- backward steps to realize that b’s have been ●
exhausted ● j steps
● 2 steps- to restore b ( for next iteration) ● i+1 steps
● 4 steps- backward to reach next a
Case 1: j= Θ(1)
Input size= theta(i)
T.C = Θ(i^2) , quadratic in input size
Case 2: j= Θ(i)
Input size= Θ(i^2)
T.C= Θ(i^3)-> Θ( (i^2)^(3/2))
Case 3: j= O(i)
Input Size= O(i^2)
T.C = O(i^3) = O( (I^2)^(3/2))
Question 2)
Implement a Turing machine that, given an input number n in binary
representation, increments n by 1.
State Diagram:
Time Complexity:
Input size= log2(n) --given number n, we have to represent it in binary.
The Time Complexity will be Θ(log2(n)) as in the best as well as the worst case, we’re traversing
the input bits twice( one forward pass and one backward pass)
Question 3)
Give a PDA that accepts exactly the strings over {a, b} that contain the
same number of a 0 s and b 0 s. For example, aabbaabbab is accepted but
aba is not. Also bbbbaaaa and aaaabbbb are both accepted.
Sol) Here’s what we need to do:
● We’ll push the first symbol onto the stack.
● Thereafter, if the top of stack and current symbol is the same we’ll push. Otherwise, we’ll
pop.
State Diagram of PDA:
Question 4)
Outline the logic for constructing a Turing machine that computes/prints
x^2 prime numbers, given an integer x. That is, if x = 3, TM will output 9
prime numbers. Mention the input size and the complexity (poly/exp in
input size) of the logic in unary representation.
Iteration:-1
first B is converted to A
111111111$AABBBBBBBB…
[since primality checking is done in bigoh(n^2) for TM]
Checking whether AA(two) is prime or not using k(2^2) operations where k is some
constant
If two comes out to be prime, the last A is changed to P, R/W pointer finds the leftmost 1
and changes it to D
Else R/W pointer simply goes to $
D11111111$APBBBBBBBB…
Iteration:-2
first B is converted to A
D11111111$APABBBBBBB…
Checking whether APA(three) is prime or not using k(3^2) operations where k is some
constant
DD1111111$APPBBBBBBBB….
Iteration:-3
first B is converted to A
DD1111111$APPABBBBBBB….
Checking whether APPA(four) is prime or not using k(4^2) operations where k is some
constant
DD1111111$APPABBBBBBB….
.
.
.
Since we know that the nth prime number pn can be approximated as(asymptotically) :
n * ln(n) + n * (ln(ln(n))−1) < pn < n * ln(n) + n * ln(ln(n)) f or n≥6
(Source: I s there a known mathematical equation to find the nth prime? See answer by
ShreevatsaR)
[Assumption]: a = max(a, b) and b = min(a, b) where both a and b are positive integers for all
the mathematical models below.
PDA:
a - b be can be implemented as:
Input representation: unary : 11111(a times) 0 1111(b times)$BBBBBB...
Mathematical model: PDA
So, we push 1’s for every 1 present in a and pop for every 1 present in b. Final result a-b
is stored in the stack. Since the Read head is restrained to move right and cannot move
to the left, it is not possible to subtract ‘b’ multiple times from the result stored in the
stack. Since repeated subtraction is impossible => Integer division a/b is impossible.
TM:
a / b be can be implemented as:
Input representation: unary : 11111(a times) 0 1111(b times)$BBBBBB...
input size: 2a + 2b + 2 = Θ(a)
Mathematical model: TM
Using a-b subtraction as a subroutine it is possible to do integer division in TM.
a-b subroutine will be executed a/b times.
● Case1: (b = Θ(1)): TC of (a-b) = Θ(a)
TC of (a/b) = Θ(a) * (a / Θ(1)) = Θ(a^2)
● Case2: (b = Θ(a)): TC of (a-b) = Θ(a^2)
TC of (a/b) = Θ(a^2) * (a / Θ(a)) = Θ(a^2)
● Case3: (b = O(a)): TC of (a-b) = O(a^2)
TC of (a/b) = O(a^2) * (a / O(a)) <= O(a^2) * (a) = O(a^3)
(Since input size is Θ(a), these complexities are based upon the input size and not upon the
magnitude)
*************************************************************************************************************
Turing Machine for division is shown below:
Iteration 1:
● 10 steps - to reach first 1 in b (and flip ● a + 1 steps
it to X)
● 2 steps - backward to reach ● b-1 steps
corresponding 1 in a (and flip it to Y)
● 3 steps- forward to reach next 1 in ● b steps
b(and flip it to X)
● 4 steps- backward to reach ● b + 1 steps
corresponding 1 in a (and flip to Y)
● 5 steps- forward for next 1 in b ● b+2 steps
● 6 steps - backward for next 1 in a ● 2b steps
● 8 steps - forward to realize b has been ● 2b + iteration_no + 1 steps
exhausted and turn the first B after $
to 1
● 6 steps - to restore b ● b+2 + iteration_no steps
Iteration 2:
● 5 steps - backward after flipping the ● 2b - 1 steps
first 1 in b
● 6 steps- forward for next 1 in b ● 2b steps
● 7 steps - backward for next 1 in a ● 2b + 1 steps
● 8 steps- forward for next 1 in b ● 2b +2 steps
● 9 steps - backward for next 1 in a ● 3b steps
● 12 steps- forward to realize all b’s ● 3b + iteration_no + 1 steps
have been exhausted and to turn the
next blank to 1
● 7 steps to restore b ● b + 2 + iteration_no steps
Iteration 3:
● 8 steps - backward ● 3b -1 steps
● 9 steps- forward ● 3b steps
● 10 steps- backward ● 3b+1 steps
● 11 steps- forward ● 3b +2 steps
● 12 steps- backward ● 4b steps
● 16 steps- forward and to turn the next ● 4b + iteration_no + 1 steps
blank to 1
● 8 steps to restore b ● b +2 + iteration_no steps
Concluding Step:
● 11 steps to realize a has been ● a +2
exhausted
● HALT!
[ a + 1] +
[ (b-1) + (2b -1) +...+ ((a/b)(b) -1)] +
[b + 2b +...+ (a/b)b] +
[ (b+1) + (b+2) +...+ ((a/b)b + 1)] +
+....+ [2b + 3b +...+ ((a/b +1)(b) ) ] +
[2b + 3b +...+ ((a/b +1)(b) ) ] +
[ 1+ 2 +...+ a/b] +
[ 1 + 1 +...+1] a/b times +
[ 2+ 2+...+2] a/b times +
[1 + 2 +…+ (a/b) ]
Case 1: b= Θ(1)
Input size - Θ(a)
T.C- Θ( a^2)
Case 2: b= Θ(a)
Input Size- Θ(a)
T.C- Θ(a^2)
Case 3: b= O(a)
Input Size= O(a)
T.C- O(a^3)
*************************************************************************************************************
RAM Model:
Base 2:
a / b be can be implemented as:
Input representation: binary : log 2 a bits of a and log 2 b bits of b are given as input.
[Assumption]: 0 bits are padded to the left of b, so that log 2 a and log 2 b are the same.
Also a = max(a,b). Thus input size: log 2 a
Mathematical model: RAM (base 2)
Using a-b subtraction as a subroutine it is possible to do integer division in RAM.
Complexity of a-b = Θ(log 2 a)
● Case1: (b = Θ(1)): TC of (a-b) = Θ(log 2 a)
TC of (a/b) = Θ(log 2 a) * (a / Θ(1)) = Θ(a.log 2 a) = Θ(2log2 a .log 2 a) (exponential in
input size)
● Case2: (b = Θ(a)): TC of (a-b) = Θ(log 2 a)
TC of (a/b) = Θ(log 2 a) * (a / Θ(a)) = Θ(log 2 a) (linear in input size)
● Case3: (b = O(a)): TC of (a-b) = Θ(log 2 a)
TC of (a/b) = Θ(log 2 a) *(a / O(a)) <= Θ(log 2 a) * (a) = O( a * log 2 a ) =
O(2log2 a .log 2 a) (exponential in input size)
Base 10:
Input representation: binary : log 10 a bits of a and log 10 b bits of b are given as input.
Mathematical model: RAM (base 10)
Complexity analysis:
Given input: 0^k 1^k
We need 1 more iteration at the end to make sure only one 0 and one 1 remains.
So, total number of iterations = log 2 k
Time complexity of one iteration = Θ(k)
Time complexity of completing all iterations = Θ(k.log 2 k)
Input size: n = 2k + 1 = Θ(k)
therefore time complexity in terms of input size ‘n’ = Θ(n.log 2 n)
TM Description:
The state diagram for T.M that accepts a^n b^n ( n>=0) in Θ (nlogn) is given below.
*************************************************************************************************************
Scans the entire tape at once, switching the heads = Θ (1) time
Thus overall complexity = Θ (k) = Θ (n)
*************************************************************************************************************
First step: 4 k
Iteration1: A. 1 .
B. 2
Iteration2: A. 3 .
B. 4
Iteration3: A. 5 .
B. 6
Iteration4: A. 7
B. 8
Total 1+2+3+…+8 1 + 2 + 3 + … + 2k
__________________________________END______________________________________