1 cpsc 320: intermediate algorithm design and analysis july 25, 2014
TRANSCRIPT
1
CPSC 320: Intermediate AlgorithmDesign and Analysis
July 25, 2014
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
3
Dynamic Programming
4
Dynamic Programming
• Greedy algorithm: combines a choice with result of taking the choice
• What if right choice can’t be found easily?
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(, )=
• Is a greedy solution always optimal?
• Example: GreedyCoinChange(, )=, optimal is
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
7
Coin Change Recursive
Algorithm CoinChangeRecursive(, ) – is set of coins, is amount
If Then
Return
,
For Each Do
If Then
CoinChangeRecursive(, )
If Then
Return
8
Recursive
• Is this efficient?
• Value for subproblems is calculated repeatedly
• Example: CoinChange(,), value for 86 is calculated on:
• CoinChange()
• CoinChange()
• CoinChange()
• CoinChange()
• Can we save the value (“memoize”) and reuse it?
• Can we calculate from the bottom-up?
9
Dynamic Programming Approach
Algorithm CoinChangeDP(, ) – is set of coins, is amount
For To Do
(length infinity)
For Each Do
If And Then
Return
10
Dynamic Programming Approach - Faster
Algorithm CoinChangeDP2(, ) – is set of coins, is amount
,
For To Do
,
For Each Do
If And Then
,
While Do
,
Return
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:
• 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
12
Rod Cutting Problem
Algorithm RodCutting(, ) – is size, is price array
For To Do
,
For To Do
If Then
,
While Do
,
Return
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 of 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
14
Weighted Interval Scheduling
Algorithm WeightedIntervalSched(, ) – is set of intervals, is weight function
Sort by increasing finishing time
For To Do
last event that ends before starts (0 if none)
If Then
, false
Else
, true
…
15
Weighted Interval Scheduling (cont.)
…
For DownTo Do
If Then
Add to
last event that ends before starts (0 if none)
Return
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 optimal for limit
17
Subset Sum Problem
Algorithm SubsetSum(, ) – is array of weights, is limit
, false for
For To Do
For To Do
If Or Then
,
Else
,
,
While And is not false Do
, , ,
Return
18
Knapsack Problem
Algorithm Knapsack(, , ) – is array of weights, is array of values, is limit
, false for
For To Do
For To Do
If Or Then
,
Else
,
,
While And is not false Do
, , ,
Return