Beruflich Dokumente
Kultur Dokumente
Q2.a)
= 2k T (n k) +
k1 i=0
2i (n i)
= 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
/*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
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;
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.
n0
log((n
n + log((n log(n
c(n
1)!)
log n + c(n
1) log(n
1)
log n
= =
n0
Induction hypothesis:
1)!) = log
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!
+ ...
ex > 1 + x.
Substituting
1 n1
for x, we have
1 n1
>1+
1 n1
n 1
1)) = log n
log(n
1). log e
1) > log n
log
1) log(n
1) log 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
log e, i.e., n
3. Hence, for c =
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
n0
= 2. Done!