Upload
hisuin
View
214
Download
0
Embed Size (px)
DESCRIPTION
lo03
Citation preview
Technische Universitat MunchenFakultat fur InformatikLehrstuhl fur Effiziente AlgorithmenProf. Dr. Harald RackeChintan Shah
Wintersemester 2012Losungsblatt 3
16 November 2012
Effiziente Algorithmen und Datenstrukturen I
Aufgabe 1 (10 Punkte)
1. Solve the following recurrence relation without using generating functions:
an = an−1 + 2n−1 for n ≥ 1 with a0 = 2.
2. Give tight asymptotic upper and lower bounds for T (n):
T (n) = T (n− 1) + log n.
Losungsvorschlag
1.
an = an−1 + 2n−1 and
an−1 = an−2 + 2n−2 for n ≥ 2 gives us
an = 3an−1 − 2an−2 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∑
i=1
log(i)
= log(n∏
i=1
i)
= log(n!)
= Θ(n log n)
Aufgabe 2 (10 Punkte)Give tight asymptotic upper and lower bounds for T (n):
T (n) = T(n
2
)+ T
(n4
)+ T
(n8
)+ n.
Losungsvorschlag
We guess that T (n) = Θ(n).We first prove that T (n) = O(n) by induction. The inductive hypothesis is that T (n) ≤ cnfor some constant c. Then,
T (n) = T(n
2
)+ T
(n4
)+ T
(n8
)+ n
≤ cn
2+cn
4+cn
8+ n
=7
8cn+ n
= (1 +7c
8)n
≤ cn, if c ≥ 8
This shows that T (n) = O(n).Further, T (n) = T
(n2
)+ T
(n4
)+ T
(n8
)+ n = Ω(n).
Hence, T (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 returnk ← b(j − i+ 1)/3cRECURSIVE-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.
Losungsvorschlag
1. We prove by induction on l = j − i + 1, the length of the array, that RECURIVE-SORT sorts the input. The basis of induction is l = 2. The basis is true, since weswap the first and last elements of the array if A[1] > A[n] = A[2]. For l ≥ 3, wehave 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 becontained in A[1 . . . k]. Hence, after the second recursive call, the largest k elementsare 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
T (n) = 3T
(2n
3
)+ c, where c is a constant
By Case 1 of the Master Theorem, the running time is Θ(nlog 3
23)
or ω(n2.25).
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 ·Bby performing n3 multiplications. Now let us consider the following approach. We partitionA, B and C into equally sized block matrices as follows:
A =
[A11 A12
A21 A22
]B =
[B11 B12
B21 B22
]C =
[C11 C12
C21 C22
]Consider the following matrices:
M1 = (A11 + A22)(B11 +B22)
M2 = (A21 + A22)B11
M3 = A11(B12 −B22)
M4 = A22(B21 −B11)
M5 = (A11 + A12)B22
M6 = (A21 − A11)(B11 +B12)
M7 = (A12 − A22)(B21 +B22)
Then,
C11 = M1 +M4 −M5 +M7
C12 = M3 +M5
C21 = M2 +M4
C22 = M1 −M2 +M3 +M6
1. Convince yourself that the matrices Cij evaluated as above are indeed correct. Don’twrite anything to prove this.
2. Design an efficient algorithm for multiplying two n×n matrices based on these facts.Analyze its running time.
Losungsvorschlag
1. Refer Strassen’s Algorithm
2. The running time T (n) is given by the recurrence relation
T (n) = 7T(n
2
)+ cn2, where c is a constant
By Case 1 of the Master Theorem, the running time is Θ(nlog2 7
)or Θ (nx) where
x ≈ 2.807.
3