Title: COSC 3101A Design and Analysis of Algorithms 8
1COSC 3101A - Design and Analysis of Algorithms8
- Elements of DP
- Memoization
- Longest Common Subsequence
- Greedy Algorithms
Many of these slides are taken from Monica
Nicolescu, Univ. of Nevada, Reno,
monica_at_cs.unr.edu
2Elements of Dynamic Programming
- Optimal Substructure
- An optimal solution to a problem contains within
it an optimal solution to subproblems - Optimal solution to the entire problem is build
in a bottom-up manner from optimal solutions to
subproblems - Overlapping Subproblems
- If a recursive algorithm revisits the same
subproblems over and over ? the problem has
overlapping subproblems
3Optimal Substructure - Examples
- Assembly line
- Fastest way of going through a station j
contains - the fastest way of going through station j-1 on
either line - Matrix multiplication
- Optimal parenthesization of Ai ? Ai1 ??? Aj that
splits the product between Ak and Ak1 contains - an optimal solution to the problem of
parenthesizing Ai..k and Ak1..j
4Discovering Optimal Substructure
- Show that a solution to a problem consists of
making a choice that leaves one or more similar
problems to be solved - Suppose that for a given problem you are given
the choice that leads to an optimal solution - Given this choice determine which subproblems
result - Show that the solutions to the subproblems used
within the optimal solution must themselves be
optimal - Cut-and-paste approach
5Parameters of Optimal Substructure
- How many subproblems are used in an optimal
solution for the original problem - Assembly line
- Matrix multiplication
- How many choices we have in determining which
subproblems to use in an optimal solution - Assembly line
- Matrix multiplication
One subproblem (the line that gives best time)
Two subproblems (subproducts Ai..k, Ak1..j)
Two choices (line 1 or line 2)
j - i choices for k (splitting the product)
6Parameters of Optimal Substructure
- Intuitively, the running time of a dynamic
programming algorithm depends on two factors - Number of subproblems overall
- How many choices we look at for each subproblem
- Assembly line
- ?(n) subproblems (n stations)
- 2 choices for each subproblem
- Matrix multiplication
- ?(n2) subproblems (1 ? i ? j ? n)
- At most n-1 choices
?(n) overall
?(n3) overall
7Memoization
- Top-down approach with the efficiency of typical
dynamic programming approach - Maintaining an entry in a table for the solution
to each subproblem - memoize the inefficient recursive algorithm
- When a subproblem is first encountered its
solution is computed and stored in that table - Subsequent calls to the subproblem simply look
up that value
8Memoized Matrix-Chain
- Alg. MEMOIZED-MATRIX-CHAIN(p)
- n ? lengthp 1
- for i ? 1 to n
- do for j ? i to n
- do mi, j ? ?
- return LOOKUP-CHAIN(p, 1, n)
Initialize the m table with large values that
indicate whether the values of mi, j have been
computed
Top-down approach
9Memoized Matrix-Chain
- Alg. LOOKUP-CHAIN(p, i, j)
- if mi, j lt ?
- then return mi, j
- if i j
- then mi, j ? 0
- else for k ? i to j 1
- do q ? LOOKUP-CHAIN(p, i, k)
- LOOKUP-CHAIN(p, k1, j) pi-1pkpj
- if q lt mi, j
- then mi, j ? q
- return mi, j
Running time is O(n3)
10Dynamic Progamming vs. Memoization
- Advantages of dynamic programming vs. memoized
algorithms - No overhead for recursion, less overhead for
maintaining the table - The regular pattern of table accesses may be used
to reduce time or space requirements - Advantages of memoized algorithms vs. dynamic
programming - Some subproblems do not need to be solved
11Matrix-Chain Multiplication - Summary
- Both the dynamic programming approach and the
memoized algorithm can solve the matrix-chain
multiplication problem in O(n3) - Both methods take advantage of the overlapping
subproblems property - There are only ?(n2) different subproblems
- Solutions to these problems are computed only
once - Without memoization the natural recursive
algorithm runs in exponential time
12Longest Common Subsequence
- Given two sequences
- X ?x1, x2, , xm?
- Y ?y1, y2, , yn?
- find a maximum length common subsequence (LCS)
of X and Y - E.g.
- X ?A, B, C, B, D, A, B?
- Subsequences of X
- A subset of elements in the sequence taken in
order - ?A, B, D?, ?B, C, D, B?, etc.
13Example
- X ?A, B, C, B, D, A, B? X ?A, B, C, B,
D, A, B? - Y ?B, D, C, A, B, A? Y ?B, D, C, A,
B, A? - ?B, C, B, A? and ?B, D, A, B? are longest common
subsequences of X and Y (length 4) - ?B, C, A?, however is not a LCS of X and Y
14Brute-Force Solution
- For every subsequence of X, check whether its a
subsequence of Y - There are 2m subsequences of X to check
- Each subsequence takes ?(n) time to check
- scan Y for first letter, from there scan for
second, and so on - Running time ?(n2m)
15Notations
- Given a sequence X ?x1, x2, , xm? we define
the i-th prefix of X, for i 0, 1, 2, , m - Xi ?x1, x2, , xi?
- ci, j the length of a LCS of the sequences
Xi ?x1, x2, , xi? and Yj ?y1, y2, , yj?
16A Recursive Solution
- Case 1 xi yj
- e.g. Xi ?A, B, D, E?
- Yj ?Z, B, E?
- Append xi yj to the LCS of Xi-1 and Yj-1
- Must find a LCS of Xi-1 and Yj-1 ? optimal
solution to a problem includes optimal solutions
to subproblems
ci, j
ci - 1, j - 1 1
17A Recursive Solution
- Case 2 xi ? yj
- e.g. Xi ?A, B, D, G?
- Yj ?Z, B, D?
- Must solve two problems
- find a LCS of Xi-1 and Yj Xi-1 ?A, B, D? and
Yj ?Z, B, D? - find a LCS of Xi and Yj-1 Xi ?A, B, D, G? and
Yj ?Z, B? - Optimal solution to a problem includes optimal
solutions to subproblems
max ci - 1, j, ci, j-1
ci, j
18Overlapping Subproblems
- To find a LCS of X and Y
- we may need to find the LCS between X and Yn-1
and that of Xm-1 and Y - Both the above subproblems has the subproblem of
finding the LCS of Xm-1 and Yn-1 - Subproblems share subsubproblems
193. Computing the Length of the LCS
- 0 if i 0 or j 0
- ci, j ci-1, j-1 1 if xi yj
- max(ci, j-1, ci-1, j) if xi ? yj
0
1
2
n
yj
y1
y2
yn
xi
0
x1
1
x2
2
i
xm
m
j
20Additional Information
- A matrix bi, j
- For a subproblem i, j it tells us what choice
was made to obtain the optimal value - If xi yj
- bi, j
- Else, if ci - 1, j ci, j-1
- bi, j ?
- else
- bi, j ?
- 0 if i,j 0
- ci, j ci-1, j-1 1 if xi yj
- max(ci, j-1, ci-1, j) if xi ? yj
0
1
2
n
3
b c
yj
A
C
F
D
xi
0
A
1
B
2
i
3
C
D
m
j
21LCS-LENGTH(X, Y, m, n)
- for i ? 1 to m
- do ci, 0 ? 0
- for j ? 0 to n
- do c0, j ? 0
- for i ? 1 to m
- do for j ? 1 to n
- do if xi yj
- then ci, j ? ci - 1, j - 1 1
- bi, j ?
- else if ci - 1, j ci, j - 1
- then ci, j ? ci - 1, j
- bi, j ? ?
- else ci, j ? ci, j - 1
- bi, j ? ?
- return c and b
The length of the LCS if one of the sequences is
empty is zero
Case 1 xi yj
Case 2 xi ? yj
Running time ?(mn)
22Example
0 if i 0 or j 0 ci, j
ci-1, j-1 1 if xi yj
max(ci, j-1, ci-1, j) if xi ? yj
- X ?B, D, C, A, B, A?
- Y ?A, B, C, B, D, A?
0
1
2
6
3
4
5
yj
B
D
A
C
A
B
0
xi
1
A
? 0
? 0
? 0
?1
2
B
? 1
?1
?1
?2
3
C
4
B
5
D
6
A
7
B
234. Constructing a LCS
- Start at bm, n and follow the arrows
- When we encounter a in bi, j ? xi yj
is an element of the LCS
0
1
2
6
3
4
5
yj
B
D
A
C
A
B
0
xi
1
A
? 0
? 0
? 0
?1
2
B
? 1
?1
?1
?2
3
C
4
B
5
D
6
A
7
B
24PRINT-LCS(b, X, i, j)
- if i 0 or j 0
- then return
- if bi, j
- then PRINT-LCS(b, X, i - 1, j - 1)
- print xi
- elseif bi, j ?
- then PRINT-LCS(b, X, i - 1, j)
- else PRINT-LCS(b, X, i, j - 1)
- Initial call PRINT-LCS(b, X, lengthX,
lengthY)
Running time ?(m n)
25Improving the Code
- What can we say about how each entry ci, j is
computed? - It depends only on ci -1, j - 1, ci - 1, j,
and ci, j - 1 - Eliminate table b and compute in O(1) which of
the three values was used to compute ci, j - We save ?(mn) space from table b
- However, we do not asymptotically decrease the
auxiliary space requirements still need table c - If we only need the length of the LCS
- LCS-LENGTH works only on two rows of c at a time
- The row being computed and the previous row
- We can reduce the asymptotic space requirements
by storing only these two rows
26Greedy Algorithms
- Similar to dynamic programming, but simpler
approach - Also used for optimization problems
- Idea When we have a choice to make, make the one
that looks best right now - Make a locally optimal choice in hope of getting
a globally optimal solution - Greedy algorithms dont always yield an optimal
solution - When the problem has certain general
characteristics, greedy algorithms give optimal
solutions
27Activity Selection
- Schedule n activities that require exclusive use
of a common resource - S a1, . . . , an set of activities
- ai needs resource during period si , fi)
- si start time and fi finish time of activity
ai - 0 ? si lt fi lt ?
- Activities ai and aj are compatible if the
intervals si , fi) and sj, fj) do not overlap
fj ? si
fi ? sj
i
j
j
i
28Activity Selection Problem
- Select the largest possible set of
nonoverlapping (mutually compatible) activities. - E.g.
- Activities are sorted in increasing order of
finish times - A subset of mutually compatible activities a3,
a9, a11 - Maximal set of mutually compatible activities
- a1, a4, a8, a11 and a2, a4, a9, a11
29Optimal Substructure
- Define the space of subproblems
- Sij ak ? S fi sk lt fk sj
- activities that start after ai finishes and
finish before aj starts - Activities that are compatible with the ones in
Sij - All activities that finish by fi
- All activities that start no earlier than sj
30Representing the Problem
- Add fictitious activities
- a0 -?, 0)
- an1 ?, ? 1)
- Range for Sij is 0 ? i, j ? n 1
- In a set Sij we assume that activities are sorted
in increasing order of finish times - f0 ? f1 ? f2 ? ? fn lt fn1
- What happens if i j ?
- For an activity ak ? Sij fi ? sk lt fk ? sj lt fj
- contradiction with fi ? fj!
- ? Sij ? (the set Sij must be empty!)
- We only need to consider sets Sij with 0 ? i lt j
? n 1
S S0,n1 entire space of activities
31Optimal Substructure
- Subproblem
- Select a maximum size subset of mutually
compatible activities from set Sij - Assume that a solution to the above subproblem
includes activity ak (Sij is non-empty) -
- Solution to Sij (Solution to Sik) ? ak ?
(Solution to Skj) - ?Solution to Sij ? ?Solution to Sik ? 1
?Solution to Skj ?
32Optimal Substructure (cont.)
- Aij Optimal solution to Sij
- Claim Sets Aik and Akj must be optimal solutions
- Assume ? Aik that includes more activities than
Aik - SizeAij SizeAik 1 SizeAkj gt
SizeAij - ? Contradiction we assumed that Aij is the
maximum of activities taken from Sij
33Recursive Solution
- Any optimal solution (associated with a set Sij)
contains within it optimal solutions to
subproblems Sik and Skj - ci, j size of maximum-size subset of mutually
compatible activities in Sij - If Sij ? ? ci, j 0 (i j)
34Recursive Solution
- If Sij ? ? and if we consider that ak is used in
an optimal solution (maximum-size subset of
mutually compatible activities of Sij) -
- ci, j
ci,k ck, j 1
35Recursive Solution
- 0 if Sij ?
- ci, j max ci,k ck, j 1 if Sij ? ?
- There are j i 1 possible values for k
- k i1, , j 1
- ak cannot be ai or aj (from the definition of
Sij) - Sij ak ? S fi sk lt fk sj
- We check all the values and take the best one
- We could now write a dynamic programming
algorithm
i lt k lt j ak ? Sij
36Theorem
- Let Sij ? ? and am be the activity in Sij with
the earliest finish time - fm min fk ak ? Sij
- Then
- am is used in some maximum-size subset of
mutually compatible activities of Sij - There exists some optimal solution that contains
am - Sim ?
- Choosing am leaves Smj the only nonempty
subproblem
37Proof
- Assume ? ak ? Sim
- fi ? sk lt fk ? sm lt fm
- ? fk lt fm contradiction !
- am did not have the earliest finish time
- ? There is no ak ? Sim ? Sim ?
-
Sij
sm
fm
ak
am
38Proof
Greedy Choice Property
- am is used in some maximum-size subset of
mutually compatible activities of Sij - Aij optimal solution for activity selection
from Sij - Order activities in Aij in increasing order of
finish time - Let ak be the first activity in Aij ak,
- If ak am Done!
- Otherwise, replace ak with am (resulting in a set
Aij) - since fm ? fk the activities in Aij will
continue to be compatible - Aij will have the same size with Aij ? am is
used in some maximum-size subset
Sij
39Why is the Theorem Useful?
2 subproblems Sik, Skj
1 subproblem Smj Sim ?
1 choice the activity with the earliest finish
time in Sij
j i 1 choices
- Making the greedy choice (the activity with the
earliest finish time in Sij) - Reduce the number of subproblems and choices
- Solve each subproblem in a top-down fashion
40Greedy Approach
- To select a maximum size subset of mutually
compatible activities from set Sij - Choose am ? Sij with earliest finish time (greedy
choice) - Add am to the set of activities used in the
optimal solution - Solve the same problem for the set Smj
- From the theorem
- By choosing am we are guaranteed to have used an
activity included in an optimal solution - ? We do not need to solve the subproblem Smj
before making the choice! - The problem has the GREEDY CHOICE property
41Characterizing the Subproblems
- The original problem find the maximum subset of
mutually compatible activities for S S0, n1 - Activities are sorted by increasing finish time
- a0, a1, a2, a3, , an1
- We always choose an activity with the earliest
finish time - Greedy choice maximizes the unscheduled time
remaining - Finish time of activities selected is strictly
increasing
42A Recursive Greedy Algorithm
- Alg. REC-ACT-SEL (s, f, i, j)
- m ? i 1
- while m lt j and sm lt fi ?Find first activity in
Sij - do m ? m 1
- if m lt j
- then return am ? REC-ACT-SEL(s, f, m, j)
- else return ?
- Activities are ordered in increasing order of
finish time - Running time ?(n) each activity is examined
only once - Initial call REC-ACT-SEL(s, f, 0, n1)
ai
fi
43Example
k sk fk
a1
m1
a0
a2
a1
a3
a1
a4
m4
a1
a5
a4
a1
a6
a4
a1
a7
a1
a4
a8
m8
a4
a1
a9
a1
a4
a8
a10
a4
a1
a8
a11
m11
a1
a4
a8
a11
a1
a4
a8
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
44An Iterative Greedy Algorithm
- Alg. GREEDY-ACTIVITY-SELECTOR(s, f)
- n ? lengths
- A ? a1
- i ? 1
- for m ? 2 to n
- do if sm fi ? activity am is
compatible with ai - then A ? A ? am
- i ? m ? ai is most recent addition to
A - return A
- Assumes that activities are ordered in increasing
order of finish time - Running time ?(n) each activity is examined
only once
am
fm
am
fm
am
fm
ai
fi
45Steps Toward Our Greedy Solution
- Determine the optimal substructure of the problem
- Develop a recursive solution
- Prove that one of the optimal choices is the
greedy choice - Show that all but one of the subproblems resulted
by making the greedy choice are empty - Develop a recursive algorithm that implements the
greedy strategy - Convert the recursive algorithm to an iterative
one
46Designing Greedy Algorithms
- Cast the optimization problem as one for which
- we make a choice and are left with only one
subproblem to solve - Prove that there is always an optimal solution to
the original problem that makes the greedy choice - Making the greedy choice is always safe
- Demonstrate that after making the greedy choice
- the greedy choice an optimal solution to the
resulting subproblem leads to an optimal solution
47Correctness of Greedy Algorithms
- Greedy Choice Property
- A globally optimal solution can be arrived at by
making a locally optimal (greedy) choice - Optimal Substructure Property
- We know that we have arrived at a subproblem by
making a greedy choice - Optimal solution to subproblem greedy choice ?
optimal solution for the original problem
48Activity Selection
- Greedy Choice Property
- There exists an optimal solution that includes
the greedy choice - The activity am with the earliest finish time in
Sij - Optimal Substructure
- If an optimal solution to subproblem Sij
includes activity ak ? it must contain optimal
solutions to Sik and Skj - Similarly, am optimal solution to Sim ?
optimal sol.
49Dynamic Programming vs. Greedy Algorithms
- Dynamic programming
- We make a choice at each step
- The choice depends on solutions to subproblems
- Bottom up solution, from smaller to larger
subproblems - Greedy algorithm
- Make the greedy choice and THEN
- Solve the subproblem arising after the choice is
made - The choice we make may depend on previous
choices, but not on solutions to subproblems - Top down solution, problems decrease in size
50The Knapsack Problem
- The 0-1 knapsack problem
- A thief rubbing a store finds n items the i-th
item is worth vi dollars and weights wi pounds
(vi, wi integers) - The thief can only carry W pounds in his knapsack
- Items must be taken entirely or left behind
- Which items should the thief take to maximize the
value of his load? - The fractional knapsack problem
- Similar to above
- The thief can take fractions of items
51Fractional Knapsack Problem
- Knapsack capacity W
- There are n items the i-th item has value vi and
weight wi - Goal
- find xi such that for all 0 ? xi ? 1, i 1, 2,
.., n - ? wixi ? W and
- ? xivi is maximum
52Fractional Knapsack Problem
- Greedy strategy 1
- Pick the item with the maximum value
- E.g.
- W 1
- w1 100, v1 2
- w2 1, v2 1
- Taking from the item with the maximum value
- Total value taken v1/w1 2/100
- Smaller than what the thief can take if choosing
the other item - Total value (choose item 2) v2/w2 1
53Fractional Knapsack Problem
- Greedy strategy 2
- Pick the item with the maximum value per pound
vi/wi - If the supply of that element is exhausted and
the thief can carry more take as much as
possible from the item with the next greatest
value per pound - It is good to order items based on their value
per pound -
54Fractional Knapsack Problem
- Alg. Fractional-Knapsack (W, vn, wn)
- While w gt 0 and as long as there are items
remaining - pick item with maximum vi/wi
- xi ? min (1, w/wi)
- remove item i from list
- w ? w xiwi
- w the amount of space remaining in the knapsack
(w W) - Running time ?(n) if items already ordered else
?(nlgn)
55Fractional Knapsack - Example
50
20 --- 30
50
80
Item 3
30
20
Item 2
100
20
Item 1
10
10
60
60
100
120
240
6/pound
5/pound
4/pound
56Greedy Choice
- Items 1 2 3 j n
- Optimal solution x1 x2 x3 xj xn
- Greedy solution x1 x2 x3 xj xn
- We know that x1 ? x1
- greedy choice takes as much as possible from item
1 - Modify the optimal solution to take x1 of item 1
- We have to decrease the quantity taken from some
item j the new xj is decreased by (x1 - x1)
w1/wj - Increase in profit
- Decrease in profit
-
True, since x1 had the best value/pound ratio
?
57Optimal Substructure
- Consider the most valuable load that weights at
most W pounds - If we remove a weight w of item j from the
optimal load - The remaining load must be the most valuable
load weighing at most W w that can be taken
from the remaining n 1 items plus wj w pounds
of item j
58The 0-1 Knapsack Problem
- Thief has a knapsack of capacity W
- There are n items for i-th item value vi and
weight wi - Goal
- find xi such that for all xi 0, 1, i 1, 2,
.., n - ? wixi ? W and
- ? xivi is maximum
590-1 Knapsack - Greedy Strategy
50
50
Item 3
30
20
Item 2
100
20
Item 1
10
10
60
60
100
120
160
6/pound
5/pound
4/pound
- None of the solutions involving the greedy choice
(item 1) leads to an optimal solution - The greedy choice property does not hold
600-1 Knapsack - Dynamic Programming
- P(i, w) the maximum profit that can be
obtained from items 1 to i, if the knapsack
has size w - Case 1 thief takes item i
- P(i, w)
- Case 2 thief does not take item i
- P(i, w)
vi P(i - 1, w-wi)
P(i - 1, w)
610-1 Knapsack - Dynamic Programming
Item i was taken
Item i was not taken
- P(i, w) max vi P(i - 1, w-wi), P(i - 1, w)
W
w - wi
0
1
w
0
i-1
i
n
62W 5
Example
P(i, w) max vi P(i - 1, w-wi), P(i - 1, w)
0
1
2
3
4
5
0
12
12
12
12
1
10
12
22
22
22
2
10
12
22
30
32
3
10
15
25
30
37
4
63Reconstructing the Optimal Solution
0
1
2
3
4
5
0
12
12
12
12
0
1
10
12
22
22
22
2
10
12
22
30
32
3
10
15
25
30
37
4
- Start at P(n, W)
- When you go left-up ? item i has been taken
- When you go straight up ? item i has not been
taken
64Optimal Substructure
- Consider the most valuable load that weights at
most W pounds - If we remove item j from this load
- The remaining load must be the most valuable
load weighing at most W wj that can be taken
from the remaining n 1 items
65Overlapping Subproblems
- P(i, w) max vi P(i - 1, w-wi), P(i - 1, w)
w
W
0
1
0
i-1
i
n
E.g. all the subproblems shown in grey may
depend on P(i-1, w)
66Readings