Algorithm Design Techniques
Algorithm Design Techniques
Antonio Carzaniga
Faculty of Informatics
University of Lugano
December 5, 2011
Outline
Greedy strategy
Dynamic programming
Generic-MST(G, w)
1 A=∅
2 while A is not a spanning tree
3 find a safe edge e = (u, v) // the lightest that. . .
4 A = A ∪ {e}
Example
The absolutely trivial gift-selection problem
◮ out of a set X = {x1 , x2 , . . . , xn } of valuable objects,
where v(xi ) is the value of xi
◮ you will be given, as a gift, k objects of your choice
◮ how do you maximize the total value of your gifts?
Greedy-choice property
◮ if v(xi ) = maxx∈X v(x), then there is a globally optimal solution
A that contains xi
Optimal-substructure property
◮ if v(xi ) = maxx∈X v(x) and A′ is an optimal solution for
X ′ = X − {xi }, then A′ ⊂ A
© 2007 Antonio Carzaniga 8
Observation
Inventing a greedy algorithm is easy
◮ it is easy to come up with greedy choices
Making Change
My favorite pasta lunch typically costs Fr. 15.20; I usually pay
with a Fr. 20 bill, and get Fr. 4.80 of change
Question: how can I get the least amount of coins?
(Available denominations: 5, 2, 1, 0.5, 0.2, 0.1)
Solution: 2 × 2 + 0.5 + 0.2 + 0.1 = 4.8 (5 coins/bills)
Exercise:
1. formulate a reasonable greedy choice
2. prove that it doesn’t work with a counter-example
3. go back to (1) and repeat a couple of times
Example
activity a b c d e f g h i j k
start 8 0 2 3 5 1 5 3 12 6 8
finish 12 6 13 5 7 4 9 8 14 10 11
k
j
i
h
g
f
e
d
c
b
a
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i
c
a
j
j
g
h
e
b
d
f
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Greedy choice: earliest finish time
Huffman Coding
Suppose you have a large sequence S of the six characters: ‘a’,
‘b’, ‘c’, ‘d’, ‘e’, and ‘f’
Can we do better?
Idea
Variable-length code
symbol code
a 000
b 001
c 010
d 011
e 10
f 11
M = 3n(fa + fb + fc + fd ) + 2n(fe + ff )
© 2007 Antonio Carzaniga 20
Problem Definition
Given a set of symbols C and a frequency function
f : C → [0, 1]
E is a prefix code
is minimal
Example
Huffman(C)
sym. freq. code
1 n = |C|
2 Q=C a 45% 0
3 for i = 1 to n − 1 b 13% 100
4 create a new node z c 12% 101
5 z. left = Extract-Min(Q) d 16% 110
6 z. right = Extract-Min(Q) e 9% 1110
7 f (z) = f (z. left) + f (z. right)
f 5% 1111
8 Insert(Q, z)
9 return Extract-Min(Q)
100 1
55 1
0 0 30 1
0 25 1 0 0 14 1
a:45 b:13 c:12 d:16 e:9 f:5
Activity-Selection Problem
k
j
i
h
g
f
e
d
c
b
a
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
i
c
a
k
j
g
h
e
b
d
f
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Greedy choice: earliest finish time
i 2
c 9
a 3
k 5
j 8
g 1
h 7
e 3
b 4
d 5
f 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Is the earliest-finish greedy choice still optimal?
Is any greedy choice optimal?
i 2
c 9
a 3
k 5
j 8
g 1
h 7
e 3
b 4
d 5
f 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Case 1: activity i is in the optimal schedule
Case 2
i 2
c 9
a 3
k 5
j 8
g 1
h 7
e 3
b 4
d 5
f 1
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
Case 2: activity i is not in the optimal schedule
18 v
23
x3
6 x4 25
2
u 20
3
1
x2 x1
a f j
e b c g
h d i k
a b c d e f g h i j k
a b c d e f g h i j k
3 3 2 3 ∞ 2 1 ∞ 1 ∞ 0
5 2 8 6 3 6 9 7
A maximal-length subsequence is
2 3 6 9
Dynamic Programming
First, the name “dynamic programming”
◮ does not mean writing a computer program
◮ term used in the 1950s, when “programming” meant “planning”
Problem domain
◮ typically optimization problems
◮ longest sequence, shortest path, etc.
General strategy
◮ decompose a problem in (smaller) subproblems
◮ must satisfy the optimal substructure property
◮ subproblems may overlap (indeed they should overlap!)
◮ solve the subproblems
◮ derive the solution from (one of) the solutions to the
subproblems
© 2007 Antonio Carzaniga 36
Examples
Unweighted shortest path: given G = (V , E), find the length of
the shortest path from u to v
◮ decompose u ⇝ v into u ⇝ w ⇝ v
◮ easy to prove that, if u ⇝ w ⇝ v is minimal, then w ⇝ v is
also minimal
◮ this is the optimal substructure property
Subsequence subproblems
◮ Input: x1 , x2 , . . . , xn
◮ Subproblem: xi , xi+1 , . . . , xj , with 1 ≤ i < j ≤ n
◮ O(n2 ) subproblems
⇓ − ⇓ + + − −
J y e r
C a r z a n i g a
J a z a y e r i
Let E(i, j) be the smallest set of changes that turn the first i
characters of x into the first j characters of y
Now, the last column of the alignment of E(i, j) can have either
◮ a gap for x (i.e., insertion)
◮ a gap for y (i.e., deletion)
◮ no gaps (i.e., modification iff x[i] 6= y[j])
Knapsack
Problem definition
◮ Input: a set of n objects with their weights w1 , w2 , . . . wn and
their values v1 , v2 , . . . vn , and a maximum weight W
P
◮ Output: a Psubset K of the objects such that i∈K wi ≤ W and
such that i∈K vi is maximal
Dynamic-programming solution
Recursion tries to solve the same problem over and over again
Memoization
Problem: recursion solves the same problems repeatedly
Exercise
Puzzle 0: is it possible to insert some ‘+’ signs in the string
“213478” so that the resulting expression would equal 214?
◮ Yes, because 2 + 134 + 78 = 214
Puzzle 1: is it possible to insert some ‘+’ signs in the strings
of digits to obtain the corresponding target number?
digits target
646805736141599100791159198 472004
6152732017763987430884029264512187586207273294807 560351
48796142803774467559157928 326306
195961521219109124054410617072018922584281838218 7779515