Sie sind auf Seite 1von 3

Assignment 3

Shahrzad TighnavardMollasaraei 260413622 February 29, 2012

Q2.a)

T (n) = 2T (n 1) + n = 2(2T (n 2) + (n 1)) + n = 22 T (n 2) + 2(n 1) + n = 22 (2T (n 3) + (n 2)) + 2(n 1) + n = 23 T (n 3) + 22 T (n 2) + 2(n 1) + n


...

= 2k T (n k) +

k1 i=0

2i (n i)

if k=n-1 the recurrence stops.

= 2n1 T (1) + 1=0 2i (n i) n2 n2 = 2n1 + n i=0 2i i=0 i.2i n1 n2 ((n 2) 1))) = 2n1 + n 12 12 (2(1 + 2 n1 n1 =2 n(1 2 ) (2 + 2n1 (n 3)) = 2n1 n + n2n1 2 n + 3.2n1 = 22 .2n1 (n + 2) n+1 the explicit formula is T (n) = 2 (n + 2) 1+1 b) base case: T (1) = 2 (1 + 2) = 1 k+1 induction step: assume S(k) : T (k) = 2 (k + 2) if S(k) is true S(k+1) k+2 holds too meaning S(k + 1) : T (k + 1) = 2 (k + 3) T (k + 1) = 2k+2 (k + 3) = 2T ((k + 1) 1) + (k + 1) = 2T (k) + (k + 1)
and by our assumption

n11

2(2k+1 (k + 2)) + (k + 1) = 2k+2 2(k + 2) + k + 1 = 2k+2 (k + 3)


Q3.a) Algorithm CountSunsets(altitudes, n) Input: An array altitudes[0...n-1] Output: The number of sunsets witnessed. int sunCount,imax1,imax2,maxalt1,maxalt2

/*int variables for holding the index of sunsets, the index of the rst maximum alt and the 2nd maximum alt and the actual 1st and 2nd maximum altitudes*/ int k for i

altitudes.length/5 1 to array.length do imax2;


maxalt1

if(i>k&&(imax1<i-k)) //if we are not in the k range of our rst max then imax1

maxalt2;

maxalt2

0;

if(altitudes[i]>maxalt1)then/*if the altitude at the current index is bigger than our rst max then sunset can be seen and we reset out rst max alt to the alt at the current index*/ count++; max1

altitudes[i];

imax1

i;

else if (altitudes[i]>maxalt2)then//setting the 2nd max alt max2

altitudes[i];

indexMax2

i;

b) the algorithm runs in O(n) because we only have one for loop which has n iterations and the step inside the for loops happen c times so its cn times which is O(n) Q4.

AlgorithmorderCards(n) Input: An integer n Output: Prints the correct card ordering


if(n=1) then print(1) if(n=2)then print(2,1) if(n>=3) then {Deque cards//create a deque of integer using linkedlist or another structure cards.addFirst(n-1); cards.addFirst(n); cards.addFirst(n-2); for i<-0 to n-3 do { cards.addFirst(cards.getLast()); cards.removeLast(); cards.addFirst(n-3-i); } print(cards) } Q5.i)proving that is log(n!)O(nlogn) log(n!)=log(n(n-1)(n-2)...(1)) =log(n)+log(n-1)+log(n-2)+...+log(1) =<log(n)+log(n)+log(n)+...+log(n) =nlog so log(n!)=<nlog(n) for all n>=1, where c=1 log(n!)is O(nlog(n)) We need to show that log(n!) and (n log n), i.e., that there are constant c > 0

n0

log((n

such that log(n!) 1)!)

n + log((n log(n

c(n

1)!)

log n + c(n

cn log n for every n

1) log(n

1)

log n

= =

n0

Induction hypothesis:

1). We have: log(n!) = log(n.(n 1) log(n

1)!) = log

1) (1) Now, we need to show that

o for a very small epsilon. A constant o is not enough, as

we would not get the claim, we need something of order 1/n. For this we will use the Taylor expansion of the exponential function

ex = 1 +

x 1!

x2 2!

+ ...

Hence, for any x > 0, we have

ex > 1 + x.

Substituting

1 n1

for x, we have

1 n1

>1+

> log(n/(n e/(n

1 n1

n 1

1)) = log n

. The logarithm on both sides, we get 1/(n

log(n

1) , and hence, log(n

1). log e

1) > log n

log

1) , which is what we wanted. Now, let's plug it back to (1): log(n!)

log n + c(n + c(n

1) log(n

1) log n

to show that (1 show that log n 1/2 and n

= =

1) > log n + c(n

c log= cn log n + (1

c) log n

c log e

0.

1)(log n

c) log n

log e/(n

1)) = log n

c log e Now, it is enough

Let's set c = 1/2. Then it's enough to

log e, i.e., n

e. This is true, for all n

3. Hence, for c =

3, it follows that log(n!)

cn log n , which nishes the induction

step. It is easy to check the base case, which we can set in this case to n = 2 (since, induction step can be done for any n = 1

3). For n = 2, we have log(2!)

1/2.2 log 2 = 1, and hence log(n!)

cn log n is true in the base case as

well. To summarize we have log(n!)

1/2n log n, for every n

n0

= 2. Done!

Das könnte Ihnen auch gefallen