+ All Categories
Home > Documents > CSC 413/513: Intro to Algorithms

CSC 413/513: Intro to Algorithms

Date post: 05-Feb-2016
Category:
Upload: shiloh
View: 68 times
Download: 0 times
Share this document with a friend
Description:
CSC 413/513: Intro to Algorithms. Dynamic Programming. Dynamic Programming. Another strategy for designing algorithms is dynamic programming A metatechnique, not an algorithm (like divide & conquer) The word “programming” is historical and predates computer programming - PowerPoint PPT Presentation
35
CSC 413/513: Intro to Algorithms Dynamic Programming
Transcript
Page 1: CSC 413/513: Intro to Algorithms

CSC 413/513: Intro to Algorithms

Dynamic Programming

Page 2: CSC 413/513: Intro to Algorithms

Dynamic Programming

● Another strategy for designing algorithms is dynamic programming■ A metatechnique, not an algorithm

(like divide & conquer)■ The word “programming” is historical and

predates computer programming

● Use when problem breaks down into recurring smaller subproblems

Page 3: CSC 413/513: Intro to Algorithms

Dynamic Programming Example: Longest Common Subsequence

● Longest common subsequence (LCS) problem: ■ Given two sequences x[1..m] and y[1..n], find the

longest subsequence which occurs in both■ Ex: x = {A B C B D A B }, y = {B D C A B A}■ {B C} and {A A} are both subsequences of both

○ What is the LCS?

■ Brute-force algorithm: For every subsequence of x, check if it’s a subsequence of y

○ How many subsequences of x are there?○ What will be the running time of the brute-force alg?

Page 4: CSC 413/513: Intro to Algorithms

LCS Algorithm

● Brute-force algorithm: 2m subsequences of x to check against n elements of y: O(n 2m)

● We can do better: for now, let’s only worry about the problem of finding the length of LCS■ When finished we will see how to backtrack from this

solution back to the actual LCS

● Notice LCS problem has optimal substructure■ Subproblems: LCS of pairs of prefixes of X and Y

■ Define Xi, Yj to be the prefixes of X and Y of length i and j respectively

Page 5: CSC 413/513: Intro to Algorithms

Finding LCS Length

● Define c[i,j] to be the length of the LCS of x[1..i] and y[1..j]■ What is the length of LCS of x and y?

● Theorem:

otherwise]),1[],1,[max(

],[][ if1]1,1[],[

jicjic

jyixjicjic

Page 6: CSC 413/513: Intro to Algorithms

LCS recursive solution

● We start with i = j = 0 (empty substrings of x and y)

● Since X0 and Y0 are empty strings, their LCS is

always empty (i.e. c[0,0] = 0)

● LCS of empty string and any other string is empty,

so for every i and j: c[0, j] = c[i,0] = 0

otherwise]),1[],1,[max(

],[][ if1]1,1[],[

jicjic

jyixjicjic

Page 7: CSC 413/513: Intro to Algorithms

LCS recursive solution

● When we calculate c[i,j], we consider two cases:

● First case: x[i]=y[j]: one more symbol in strings X

and Y matches, so the length of LCS Xi and Yj equals

to the length of LCS of smaller strings Xi-1 and Yi-1 ,

plus 1

otherwise]),1[],1,[max(

],[][ if1]1,1[],[

jicjic

jyixjicjic

Page 8: CSC 413/513: Intro to Algorithms

LCS recursive solution

● Second case: x[i] != y[j]

● As symbols don’t match, our solution is not

improved, and the length of LCS(Xi , Yj) is the same

as before (i.e. maximum of LCS(Xi, Yj-1) and

LCS(Xi-1,Yj)

otherwise]),1[],1,[max(

],[][ if1]1,1[],[

jicjic

jyixjicjic

Why not just take the length of LCS(Xi-1, Yj-1) ?

Page 9: CSC 413/513: Intro to Algorithms

LCS Length Algorithm

LCS-Length(X, Y)1. m = length(X) // get the # of symbols in X2. n = length(Y) // get the # of symbols in Y

3. for i = 1 to m c[i,0] = 0 // special case: Y0

4. for j = 1 to n c[0,j] = 0 // special case: X0

5. for i = 1 to m // for all Xi

6. for j = 1 to n // for all Yj

7. if ( Xi == Yj )8. c[i,j] = c[i-1,j-1] + 19. else c[i,j] = max( c[i-1,j], c[i,j-1] )10. return c

Page 10: CSC 413/513: Intro to Algorithms

LCS Example

We’ll see how LCS algorithm works on the following example:

● X = ABCB● Y = BDCAB

LCS(X, Y) = BCBX = A B C BY = B D C A B

What is the Longest Common Subsequence of X and Y?

Page 11: CSC 413/513: Intro to Algorithms

LCS Example (0)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

X = ABCB; m = |X| = 4Y = BDCAB; n = |Y| = 5Allocate array c[5,4]

ABCBBDCAB

Page 12: CSC 413/513: Intro to Algorithms

LCS Example (1)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

for i = 1 to m c[i,0] = 0 for j = 1 to n c[0,j] = 0

ABCBBDCAB

Page 13: CSC 413/513: Intro to Algorithms

LCS Example (2)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

0

ABCBBDCAB

Page 14: CSC 413/513: Intro to Algorithms

LCS Example (3)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

0 0 0

ABCBBDCAB

Page 15: CSC 413/513: Intro to Algorithms

LCS Example (4)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

0 0 0 1

ABCBBDCAB

Page 16: CSC 413/513: Intro to Algorithms

LCS Example (5)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

000 1 1

ABCBBDCAB

Page 17: CSC 413/513: Intro to Algorithms

LCS Example (6)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

0 0 10 1

1

ABCBBDCAB

Page 18: CSC 413/513: Intro to Algorithms

LCS Example (7)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 1 11

ABCBBDCAB

Page 19: CSC 413/513: Intro to Algorithms

LCS Example (8)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 1 1 1 2

ABCBBDCAB

Page 20: CSC 413/513: Intro to Algorithms

LCS Example (10)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj )c[i,j] = c[i-1,j-1] + 1

else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

21 1 11

1 1

ABCBBDCAB

Page 21: CSC 413/513: Intro to Algorithms

LCS Example (11)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 21 11

1 1 2

ABCBBDCAB

Page 22: CSC 413/513: Intro to Algorithms

LCS Example (12)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj )c[i,j] = c[i-1,j-1] + 1

else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 21 1

1 1 2

1

22

ABCBBDCAB

Page 23: CSC 413/513: Intro to Algorithms

LCS Example (13)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 21 1

1 1 2

1

22

1

ABCBBDCAB

Page 24: CSC 413/513: Intro to Algorithms

LCS Example (14)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj )c[i,j] = c[i-1,j-1] + 1

else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 21 1

1 1 2

1

22

1 1 2 2

ABCBBDCAB

Page 25: CSC 413/513: Intro to Algorithms

LCS Example (15)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

if ( Xi == Yj ) c[i,j] = c[i-1,j-1] + 1 else c[i,j] = max( c[i-1,j], c[i,j-1] )

1000 1

1 21 1

1 1 2

1

22

1 1 2 2 3

ABCBBDCAB

Page 26: CSC 413/513: Intro to Algorithms

LCS Algorithm Running Time

● LCS algorithm calculates the values of each entry of the array c[m,n]

● So what is the running time?

O(m*n)

since each c[i,j] is calculated in constant time, and there are m*n elements in the array

Page 27: CSC 413/513: Intro to Algorithms

2

2 3

2 For example, here c[i,j] = c[i-1,j-1] +1 = 2+1=3

How to find actual LCS

● So far, we have just found the length of LCS, but not LCS itself.

● We want to modify this algorithm to make it output Longest Common Subsequence of X and Y

Each c[i,j] depends on c[i-1,j] and c[i,j-1]

or c[i-1, j-1]

For each c[i,j] we can remember how it was acquired:

Page 28: CSC 413/513: Intro to Algorithms

How to find actual LCS - continued

● Remember that

otherwise]),1[],1,[max(

],[][ if1]1,1[],[

jicjic

jyixjicjic

So we can start from c[m,n] and go backwards Whenever c[i,j] = c[i-1, j-1]+1, remember x[i]

(because x[i] is a part of LCS) When i=0 or j=0 (i.e. we reached the beginning),

output remembered letters in reverse order

Page 29: CSC 413/513: Intro to Algorithms

Finding LCSj 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

Yj BB ACD

0

0

00000

0

0

0

1000 1

1 21 1

1 1 2

1

22

1 1 2 2 3B

Page 30: CSC 413/513: Intro to Algorithms

Finding LCS (2)j 0 1 2 3 4 5

0

1

2

3

4

i

Xi

A

B

C

Yj BB ACD

0

0

00000

0

0

0

1000 1

1 21 1

1 1 2

1

22

1 1 2 2 3B

B C BLCS (reversed order):

LCS (straight order): B C B

Page 31: CSC 413/513: Intro to Algorithms

Matrix Chain Multiplication

● Minimization problem

● Many ways to parenthesize A1 A2 A3 A4

■ Size of Ai: pi-1 X pi

● Different costs (#scalar products)● Let m[i,j] = min #scalar prods for multiplying

AiAi+1…Aj

■ Always: i <= j. Therefore, need half of nXn table■ m[i,i] = 0

■ m[i,j] = min i<=k<j {m[i,k]+m[k+1,j]+pi-1pkpj}, if i<j

Page 32: CSC 413/513: Intro to Algorithms
Page 33: CSC 413/513: Intro to Algorithms

Algorithm

Page 34: CSC 413/513: Intro to Algorithms

Coming up: Knapsack problem

Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W.So we must consider weights of items as well as their value.

Item # Weight Value 1 1 8 2 3 6 3 5 5

Page 35: CSC 413/513: Intro to Algorithms

Knapsack problem

There are two versions of the problem:(1) “0-1 knapsack problem” and(2) “Fractional knapsack problem”

(1) Items are indivisible; you either take an itemor not. Solved with dynamic programming(2) Items are divisible: you can take any fraction of an item. Solved with a greedy algorithm.


Recommended