Docsity

Sample Homework 1 with Solution - System Programming | CS 483, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS483 Fall03 - Sample Homework 1, Solutions,Jana Koˇseck´a
1. Consider sorting nnumbers in an array by first finding the smallest element of Aand exchanging
it with A[1], then finding second smallest element and exchanging it with A[2] and continue in this
manner for the first n−1 elements. Write a pseudocode of this algorithm. Why does the algorithm
need to run for only n−1 elements? Give the best-case and worst case running time in terms of Θ
notation.
Pseuso-code of the algorithm :
Selection Sort(A,n)
for i=1 to n-1 do
% find the smallest element in A[i] ... A[n]
min = A[i] ; loc = i ;
for j = i+1 to n do
if A[j] < min then
min = A[j]; loc = j;
endif
end
swap A[i] and A[loc]
end
Analysis:
We have two loops o ne for 1 ...n−1 and one for i...n, and suppose that number of operations with
a loop will take constant time c1and c2. Then total time in the worst case will be
T(n)=
n−1
X
i=1
(c1+
n
X
j=i+1
c2)=
n−1
X
i=1
(c1+c2(n−i))
=c1(n−1) + c2
n
X
i=1
i=c1(n−1) + c2n(n−1)/2=>T(n)=Θ(n2)
2. Compare the functions (3
2)n,n
3,lg2n, 22n,1.
Answer (using either limit or definition) :
1=O(lg2n),lg
2n=O(n3),n
3=O((3
2)n),3
2)n=O(22n).
3. Compute the solutions of the following recurrences:
a. T(n)=4T(n/2) + n2
Using Master’s theorem: a=4,b=2,f(n)=n2,log24=2
Case 2 applies: f(n)=Θ(n2)=>T(n)=Θ(n2lg n).
b. T(n)=T(n−1) + nfor n≥2andT(1) = 1
T(n)=T(n−2) + n−1+n=...=1+2+3+...+(n−1) + n== n(n−1)/2=>T(n)=Θ(n2)
c. T(n)=1forn=1andT(n)=3T(n
2)+nlogn for n>1
Master’s theorem: a=3,b =2f(n)=nlg n, lg23=1.585... > 1. Then f(n)=O(nlg23−)forsome
>0 (e.g. using limits). Case 1 applies T(n)=Θ(nlg23).
d. T(n)=6T(n
5)+nlg6n
a=6,b=5;f(n)=nlg6n, log56=1.11...
Case 1 applies: f(n)=O(n1.1−)=>T(n)=Θ(nlog56).
4. Given the recurrence equation T(n)=2T(n
2)+2 and T(1) = 1. Prove by induction that T(n)=3n−2.
Base case: T(1) = 3.2−2=1.Induction hypothesis: T(k)=2T(k
2)−2for0<k<n.Toprove:
T(n)=2T(n
2) + 2 use induction hypothesis for T(n
2). Then T(n)=2.(3 n
2−2) + 2 = 3n−2.
5. (10) Solve the following recurrence by appealing to the recurrence tree.
T(n)=T(n/3) + T(2n/3) + cn
Lower bound is provided by the length of the shortest path in the recurrence tree. In this case the
shortest branch has length lg3nand since at each level the cost is cn then T(n)=Ω(nlg3n)=Ω(nlg n).

Partial preview of the text

Download Sample Homework 1 with Solution - System Programming | CS 483 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS483 Fall03 - Sample Homework 1, Solutions, Jana Koˇseck´a

  1. Consider sorting n numbers in an array by first finding the smallest element of A and exchanging it with A[1], then finding second smallest element and exchanging it with A[2] and continue in this manner for the first n − 1 elements. Write a pseudocode of this algorithm. Why does the algorithm need to run for only n − 1 elements? Give the best-case and worst case running time in terms of Θ notation.

Pseuso-code of the algorithm : Selection Sort(A,n) for i=1 to n-1 do % find the smallest element in A[i] ... A[n] min = A[i] ; loc = i ; for j = i+1 to n do if A[j] < min then min = A[j]; loc = j; endif end swap A[i] and A[loc] end

Analysis: We have two loops one for 1... n − 1 and one for i... n, and suppose that number of operations with a loop will take constant time c 1 and c 2. Then total time in the worst case will be

T (n) =

n∑− 1

i=

(c 1 +

∑^ n

j=i+

c 2 ) =

n∑− 1

i=

(c 1 + c 2 (n − i))

= c 1 (n − 1) + c 2

∑^ n

i=

i = c 1 (n − 1) + c 2 n(n − 1)/ 2 => T (n) = Θ(n^2 )

  1. Compare the functions ( 32 )n, n^3 , lg^2 n, 22

n , 1. Answer (using either limit or definition) : 1 = O(lg^2 n), lg^2 n^ =^ O(n^3 ), n^3 =^ O((^32 )n),^32 )n^ =^ O(2^2

n ).

  1. Compute the solutions of the following recurrences:

a. T (n) = 4T (n/2) + n^2 Using Master’s theorem: a = 4, b = 2, f (n) = n^2 , log 2 4 = 2 Case 2 applies: f (n) = Θ(n^2 ) => T (n) = Θ(n^2 lg n). b. T (n) = T (n − 1) + n for n ≥ 2 and T (1) = 1 T (n) = T (n − 2) + n − 1 + n =... = 1 + 2 + 3 +... + (n − 1) + n == n(n − 1)/2 => T (n) = Θ(n^2 ) c. T (n) = 1 for n = 1 and T (n) = 3T ( n 2 ) +^ nlogn^ for^ n >^1 Master’s theorem: a = 3, b = 2f (n) = n lg n, lg 2 3 = 1.^585 ... >^ 1. Then^ f^ (n) =^ O(nlg^2 3 −) for some  > 0 (e.g. using limits). Case 1 applies T (n) = Θ(nlg^2 3 ). d. T (n) = 6T ( n 5 ) + nlg^6 n a = 6, b = 5; f (n) = nlg^6 n, log 5 6 = 1. 11 ... Case 1 applies: f (n) = O(n^1.^1 −) => T (n) = Θ(nlog^5 6 ).

  1. Given the recurrence equation T (n) = 2T ( n 2 )+2 and^ T^ (1) = 1. Prove by induction that^ T^ (n) = 3n−2. Base case: T (1) = 3. 2 − 2 = 1. Induction hypothesis: T (k) = 2T ( k 2 ) − 2 for 0 < k < n. To prove: T (n) = 2T ( n 2 ) + 2 use induction hypothesis for T ( n 2 ). Then T (n) = 2.(3 n 2 − 2) + 2 = 3n − 2.
  2. (10) Solve the following recurrence by appealing to the recurrence tree.

T (n) = T (n/3) + T (2n/3) + cn Lower bound is provided by the length of the shortest path in the recurrence tree. In this case the shortest branch has length lg 3 n and since at each level the cost is cn then T (n) = Ω(n lg 3 n) = Ω(n lg n).