You are on page 1of 5

CS 300

DATA STRUCTURES
Problem Set 2
(Not to be graded)

Problem 1*
The set of extremely boring strings (EBSs) is constructed using the symbols a and b with
the recursive procedure below:

BASIS : The string a is an EBS


IN DU CT ION : If X is an EBS, so are the concatenated strings Xb and Xaa.

For example ab and aaa are two EBSs that can be generated from the basis string a using
the induction step once.

a) List all EBSs of length 4.

b) Write a recurrence for E(n), the number of EBSs of length n. Please be very careful
about the basis case(s). Do NOT attempt to solve the recurrence exactly, but state
it in terms of another function you should already know about.

Problem 2*
A program for an algorithm takes 1 millisecond for an input size of 64. How long will it
take for input size of 2048 if the running time of the program (ignoring lower order terms)
is

a) O(N 2 log N )?

b) cubic ?

c) O( N ) ?

d) quadratic ?

1
Problem 3*
Complete the following table that shows the size of a problem that can be solved with an
algorithm with given complexity function in one hour of CPU time. So for example if the
algorithm has O(log N ) complexity and you can solve a problem of size A in one hour,
what is the size of the problem that you can solve in one hour if the CPU is 100 times
faster?

Complexity Size Size Size


CPU Speed = 1 CPU Speed = 100 CPU Speed = 1000
log N A

log2 N B

2N C
2
2N D

Problem 4*
Write the recurrence for A(n), the number of asterisks (*) printed by the following pro-
gram. Be very careful when you specify your basis cases.

void Mystery(int n)
{
if (n >= 3) {
Mystery(n-3);
cout << "***";
Mystery(n-2);
cout << "**";
Mystery(n-1);
}
}

2
Problem 5*
Suppose you are given the following procedure which computes simultaneously the max-
imum and the minimum of an array of N numbers.

void maxmin(int A[], int nBegin, int nEnd, int *pnMaxResult, int *pnMinResult)
{
int nMax1, nMin1, nMax2, nMin2;
int N = nEnd - nBegin + 1;
if (N == 2) {
if (A[nBegin] < A[nEnd]) {
*pnMaxResult = A[nEnd];
*pnMinResult = A[nBegin];
}
else {
*pnMaxResult = A[nBegin];
*pnMinResult = A[nEnd];
}
return;
}
maxmin(A, nBegin, nBegin+N/2 - 1, &nMax1, &nMin1);
maxmin(A, nBegin+N/2, nEnd , &nMax2, &nMin2);
if (nMax1 < nMax2) {
*pnMaxResult = nMax2;
}
else {
*pnMaxResult = nMax1;
}
if (nMin1 < nMin2) {
*pnMinResult = nMin1;
}
else {
*pnMinResult = nMin2;
}
return;
}

You can assume that N is a power of 2 and that this procedure is called from a main
program such as:

main()
{
int A[]={1,3,5,7,2,4,6,8};
int nMax,nMin;
maxmin(A,0,7,&nMax,&nMin);
printf("Max %d Min %d\n", nMax,nMin);
}

3
a) Write a recurrence that computes the number of comparisons involving array ele-
ments or their maximums or minimums as a function of the array size N .1 Be very
careful about the initial conditions.

b) Solve the recurrence exactly by successive expansion.

Problem 6
Let a, b and c be nonnegative constants. Show that the solution to the recurrence
(
b, for n = 1
T (n) =
aT (n/c) + bn, for n > 1

for n a power of c is 

 O(n), for a < c
T (n) = O(n log n), for a = c
O(nlogc a ), for a > c

Problem 7
The following are some O(. . .) relationships. For each, give values of n0 and c that can
be used to prove the relationship

a) 25n4 − 19n3 + 13n2 − 106n + 77 = O(n4 )

b) 2n+10 = O(2n )

c) n10 = O(3n )

b) log n = O( n)

1
So the comparison in if (N == 2) {.. should not be counted.

4
Problem 8*
Consider the following recursive procedure (where you can assume that n = 2m for some
m):

int f(int n)
{
int b;
if (n == 2) then return(2);
b = f(n/2);
return(b*b*b);
}

a) Write a recurrence for the number of multiplications M (n) that are executed when
computing f(n) and solve it exactly.

b) What is the exact mathematical relationship between n and f (n)?

Problem 9
Consider the following recursive function UselessFunction that takes two arguments a
and b where it is always guaranteed that a ≤ b.
void UselessFunction(int a, int b) {
if (a == b)
cout << "0" << endl;
else {
UselessFunction(a+1, b);
UselessFunction(a, b-1);
}
}

a) How can you define n, the problem size you can use to model the run time of
UselessFunction?

b) Write a recurrence for T (n) where n is the size parameter you chose in a), assuming
that printing takes constant time, and procedure calls take constant additional time
(in addition to the time the procedure itself takes).

c) Express T (n) as O(g(n)).

You might also like