Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Quiz 6: Recursion and Time Complexity, Quizzes of Algorithms and Programming

The solutions to quiz 6 of the csis-385 course, focusing on recursion and time complexity. It includes the number of times a basic operation is executed for given codes, the exact bounds on running time, and explanations of how the running time was determined.

Typology: Quizzes

Pre 2010

Uploaded on 08/09/2009

koofers-user-eav
koofers-user-eav 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS-385: Quiz 6 Name____________________________________
1. Given the following code:
fun(n) {
if (n == 0)
return;
else {
basic_op;
fun(n-1);
}
}
a. Exactly how many times it the basic
operation executed when n equal 3.
Answer: 3
b. What is the exact bound on the running
time, i.e., f(n)= (?)
Answer: f(n)= (n)
c. Explain how you figured out the running
time.
Answer:
c(n) = 1 + c(n-1);
c(0) = 0
Thus, c(n) =
1 + c(n-1) =
1 + 1 + c(n-2) =
1 + 1 + 1 + c(n-3) =
n + c(0) = n
Each time n is decremented, we do
one basic operation.
2. Given the following code:
fun(a, n) {
if (n == 1)
return a;
else {
m = fun(a,n/2);
return m*m; basic_op
}
}
a. What is the output of the algorithm
when n equals 4.
Answer: a4
b. What is the exact bound on the running
time, i.e., f(n)= (?)
Answer: f(n)= (log n)
c. Explain how you figured out the running
time.
Answer:
c(n) = 1 + c(n/2);
c(1) = 0
Thus, c(n) =
1 + c(n/1) =
1 + 1 + c(n/4) =
1 + 1 + 1 + c(n/8) =
log n + c(1) = log n
Each time n is divided by 2, we do one
basic operation. n can only be divided by
two log n times before n will equal 1.
pf3
pf4

Partial preview of the text

Download Quiz 6: Recursion and Time Complexity and more Quizzes Algorithms and Programming in PDF only on Docsity!

CSIS-385: Quiz 6 Name____________________________________

  1. Given the following code: fun(n) { if (n == 0) return; else { basic_op; fun(n-1); } } a. Exactly how many times it the basic operation executed when n equal 3. Answer: 3 b. What is the exact bound on the running time, i.e., f(n)= (?) Answer : f(n)=(n) c. Explain how you figured out the running time. Answer : c(n) = 1 + c(n-1); c(0) = 0 Thus, c(n) = 1 + c(n-1) = 1 + 1 + c(n-2) = 1 + 1 + 1 + c(n-3) = n + c(0) = n Each time n is decremented, we do one basic operation.
    1. Given the following code: fun(a, n) { if (n == 1) return a; else { m = fun(a,n/2); return mm;*  basic_op } } a. What is the output of the algorithm when n equals 4. Answer: a^4 b. What is the exact bound on the running time, i.e., f(n)= (?) Answer : f(n)=(log n) c. Explain how you figured out the running time. Answer : c(n) = 1 + c(n/2); c(1) = 0 Thus, c(n) = 1 + c(n/1) = 1 + 1 + c(n/4) = 1 + 1 + 1 + c(n/8) = log n + c(1) = log n Each time n is divided by 2, we do one basic operation. n can only be divided by two log n times before n will equal 1.
  1. Given the following code: fun(n) { if (n == 1) return; else { for x = 1 to n { basic_op; } fun(n/2) fun(n/2) fun(n/2) } } What is the exact bound on the running time, i.e., f(n)= (?) Answer : ( ) ( ) log 23 f n  n
  2. Given the following code: fun(n) { if (n == 1) return; else { m = n/4; for x = 1 to 10 { for x = 1 to 10 { basic_op; } } fun(m); fun(m); } } What is the exact bound on the running time, i.e., f(n)= (?) Answer : f ( n )( n log^4 2 )( n^0.^5 )( n ) k = 0 because the internal complexity is constant, i.e., it always does 100 basic operations. a = 2 b = 4 2 > 4^0
  3. What is the exact bound, i.e., f(n)= (?), on the total value of these summations? For simplicity assume n is a power of 2 and that it is great than 64. a. f(n) = n + n/2 + n/4 + n/8 + …. 4 + 2 + 1 Answer: f(n)=(n) b. f(n) = n^2 + (n/2)^2 + (n/4)^2 + (n/8)^2 + … + 2^2 + 1 Answer: f(n)=(n^2 )
  1. Write a recursive algorithm to print all the subsets of X. Hint: Use Y to compute all the subsets and use the recursive framework below. All you have to do is decide when to add items to Y and when to remove items for X. main () { Queue X; Stack Y; for (i = 1 to 3) X.enqueue(i); fun(X, Y); } fun (X, Y) { if (X.size() > 0) { Y.push(X.dequeue()) fun(X,Y); Y.pop() fun(X,Y); } else { println(“Y = ”, Y); } }