CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014 - - PowerPoint PPT Presentation

β–Ά
cpsc 320 intermediate algorithm
SMART_READER_LITE
LIVE PREVIEW

CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014 - - PowerPoint PPT Presentation

CPSC 320: Intermediate Algorithm Design and Analysis July 25, 2014 1 Course Outline Introduction and basic concepts Asymptotic notation Greedy algorithms Graph theory Amortized analysis Recursion


slide-1
SLIDE 1

1

CPSC 320: Intermediate Algorithm Design and Analysis

July 25, 2014

slide-2
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
SLIDE 3

3

Dynamic Programming

slide-4
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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 𝑑