1 cpsc 320: intermediate algorithm design and analysis july 25, 2014
TRANSCRIPT
![Page 1: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/1.jpg)
1
CPSC 320: Intermediate AlgorithmDesign and Analysis
July 25, 2014
![Page 2: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/2.jpg)
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
![Page 3: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/3.jpg)
3
Dynamic Programming
![Page 4: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/4.jpg)
4
Dynamic Programming
• Greedy algorithm: combines a choice with result of taking the choice
• What if right choice can’t be found easily?
![Page 5: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/5.jpg)
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
![Page 6: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/6.jpg)
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
![Page 7: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/7.jpg)
7
Coin Change Recursive
Algorithm CoinChangeRecursive(, ) – is set of coins, is amount
If Then
Return
,
For Each Do
If Then
CoinChangeRecursive(, )
If Then
Return
![Page 8: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/8.jpg)
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?
![Page 9: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/9.jpg)
9
Dynamic Programming Approach
Algorithm CoinChangeDP(, ) – is set of coins, is amount
For To Do
(length infinity)
For Each Do
If And Then
Return
![Page 10: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/10.jpg)
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
![Page 11: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/11.jpg)
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
![Page 12: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/12.jpg)
12
Rod Cutting Problem
Algorithm RodCutting(, ) – is size, is price array
For To Do
,
For To Do
If Then
,
While Do
,
Return
![Page 13: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/13.jpg)
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
![Page 14: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/14.jpg)
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
…
![Page 15: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/15.jpg)
15
Weighted Interval Scheduling (cont.)
…
For DownTo Do
If Then
Add to
last event that ends before starts (0 if none)
Return
![Page 16: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/16.jpg)
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
![Page 17: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/17.jpg)
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
![Page 18: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014](https://reader030.vdocument.in/reader030/viewer/2022033105/56649e4e5503460f94b447d6/html5/thumbnails/18.jpg)
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