dynamic programming practice

21
Dynamic programming practice 10/21/21

Upload: others

Post on 30-Dec-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Dynamic programming practice

Dynamic programming practice

10/21/21

Page 2: Dynamic programming practice

Recall: Matrix chain multiplication

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

to

from1 2 3 4 5 6

1

2

3

4

5

6

Page 3: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

to

from1 2 3 4 5 6

1

2

3

4

5

6

What goes in the colored cell?A. 0B. 10C. 200D. Cannot be determinedE. None of the above

Page 4: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

to

from1 2 3 4 5 6

1

2

3

4

5

6

What goes in the colored cell?A. 0B. 10C. 200D. Cannot be determinedE. None of the above

Page 5: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

0

0

0

0

0

0

to

from1 2 3 4 5 6

1

2

3

4

5

6

What goes in the colored cell?A. 0B. 50C. 750D. 1000E. None of the above

Page 6: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

0

0

0

0

0

0

to

from1 2 3 4 5 6

1

2

3

4

5

6

What goes in the colored cell?A. 0B. 50C. 750D. 1000E. None of the above

Page 7: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

05000

01000

750

2625 0

0

015750

0

to

from1 2 3 4 5 6

1

2

3

4

5

6

Page 8: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

05000

01000

750

2625 0

0

015750

0

to

from1 2 3 4 5 6

1

2

3

4

5

6

What goes in the colored cell?A. 750B. 1000C. 2500D. 3750E. None of the above

Page 9: Dynamic programming practice

Finding a subproblem

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

05000

01000

750

2625 0

0

015750

0

to

from1 2 3 4 5 6

1

2

3

4

5

6

What goes in the colored cell?A. 750B. 1000C. 2500D. 3750E. None of the above

Page 10: Dynamic programming practice

Matrix Chain multiplication

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 61 2 3 4 5 6

15125 10500 5375 3500 05000

11875 7125

9375

2500 01000

4375

7875

750

2625 0

0

015750

0

to

from

1

2

3

4

5

6

Page 11: Dynamic programming practice

Matrix Chain multiplication

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

What is the running time of this algorithm?A. ⍬(n)B. ⍬(n2)C. ⍬(n2 log n)D. ⍬(n3)E. None of the above

1 2 3 4 5 615125 10500 5375 3500 05000

11875 7125

9375

2500 01000

4375

7875

750

2625 0

0

015750

0

to

from

1

2

3

4

5

6

Page 12: Dynamic programming practice

Matrix Chain multiplication

• What is the optimal number of multiplications to combine a range of matrices?

(30, 35, 15, 5, 10, 20, 25)0 1 2 3 4 5 6

What is the running time of this algorithm?A. ⍬(n)B. ⍬(n2)C. ⍬(n2 log n)D. ⍬(n3)E. None of the above

1 2 3 4 5 615125 10500 5375 3500 05000

11875 7125

9375

2500 01000

4375

7875

750

2625 0

0

015750

0

to

from

1

2

3

4

5

6

Page 13: Dynamic programming practice

Dynamic programming

• Identify subproblems– How do you use them to solve bigger problems?– How do you store their solutions?

Page 14: Dynamic programming practice

Subtlety in optimal substructure

• Optimal substructure: An optimal solution contains the optimal solution to subproblems

Page 15: Dynamic programming practice

Subtlety in optimal substructure

• Optimal substructure: An optimal solution contains the optimal solution to subproblems

• Unweighted shortest path: Given a graph, find a path from u to v with the fewest edges

u vw

Page 16: Dynamic programming practice

Subtlety in optimal substructure

• Optimal substructure: An optimal solution contains the optimal solution to subproblems

• Unweighted longest simple path: Given a graph, find a path from u to v visits each vertex at most once each and has the most edges

uv

w

Page 17: Dynamic programming practice

Longest common subsequence (LCS)

Given two sequences, what is the longest subsequence that they share? (Taking some values and preserving order)

B, D, C, A, B, A

A, B, C, B, D, A, B

Page 18: Dynamic programming practice

What is the length of the LCS for the sequences below?

A, B, A, C, A, B, C, A, B, A, CC, A, B, C, C, B, C, B, A

A. 4B. 5C. 6D. 7E. None of the above

Page 19: Dynamic programming practice

What is the length of the LCS for the sequences below?

A, B, A, C, A, B, C, A, B, A, CC, A, B, C, C, B, C, B, A

A. 4B. 5C. 6D. 7E. None of the above

Page 20: Dynamic programming practice

Subproblem

A[i,j] = length of LCS using first i chars of 1st

sequence and first j chars of 2nd

Page 21: Dynamic programming practice

𝜀 A B A C A B C A B A C𝜀CA

B

CC

B

CB

A

0 if i=0 or j=0A[i,j] = 1+A[i-1,j-1] if s[i]=t[j]

max{A[i-1,j], A[i,j-1]} o.w. {A[i,j] = length of LCS using i chars of s and j chars of t