13 - 06 feb - dynamic programming

50
CS 321. Algorithm Analysis & Design Lecture 13 Dynamic Programming Edit Distance Knapsacks Shortest Paths

Upload: neeldhara-misra

Post on 14-Apr-2017

87 views

Category:

Education


0 download

TRANSCRIPT

Page 1: 13 - 06 Feb - Dynamic Programming

CS 321. Algorithm Analysis & Design Lecture 13

Dynamic Programming

Edit DistanceKnapsacks

Shortest Paths

Page 2: 13 - 06 Feb - Dynamic Programming
Page 3: 13 - 06 Feb - Dynamic Programming

Items with weights wi

and value vi

Page 4: 13 - 06 Feb - Dynamic Programming

Knapsack with capacity C

Items with weights wi

and value vi

Page 5: 13 - 06 Feb - Dynamic Programming
Page 6: 13 - 06 Feb - Dynamic Programming

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 7: 13 - 06 Feb - Dynamic Programming

(What we want)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 8: 13 - 06 Feb - Dynamic Programming

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 9: 13 - 06 Feb - Dynamic Programming

A[i] stores the best value for the first i items.

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 10: 13 - 06 Feb - Dynamic Programming

A[i] stores the best value for the first i items.

A[i] = max(A(i-1), A[i-1] + vi)

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 11: 13 - 06 Feb - Dynamic Programming

A[i] stores the best value for the first i items.

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 12: 13 - 06 Feb - Dynamic Programming

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

Page 13: 13 - 06 Feb - Dynamic Programming

A[i,j] stores the best value for the first i items.

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

that fits in a knapsack of size j.

Page 14: 13 - 06 Feb - Dynamic Programming

A[i,j] stores the best value for the first i items.

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(What we want)

(Build up the array bottom to top.)

w1

v1

w2

v2

wi

vi

wn-1

vn-1

wn

vn… …

that fits in a knapsack of size j.

Page 15: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1

2

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

Page 16: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1

2

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 17: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1

2

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 18: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1

2

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 19: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 20: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 21: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 22: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 23: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 24: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3 0 6 7 9 9

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 25: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3 0 6 7 9 9

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 26: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3 0 6 7 9 9

4

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 27: 13 - 06 Feb - Dynamic Programming

1 2 3 4 5

1 0 3 3 3 3

2 0 6 6 9 9

3 0 6 7 9 9

4 4 6 10 11 13

A[i,j] = max(A(i-1,j), A[i-1,j-wi] + vi)

(2,3), (2,6), (3,7), (1,4) - weight/value pairs

Page 28: 13 - 06 Feb - Dynamic Programming
Page 29: 13 - 06 Feb - Dynamic Programming

Running time = # of subproblems x time/subproblem

Page 30: 13 - 06 Feb - Dynamic Programming

Running time = # of subproblems x time/subproblem

nC x O(1) = O(nC) (n = #items, C = Capacity)

Page 31: 13 - 06 Feb - Dynamic Programming

Running time = # of subproblems x time/subproblem

nC x O(1) = O(nC) (n = #items, C = Capacity)

Is this polynomial?

Page 32: 13 - 06 Feb - Dynamic Programming

Running time = # of subproblems x time/subproblem

nC x O(1) = O(nC) (n = #items, C = Capacity)

Is this polynomial?

pseudo-polynomial

Page 33: 13 - 06 Feb - Dynamic Programming

Edit Distance

Page 34: 13 - 06 Feb - Dynamic Programming

Edit Distance

The cost of changing one word to another, using insertions/deletions/changes.

Page 35: 13 - 06 Feb - Dynamic Programming

Edit Distance

The cost of changing one word to another, using insertions/deletions/changes.

Each operation can have a certain cost.

Page 36: 13 - 06 Feb - Dynamic Programming

A C G T

A 0 1 1 3

C 2 0 1 1

G 1 2 0 2

T 3 2 1 0

Page 37: 13 - 06 Feb - Dynamic Programming

SUNNY DEOL

SUNNY LEONE

Page 38: 13 - 06 Feb - Dynamic Programming

SUNNY DEOL

SUNNY LEONE

Page 39: 13 - 06 Feb - Dynamic Programming

SUNNY DEOL

SUNNY LEONE

SUNNY LEOL

Page 40: 13 - 06 Feb - Dynamic Programming

SUNNY DEOL

SUNNY LEONE

SUNNY LEOL

SUNNY LEON

Page 41: 13 - 06 Feb - Dynamic Programming

SUNNY DEOL

SUNNY LEONE

SUNNY LEOL

SUNNY LEON

SUNNY LEONE

Page 42: 13 - 06 Feb - Dynamic Programming
Page 43: 13 - 06 Feb - Dynamic Programming
Page 44: 13 - 06 Feb - Dynamic Programming

Work out the solution for pairs x[i:] and y[j:].

Page 45: 13 - 06 Feb - Dynamic Programming

(What we want)

(Build up the array bottom to top.)

Page 46: 13 - 06 Feb - Dynamic Programming

(What we want)

(Build up the array bottom to top.)

c[i,j] stores the edit distance between x[1…i] and y[1…j].

Page 47: 13 - 06 Feb - Dynamic Programming

(What we want)

(Build up the array bottom to top.)

c[i,j] stores the edit distance between x[1…i] and y[1…j].

c[i,j] = min(c[i-1,j] + w1, c[i,j-1] + w2, c[i-1,j-1] + w3)

Page 48: 13 - 06 Feb - Dynamic Programming

(What we want)

(Build up the array bottom to top.)

c[i,j] stores the edit distance between x[1…i] and y[1…j].

Page 49: 13 - 06 Feb - Dynamic Programming
Page 50: 13 - 06 Feb - Dynamic Programming