Académique Documents
Professionnel Documents
Culture Documents
Problem Set
Week 1
April/May 2015
Overview of Algorithm Design
1. Write down the problem definition for finding the median of a set of positive integers. Make sure
you carefully write the input, output and any special conditions or constraints that are to be
applicable.
ANS:
ALGORITHM Median ( A[0 ... n-1])
// finds the median of set of positive integers .
// INPUT: A[0 ... n-1] an array of positive integers which are sorted in ascending order.
// OUTPUT: M- median of the given set of integers.
low<- 0;
high<- n-1;
M<-0;
mid<- (low+high)/2;
M<- A[mid];
return M;
2. Consider the problem of finding the largest and smallest of n distinct positive integers. Write
down the general recursive definition using the concept of 'splitting' of the original list. Analyze the
initial solution using (a) recurrence relations and Master Theorem and also using (b) recursion trees
generated. Determine the optimal split, develop the final algorithm and present the same.
ANS:
To find Largest
ALGORITHM Large ( A[0.... n-1], i, big)
// To find the largest of n distinct positive integers using recursive definition
// INPUT: A[ 0.. n-1] an array holding the n distinct integers
i- holds the index of the current integer under consideration
big holds the big
// OUTPUT: big- Which holds the largest among the integers from index 0 to i.
If i == n ? return big;
else if big> A[i] ?
return Large ( A[0.. n-1] , i+i, big);
else
return Large ( A[0 ... n-1] , i+1, A[i]);
To find smallest
ALGORITHM Least ( A[0.... n-1], i, small)
// To find the smallest of n distinct positive integers using recursive definition
// INPUT: A[ 0.. n-1] an array holding the n distinct integers
i- holds the index of the current integer under consideration
small holds the small
// OUTPUT: small - Which holds the least among the integers from index 0 to i.
If i == n ? return small ;
else if small < A[i] ?
return Least ( A[0.. n-1] , i+i, small);
else
return Least( A[0 ... n-1] , i+1, A[i]);
ANALYSIS
step1: Parameter to be considered in the above algorithms is n- which is the size of the array A[]
i.e. The no of integers given.
step 2: identification of basic operation : The basic operation in both the above algorithms is when
the value of i is equal to the value of n .
step3: consider
{
} OR
{
else if small < A[i] ?
return Least ( A[0.. n-1] , i+i, small);
else
return Least( A[0 ... n-1] , i+1, A[i]);
}
in both the above set of codes, the recursive call is made only once since it is a if- else statement.
i.e. Even though there are two recurrsive calls in both the code, only one among them will be
executed. And the recurrsion continues until the basic condition is satisfied.
So the no of recursive calls done is equal to the no of items in the list. The termination is only when
you reach the end of the list no matter of the value of your big or small.
i.e n;
hence time complexity of the algorithms can be stated as Big Theta( n)
Optimal Split would be n/2 as in case of the binary search algorithm.
Analysis based on master theorm
T(n) = {
big Theta( nd )
if a<bd
if a>bd
}
here- d=
a=1 b= 2 d= 1
nd = n
hence we have a<bd i.e 1 < 21
therefore we get Big Theta(n) ..
3. In the following C function, let n m.
int GCD(int n, int m)
{
if (n%m==0)
return m;
n=n%m;
return GCD(m,n);
}
How many recursive calls are made by this function?
ANS:
T(n) = 1+ 1. T( n-m -1)
=> 1+1 +T(n- m-2)
=> 2+1+T(n- m -3)
.
.
= i+ T(n- m-i)
now let i=1 ( initial condition)
then
T(n) = n+T(n-m -n)
= n+ T( m)
hence BigTheta(nm)
4. Write down the recursive definition for the problem of finding n Fibonacci numbers. If you did
not remember identical subproblems, how many recursive calls would the algorithm make? Now
write an alternative recursive definition in such a way that you can computer the Fibonacci numbers
in O(n) time but without the need to have a separate array to remember the previously computed
values.
ANS:
ALGORITHM Fibnocci ( n)
// Finds the nth fibin bd occi number
// INPUT: n: a positive integer
// OUTPUT: nth fibinocci number
if n ==0 ? return 0;
if n == 1? return 1;
return Fibinocci(n-1) + Fibinocci(n-2) ;
Number of recursive calls made is
t(n) = t(n-1) + t(n-2)
= t( n-2) + t(n-3) + t( n-3) + t( n-4)
= t(n-2) + 2 t(n-3) + t(n-4)
=> t( n-3) + t(n-4) + 2( t(n-4) + t(n-5) ) + t( n-5) + t( n-6)
= t( n-3) + 3 t(n-4) + 3 t(n-5) + t(n-6)
...
= t(n-i) + i(
when i= 1 t(n) = 1
= i.t(n)
No of recursive calls is (n-1) 2
5. Consider the problem of Finding the Median of n positive distinct integers. Write down the
recursive definition by a simple modification of Quicksort by choosing a pivot element randomly
and splitting the list based on the elements less than or greater than the pivot and then performing
recursion. What is the complexity of this scheme in the average case?
ANS:
Algorithm: SELECT(A[1, n], k)
// finds the Median of the set of numbers using the divide and conquer technique
// INPUT: a[1 .. n] holds the set of integers
k holds the pivot
// OUTPUT: Median
Split A into n/N groups of N elements each. (b) Let bi be the median of the ith group.
Let B = [b1, b2, , bn/N ].
medianB = SELECT(B, B.length/2).
Rearrange A so that all elements smaller than medianB come before medianB, all elements larger
than
medianB come after medianB, and elements equal to medianB are next to medianB.
j = position of medianB in rearranged A (if more medianBs, then take the closest position to n/2).
(g) If k < j return SELECT(A[1, j 1], k).
if k = j return medianB.
If k > j return SELECT(A[j + 1, n], k j).
Elements of Algorithm Analysis
6. Find the optimal bound for the following functions by long-hand computation or by
hypothesisandtest:
= t(n-3) + 6n 9
= t( n-4) + 8n 16
...
= t(n-i) + 2*i*n -i 2
when i == n
=2n 2 n 2
= (2-1) n 2
=n2
2. t(m, n) = 2 t(m/2, n/2) + mn
=2( 2 t( m/4, n/4) +
(m+n) /2 ) + mn
when k == m
.. 7. Solve the following recurrence relations by using the Master Theorem. Validate every solution by
long-hand computation or by hypothesisandtest.
ANS:
1. a = 5
b= 2 d= 3
d
b =8 >5
therefore t( n) = BigTheta( n3 )
2. a= 8
b= 4 d = 3/2
bd = 4 <8
hence t(n) = Bigtheta( n
3. a =7 b =2 d = 1
bd = 2< 7
t(n) = Bigtheta( n* ( log 1 to the base 2) )
8. Consider a generalization of the linear-time algorithm to find the kth smallest element from an
array A where
A.length = n, 1 k n, and N is an odd natural number (N = 5 in the video).
Algorithm: SELECT(A[1, n], k)
(a) Split A into n/N groups of N elements each. (b) Let bi be the median of the ith group.
(c) Let B = [b1, b2, , bn/N ].
(d) medianB = SELECT(B, B.length/2).
(e) Rearrange A so that all elements smaller than medianB come before medianB, all elements larger
than
medianB come after medianB, and elements equal to medianB are next to medianB.
(f ) j = position of medianB in rearranged A (if more medianBs, then take the closest position to
n/2). (g) If k < j return SELECT(A[1, j 1], k).
(h)If k = j return medianB.
(i) If k > j return SELECT(A[j + 1, n], k j).
Answer the following:
(a) Prove that SELECT(A[1, n], k) works correctly for any odd N 3.
(b) Derive the recurrence relation for T (n, N ) where T (n) is the time taken by SELECT(A[1, n], k)
for an odd constant N .
(c) Show that T (n, 5) = O(n) (solved in the video). (d) Show that T (n, 3) > O(n).
(e) Compute the upper bound of T (n, N ), for N 5.
(f ) Justify that N = 5 is optimal for this algorithm.
ANS:
-
9. You have 4 functions to compute the gcd of two natural numbers. Compare these functions with
respect to their time and space complexities for the following cost model and suggest which
function/s to use.
ANS:
if n=1
t(n/2) +1 otherwise
}
solving this we get
t(n) = i * (log 2 to the base 2 )
ANS:
15. Consider the Huffman Coding problem of constructing the optimal prefix code. Huffman coding
is used for compressing files by finding unique short binary character codes to represent each
symbol in the file so that no ambiguity results during decryption. Variable length prefix codes
(Codes in which no codeword is also a prefix of some other codeword) should be used. If the
coding is represented using a binary tree T , each branch labelled by a 0 (for the left branch) or a
1 (for the right branch), whose leaves are the given characters, then the binary codeword for a
character is the path from the root to the character. An optimal code for a file is always represented
by a full binary tree (every nonleaf node has two children).
Let C be the alphabet from which the characters are drawn.Let f (c) denote the frequency of c C
in the givenfile, and let dT (c) denote the depth of cs leaf in the tree (the length of the binary
codeword). The number of bits required to encode the file is therefore:
B(T ) = cC f (c)dT (c)
which is called the cost of the tree T .
The Huffman coding problem requires that a coding be constructed the produces the minimum cost
tree T . Develop a recursive definition based solution for the Huffman coding problem and derive
the final algorithm. Explain which basic design method you are following.
ANS: