Dynamic Programming Has nothing to do with programming in the way - - PDF document

dynamic programming
SMART_READER_LITE
LIVE PREVIEW

Dynamic Programming Has nothing to do with programming in the way - - PDF document

Dynamic Programming Has nothing to do with programming in the way we normally use that term Dynamic Programming More like TV programming (scheduling) Filling in a table Chapter 16 Algorithm design technique CPTR 318 1 2


slide-1
SLIDE 1

1

Dynamic Programming

Chapter 16

CPTR 318

1

Dynamic Programming

  • Has nothing to do with programming in the

way we normally use that term

– More like TV programming (scheduling) – Filling in a table

  • Algorithm design technique

2

Longest Common Subsequence

  • Given two sequences

X = x0 x1 x2 … xm-1 Y = y0 y1 y2 … yn-1 find a longest sequence common to both

  • Example:

X = A B C B D A B Y = B D C A B A

– B D A B – B C A B – B C B A

3

Longest Common Subsequence

  • Given two sequences

X = x0 x1 x2 … xm-1 Y = y0 y1 y2 … yn-1 find a longest sequence common to both

  • Example:

X = A B C B D A B Y = B D C A B A

– B D A B – B C A B – B C B A

4

Longest Common Subsequence

  • Given two sequences

X = x0 x1 x2 … xm-1 Y = y0 y1 y2 … yn-1 find a longest sequence common to both

  • Example:

X = A B C B D A B Y = B D C A B A

– B D A B – B C A B – B C B A

5

Longest Common Subsequence

  • Given two sequences

X = x0 x1 x2 … xm-1 Y = y0 y1 y2 … yn-1 find a longest sequence common to both

  • Example:

X = A B C B D A B Y = B D C A B A

– B D A B – B C A B – B C B A

6

slide-2
SLIDE 2

2

Simple Algorithm

  • Brute force

– Try all subsequences s of X to see if s is a subsequence of Y – Select a longest such subsequence

7

Brute Force Pseudocode

string LCS(string X[0..m-1], string Y[0..n-1]) { string result = ""; if (|X| > 0 && |Y| > 0) { if (X[0] == Y[0]) result = X[0] + LCS(X[1..m-1], Y[1, n-1]); else result = maxlen(LCS(X, Y[1..n-1]), LCS(X[1..m-1], Y)); } return result; }

8

Brute Force Pseudocode

string LCS(string X[0..m-1], string Y[0..n-1]) { string result = ""; if (|X| > 0 && |Y| > 0) { if (X[0] == Y[0]) result = X[0] + LCS(X[1..m-1], Y[1, n-1]); else result = maxlen(LCS(X, Y[1..n-1]), LCS(X[1..m-1], Y)); } return result; }

9

+ here means

string concatenation

Brute Force Pseudocode

string LCS(string X[0..m-1], string Y[0..n-1]) { string result = ""; if (|X| > 0 && |Y| > 0) { if (X[0] == Y[0]) result = X[0] + LCS(X[1..m-1], Y[1, n-1]); else result = maxlen(LCS(X, Y[1..n-1]), LCS(X[1..m-1], Y)); } return result; }

10

+ here means

string concatenation maxlen returns the longer of two strings

Brute Force Pseudocode

string LCS(string X[0..m-1], string Y[0..n-1]) { string result = ""; if (|X| > 0 && |Y| > 0) { if (X[0] == Y[0]) result = X[0] + LCS(X[1..m-1], Y[1, n-1]); else result = maxlen(LCS(X, Y[1..n-1]), LCS(X[1..m-1], Y)); } return result; }

11

+ here means

string concatenation maxlen returns the longer of two strings “Absolute value” bars mean string length

Brute Force Pseudocode

string LCS(string X[0..m-1], string Y[0..n-1]) { string result = ""; if (|X| > 0 && |Y| > 0) { if (X[0] == Y[0]) result = X[0] + LCS(X[1..m-1], Y[1, n-1]); else result = maxlen(LCS(X, Y[1..n-1]), LCS(X[1..m-1], Y)); } return result; }

12

slide-3
SLIDE 3

3

Brute Force Complexity

  • Need to check each subsequence of X
  • Θ(n) to check a subsequence of X
  • There are Θ(2m) subsequences of X
  • Total time Θ(n∙2m)

13

Dynamic Programming

  • Overlapping subproblems

– The solution to a subproblem is recorded and can be used multiple times

  • Optimal substructure

– Optimal solutions of subproblems are used to compute the optimal solution to the overall problem

14

Table of Subsequence Lengths

  • Consider length of LCS(X,Y)
  • Consider prefixes of X and Y
  • Build a table of prefix lengths

– C[i][j] = | LCS(X[0..i - 1], Y[0..j - 1]) | – C[m-1][n-1] = | LCS(X, Y) |

  • C[i][j] = C[i - 1][j - 1] + 1 , if X[i] = Y[j]
  • r max(C[i][j - 1] , C[i -1][j] ), otherwise

15

Build the DP Table

Table LCS_table(string X[0..m-1], string Y[0..n-1]) { Table C[0..m][0..n]; for (int i = 0; i <= m; i++) C[i][0] = 0; for (int j = 0; j <= n; j++) C[0][j] = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (X[i] == Y[j]) C[i + 1][j + 1] = C[i][j] + 1; else C[i + 1][j + 1] = max(C[i + 1][j], C[i][j + 1]); return C; }

16

Build the DP Table

Table LCS_table(string X[0..m-1], string Y[0..n-1]) { Table C[0..m][0..n]; for (int i = 0; i <= m; i++) C[i][0] = 0; for (int j = 0; j <= n; j++) C[0][j] = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (X[i] == Y[j]) C[i + 1][j + 1] = C[i][j] + 1; else C[i + 1][j + 1] = max(C[i + 1][j], C[i][j + 1]); return C; }

17

Initialization

Build the DP Table

Table LCS_table(string X[0..m-1], string Y[0..n-1]) { Table C[0..m][0..n]; for (int i = 0; i <= m; i++) C[i][0] = 0; for (int j = 0; j <= n; j++) C[0][j] = 0; for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) if (X[i] == Y[j]) C[i + 1][j + 1] = C[i][j] + 1; else C[i + 1][j + 1] = max(C[i + 1][j], C[i][j + 1]); return C; }

18

Table construction

slide-4
SLIDE 4

4

19

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

Extract the Subsequence

string back_trace(Table C, string X, string Y, int i, int j) { if (i < 0 || j < 0) return ""; else if (X[i] == Y[j]) return back_trace(C, X, Y, i - 1, j - 1) + X[i]; else if (C[i + 1][j] > C[i][j + 1]) return back_trace(C, X, Y, i, j - 1); else return back_trace(C, X, Y, i - 1, j); }

20

Extract the Subsequence

string back_trace(Table C, string X, string Y, int i, int j) { if (i < 0 || j < 0) return ""; else if (X[i] == Y[j]) return back_trace(C, X, Y, i - 1, j - 1) + X[i]; else if (C[i + 1][j] > C[i][j + 1]) return back_trace(C, X, Y, i, j - 1); else return back_trace(C, X, Y, i - 1, j); }

21

Initial call: back_trace(C, X, Y, m – 1, n – 1)

22

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

23

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

BCAB

24

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

BCAB

slide-5
SLIDE 5

5

25

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

26

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

27

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

28

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

29

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

30

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

slide-6
SLIDE 6

6

31

X Y A B C B D A B B 1 1 1 1 1 1 D 1 1 1 2 2 2 C 1 2 2 2 2 2 A 1 1 2 2 2 3 3 B 1 2 2 3 3 3 4 A 1 2 2 3 3 4 4

BDAB DP Complexity

  • Θ(m∙n) to fill out the table
  • Θ(max(m , n)) to backtrace
  • Space: Θ(m∙n)

32