SLIDE 1
1
CPSC 320: Intermediate Algorithm Design and Analysis
July 25, 2014
SLIDE 2 2
Course Outline
- Introduction and basic concepts
- Asymptotic notation
- Greedy algorithms
- Graph theory
- Amortized analysis
- Recursion
- Divide-and-conquer algorithms
- Randomized algorithms
- Dynamic programming algorithms
- NP-completeness
SLIDE 3
3
Dynamic Programming
SLIDE 4 4
Dynamic Programming
- Greedy algorithm: combines a choice with result of taking the choice
- What if right choice canβt be found easily?
SLIDE 5 5
Coin Change Problem
- Assume a set of possible coins, and an amount, find the smallest number of coins
that add up to the exact amount
- Example: CoinChange({1,5,10,25}, 47)=[25, 10,10,1,1]
- Is a greedy solution always optimal?
- Example: GreedyCoinChange({1,5,12,25}, 29)=[25, 1,1,1,1], optimal is [12,12,5]
SLIDE 6 6
Coin Change Problem
- What is a subproblem that can be solved
- If an optimal solution π‘ for π contains π, then an optimal solution for π β π is
π‘ β π
- So the subproblem is solving for π β π
- Letβs define the problem recursively:
- For each coin π, calculate π‘π = CoinChange(π·, π β π)
- Select coin π with smallest number of coins in subproblem
- Attach coin π to π‘π and return
SLIDE 7
7
Coin Change Recursive
Algorithm CoinChangeRecursive(π·, π) β π· is set of coins, π is amount If π = 0 Then Return β
π β +β, π‘ β β
For Each π β π· Do If π β€ π Then π‘π β CoinChangeRecursive(π·, π β π) If π‘π + 1 < π Then π β π‘π + 1 π‘ β π‘π βͺ {π} Return π‘
SLIDE 8 8
Recursive
- Is this efficient?
- Value for subproblems is calculated repeatedly
- Example: CoinChange( 1,3,4 ,90), value for 86 is calculated on:
- 4 +CoinChange(86)
- 3 + 1 +CoinChange(86)
- 1 + 3 +CoinChange(86)
- 1 + 1 + 1 + 1 +CoinChange(86)
- Can we save the value (βmemoizeβ) and reuse it?
- Can we calculate from the bottom-up?
SLIDE 9
9
Dynamic Programming Approach
Algorithm CoinChangeDP(π·, π) β π· is set of coins, π is amount π 0 β β
For π β 1 To π Do π π β β
(length infinity) For Each π β π· Do If π β€ π And π π β π + 1 < π π Then π[π] β π π β π βͺ {π} Return π[π]
SLIDE 10
10
Dynamic Programming Approach - Faster
Algorithm CoinChangeDP2(π·, π) β π· is set of coins, π is amount π[0] β β
, π 0 β 0 For π β 1 To π Do π π β β
, π π β +β For Each π β π· Do If π β€ π And π π β π + 1 < π[π] Then π π β π, π[π] β π[π β π] + 1 π‘ β β
While π > 0 Do π‘ β π‘ βͺ π π , π β π β π[π] Return π‘
SLIDE 11 11
Rod Cutting Problem
- Problem: given a rod of length π and a table of prices ππ for rods of length π β€ π,
determine the maximum revenue π
π obtainable by cutting it in pieces
- Example: π = 5, π1 = 1, π2 = 5, π3 = 8, π4 = 9, π5 = 10
- Full rod: revenue is 10
- Cut in 5 pieces of 1: revenue is 5
- Cut in pieces of 2 and 3: revenue is 13
- Problem can be solved with dynamic programming
- Choose each possible size, then solve for remainder
SLIDE 12
12
Rod Cutting Problem
Algorithm RodCutting(π, π) β π is size, π is price array π 0 = 0 For π β 1 To π Do π π β π[π], π π β π For π β₯ 1 To π β 1 Do If π π < π π β π + π[π] Then π π β π π β π + π[π], π π β π π‘ β β
While π > 0 Do π‘ β π‘ βͺ π π , π β π β π[π] Return π‘
SLIDE 13 13
Weighted Interval Scheduling
- Problem: same as interval scheduling, but with preferences
- Given a set of intervals π, and a weight function π₯, return a compatible subset
- f π with maximum total weight
- Greedy approach doesnβt work anymore
- Define the subproblem
- In optimal solution, if we remove last interval π, remainder is optimal ending
before π‘π
SLIDE 14
14
Weighted Interval Scheduling
Algorithm WeightedIntervalSched(π½, π₯) β π½ is set of intervals, π₯ is weight function Sort π½ by increasing finishing time π 0 β 0 For π β 1 To π½ Do π β last event that ends before π½[π] starts (0 if none) If π π β 1 > π₯ π½ π + π π Then π π β π π β 1 , π· π β false Else π π β π₯ π½ π + π π , π· π β true β¦
SLIDE 15
15
Weighted Interval Scheduling (cont.)
β¦ π β β
For π β |π½| DownTo 1 Do If π·[π] Then Add π½[π] to π π β last event that ends before π½[π] starts (0 if none) Return π
SLIDE 16 16
Subset Sum Problem
- Problem: given a set of weights, find the largest sum of weights below a limit
- Applications: get a bag as full as possible, fill a server with limited resource
time as much as possible
- Extension: each weight has a value associated to it (knapsack problem)
- Now the subproblem is slightly different
- If an optimal solution contains weight π₯ for limit π, then the remainder is
- ptimal for limit π β π₯
SLIDE 17
17
Subset Sum Problem
Algorithm SubsetSum(π₯, π) β π₯ is array of weights, π is limit π 0, π β 0, π 0, π β false for π = 0,1,2, β¦ , π For π β 1 To π₯ Do For π β 1 To π Do If π₯ π > π Or π π β 1, π > π π β 1, π β π₯ π + π₯[π] Then π π, π β π π β 1, π , π π, π β π[π β 1, π] Else π π, π β π π β 1, π β π₯ π + π₯[π], π π, π β π π‘ β β
, π¦ β π₯ While π > 0 And π[π¦, π] is not false Do π¦ β π[π¦, π], π‘ β π‘ βͺ π¦, π β π β π₯ π¦ , π¦ β π¦ β 1 Return π‘
SLIDE 18
18
Knapsack Problem
Algorithm Knapsack(π₯, π, π) β π₯ is array of weights, π is array of values, π is limit π 0, π β 0, π 0, π β false for π = 0,1,2, β¦ , π For π β 1 To π₯ Do For π β 1 To π Do If π₯ π > π Or π π β 1, π > π π β 1, π β π₯ π + π[π] Then π π, π β π π β 1, π , π π, π β π[π β 1, π] Else π π, π β π π β 1, π β π₯ π + π[π], π π, π β π π‘ β β
, π¦ β π₯ While π > 0 And π[π¦, π] is not false Do π¦ β π[π¦, π], π‘ β π‘ βͺ π¦, π β π β π₯ π¦ , π¦ β π¦ β 1 Return π‘