cs4102 l14 longestcommonsubsequencejh2jf/courses/spring2019/cs4102/lectur… · –java or python...

19
CS4102 Algorithms Spring 2019 1 Can you fill a !×! board with the corners missing using dominoes? Can you tile this? With these?

Upload: others

Post on 03-Jul-2020

7 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure

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

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
Page 4: cs4102 L14 longestCommonSubsequencejh2jf/courses/spring2019/cs4102/lectur… · –Java or Python •Grades Released –HW3 –Midterm 6. Dynamic Programming •Requires Optimal Substructure

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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