91
Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1 Contest Algorithms: 11. DP

Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Embed Size (px)

Citation preview

Page 1: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

1

Contest AlgorithmsJanuary 2016

Introduce DP; Look at several examples; Compare DP to Greedy

11. Dynamic Programming (DP)

Contest Algorithms: 11. DP

Page 2: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 2

1. DP Features 2. The fibonacci series 3. Binomial coefs 4. 0-1 knapsack 5. Longest Common Subsequence 6. Edit distance 7. Compare DP to Greedy using fractional knapsack

DP Topics

Page 3: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

An optimal (best) solution to the problem is a composition of optimal (best) subproblem solutions

makes the code recursive (perhaps)

The same subproblems appear many times while solving the problem

use tabling / memoziation to 'remember' answers perhaps calculate subproblems first; called bottom-

up evaluation

The current best solution choice may change solutions choices made earlier.

1. DP Features

Page 4: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Series defined by fib0 = 0 fib1 = 1 fibn = fibn-1 + fibn-2

Recursive algorithm:

Running Time? O(2n)

2. Fibonacci Series

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

public static int fib(int n) { if (n < 2) return n; else return fib(n-1) + fib(n-2);}

see FibMemo.java

Page 5: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Fibonacci can be a DP problem: the solution (fib(n)) is a combination of sub-

solutions fib(n-1) and fib(n-2)) There are many repeated subproblems

2n subproblems, but only n are different

5

Page 6: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Running time is linear = O(n) Requires extra space for the fibs[] table = O(n)

Memoization + top-down fib()private static long fibs = new long[MAX+1];

// in main()fibs[0] = 0;fibs[1] = 1;

public static long fib(int n) { if (n < 2) return n; else { if (fibs[n] == 0) fibs[n] = fib(n−1) + fib(n−2) return fibs[n]; }}

::

fibs[]0123

MAX

010

0

0

::

Page 7: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 7

10th Fib: 55Number of fib() calls: 19

10th Fib: 55Number of fib() calls: 1

11th Fib: 89Number of fib() calls: 3

11th Fib: 89Number of fib() calls: 1

Speed-up

Page 8: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Bottom-up Fibint fib(int n){

if (n == 0) return 0; else { // deal with 1, 1, 2,... int prev = 0; int curr = 1; int temp; for (int i=1; i < n; i++) { temp = prev + curr; prev = curr; curr = temp; } return curr; }}

Running time = O(n)

Space requirement is 5 variables = O(1) !

temp prev curr

F(k-1)F(k-2)F(k-1)

temp prev curr

F(k-1)F(k) F(k)

+

Page 9: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

9

2. Binomial Coefficients

How many ways are there to choose k things out of n?

Used to calculate the coefficients of of (a+b)n

!

( )! !

n n

k n k k

1( )0 1

n n n n k k nn n n na b a a b a b b

k n

Page 10: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

10

It’s difficult to calculate the value of binomial coefficients using that equation:

arithmetic overflow will happen for n > 12! Use Pascal’s triangle instead:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

2 5 10 10 5 1

1 6 15 20 15 6 1

1 1

1

n n n

k k k

n == 0

n == 1

start counting k = 0 from left

::

a table of subproblems thatare used repeatedly. A DP problem

Page 11: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

11

Binomial Coefficientsprivate static final int MAX = 100;

// return n choose mstatic long binCoef(int n, int m) {long[][] bc = new long[MAX][MAX}; //table of binomial coefsfor(int i = 0; i <= n; i++) bc[i][0] = 1; // init upper edgesfor(int j = 0; j <= n; j++) bc[j][j] = 1;for(int i = 1; i <= n; i++) // fill inside triangle

for(int j = 1; j < i; j++)bc[i][j] = bc[i-1][j-1] + bc[i-1][j];

return bc[n][m];}

see Binomial.java

Page 12: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

11

item 0

26

item 1

518

item 2

622

item 3

728

item 4

11

maximize cost, buttotal weight ≤ 11

3. 0-1 Knapsack Problem

Crucial idea: total weight must be one of 12 values: 0-11

indivisible; use ornot use a weight

Page 13: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

11

item 0

26

item 1

518

item 2

Maximize costwith at most 3 items:

0 1 2 3 4 5 6 7 8 9 10 11w =

c = 0 1 6 7 7 18 19 24 25 25 25 25

Try to add item 3: 622

item 3

0 1 2 3 4 5 6 7 8 9 10 11w =

c = 0 1 6 7 7 18 22 24 28 29 29 40

Page 14: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms 14

Items 0, 1, ... n have weights w0, w1, ...wn and costs c0, c1, ...cn. All values are positive integers.

W is the maximum capcity of the knapsack.

Define m[i,w] to be the maximum total cost with weight ≤ w using items 0 to i.

Mathematically

Page 15: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 15

m[0,w] = c0 if w0 ≤ w m[i,w] = m[i-1,w], if wi > w

 the i item weighs more than the current weight limit

m[i,w] = max( m[i-1,w], m[i-1, w-wi]+ci ), if wi ≤ w

The solution is m[n,W]. To do this efficiently we must use a table to store previous computations.

Define m[i,w] recursivelythe maximum total cost with weight ≤ w using items 0 to i

Page 16: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Why use Dynamic Prog.? The current selection may change an earlier

selection:

11

item 0

26

item 1

518

item 2

c == 25; w == 8

earlier selection

518

item 2

622

item 3

c == 40; w == 11

current selection

Page 17: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms 17

public static void main(String[] args) { int W = 11; // knapsack capacity System.out.println("Knapsack capacity: " + W);

// costs and weights for items int[] ci = { 1, 6, 18, 22, 28}; int[] wi = { 1, 2, 5, 6, 7}; int numItems = ci.length; for (int i=0; i < numItems; i++) System.out.println("Item " + i + ": weight = " + wi[i] + ", cost = " + ci[i]); System.out.println();

// totCosts[i, w] stores the maximum total cost // of some items in {0,1,...,i} of combined weight <= w int[][] totCosts = new int[numItems][W + 1];

// used[i, weight] is true when item i is part of the solution for weight boolean[][] used = new boolean[numItems][W + 1]; // all false by default :

Codesee Knapsack0l.java

m[][] has become totCosts[][]

Page 18: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 18

// compute maximum cost for first item for (int w = 0; w <= W; w++) { if (wi[0] <= w) { totCosts[0][w] = wi[0]; used[0][w] = true; // means that item 0 can be used when weight is w } else totCosts[0][w] = 0; } // compute maximum cost for rest of items for (int i = 1; i < numItems; i++) { for (int w = 0; w <= W; w++) { // w == current weight limit if (wi[i] <= w) { // item within current weight limit int costWith_i = ci[i] + totCosts[i-1][w-wi[i]]; if (costWith_i > totCosts[i-1][w]) { // higher cost is better totCosts[i][w] = costWith_i; used[i][w] = true; } else // leave cost unchanged totCosts[i][w] = totCosts[i-1][w]; } else // item exceeds current weight limit; don't use totCosts[i][w] = totCosts[i-1][w]; } } printTables(totCosts, used); itemsUsed(used, ci, wi); } // end of main()

Page 19: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 19

private static void itemsUsed(boolean[][] used, int[] ci, int[] wi) { System.out.println("Items used:"); int wCapacity = used[0].length-1; // start at maximum weight (W) int usedWeight = 0; int usedCost = 0;

// check if i is part of the set of items weighing wCapacity, // if yes, print i info, and reduce wCapacity by item i's weight // and find the next item for this new capacity for (int i = used.length-1; i >= 0; i--) { if (used[i][wCapacity]) { System.out.println("Item " + i + ": weight = " + wi[i] + ", cost = " + ci[i]); usedWeight += wi[i]; usedCost += ci[i]; wCapacity = wCapacity - wi[i]; } } System.out.println("Total weight: " + usedWeight + "; Total cost: " + usedCost); } // end of itemsUsed()

Page 20: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms 20

Execution

Page 21: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms 21

Items used: Weight 0 1 2 3 4 5 6 7 8 9 10 11 Item 0: X X X X X X X X X X X Item 1: X X X X X X X X X X Item 2: X X X X X X X Item 3: X X X X X Item 4: X X X X

Using used[][]

W = 11Item 3 used; weight == 6

W = 11 – 6 = 5Item 2 used; weight == 5

W = 5 – 5 = 0No item used; stop

Page 22: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

4. Longest Common Subsequence (LCS)

• Given two sequences x[1 . . m] and y[1 . . n], find a longest subsequence common to them both.

x: A B C B D A B

y: B D C A B A

BCBA = LCS(x, y)

orBDAB, BCAB

Page 23: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Check every subsequence of x[1 . . m] to see if it is also a subsequence of y[1 . . n].

Analysis• Checking time for each subsequence is O(n).• 2m subsequences of x[] (can use or not use each

element in x). Worst-case running time = O(n*2m), exponential time.

Brute-force LCS Algorithm

Page 24: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Simplify the problem:1. Find the length of a LCS

2. We'll extend the algorithm later to find the LCS.

Towards a Better LCS Algorithm

Page 25: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

If X = < A, B, C, B, D, A, B > then

A prefix is x[1 .. 4] == < A, B, C, B > we abbreviate this as x4

Also x0 is the empty sequence

Prefixes

Page 26: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

c[] is a table (2D array) for storing LCS lengths:c[i, j] = | LCS(x[1. . i], y[1. . j]) |

| s | is the length of a sequence s

Since x is of length m, and y is of length n, then c[m, n] = | LCS(x, y) |

Creating a Table of Lengths

Page 27: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Since X0 and Y0 are empty strings, their LCS is always empty (i.e.

c[0, 0] == 0)

The LCS of an empty string and any other string is empty, so for

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

Calculating LCS Lengths

Page 28: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Initial c[] 0 1 2 3 4 5

0

1

2

3

4

0

0

00000

0

0

0

Page 29: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

The first line of this definition fills the top row and first column of c[] with 0's.

Recursive Definition of c[]

Page 30: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

When we calculate c[i, j], there are 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 the length of LCS

of smaller strings Xi-1 and Yi-1 , plus 1

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

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

jicjic

jyixjicjic

Page 31: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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 the biggest from

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

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

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

jicjic

jyixjicjic

Page 32: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Does the LCS() algorithm have many repeating (overlapping) subproblems?

Consider the worst case execution x[i] ≠ y[ j], in which case the algorithm evaluates two

subproblems, each with only one parameter decremented

Repeated Subproblems?

Page 33: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Height = m + n. The total work is exponential, butwe’re repeating lots of subproblems.

Recursion Tree (in worst cases)

Page 34: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

LCS(x, y, i, j) if c[i, j] is empty then // calculate if not already in c[i, j]

if i == 0 or j == 0 then c[i, j] ← 0 else if x[i] == y[ j]

then c[i, j] ← LCS(x, y, i–1, j–1) + 1else c[i, j] ← max( LCS(x, y, i–1, j),

LCS(x, y, i, j–1) ) return c[i, j]

Memoization

Time = O(m*n) == constant work per table entrySpace = O(m*n)

Page 35: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

This algorithm works top-down start with large subsequences, and calculate the smaller

subsequences

Let's switch to bottom-up execution calculate the small subsequences first, then move to larger

ones

Bottom-up Execution

Page 36: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

LCS Length Bottom-up

the same recursivedefinition of c[] asbefore

Page 37: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

We’ll see how a bottom-up LCS works on: X = ABCB Y = BDCAB

Bottom-up Examples

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

LCS-length(X, Y) = 3

Page 38: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

LCS Example 1j 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 39: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

ABCBBDCAB

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

j 0 1 2 3 4 5

Page 40: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

0

ABCBBDCAB

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] )

j 0 1 2 3 4 5

Page 41: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

0 0 0

ABCBBDCAB

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] )

j 0 1 2 3 4 5

Page 42: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

0 0 0 1

ABCBBDCAB

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] )

j 0 1 2 3 4 5

Page 43: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

000 1 1

ABCBBDCAB

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] )

j 0 1 2 3 4 5

Page 44: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

0

1

2

3

4

i

Xi

A

B

C

B

Yj BB ACD

0

0

00000

0

0

0

0 0 10 1

1

ABCBBDCAB

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] )

j 0 1 2 3 4 5

Page 45: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 46: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 47: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 48: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 49: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 50: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 51: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 52: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

ABCBBDCABj 0 1 2 3 4 5

Page 53: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

The bottom-up 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

Running Time

Page 54: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 54

private static void lcs(String x, String y) { int[][] lengths = new int[x.length()+1][y.length()+1]; // filled with 0's by default for (int i = 1; i <= x.length(); i++) { for (int j = 1; j <= y.length(); j++) { if (x.charAt(i-1) == y.charAt(j-1)) // Java string index starts at 0, not 1 lengths[i][j] = lengths[i-1][j-1]+1; else lengths[i][j] = Math.max(lengths[i][j-1], lengths[i-1][j]); } } System.out.println("LCS length: " + lengths[x.length()][y.length()]);

System.out.println("LCS: " + getSubSeq(x, y, lengths)); } // end of lcs()

Code see LCS.java

c[][] has become lengths[][]

Page 55: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

We have found the length of LCS. We want to modify this algorithm to have it calculate

LCS 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 trace back how it was calculated:

Finding the LCS

2

2 3

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

Page 56: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

So we can start from c[m,n] (bottom right of c[]) and move backwards

Whenever c[i,j] = c[i-1, j-1]+1, record x[i] as part of the LCS

When i=0 or j=0, we have reached the beginning, so can stop.

Remember that:

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

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

jicjic

jyixjicjic

Page 57: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Finding LCS Example 1j 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 58: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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

Page 59: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

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:

Page 60: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

There may be several paths through the table, which represent different answers for LCS()

LCS() = BDAB

All of them have the LCS-length of 4

LCS() Offers Choices

0 0 0 0 0 0 0 0

0 0 1 1 1 1 1 1

0 0 1 1 1 2 2D 2

0 0 1 2 2 2 2C 2

0 1 1 2 2 2 3A 3

0 1 2 2 3 3 3B 4

0 1 2 2 3 3

AA B C B D B

B

A 4 4

0

4

0

1

2

3

1

2

3

4

Page 61: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 61

private static String getSubSeq(String x, String y, int[][] lengths) { StringBuffer sb = new StringBuffer(); int i = x.length(); // start at bottom right int j = y.length(); while ((i != 0) && (j != 0)) { if (x.charAt(i-1) == y.charAt(j-1)) { // Java string index starts at 0, not 1 sb.append(x.charAt(i-1)); i--; j--; } else if (lengths[i][j-1] >= lengths[i][j]) j--; else i--; } return sb.reverse().toString(); } // end of getSubSeq()

Code see LCS.java

c[][] has become lengths[][]

Page 62: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 62

Execution

Page 63: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

5. Edit Distance (Levenshtein distance)

How to measure the difference between a pair of strings? Three natural types of edits

Substitution: “shot” to “spot” Insertion: “ago” to “agao” Deletion: “hour” to “hor” There's also "match", which isn't an edit.

Edit distance: the minimum number of edits needed to transform one string into another

assign a score of 1 to each type of change (this could be different) the edit distance is the minimum total score

63

Page 64: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Example The edit distance from

TGCATAT to ATCCGAT is 4

In short hand, the edit path is "S S M S S M M"

there are usually other possible edit paths of the same distance

64

TGCATAT

substitute T -> A

substitute G -> T

substitute A -> C

substitute T -> G

match C's

match A's

match T's

AGCATAT

ATCATAT

ATCATAT

ATCCTAT

ATCCGAT

ATCCGAT

ATCCGAT

Page 65: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 65

Consider the last chars of strings S and T:

If the characters are the same (they match): recursively move onto finding the edit distance between the

two strings left when this last char is deleted from both S and T

Otherwise, make one of three changes: delete the last char of S insert the last char of T substitute the last char of S for the last char of T

What is the edit distance from S (source) to T (target)?

Page 66: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Prefixes and dist() function

66

• The prefix substring of string S is s.• The prefix substring of string T is t. • The length of s is i, the length of t is j.

• The edit distance between s and t is dist(i, j)• The edit distance for the complete strings is

dist(S.size(), T.size())

• Build a dist[][] table to store the dist(i, j) values

Page 67: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Calculating dist(i, j)

67

S T

i j

Consider character at location i in s (si) and location j in t (tj). How is tj obtained? Three cases:1.Match or substitution of si

• sub-problem: dist (i-1, j-1)2.Insertion

• sub-problem: dist (i, j-1)3.Deletion

• sub-problem: dist (i-1, j)So, dist(i, j) = min { dist(i-1, j-1) + sub(i, j), dist(i, j-1) + 1, dist(i-1, j) +1 }

int sub(char a, char b){ if (c == d) return 0; else return 1;}

Page 68: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 68

The edit distance between an empty string and another string is the length of the second string.

This corresponds to having to insert each letter for the transformation.

Empty String Case

Page 69: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Example: "keep" --> "hello" s="keep" and t="hello".

To deal with empty strings, an extra row and column have been added to the chart below:

An entry in this table simply holds the dist() value between the prefixes of the two strings.

For example, the highlighted square indicates that the edit distance between the strings "he" and "keep" is 3.

h e l l o0 1 2 3 4 5

k 1 1 2 3 4 5e 2 2 1 2 3 4e 3 3 2 2 3 4p 4 4 3 3 3 4

Page 70: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Building the Table1) Initialize values corresponding to the empty

string base case.

h e l l o0 1 2 3 4 5

k 1e 2e 3p 4

Page 71: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

2) Loop through the table from the top left to the bottom right. In doing so, follow the recursive definition.

If the characters match (e.g. "e" and "e"):

h e l l o0 1 2 3 4 5

k 1 1 2 3 4 5e 2 2e 3p 4

Copy down the upper left value.

Page 72: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 72

If the characters do not match e.g. "l" and "e":

h e l l o0 1 2 3 4 5

k 1 1 2 3 4 5e 2 2 1e 3p 4

copy min ( 1+ above, 1+ left, 1+diag up)

Page 73: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 73

public static int levDistance(String s, String t) // distance from s to t (source to target) { int m = s.length(); int n = t.length(); dist = new int[m+1][n+1]; // s is down row; t is across col for (int i = 0; i <= m; i++) dist[i][0] = i; for (int j = 0; j <= n; j++) dist[0][j] = j;

// iterate though, and check last char for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { int sub = ((s.charAt(i-1) == t.charAt(j-1)) ? 0 : 1); // matching chars? dist[i][j] = min3( dist[i-1][j] + 1, // delete dist[i][j-1] + 1, // insert dist[i-1][j-1] + sub ); // match or sub } } return dist[m][n]; } // end of levDistance()

Code

Page 74: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 74

private static void printDists(String s, String t) { System.out.print("\n "); for (int j = 0; j < t.length(); j++) System.out.print(" " + t.charAt(j) + " "); System.out.println();

for (int i = 0; i < dist.length; i++) { if (i == 0) System.out.print(" : "); else System.out.printf(" " + s.charAt(i-1) + " : "); for (int j = 0; j < dist[0].length; j++) System.out.printf("%2d ", dist[i][j]); System.out.println(); } System.out.println(); } // end of printDists()

Printing the dist[][] Table

Page 75: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 75

private static String buildPath(String s, String t) { // walk back through matrix to figure out path StringBuilder rPath = new StringBuilder(); int i = s.length(); int j = t.length(); while ((i != 0) && (j != 0)) { if (s.charAt(i-1) == t.charAt(j-1)) { // chars match rPath.append("M "); i--; j--; } else { // find previous position if (dist[i][j] == (dist[i-1][j-1] + 1)) { // substitute rPath.append("S "); i--; j--; } else if (dist[i][j] == (dist[i-1][j] + 1)) { // delete rPath.append("D "); i--; } else if (dist[i][j] == (dist[i][j-1] + 1)) { // insert rPath.append("I "); j--; } } } while (i != 0) { // j == 0, so must move up (i.e. delete) rPath.append("D "); i--; } while (j != 0) { // i == 0, so must move left (i.e. insert) rPath.append("I "); j--; } return rPath.reverse().toString(); } // end of buildPath()

Building the

Edit Path

Page 76: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 76

Execution

Page 77: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Example

77

S: “thou shalt not” T: “you should not”

D S M M M M M I S M S M M M M

Page 78: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 78

More Examples

Page 79: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

DP Compared to Greedy

DP features Again Optimal sub-structure:

the best solution to the problem uses the best solutions to sub-problems

use recursive code

Overlapping (repeating) problems: the same subproblems appear several times while solving

the problem use tabling / memoziation to 'remember' answer

The current best solution choice may change solutions choices made earlier.

Page 80: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Greedy Features Optimal sub-structure: (same as DP)

the best solution to the problem uses the best solutions to sub-problems

use recursive code

The current best solution choice is made using only current ('local') information

greedy algorithms never change choices made earlier in the calculation

makes "greedy" code easier to implement than DP

Page 81: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Previous Examples: Minimum Spanning Tree Algorithms – Kruskal’s and Prim’s Dijkstra’s Algorithm

Page 82: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Fractional Knapsack Problem

Maximize the value of a knapsack that can hold at most W units worth of goods from a list of items I1, I2, ... In.

Each item i has two attributes:1) Value/unit == vi

2) Weight == wi

Page 83: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

83

1)Sort the items into a list by value/unit.

2)Take as much of the most expensive item as possible, then move down the list.

3)You may end up taking a fractional portion of the last item.

Fractional Knapsack Algorithm

Contest Algorithms

Page 84: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

11

item 0

26

item 1

518

item 2

622

item 3

728

item 4

11

maximize cost, buttotal weight ≤ 11

Fractional Knapsack Problem

uses a greedyalgorithm

Crucial idea: order by cost per unitweight

divisible; can use parts of a weight

Page 85: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

11

item 0

26

item 1

518

item 2

622

item 3

728

item 4

11

item 0

26

item 1

518

item 2

622

item 3

728

item 4

cost/unitweight:

4

reorder by decreeasingcost/unit weight:

3.666 3.6 3 1

Page 86: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Maximize cost by adding weights (or parts) in decreasing cost/ unit weight:

Max weight == 11 :

622

item 3

728

item 4

Max cost == 7 * 4 4 * 3.666

7 + 4

+

= 28 + 14.666

= 42.666

Page 87: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms 87

No. of items, knapsack W Lines of item info; on each line:

ci, wi for an item

e.g. 5 11

1 1 6 2 18 5 22 6 28 7

Input Data Format

see fkData.txt

This is the examplefrom the previousslides.

Page 88: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms 88

public static void main(String[] args) throws Exception { if (args.length != 1) { System.out.println("Usage: java FracKnapsack <data-file>"); return; }

Scanner sc = new Scanner(new File(args[0])); int numItems = sc.nextInt(); int maxW = sc.nextInt();

LinkedList<KItem> items = new LinkedList<KItem>(); for (int i = 0; i < numItems; i++) items.add( new KItem(sc.nextInt(), sc.nextInt()) ); Collections.sort(items); :

Code

Page 89: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 89

int currWeight = 0; double currCost = 0; while ((currWeight < maxW) && (!items.isEmpty())) { int remWeight = maxW - currWeight; KItem item = items.poll(); if (item.weight <= remWeight) { // add all of the item currWeight += item.weight; currCost += item.cost; } else { // item.weight > remWeight // add a fraction of the item currCost += remWeight * item.costWeightRatio; currWeight += remWeight; } } System.out.printf("%.3f", currCost); } // end of main()

Page 90: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Contest Algorithms: 11. DP 90

public class KItem implements Comparable<KItem>{ public int cost, weight; public double costWeightRatio;

public KItem(int cost, int weight) { this.cost = cost; this.weight = weight; costWeightRatio = ((double) cost) / weight; }

public int compareTo(KItem i) { double diff = costWeightRatio - i.costWeightRatio; if (diff > 0) return -1; else if (diff == 0) return 0; else return 1; } // end of compareTo()

public String toString() { return "(cost: " + cost + ", weight: " + weight + ")"; }} // end of KItem class

Page 91: Contest Algorithms January 2016 Introduce DP; Look at several examples; Compare DP to Greedy 11. Dynamic Programming (DP) 1Contest Algorithms: 11. DP

Why is it Greedy? The current selection does not affect the earlier

selection:

earlier selection

c == 42.666; w == 11

current selection

728

item 4

7 * 4

c = 28; w == 7

622

item 3

728

item 4

7 * 4 4 * 3.666+