Sunteți pe pagina 1din 3

Technische Universitat M

unchen
Fakultat f
ur Informatik
Lehrstuhl f
ur Effiziente Algorithmen
Prof. Dr. Harald Racke
Chintan Shah

Wintersemester 2012
Losungsblatt 3
16 November 2012

Effiziente Algorithmen und Datenstrukturen I


Aufgabe 1 (10 Punkte)
1. Solve the following recurrence relation without using generating functions:
an = an1 + 2n1 for n 1 with a0 = 2.
2. Give tight asymptotic upper and lower bounds for T (n):
T (n) = T (n 1) + log n.
Lo
sungsvorschlag
1.
an = an1 + 2n1 and
an1 = an2 + 2n2 for n 2 gives us
an = 3an1 2an2 for n 2
The characteristic polynomial of this recurrence is
2 3 + 2 = 0
The roots are 2 and 1. So, the solution is of the form
an = (2)n + (1)n
a0 = 2 = +
a1 = 3 = 2 +
Solving for and gives us = = 1. Hence,
an = 2n + 1
2.
T (n) =

n
X

log(i)

i=1
n
Y
= log( i)
i=1

= log(n!)
= (n log n)

Aufgabe 2 (10 Punkte)


Give tight asymptotic upper and lower bounds for T (n):
n
n
n
T (n) = T
+T
+T
+ n.
2
4
8
Lo
sungsvorschlag
We guess that T (n) = (n).
We first prove that T (n) = O(n) by induction. The inductive hypothesis is that T (n) cn
for some constant c. Then,
n
n
n
T (n) = T
+T
+T
+n
2
4
8
cn cn cn

+
+
+n
2
4
8
7
=
cn + n
8
7c
= (1 + )n
8
cn, if c 8
This shows that T (n)= O(n).
Further, T (n) = T n2 + T n4 + T
Hence, T (n) = (n).

n
8

+ n = (n).

Aufgabe 3 (10 Punkte)


Consider the following procedure:
RECURSIVE-SORT(A, i, j){
if (A[i] > A[j]) then swap A[i] A[j]
if i + 1 j then return
k b(j i + 1)/3c
RECURSIVE-SORT(A, i, j k)
RECURSIVE-SORT(A, i + k, j)
RECURSIVE-SORT(A, i, j k)
}
1. Argue that RECURSIVE-SORT(A, 1, n) correctly sorts a given array A[1 . . . n].
2. Analyze the running time of RECURSIVE-SORT using a recurrence relation.
L
osungsvorschlag
1. We prove by induction on l = j i + 1, the length of the array, that RECURIVESORT sorts the input. The basis of induction is l = 2. The basis is true, since we
swap the first and last elements of the array if A[1] > A[n] = A[2]. For l 3, we
have k 1 and hence each of the 3 recursive calls is to a problem of smaller size.
Hence by induction, we can assume that these 3 calls sort the respective subarrays.
Further, note that after the first recursive call, the largest k elements can not be
contained in A[1 . . . k]. Hence, after the second recursive call, the largest k elements
are contained in A[j k + 1 . . . j] and in sorted order. After the third recursive call,
the elements other than the k largest elements are also sorted.
2

2. The running time T (n) is given by the recurrence relation


 
2n
+ c, where c is a constant
T (n) = 3T
3
 log 3 
3
or (n2.25 ).
By Case 1 of the Master Theorem, the running time is n 2

Aufgabe 4 (10 Punkte)


Given two n n matrices A and B where n is a power of 2, we know how to find C = A B
by performing n3 multiplications. Now let us consider the following approach. We partition
A, B and C into equally sized block matrices as follows:






A11 A12
B11 B12
C11 C12
A=
B=
C=
A21 A22
B21 B22
C21 C22
Consider the following matrices:
M1
M2
M3
M4
M5
M6
M7

=
=
=
=
=
=
=

(A11 + A22 )(B11 + B22 )


(A21 + A22 )B11
A11 (B12 B22 )
A22 (B21 B11 )
(A11 + A12 )B22
(A21 A11 )(B11 + B12 )
(A12 A22 )(B21 + B22 )

Then,
C11
C12
C21
C22

=
=
=
=

M1 + M4 M5 + M7
M3 + M5
M2 + M4
M1 M2 + M3 + M6

1. Convince yourself that the matrices Cij evaluated as above are indeed correct. Dont
write anything to prove this.
2. Design an efficient algorithm for multiplying two nn matrices based on these facts.
Analyze its running time.
L
osungsvorschlag
1. Refer Strassens Algorithm
2. The running time T (n) is given by the recurrence relation
n
T (n) = 7T
+ cn2 , where c is a constant
2

By Case 1 of the Master Theorem, the running time is nlog2 7 or (nx ) where
x 2.807.
3

S-ar putea să vă placă și