Universität Siegen Algorithmics 1
Lehrstuhl Theoretische Informatik WS 2020/21
Markus Lohrey
Exercise 6
Task 1
Given the following Fibonacci heap:
7 17 24
23 21 18 30 26 46
52 39 38 35
41
Perform the following operations in that order:
delete-min, decrease-key(“52”, 9), decrease-key(“46”, 3), insert(42), delete-min,
decrease-key(“35”, 7)
Solution
1. delete-min: The node with key 7 gets deleted.
23 21 18 17 24
52 39 38 30 26 46
41 35
And we tidy the forest a bit.
23 17 24
21 30 18 26 46
52 39 38 35
41
1
2. decrease-key(“52”, 9): 9 moves up, 21 gets marked.
23 9 17 24
21 30 18 26 46
39 38 35
41
3. decrease-key(“46”, 3): 3 moves up, 24 cannot be marked.
23 9 17 24 3
21 30 18 26
39 38 35
41
4. insert(42): Inserting 42 as a new tree.
23 9 17 24 3 42
21 30 18 26
39 38 35
41
5. delete-min: Node with key 3 gets deleted and we tidy the forest.
2
17 9 42
21 30 18 23 24
39 38 26
41 35
6. decrease-key(“35”, 7): 7 moves up and 26 as well, since it is marked (but loses its
mark). 24 gets marked.
17 7 26 9 42
21 30 18 23 24
39 38
41
Task 2
Show Theorem 17 from the lecture: For all k ≥ 0 we have
√ !k+1 √ !k+1
1 1+ 5 1 1− 5
Fk = √ −√ .
5 2 5 2
On the last sheet we used F0 = 0 and F1 = 1, but here we use F0 = F1 = 1!
Solution √ √
Let x2 = x + 1. The two solutions to this equation are r := 1+2 5 and s := 1−2 5 , so we
know that r2 = r + 1 and s2 = s + 1.
For k = 0 we have
√ √ !
1 1 1 1 1 1 1+ 5 1− 5
√ r − √ s = √ (r − s) = √ − = 1 = F0
5 5 5 5 2 2
For k = 1 we have
1 1 1 1 1
√ r2 − √ s2 = √ (r2 − s2 ) = √ ((r + 1) − (s + 1)) = √ (r − s) = 1 = F1
5 5 5 5 5
3
Assume the statement is already true for k + 1. Now we prove it for k + 2:
Fn+2 = Fn+1 + Fn
1 1 1 1
= √ rk+1 − √ sk+1 + √ rk − √ sk
5 5 5 5
1
= √ rk+1 − sk+1 + rk − sk
5
1
= √ rk (r + 1) − sk (s + 1)
5
1
= √ rk+2 − sk+2
5
1 k+2 1
=√ r − √ sk+2
5 5
The induction still works fine, if we use the convention from Sheet 5 for the Fibonacci
numbers. Just the formula changes to
√ !k √ !k
1 1+ 5 1 1− 5
Fk = √ −√ .
5 2 5 2
Task 3
Prove or disprove: The height of a Fibonacci heap of size n is at most O(log n).
Solution
Wrong: A Fibonacci heap of size n can have height n. In order to prove this, we will fix
some notation. A Fibonacci heap is a forest, where the roots of the trees have pointers.
For trees t1 , t2 , . . . , tl we write [t1 t2 · · · tl ] for the corresponding Fibonacci heap. For a tree
t we write a(s1 · · · sj ), if a is its root and the si are the subtrees pointing at root a.
We can get a Fibonacci heap of size 1 with height 1 by a single call to insert. Assume we
have a Fibonacci heap of size n with height n, say [a(t)], so t has height n−1 and size n−1.
We add three nodes (three calls to insert) with value b < a (so b is the smallest value in
the whole forest). This yields [bbba(t)]. Then we do one call to delete-min: This removes
one of the three nodes we just added: [bba(t)]. It also combines the other two new nodes
into a tree of rank 1, since both have rank 0: [b(b)a(t)] (rank = number of children). This
tree in turn is combined with the old tree, since it also has rank 1: [b(a(t)b)]. Since b is
the smallest value, it became the new root node. By deleting the single child node labelled
with b (by calling decrease-key on it and then delete-min) we obtain [b(a(t))] which is a
tree of size n + 1 and height n + 1.
Task 4
Find the optimal order to compute the following product (only the dimensions of the
matrices are given):
(2 × 4) · (4 × 6) · (6 × 1) · (1 × 10) · (10 × 10)
4
Solution
We compute the number of multiplications by dynamic programming.
Matrix products of length 2: 48 | 24 | 60 | 100
Matrix products of length 3 (2 + 1 or 1 + 2):
48 + 12 ; 24 + 8 = 32 | 24 + 40 = 64 ; 60 + 240 | 60 + 600 ; 100 + 60 = 160
Matrix products of length 4 (3 + 1 or 2 + 2 or 1 + 3):
32 + 20 = 52 ; 48 + 60 + 120 ; 64 + 80 | 64 + 400 ; 24 + 100 + 40 = 164 ; 160 + 240
Matrix product of length 5 (4 + 1 or 3 + 2 or 2 + 3 or 1 + 4):
52 + 200 ; 32 + 100 + 20 = 152 ; 48 + 160 + 120 ; 164 + 80
Hence, to compute the product ABCDE it is the best to compute X = BC and Y = DE
first, then Z = AX and finally ZY , which takes 152 multiplications.
Task 5
Let X = (x1 , . . . , xm ) and Y = (y1 , . . . , yn ) be two sequences. We say X is a subsequence
of Y if there are indices 1 ≤ i1 < i2 < · · · < im ≤ n such that for all 1 ≤ j ≤ m it holds
that xj = yij .
Use dynamic programming to implement an algorithm that runs in polynomial time which,
given two sequences X and Y , computes the length of the longest common subsequence of
X and Y .
Solution
Let c[i, j] be the length of a LCS of (x1 , . . . , xi ) and (y1 , . . . , yj ). We have
0
if i = 0 or j = 0,
c[i, j] = c[i − 1, j − 1] + 1 if i, j > 0 and xi = yj ,
max(c[i − 1, j], c[i, j − 1]) if i, j > 0 and xi 6= yj .
We iniciate the table with 0 at position c[i, j], where i or j is 0. Wlog. let n < m. In
the first step, we compute c[i, 1] for i = 1, . . . , n and c[1, j] for j = 1, . . . , m. In step k we
compute c[i, k] for i = k, . . . , n and c[k, j] for j = k, . . . , m. After min(n, m) = n steps we
filled in exactly the whole table and we know the value c[n, m]. The algorithm works in
time O(n · m) ⊆ O(m2 ).
Example: X = (1, 2, 4), Y = (2, 3, 4, 6). The goal is c[3, 4].
i\j 0 1 2 3 4
0 0 0 0 0 0
1 0 0 0 0 0
2 0 1 1 1 1
3 0 1 1 2 2
Since 3 < 4, we can also just fill in the table row by row.