Study Guide for Exam - Algorithm Design and Analysis | CISC 621, Exams of Computer Science

Material Type: Exam; Class: Algorithm Design and Analysis; Subject: Computer/Information Sciences; University: University of Delaware; Term: Unknown 1989;

Typology: Exams

Pre 2010

Uploaded on 09/02/2009

koofers-user-1d4
koofers-user-1d4 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CISC 621: Algorithm Design and Analysis
H/W H: Partition
Model Answer
1. A call to 'partition' first calculates the sum of the elements in the set taking ฮ˜(n) time.
It then calls 'SubsetSum' on the n elements if the sum is even. Thus the time taken by
'partition' in the worst case is:
T(n) = TSubsetSun (n) + ฮ˜(n)
In the worst case every call to 'SubsetSum' will make two recursive calls to itself on an
input of size (n โ€“ 1). Thus the time taken by 'SubsetSum' on n elements is given by the
following recurrence relation:
TSubsetSun (n) = 2 TSubsetSun (n - 1) + ฮ˜(1) .1
The above recurrence may be solved in two ways:
1. Using the substitution method:
Since the question tells us that we can prove O(2 n ), we guess that the solution is
TSubsetSun(n) = O(2 n ). To prove by induction we note that the recurrence holds for the
boundary conditions n = 0 and for n = 1 and we assume that it holds for TSubsetSun(n โ€“
1), that is:
TSubsetSun(n โ€“ 1) โ‰ค c2 n โ€“ 1 โ€“ b .2
where b is some constant. Substituting 2 in 1 (replacing ฮ˜(1) by a constant d), we
get:
TSubsetSun(n) = 2 (c2 n โ€“ 1 โ€“ b) + d
TSubsetSun(n) = c2 n โ€“ 2b + d
TsubsetSun(n) โ‰ค c2 n โ€“ b
Where the last inequality holds if (2b โ€“ d) โ‰ค b or b โ‰ค d for all values of c. Thus we
get:
T(n) = O( 2n )
2. Using the recurrence tree method:
Drawing the recurrence tree (shown overleaf), we get:
TsubsetSun(n) = ฮ˜(1) (20 + 21 + 22 + 23 + 24 ... ... 2n )
TsubsetSun(n) =
๎‚ฎ๎‚ž 1๎‚Ÿ ๎‚ฒ๎‚ži=0๎‚Ÿ
n๎‚ž2i๎‚Ÿ=๎‚ฎ๎‚ž1๎‚Ÿ๎‚ž2๎‚žn๎‚ƒ1๎‚Ÿโˆ’1๎‚Ÿ
๎‚ž2โˆ’1๎‚Ÿ=๎‚ฎ๎‚ž1๎‚Ÿ๎‚ž2๎‚žn๎‚ƒ1๎‚Ÿโˆ’1๎‚Ÿ=O๎‚ž2n๎‚Ÿ
pf2

Partial preview of the text

Download Study Guide for Exam - Algorithm Design and Analysis | CISC 621 and more Exams Computer Science in PDF only on Docsity!

CISC 621: Algorithm Design and Analysis

H/W H: Partition Model Answer

  1. A call to 'partition' first calculates the sum of the elements in the set taking ฮ˜(n) time. It then calls 'SubsetSum' on the n elements if the sum is even. Thus the time taken by 'partition' in the worst case is: T(n) = TSubsetSun (n) + ฮ˜(n) In the worst case every call to 'SubsetSum' will make two recursive calls to itself on an input of size (n โ€“ 1). Thus the time taken by 'SubsetSum' on n elements is given by the following recurrence relation: TSubsetSun (n) = 2 TSubsetSun (n - 1) + ฮ˜(1). The above recurrence may be solved in two ways:
    1. Using the substitution method: Since the question tells us that we can prove O(2 n^ ), we guess that the solution is TSubsetSun(n) = O(2 n^ ). To prove by induction we note that the recurrence holds for the boundary conditions n = 0 and for n = 1 and we assume that it holds for TSubsetSun(n โ€“ 1), that is: TSubsetSun(n โ€“ 1) โ‰ค c2 n โ€“ 1^ โ€“ b. where b is some constant. Substituting 2 in 1 (replacing ฮ˜(1) by a constant d), we get: TSubsetSun(n) = 2 (c2 n โ€“ 1^ โ€“ b) + d TSubsetSun(n) = c2 n^ โ€“ 2b + d TsubsetSun(n) โ‰ค c2 n^ โ€“ b Where the last inequality holds if (2b โ€“ d) โ‰ค b or b โ‰ค d for all values of c. Thus we get: T(n) = O( 2n^ )
    2. Using the recurrence tree method: Drawing the recurrence tree (shown overleaf), we get: TsubsetSun(n) = ฮ˜(1) (2^0 + 2^1 + 2^2 + 2^3 + 2^4 ... ... 2n^ ) TsubsetSun(n) = ๎‚ฎ๎‚ž 1 ๎‚Ÿ ๎‚ฒ๎‚ž i = 0 ๎‚Ÿ n ๎‚ž 2 i ๎‚Ÿ=๎‚ฎ๎‚ž 1 ๎‚Ÿ

๎‚ž n ๎‚ƒ 1 ๎‚Ÿ โˆ’ 1 ๎‚Ÿ ๎‚ž 2 โˆ’ 1 ๎‚Ÿ

๎‚ž n ๎‚ƒ 1 ๎‚Ÿ โˆ’ 1 ๎‚Ÿ= O ๎‚ž 2 n ๎‚Ÿ

2.The above worst case of O(2n) was obtained by assuming the binary tree that we get is complete and has a depth of n. To prove that the worst case has a lower bound of 2n (that is, the worst case is ฮฉ(2n)) we have to find a sequence of n elements that result in a recurrence tree that is a complete binary tree with a depth of n. To get such a recurrence tree, assume a data set 2, 4, 8,... 2n. In this data set, the last term is greater than the sum of all the previous terms. For this data set, the test if (T < *b) will be false for all but the last element of the data set. Hence, in each case other than the last element, we will get two calls to SubsetSum and hence we will obtain a recurrence tree which is a complete binary tree of depth n. Thus, by the recurrence tree shown above, we get: T(n) = ฮฉ( 2n^ ) 3.We may do slightly better in the average case by performing a number of checks: 1.If a single element is greater than the sum of half the elements, return false. 2.Perform a check for T โ‰ค 0, if true, return false. However, we cannot do much better in the worst case as this problem has a run time of ฮ˜( 2n^ ) and hence belongs to a class of NP complete problems and till date no one has been able to prove that NP = P. ฮ˜(n) ฮ˜(n) ฮ˜(n) ฮ˜(n) ฮ˜(n) ฮ˜(n) ฮ˜(n) T(0) T(0) T(0) T(0)

2 n