in season 9 episode 7 “the slicer” of the hit 90s tv...

41
In Season 9 Episode 7 “The Slicer” of the hit 90s TV show Seinfeld, George discovers that, years prior, he had a heated argument with his new boss, Mr. Kruger. This argument ended in George throwing Mr. Kruger’s boombox into the ocean. How did George make this discovery? 1 https://www.youtube.com/watch?v=pSB3HdmLcY4 Warm Up

Upload: others

Post on 03-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

In Season 9 Episode 7 “The Slicer” of the hit 90s TV show Seinfeld, George discovers that, years prior, he had a heated

argument with his new boss, Mr. Kruger. This argument ended in George throwing Mr. Kruger’s boombox into the

ocean. How did George make this discovery?1https://www.youtube.com/watch?v=pSB3HdmLcY4

Warm Up

Page 2: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please
Page 3: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Today’s Keywords

• Dynamic Programming• Longest Common Subsequence• Seam Carving

3

Page 4: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

CLRS Readings

• Chapter 15

4

Page 5: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Homeworks

• HW4 due 11pm Saturday, October 12– Sorting, Divide and Conquer, Dynamic Programming– Written (use LaTeX!)– Submit BOTH a pdf and a zip file (2 separate attachments)

• HW5 coming after the exam– Seam Carving!– Dynamic Programming (implementation)– Java or Python

5

Page 6: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Midterm

• Tuesday, October 15 in class– SDAC: Please schedule with SDAC for Tuesday– Mostly in-class with a (required) take-home portion

• Practice Midterm available on Collab today• Review Session– Sunday, October 13 at 3pm– Olsson 120

6

Page 7: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

7

Page 8: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Generic Top-Down Dynamic Programming Solnmem = {}def myDPalgo(problem):

if mem[problem] not blank:return mem[problem]

if baseCase(problem):solution = solve(problem)mem[problem] = solutionreturn solution

for subproblem of problem:subsolutions.append(myDPalgo(subproblem))

solution = OptimalSubstructure(subsolutions)mem[problem] = solutionreturn solution

8

Page 9: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Log Cutting Recursive Structure

9

𝐶𝑢𝑡(𝑛) = value of best way to cut a log of length 𝑛

ℓ)𝐶𝑢𝑡(𝑛 − ℓ))

𝐶𝑢𝑡 𝑛 = max𝐶𝑢𝑡 𝑛 − 1 + 𝑃 1𝐶𝑢𝑡 𝑛 − 2 + 𝑃 2…𝐶𝑢𝑡 0 + 𝑃[𝑛]

Last Cutbest way to cut a log of length 𝒏 − ℓ𝒏

𝑃 𝑖 = value of a cut of length 𝑖

Page 10: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Log Cutting Pseudocode

10

Initialize Memory CCut(n):

C[0] = 0for i=1 to n:

best = 0for j = 1 to i:

best = max(best, C[i-j] + P[j])C[i] = best

return C[n]

Run Time: 𝑂(𝑛9)

Page 11: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Matrix Chaining Recursive Structure

• In general:

11

𝐵𝑒𝑠𝑡 𝑖, 𝑗 = cheapest way to multiply together 𝑀@ through 𝑀A

𝐵𝑒𝑠𝑡 1, 𝑛 = min

𝐵𝑒𝑠𝑡 2, 𝑛 + 𝑟E𝑟9𝑐)𝐵𝑒𝑠𝑡 1,2 + 𝐵𝑒𝑠𝑡 3, 𝑛 + 𝑟E𝑟H𝑐)𝐵𝑒𝑠𝑡 1,3 + 𝐵𝑒𝑠𝑡 4, 𝑛 + 𝑟E𝑟J𝑐)𝐵𝑒𝑠𝑡 1,4 + 𝐵𝑒𝑠𝑡 5, 𝑛 + 𝑟E𝑟L𝑐)…

𝐵𝑒𝑠𝑡 1, 𝑛 − 1 + 𝑟E𝑟)𝑐)

𝐵𝑒𝑠𝑡 𝑖, 𝑗 = minAME

NO@𝐵𝑒𝑠𝑡 𝑖, 𝑘 + 𝐵𝑒𝑠𝑡 𝑘 + 1, 𝑗 + 𝑟@𝑟NQE𝑐A

𝐵𝑒𝑠𝑡 𝑖, 𝑖 = 0

Page 12: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Matrix Chaining Memory

12

30

35

×𝑀E 35

15

×𝑀915

5

×𝑀H 5

10

×𝑀J

10

20

×𝑀L 20

25

𝑀S

𝐵𝑒𝑠𝑡 𝑖, 𝑗 = minAME

NO@𝐵𝑒𝑠𝑡 𝑖, 𝑘 + 𝐵𝑒𝑠𝑡 𝑘 + 1, 𝑗 + 𝑟@𝑟NQE𝑐A

𝐵𝑒𝑠𝑡 𝑖, 𝑖 = 0 0 15750 7875 9375 11875

0 2625 4375 7125 10500

0 750 2500 5375

35000 1000

50000

0

1 2 3 4 5 61

2

3

4

5

6

𝐵𝑒𝑠𝑡 1,6 = min

𝐵𝑒𝑠𝑡 1,1 + 𝐵𝑒𝑠𝑡 2, 6 + 𝑟E𝑟9𝑐S𝐵𝑒𝑠𝑡 1,2 + 𝐵𝑒𝑠𝑡 3, 6 + 𝑟E𝑟H𝑐S𝐵𝑒𝑠𝑡 1,3 + 𝐵𝑒𝑠𝑡 4, 6 + 𝑟E𝑟J𝑐S𝐵𝑒𝑠𝑡 1,4 + 𝐵𝑒𝑠𝑡 5, 6 + 𝑟E𝑟L𝑐S𝐵𝑒𝑠𝑡 1,5 + 𝐵𝑒𝑠𝑡 6, 6 + 𝑟E𝑟S𝑐S

15125

𝑗 == 𝑖

Page 13: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Longest Common Subsequence

13

Given two sequences 𝑋 and 𝑌, find the length of their longest common subsequence

Example:𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐴𝑇𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝐿𝐶𝑆 = 𝑇𝐶𝑇𝐴

Brute force: Compare every subsequence of 𝑋 with 𝑌Ω(2))

Page 14: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

14

Page 15: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

1. Identify Recursive Structure

Let 𝐿𝐶𝑆 𝑖, 𝑗 = length of the LCS for the first 𝑖 characters of 𝑋, first 𝑗 character of 𝑌Find 𝐿𝐶𝑆(𝑖, 𝑗):

15

𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐶𝐺𝑇𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝑇

𝐿𝐶𝑆 𝑖, 𝑗 = 𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1

𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐶𝐺𝐴𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝑇

𝐿𝐶𝑆 𝑖, 𝑗 = 𝐿𝐶𝑆 𝑖, 𝑗 − 1

Case 1: 𝑋 𝑖 = 𝑌[𝑗]

Case 2: 𝑋 𝑖 ≠ 𝑌[𝑗]𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐶𝐺𝑇𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝐶

𝐿𝐶𝑆 𝑖, 𝑗 = 𝐿𝐶𝑆 𝑖 − 1, 𝑗

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise

Page 16: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

16

Page 17: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

1. Identify Recursive Structure

17

𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐶𝐺𝑇𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝑇

𝐿𝐶𝑆 𝑖, 𝑗 = 𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1

𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐶𝐺𝐴𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝑇

𝐿𝐶𝑆 𝑖, 𝑗 = 𝐿𝐶𝑆 𝑖, 𝑗 − 1

Case 1: 𝑋 𝑖 = 𝑌[𝑗]

Case 2: 𝑋 𝑖 ≠ 𝑌[𝑗]𝑋 = 𝐴𝑇𝐶𝑇𝐺𝐶𝐺𝑇𝑌 = 𝑇𝐺𝐶𝐴𝑇𝐴𝐶

𝐿𝐶𝑆 𝑖, 𝑗 = 𝐿𝐶𝑆 𝑖 − 1, 𝑗

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwiseSave to M[i,j]

Read from M[i,j] if present

Let 𝐿𝐶𝑆 𝑖, 𝑗 = length of the LCS for the first 𝑖 characters of 𝑋, first 𝑗 character of 𝑌Find 𝐿𝐶𝑆(𝑖, 𝑗):

Page 18: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

18

Page 19: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

3. Solve in a Good Order

19

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 1

0 0 1 1 1 2 2 2

0 0 1 2 2 2 2 2

0 1 1 2 2 2 3 3

0 1 2 2 3 3 3 4

0 1 2 2 3 3 4 4

𝑋 = 𝐴 𝑇 𝐶 𝑇𝑇 𝐺 𝐴1 2 3 74 5 60𝑌 =

0

1

3456

2𝑇

𝐶𝐴𝑇𝐴

𝐺

To fill in cell (𝑖, 𝑗) we need cells 𝑖 − 1, 𝑗 − 1 , 𝑖 − 1, 𝑗 , (𝑖, 𝑗 − 1)Fill from Top->Bottom, Left->Right (with any preference)

Page 20: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Run Time?

20

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 1

0 0 1 1 1 2 2 2

0 0 1 2 2 2 2 2

0 1 1 2 2 2 3 3

0 1 2 2 3 3 3 4

0 1 2 2 3 3 4 4

𝑋 = 𝐴 𝑇 𝐶 𝑇𝑇 𝐺 𝐴1 2 3 74 5 60𝑌 =

0

1

3456

2𝑇

𝐶𝐴𝑇𝐴

𝐺

Run Time: Θ(𝑛 ⋅ 𝑚) (for 𝑋 = 𝑛, 𝑌 = 𝑚)

Page 21: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Reconstructing the LCS

21

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 1

0 0 1 1 1 2 2 2

0 0 1 2 2 2 2 2

0 1 1 2 2 2 3 3

0 1 2 2 3 3 3 4

0 1 2 2 3 3 4 4

𝑋 = 𝐴 𝑇 𝐶 𝑇𝑇 𝐺 𝐴1 2 3 74 5 60𝑌 =

0

1

3456

2𝑇

𝐶𝐴𝑇𝐴

𝐺

Start from bottom right,if symbols matched, print that symbol then go diagonallyelse go to largest adjacent

Page 22: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Reconstructing the LCS

22

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 1

0 0 1 1 1 2 2 2

0 0 1 2 2 2 2 2

0 1 1 2 2 2 3 3

0 1 2 2 3 3 3 4

0 1 2 2 3 3 4 4

𝑋 = 𝐴 𝑇 𝐶 𝑇𝑇 𝐺 𝐴1 2 3 74 5 60𝑌 =

0

1

3456

2𝑇

𝐶𝐴𝑇𝐴

𝐺

Start from bottom right,if symbols matched, print that symbol then go diagonallyelse go to largest adjacent

Page 23: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Reconstructing the LCS

23

𝐿𝐶𝑆 𝑖, 𝑗 =0 if 𝑖 = 0 or𝑗 = 0𝐿𝐶𝑆 𝑖 − 1, 𝑗 − 1 + 1 if 𝑋 𝑖 = 𝑌[𝑗]max(𝐿𝐶𝑆 𝑖, 𝑗 − 1 , 𝐿𝐶𝑆 𝑖 − 1, 𝑗 ) otherwise

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 1

0 0 1 1 1 2 2 2

0 0 1 2 2 2 2 2

0 1 1 2 2 2 3 3

0 1 2 2 3 3 3 4

0 1 2 2 3 3 4 4

𝑋 = 𝐴 𝑇 𝐶 𝑇𝑇 𝐺 𝐴1 2 3 74 5 60𝑌 =

0

1

3456

2𝑇

𝐶𝐴𝑇𝐴

𝐺

Start from bottom right,if symbols matched, print that symbol then go diagonallyelse go to largest adjacent

Page 24: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Seam Carving

• Method for image resizing that doesn’t scale/crop the image

24

Page 25: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Seam Carving

• Method for image resizing that doesn’t scale/crop the image

25

Page 26: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Cropping

• Removes a “block” of pixels

26

Cropped

Page 27: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Scaling

• Removes “stripes” of pixels

27

Scaled

Page 28: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Seam Carving

• Removes “least energy seam” of pixels

28

Carved

Page 29: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Seam Carving

• Method for image resizing that doesn’t scale/crop the image

29

Cropped Scaled Carved

Page 30: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Seattle Skyline

30http://rsizr.com/

Page 31: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Energy of a Seam

• Sum of the energies of each pixel𝑒 𝑝 = energy of pixel 𝑝

• Many choices for pixel energy– E.g.: change of gradient (how much the color of this pixel differs from

its neighbors)– Particular choice doesn’t matter, we use it as a “black box”

• Goal: find least-energy seam to remove

31

Page 32: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

32

Page 33: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Identify Recursive Structure

Let 𝑆 𝑖, 𝑗 = least energy seam from the bottom of the image up to pixel 𝑝@,A

33

𝑝@,A

Page 34: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Finding the Least Energy Seam

34

𝑝),N

Want to delete the least energy seam going from bottom to top, so delete:

minf

NOE𝑆(𝑛, 𝑘)

𝑛

𝑚

Page 35: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Computing 𝑆(𝑛, 𝑘)

Assume we know the least energy seams for all of row 𝑛 − 1(i.e. we know 𝑆(𝑛 − 1, ℓ) for all ℓ)

35

𝑝),N

Known through 𝑛 − 1

𝑚

Page 36: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Computing 𝑆(𝑛, 𝑘)

36

Assume we know the least energy seams for all of row 𝑛 − 1(i.e. we know 𝑆(𝑛 − 1, ℓ) for all ℓ)

S(n-1,k-1)

𝑝),N

S(n-1,k) S(n-1,k+1)

S(n,k)

Page 37: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Computing 𝑆(𝑛, 𝑘)

37

S(n-1,k-1)

𝑝),N

S(n-1,k) S(n-1,k+1)

S(n,k)

𝑆 𝑛, 𝑘 = 𝑚𝑖𝑛 𝑆 𝑛 − 1, 𝑘 − 1 + 𝑒(𝑝),N)

𝑆 𝑛 − 1, 𝑘 + 𝑒(𝑝),N)

𝑆 𝑛 − 1, 𝑘 + 1 + 𝑒(𝑝),N)

Assume we know the least energy seams for all of row 𝑛 − 1(i.e. we know 𝑆(𝑛 − 1, ℓ) for all ℓ)

Page 38: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

38

Page 39: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

39

Page 40: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Repeated Seam Removal

44

𝑛

𝑚

Only need to update pixels dependent on the removed seam2𝑛 pixels change Θ(2𝑛) time to update pixels

Θ(𝑛 +𝑚) time to find min+backtrack

Page 41: In Season 9 Episode 7 “The Slicer” of the hit 90s TV showjh2jf/courses/fall2019/cs4102/lectures/... · –Java or Python 5. Midterm •Tuesday, October 15 in class –SDAC: Please

Dynamic Programming

• Requires Optimal Substructure– Solution to larger problem contains the solutions to smaller ones

• Idea:1. Identify the recursive structure of the problem• What is the “last thing” done?

2. Save the solution to each subproblem in memory3. Select a good order for solving subproblems• “Top Down”: Solve each recursively• “Bottom Up”: Iteratively solve smallest to largest

45