Docsity

Computer Science 245 Homework 10 Review: Algorithm Analysis - Prof. David J. Galles, Assignments of Data Structures and Algorithms

Toggle sidebar
bg1
Computer Science 245
Homework 10
Final Reveiw
Due Wednesday, May 13th, 2009
1. Give the Θ() running time for each of the following functions, in terms of the input parameter n:
(a) int f(int n) {
int i;
sum = 0;
for (i=0; i<n; i=i+2)
sum++;
return sum;
}
(b) int g(int n) {
int i;
sum = 0;
for (i=0; i<n; i=i+1)
sum += f(n); // function f from part a
return sum;
}
(c) int h(int n) {
for (i=1; i<n; i=i*2)
sum += f(n); // function f from part a
return sum;
}
2. For each of the following recursive functions, describe what the function computes, give the recurrence
relation that describes the running time for the function, and then solve the recurrence relation.
(a) int recursive1(int n) {
if (n <= 1)
return 1;
else
return recursive1(n-2) + recursive1(n-2);
}
(b) int recursive2(int n) {
if (n <= 1)
return 1;
else
return 2 * recursive2(n-2);
}
3. Look over all the visualizations for algorithms used in the class, be sure you know how all of them
work. (Nothing to turn in for this, but you really should do it, to help for the final)
1

Partial preview of the text

Download Computer Science 245 Homework 10 Review: Algorithm Analysis - Prof. David J. Galles and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Computer Science 245

Homework 10

Final Reveiw

Due Wednesday, May 13th, 2009

  1. Give the Θ() running time for each of the following functions, in terms of the input parameter n:

(a) int f(int n) { int i; sum = 0; for (i=0; i<n; i=i+2) sum++; return sum; } (b) int g(int n) { int i; sum = 0; for (i=0; i<n; i=i+1) sum += f(n); // function f from part a return sum; } (c) int h(int n) { for (i=1; i<n; i=i*2) sum += f(n); // function f from part a return sum; }

  1. For each of the following recursive functions, describe what the function computes, give the recurrence relation that describes the running time for the function, and then solve the recurrence relation.

(a) int recursive1(int n) { if (n <= 1) return 1; else return recursive1(n-2) + recursive1(n-2); } (b) int recursive2(int n) { if (n <= 1) return 1; else return 2 * recursive2(n-2); }

  1. Look over all the visualizations for algorithms used in the class, be sure you know how all of them work. (Nothing to turn in for this, but you really should do it, to help for the final)