cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python...
Transcript of cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python...
![Page 1: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/1.jpg)
CS4102 AlgorithmsSpring 2019
1
Can you fill a !×! board with the corners missing using dominoes?
Can you tile this?
With these?
![Page 2: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/2.jpg)
CS4102 AlgorithmsSpring 2019
2
Can you fill a !×! board with the corners missing using dominoes?
Can you tile this?
With these?
![Page 3: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/3.jpg)
![Page 4: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/4.jpg)
Today’s Keywords
• Midterm Review• Dynamic Programming• Longest Common Subsequence
4
![Page 5: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/5.jpg)
CLRS Readings
• Chapter 15
5
![Page 6: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/6.jpg)
Administrativa
• HW5 due March 27 at 11pm– Seam Carving!– Dynamic Programming (implementation)– Java or Python
• Grades Released– HW3– Midterm
6
![Page 7: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/7.jpg)
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
7
![Page 8: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/8.jpg)
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: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/9.jpg)
Longest Common Subsequence
9
Given two sequences ! and ", find the length of their longest common subsequence
Example:! = $%&%'$%" = %'&$%$(&) = %&%$
Brute force: Compare every subsequence of ! with "Ω(2-)
![Page 10: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/10.jpg)
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
![Page 11: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/11.jpg)
1. Identify Recursive StructureLet !"# $, & = length of the LCS for the first $ characters of (, first & character of )Find !"#($, &):
11
( = ,-"-.".-) = -.",-,-
!"# $, & = !"# $ − 1, & − 1 + 1
( = ,-"-.".,) = -.",-,-
!"# $, & = !"# $, & − 1
Case 1: ( $ = )[&]
Case 2: ( $ ≠ )[&]( = ,-"-.".-) = -.",-,"
!"# $, & = !"# $ − 1, &
!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if ( $ = )[&]max(!"# $, & − 1 , !"# $ − 1, & ) otherwise
![Page 12: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/12.jpg)
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
![Page 13: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/13.jpg)
1. Identify Recursive StructureLet !"# $, & = length of the LCS for the first $ characters of (, first & character of )Find !"#($, &):
13
( = ,-"-.".-) = -.",-,-
!"# $, & = !"# $ − 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
![Page 14: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/14.jpg)
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
14
![Page 15: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/15.jpg)
2. Solve in a Good Order
15
!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if / $ = 0[&]max(!"# $, & − 1 , !"# $ − 1, & ) otherwise
0 0 0 0 0 0 0 00 0 1 1 1 1 1 10 0 1 1 1 2 2 20 0 1 2 2 2 2 20 1 1 2 2 2 3 30 1 2 2 3 3 3 40 1 2 2 3 3 4 4
/ = 8 9 " 99 : 81 2 3 74 5 600 =
01
3456
29
"898
:
To fill in cell ($, &) we need cells $ − 1, & − 1 , $ − 1, & , ($, & − 1)Fill from Top->Bottom, Left->Right (with any preference)
![Page 16: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/16.jpg)
Run Time?
16
!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if / $ = 0[&]max(!"# $, & − 1 , !"# $ − 1, & ) otherwise
0 0 0 0 0 0 0 00 0 1 1 1 1 1 10 0 1 1 1 2 2 20 0 1 2 2 2 2 20 1 1 2 2 2 3 30 1 2 2 3 3 3 40 1 2 2 3 3 4 4
/ = 8 9 " 99 : 81 2 3 74 5 600 =
01
3456
29
"898
:
Run Time: Θ(B ⋅ D) (for / = B, 0 = D)
![Page 17: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/17.jpg)
Reconstructing the LCS
17
!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if / $ = 0[&]max(!"# $, & − 1 , !"# $ − 1, & ) otherwise
0 0 0 0 0 0 0 00 0 1 1 1 1 1 10 0 1 1 1 2 2 20 0 1 2 2 2 2 20 1 1 2 2 2 3 30 1 2 2 3 3 3 40 1 2 2 3 3 4 4
/ = 8 9 " 99 : 81 2 3 74 5 600 =
01
3456
29
"898
:
Start from bottom right,if symbols matched, print that symbol then go diagonallyelse go to largest adjacent
![Page 18: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/18.jpg)
Reconstructing the LCS
18
!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if / $ = 0[&]max(!"# $, & − 1 , !"# $ − 1, & ) otherwise
0 0 0 0 0 0 0 00 0 1 1 1 1 1 10 0 1 1 1 2 2 20 0 1 2 2 2 2 20 1 1 2 2 2 3 30 1 2 2 3 3 3 40 1 2 2 3 3 4 4
/ = 8 9 " 99 : 81 2 3 74 5 600 =
01
3456
29
"898
:
Start from bottom right,if symbols matched, print that symbol then go diagonallyelse go to largest adjacent
![Page 19: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure](https://reader033.fdocuments.net/reader033/viewer/2022060422/5f18e1d998ba5b687c11dc80/html5/thumbnails/19.jpg)
Reconstructing the LCS
19
!"# $, & =0 if $ = 0 or& = 0!"# $ − 1, & − 1 + 1 if / $ = 0[&]max(!"# $, & − 1 , !"# $ − 1, & ) otherwise
0 0 0 0 0 0 0 00 0 1 1 1 1 1 10 0 1 1 1 2 2 20 0 1 2 2 2 2 20 1 1 2 2 2 3 30 1 2 2 3 3 3 40 1 2 2 3 3 4 4
/ = 8 9 " 99 : 81 2 3 74 5 600 =
01
3456
29
"898
:
Start from bottom right,if symbols matched, print that symbol then go diagonallyelse go to largest adjacent