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 Mr. Kruger’s boombox into the ocean. How did George make this discovery? 1
https://www.youtube.com/watch?v=pSB3HdmLcY4
Today’s Keywords • Dynamic Programming • Longest Common Subsequence • Seam Carving • Seinfeld 3
CLRS Readings • Chapter 15 4
Homeworks • Hw5 Released on Friday – Programming – Dynamic Programming 5
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
Longest Common Subsequence Given two sequences 𝑌 and 𝑍 , find the length of their longest common subsequence Example: 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐵𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵 𝑀𝐷𝑇 = 𝑈𝐷𝑈𝐵 Brute force: Compare every subsequence of 𝑌 with 𝑍 Ω(2 𝑜 ) 7
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
1. Identify Recursive Structure Let 𝑀𝐷𝑇 𝑗, 𝑘 = length of the LCS for the first 𝑗 characters of 𝑌 , first 𝑘 characters of 𝑍 Find 𝑀𝐷𝑇(𝑗, 𝑘) : 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 Case 1: 𝑌 𝑗 = 𝑍[𝑘] 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 Case 2: 𝑌 𝑗 ≠ 𝑍[𝑘] 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝐵 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝐷 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗, 𝑘 − 1 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 0 if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise 9
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
1. Identify Recursive Structure Let 𝑀𝐷𝑇 𝑗, 𝑘 = length of the LCS for the first 𝑗 characters of 𝑌 , first 𝑘 character of 𝑍 Find 𝑀𝐷𝑇(𝑗, 𝑘) : 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 Case 1: 𝑌 𝑗 = 𝑍[𝑘] 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 Case 2: 𝑌 𝑗 ≠ 𝑍[𝑘] 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝐵 𝑌 = 𝐵𝑈𝐷𝑈𝐻𝐷𝐻𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝑈 𝑍 = 𝑈𝐻𝐷𝐵𝑈𝐵𝐷 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗, 𝑘 − 1 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 Read from M[i,j] 0 if 𝑗 = 0 or 𝑘 = 0 if present 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise Save to M[i,j] 11
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
2. Solve in a Good Order 0 if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝐻 𝐵 𝑈 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 𝑈 0 0 1 1 1 1 1 1 1 𝐻 2 0 0 1 1 1 2 2 2 𝐷 3 0 0 1 2 2 2 2 2 𝐵 4 0 1 1 2 2 2 3 3 𝑈 5 0 1 2 2 3 3 3 4 𝐵 6 0 1 2 2 3 3 4 4 To fill in cell (𝑗, 𝑘) we need cells 𝑗 − 1, 𝑘 − 1 , 𝑗 − 1, 𝑘 , (𝑗, 𝑘 − 1) Fill from Top->Bottom, Left->Right (with any preference) 13
Run Time? 0 if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝐻 𝐵 𝑈 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 𝑈 0 0 1 1 1 1 1 1 1 𝐻 2 0 0 1 1 1 2 2 2 𝐷 3 0 0 1 2 2 2 2 2 𝐵 4 0 1 1 2 2 2 3 3 𝑈 5 0 1 2 2 3 3 3 4 𝐵 6 0 1 2 2 3 3 4 4 Run Time: Θ(𝑜 ⋅ 𝑛) (for 𝑌 = 𝑜 , 𝑍 = 𝑛 ) 14
Reconstructing the LCS 0 if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝐻 𝐵 𝑈 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 𝑈 0 0 1 1 1 1 1 1 1 𝐻 2 0 0 1 1 1 2 2 2 𝐷 3 0 0 1 2 2 2 2 2 𝐵 4 0 1 1 2 2 2 3 3 𝑈 5 0 1 2 2 3 3 3 4 𝐵 6 0 1 2 2 3 3 4 4 Start from bottom right, if symbols matched, print that symbol then go diagonally 15 else go to largest adjacent
Reconstructing the LCS 0 if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝐻 𝐵 𝑈 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 𝑈 0 0 1 1 1 1 1 1 1 𝐻 2 0 0 1 1 1 2 2 2 𝐷 3 0 0 1 2 2 2 2 2 𝐵 4 0 1 1 2 2 2 3 3 𝑈 5 0 1 2 2 3 3 3 4 𝐵 6 0 1 2 2 3 3 4 4 Start from bottom right, if symbols matched, print that symbol then go diagonally 16 else go to largest adjacent
Reconstructing the LCS 0 if 𝑗 = 0 or 𝑘 = 0 𝑀𝐷𝑇 𝑗, 𝑘 = 𝑀𝐷𝑇 𝑗 − 1, 𝑘 − 1 + 1 if 𝑌 𝑗 = 𝑍[𝑘] max(𝑀𝐷𝑇 𝑗, 𝑘 − 1 , 𝑀𝐷𝑇 𝑗 − 1, 𝑘 ) otherwise 𝑌 = 𝐵 𝑈 𝐷 𝑈 𝐻 𝐵 𝑈 0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0 0 𝑈 0 0 1 1 1 1 1 1 1 𝐻 2 0 0 1 1 1 2 2 2 𝐷 3 0 0 1 2 2 2 2 2 𝐵 4 0 1 1 2 2 2 3 3 𝑈 5 0 1 2 2 3 3 3 4 𝐵 6 0 1 2 2 3 3 4 4 Start from bottom right, if symbols matched, print that symbol then go diagonally 17 else go to largest adjacent
Seam Carving • Method for image resizing that doesn’t scale/crop the image Cropped Scaled Carved 18
Cropping • Removes a “block” of pixels Cropped 19
Scaling • Removes “stripes” of pixels Scaled 20
Seam Carving • Removes “least energy seam” of pixels • http://rsizr.com/ Carved 21
Seattle Skyline 22
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
Identify Recursive Structure Let 𝑇 𝑗, 𝑘 = least energy seam from the bottom of the image up to pixel 𝑞 𝑗,𝑘 𝑞 𝑗,𝑘 24
Finding the Least Energy Seam Want the least energy seam going from bottom to top, so delete: 𝑛 min 𝑙=1 𝑇(𝑜, 𝑙) 𝑞 𝑜,𝑙 𝑜 25 𝑛
Computing Assume we know the least energy seams for all of row 𝑜 − 1 (i.e. we know 𝑇(𝑜 − 1, ℓ) for all ℓ ) 𝑞 𝑜,𝑙 Known through 𝑜 − 1 26 𝑛
Computing Assume we know the least energy seams for all of row 𝑜 − 1 (i.e. we know 𝑇(𝑜 − 1, ℓ) for all ℓ ) 𝑇 𝑜 − 1, 𝑙 − 1 + 𝑓(𝑞 𝑜,𝑙 ) 𝑇 𝑜, 𝑙 = 𝑛𝑗𝑜 𝑇 𝑜 − 1, 𝑙 + 𝑓(𝑞 𝑜,𝑙 ) 𝑞 𝑜,𝑙 𝑇 𝑜 − 1, 𝑙 + 1 + 𝑓(𝑞 𝑜,𝑙 ) S(n,k) S(n-1,k-1) S(n-1,k) S(n-1,k+1) 27
Bring It All Together Start from bottom of image (row 1), solve up to top Initialize 𝑇 1, 𝑙 = 𝑓(𝑞 1,𝑙 ) for each pixel in row 1 𝑜 Energy of the seam initialized to the energy of that pixel 28 𝑛
Recommend
More recommend