CS4102 Algorithms Fall 2018 Warm up In Season 9 Episode 7 The - - PowerPoint PPT Presentation

cs4102 algorithms
SMART_READER_LITE
LIVE PREVIEW

CS4102 Algorithms Fall 2018 Warm up In Season 9 Episode 7 The - - PowerPoint PPT Presentation

CS4102 Algorithms Fall 2018 Warm up 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


slide-1
SLIDE 1

Warm up 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?

CS4102 Algorithms

Fall 2018

1

slide-2
SLIDE 2

https://www.youtube.com/watch?v=pSB3HdmLcY4

slide-3
SLIDE 3

Today’s Keywords

  • Dynamic Programming
  • Longest Common Subsequence
  • Seam Carving
  • Seinfeld

3

slide-4
SLIDE 4

CLRS Readings

  • Chapter 15

4

slide-5
SLIDE 5

Homeworks

  • Hw5 Released on Friday

– Programming – Dynamic Programming

5

slide-6
SLIDE 6

Dynamic Programming

  • Requires Optimal Substructure

– Solution to larger problem contains the solutions to smaller ones

  • Idea:
  • 1. Identify recursive structure of the problem
  • What is the “last thing” done?
  • 2. Select a good order for solving subproblems
  • “Top Down”: Solve each recursively
  • “Bottom Up”: Iteratively solve smallest to largest
  • 3. Save solution to each subproblem in memory

6

slide-7
SLIDE 7

Longest Common Subsequence

7

Given two sequences 𝑌 and 𝑍, find the length of their longest common subsequence Example: 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐵𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵 𝑀𝐷𝑇 = 𝑈𝐷𝑈𝐵 Brute force: Compare every subsequence of 𝑌 with 𝑍 Ω(2𝑜)

slide-8
SLIDE 8

Dynamic Programming

  • Requires Optimal Substructure

– Solution to larger problem contains the solutions to smaller ones

  • Idea:
  • 1. Identify recursive structure of the problem
  • What is the “last thing” done?
  • 2. Select a good order for solving subproblems
  • “Top Down”: Solve each recursively
  • “Bottom Up”: Iteratively solve smallest to largest
  • 3. Save solution to each subproblem in memory

8

slide-9
SLIDE 9
  • 1. Identify Recursive Structure

Let 𝑀𝐷𝑇 𝑗, 𝑘 = length of the LCS for the first 𝑗 characters of 𝑌, first 𝑘 characters of 𝑍 Find 𝑀𝐷𝑇(𝑗, 𝑘):

9

𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝐵 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗, 𝑘 − 1

Case 1: 𝑌 𝑗 = 𝑍[𝑘] Case 2: 𝑌 𝑗 ≠ 𝑍[𝑘]

𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝐷 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise
slide-10
SLIDE 10

Dynamic Programming

  • Requires Optimal Substructure

– Solution to larger problem contains the solutions to smaller ones

  • Idea:
  • 1. Identify recursive structure of the problem
  • What is the “last thing” done?
  • 2. Select a good order for solving subproblems
  • “Top Down”: Solve each recursively
  • “Bottom Up”: Iteratively solve smallest to largest
  • 3. Save solution to each subproblem in memory

10

slide-11
SLIDE 11
  • 1. Identify Recursive Structure

Let 𝑀𝐷𝑇 𝑗, 𝑘 = length of the LCS for the first 𝑗 characters of 𝑌, first 𝑘 character of 𝑍 Find 𝑀𝐷𝑇(𝑗, 𝑘):

11

𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝐵 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗, 𝑘 − 1

Case 1: 𝑌 𝑗 = 𝑍[𝑘] Case 2: 𝑌 𝑗 ≠ 𝑍[𝑘]

𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝐷 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise

Save to M[i,j] Read from M[i,j] if present

slide-12
SLIDE 12

Dynamic Programming

  • Requires Optimal Substructure

– Solution to larger problem contains the solutions to smaller ones

  • Idea:
  • 1. Identify recursive structure of the problem
  • What is the “last thing” done?
  • 2. Select a good order for solving subproblems
  • “Top Down”: Solve each recursively
  • “Bottom Up”: Iteratively solve smallest to largest
  • 3. Save solution to each subproblem in memory

12

slide-13
SLIDE 13
  • 2. Solve in a Good Order

13

𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise

1 1 1 1 1 1 1 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 2 3 3 1 2 2 3 3 3 4 1 2 2 3 3 4 4 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝑈 𝐻 𝐵 1 2 3 7 4 5 6 1 3 4 5 6 2 𝑈 𝐷 𝐵 𝑈 𝐵 𝐻

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

slide-14
SLIDE 14

Run Time?

14

𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise

1 1 1 1 1 1 1 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 2 3 3 1 2 2 3 3 3 4 1 2 2 3 3 4 4 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝑈 𝐻 𝐵 1 2 3 7 4 5 6 1 3 4 5 6 2 𝑈 𝐷 𝐵 𝑈 𝐵 𝐻

Run Time: Θ(𝑜 ⋅ 𝑛) (for 𝑌 = 𝑜, 𝑍 = 𝑛)

slide-15
SLIDE 15

Reconstructing the LCS

15

𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise

1 1 1 1 1 1 1 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 2 3 3 1 2 2 3 3 3 4 1 2 2 3 3 4 4 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝑈 𝐻 𝐵 1 2 3 7 4 5 6 1 3 4 5 6 2 𝑈 𝐷 𝐵 𝑈 𝐵 𝐻

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

slide-16
SLIDE 16

Reconstructing the LCS

16

𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise

1 1 1 1 1 1 1 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 2 3 3 1 2 2 3 3 3 4 1 2 2 3 3 4 4 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝑈 𝐻 𝐵 1 2 3 7 4 5 6 1 3 4 5 6 2 𝑈 𝐷 𝐵 𝑈 𝐵 𝐻

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

slide-17
SLIDE 17

Reconstructing the LCS

17

𝑀𝐷𝑇 𝑗, 𝑘 = if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 )

  • therwise

1 1 1 1 1 1 1 1 1 2 2 2 1 2 2 2 2 2 1 1 2 2 2 3 3 1 2 2 3 3 3 4 1 2 2 3 3 4 4 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝑈 𝐻 𝐵 1 2 3 7 4 5 6 1 3 4 5 6 2 𝑈 𝐷 𝐵 𝑈 𝐵 𝐻

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

slide-18
SLIDE 18

Seam Carving

  • Method for image resizing that doesn’t

scale/crop the image

18

Cropped Scaled Carved

slide-19
SLIDE 19

Cropping

19

  • Removes a “block” of pixels

Cropped

slide-20
SLIDE 20

Scaling

20

Scaled

  • Removes “stripes” of pixels
slide-21
SLIDE 21

Seam Carving

21

Carved

  • Removes “least energy seam” of pixels
  • http://rsizr.com/
slide-22
SLIDE 22

Seattle Skyline

22

slide-23
SLIDE 23

Energy of a Seam

  • Sum of the energies of each pixel

– 𝑓 𝑞 = energy of pixel 𝑞

  • Many choices

– 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”

23

slide-24
SLIDE 24

Identify Recursive Structure

Let 𝑇 𝑗, 𝑘 = least energy seam from the bottom of the image up to pixel 𝑞𝑗,𝑘

24

𝑞𝑗,𝑘

slide-25
SLIDE 25

Finding the Least Energy Seam

25

𝑞𝑜,𝑙

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

𝑛 𝑙=1 𝑇(𝑜, 𝑙)

𝑜 𝑛

slide-26
SLIDE 26

Computing

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

26

𝑞𝑜,𝑙 Known through 𝑜 − 1 𝑛

slide-27
SLIDE 27

Computing

27

Assume we know the least energy seams for all

  • f row 𝑜 − 1 (i.e. we know 𝑇(𝑜 − 1, ℓ) for all ℓ)

S(n-1,k-1)

𝑞𝑜,𝑙

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

𝑇 𝑜, 𝑙 = 𝑛𝑗𝑜

𝑇 𝑜 − 1, 𝑙 − 1 + 𝑓(𝑞𝑜,𝑙) 𝑇 𝑜 − 1, 𝑙 + 𝑓(𝑞𝑜,𝑙) 𝑇 𝑜 − 1, 𝑙 + 1 + 𝑓(𝑞𝑜,𝑙)

slide-28
SLIDE 28

Bring It All Together

Start from bottom of image (row 1), solve up to top Initialize 𝑇 1, 𝑙 = 𝑓(𝑞1,𝑙) for each pixel in row 1

28

𝑜 𝑛 Energy of the seam initialized to the energy of that pixel

slide-29
SLIDE 29

Bring It All Together

Start from bottom of image (row 1), solve up to top Initialize 𝑇 1, 𝑙 = 𝑓(𝑞1,𝑙) for each pixel 𝑞1,𝑙 For 𝑗 > 2 find 𝑇 𝑗, 𝑙 = min

29

𝑜 𝑛 Energy of the seam initialized to the energy of that pixel

𝑇 𝑜 − 1, 𝑙 − 1 + 𝑓(𝑞𝑜,𝑙) 𝑇 𝑜 − 1, 𝑙 + 𝑓(𝑞𝑜,𝑙) 𝑇 𝑜 − 1, 𝑙 + 1 + 𝑓(𝑞𝑜,𝑙)

slide-30
SLIDE 30

Bring It All Together

Start from bottom of image (row 1), solve up to top Initialize 𝑇 1, 𝑙 = 𝑓(𝑞1,𝑙) for each pixel 𝑞1,𝑙 For 𝑗 > 2 find 𝑇 𝑗, 𝑙 = min Pick smallest from top row, backtrack, removing those pixels

30

𝑜 𝑛 Energy of the seam initialized to the energy of that pixel

𝑇 𝑜 − 1, 𝑙 − 1 + 𝑓(𝑞𝑜,𝑙) 𝑇 𝑜 − 1, 𝑙 + 𝑓(𝑞𝑜,𝑙) 𝑇 𝑜 − 1, 𝑙 + 1 + 𝑓(𝑞𝑜,𝑙)

slide-31
SLIDE 31

Run Time?

Start from bottom of image (row 1), solve up to top Initialize 𝑇 1, 𝑙 = 𝑓(𝑞1,𝑙) for each pixel 𝑞1,𝑙 For 𝑗 ≥ 2 find 𝑇 𝑗, 𝑙 = min Pick smallest from top row, backtrack, removing those pixels

31

𝑜 𝑛 Energy of the seam initialized to the energy of that pixel

𝑇 𝑜 − 1, 𝑙 − 1 + 𝑓(𝑞𝑗,𝑙) 𝑇 𝑜 − 1, 𝑙 + 𝑓(𝑞𝑗,𝑙) 𝑇 𝑜 − 1, 𝑙 + 1 + 𝑓(𝑞𝑗,𝑙)

Θ(𝑜 ⋅ 𝑛) Θ(𝑛) Θ(𝑜 + 𝑛)

slide-32
SLIDE 32

Repeated Seam Removal

32

𝑜 𝑛

Only need to update pixels dependent on the removed seam 2𝑜 pixels change

Θ(2𝑜) time to update pixels Θ(𝑜 + 𝑛) time to find min+backtrack