1 cpsc 320: intermediate algorithm design and analysis july 25, 2014

18
1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014

Upload: jack-gilbert

Post on 28-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014

1

CPSC 320: Intermediate AlgorithmDesign and Analysis

July 25, 2014

Page 2: 1 CPSC 320: Intermediate Algorithm Design 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

Page 3: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014

3

Dynamic Programming

Page 4: 1 CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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