Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In...

95
Chapter 5:- DYNAMIC PROGRAMMING Compiled By:- Sanjay Patel Assistant Professor, SVBIT.

Transcript of Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In...

Page 1: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Chapter 5:-

DYNAMIC PROGRAMMING

Compiled By:- Sanjay Patel

Assistant Professor,

SVBIT.

Page 2: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Outline

Introduction

The Principle of Optimality,

Problem Solving using Dynamic Programming –

Calculating the Binomial Coefficient

Making Change Problem

Assembly Line-Scheduling

Knapsack problem

Shortest path

Matrix chain multiplication,

Longest Common Subsequence..

Sanjay Patel

Page 3: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Dynamic programming is technique for solving

problems with overlapping sub problems.

In this method each sub problem is solved only once.

The result of each sub problem is recorded in a

table from which we can obtain a solution to the

original problem.

Introduction

Sanjay Patel

Page 4: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Divide and Conquer Dynamic programming

The problem is divide into small sub problems.

These sub problem are solved independently.

Finally all the solution of sub problem are

collected together to get the solution to the

given problem

In dynamic programming many decision

sequences are generated and all the

overlapping sub instances are considered.

In this method duplication in sub solution are

neglected. i.e. duplicate sub solution may be

obtained.

In dynamic computing duplication in solutions is

avoided totally.

less efficient because of rework on solution efficient than divide and conquer strategy.

uses top down approach of problem solving-

Recursive method

uses bottom up approach of problem solving-

Iterative method.

splits its input at specific deterministic points

usually in the middle.

Splits its input at every possible splits points

rather than at a particular point. After trying all

split points it deterministic which split point is

optimal.

Difference between divide and conquer

Sanjay Patel

Page 5: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Greedy method Dynamic Programming

Is used for obtaining optimum

solution

Is also for obtaining optimum

solution

In greedy a method a set of

feasible solutions and the picks up

the optimum solution

There is no special set of feasible

solutions in this method

The optimum selection is without

revising previously generated

solution

It consider all possible sequence in

order to obtain the optimum

solution

There is no such guarantee of

getting optimum solution

It is guaranteed that the dynamic

programming will generate

optimal solution using principal of

optimality.

Greedy Algo vs. Dynamic Algo

Sanjay Patel

Page 6: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

The dynamic programming algorithm obtains the

solution using principle of optimality

The principle of optimality states that “ in an

optimal sequence of decision or choices, each

subsequence must also be optimal.

When it is not possible to apply the principle of

optimality it is almost impossible to obtain the

solution using the dynamic programming approach.

Principle of optimality

Sanjay Patel

Page 7: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Suppose, the following coins are available.

1 Dollar = 100 cents

1 Quarter = 25 cents

1 Dimes = 10 cents

1 Nickels = 5 cents

1 pennies = 1 cents

For example customer wants to pay 3.37 $.

3.37$ = 3 dollar + 1 quarter + 1 dime + 2 pennies

Making change problem

Sanjay Patel

Page 8: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Another example, if there exits coins of 1, 4 , 6 units.

We have to make change for 9 units.

There are two ways.

1. Greedy method:- select one coin of 6 unit and 3

coin of 1 unit = 4 coins.

2. Better method:- select two coins of 4 units and 1

coin of 1 unit = 3 coins.

This better method is devised by dynamic programming

approach.

Cont’d..

Sanjay Patel

Page 9: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

There are 3 exits coins of 1 ,4 and 6 unit. We have

to make change for 9 units. Solve this problem with

dynamic programming.

Solution:- For solving this problem using dynamic

programming approach, we need to build up table.

Here, d1=1 , d2=4 , d3=6 , n=3, N=9 units.

Hence we will create a table with rows ranging from

1 to 3 and columns ranging from 0 to 9.

Example of making change

Sanjay Patel

Page 10: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

1. If i=1 then c[ i , J ] = 1+ c [ 1, J- d1]

2. If J < di then c[ i , J ] = c [ i-1, J]

3. Otherwise, C[i] [J] = min ( c[i-1,J], 1+ c[i, J- di])

Initially c[i,0] = 0

So, c[1,0] = 0

c[2,0] = 0

c[3,0] = 0

Condition for equation

Sanjay Patel

Page 11: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

J

i=1

i=2

i=3

Table

Sanjay Patel

0 1 2 3 4 5 6 7 8 9

d1= 1 0

d1= 4 0

d1= 6 0

Page 12: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

C[1,1] with i=1 , J=1 , and d1=1

formula 1 :-If i=1 then c[ i , J ] = 1+ c [ 1, J- d1]

C[1,1] = 1+ c [ 1, 1- 1]

= 1 + c [ 1, 0]

= 1 + 0

= 1

Cont’d..

Sanjay Patel

Page 13: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

C[2,1] with i=2 , J=1 and d2=4

As J < d2 i.e 1< 4

formula 2:- If J < di then c[ i , J ] = c [ i-1, J]

C[2,1] = c [ i-1, J]

= c [ 2-1 , 1]

= c [1,1]

= 1

Cont’d..

Sanjay Patel

Page 14: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

C[3,1] = 1 , C[1,2] = 2 , C[2,2] = 2 , C[3,2] = 2

C[1,3] = 3 , C[2,3] = 3 , C[3,3] = 3 , C[1,4] = 4.

For C[2,4] with i=2 , J=4 and d2=4

As i≠1 and J>d2

Formula 3 :-min ( c[i-1,J], 1+ c[i, J- di])

C[2,4]= min ( c[2-1,4] , 1+ c[2,4-4] )

= min ( c [1,4] , 1 + c [2,0]

= min ( 4 , 1+0 )

= 1

Cont’d..

Sanjay Patel

Page 15: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

C[3,4] = 1 , C[1,5] = 5 , C[2,5] = 2 , C[3,5] = 2

C[1,6] = 6 , C[2,6] = 3 , C[3,6] = 1 , C[1,7] = 7

C[2,7] = 4 , C[3,7] = 2 , C[1,8] = 8 , C[2,8] = 2

C[3,8] = 2 , C[1,9] = 9, C[2,9] = 3 , C[3,9] = 3

Cont’d..

Sanjay Patel

Page 16: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

J

i=1

i=2

i=3

The value c[n,N] i.e c[3,9] = 3 represents the minimum

number coins required to get the sum for 9 units. Hence we

require 3 coins for getting 9 units.

4 units + 4 units + 1 units = 9 units

1 coin + 1 coin + 1 coin = 3 coins

Table

Sanjay Patel

0 1 2 3 4 5 6 7 8 9

d1= 1 0 1 2 3 4 5 6 7 8 9

d1= 4 0 1 2 3 1 2 3 4 2 3

d1= 6 0 1 2 3 1 2 1 2 2 3

Page 17: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

For( i 1 to n ) do

c[ i, 0] 0

For ( i 1 to n ) do // initially first column by 0

{ for ( J 1 to N) do // with this loop table are computed

{ if (i=1 && J< d[i]) then

c[i][J] = infinity;

else if (i=1) then

c[i][J] = 1+ c [ 1, J- d1];

else if (j<d[i]) then

c[i][J] = c [ i-1, J];

else

c[i][J] = min ( c[i-1,J], 1+ c[i, J- di]) } }

return c [ n , N ] // total number of coins in the change

Algorithm

Sanjay Patel

Page 18: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

As the basic operation in above algorithm is to fill

up the table, which is performed within nested for

loops.

Hence, the time complexity of this algorithm is

(nN).

Analysis ( complexity )

Sanjay Patel

Page 19: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Dynamic programming is a technique for solving

problems with overlapping sub problems.

These sub problem are typically based on recurrence

relation.

To solve knapsack problem using dynamic approach ,

we will write the recurrence relation.

Knapsack problem

Sanjay Patel

Page 20: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Table[i,J] = maximum { table[i-1,J], Vi + table[i-1,J-wi] }

if J >= wi

OR

= table [i-1,J] if J < wi

That means a table is constructed using above given formula.

Initially table [0,J]=0 as well as table[i,0]=0 when J>=0

i>=0

Formula

Sanjay Patel

Page 21: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

For the given instance of problem obtain the optimal

solution for the knapsack problem using dynamic

programming.

The capacity of knapsack is W=5

Example

Sanjay Patel

Item Weight Value

1 2 3

2 3 4

3 4 5

4 5 6

Page 22: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Initially table[0,J]=0 and table[i,0]=0. there are 0

to n rows and 0 to W columns in the table.

0 1 2 3 4 5

0

1

2

3

4

Cont’d..

Sanjay Patel

0 0 0 0 0 0

0

0

0

0

Page 23: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Table[1,1] with i=1 , J=1 , Wi=2 and Vi=3

As J < Wi

table[1,1] = table [i-1,J]

= table [1-1,1]

= table[0,1]

= 0

Cont’d..

Sanjay Patel

Page 24: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Table[1,2] with i=1 , J=2 , Wi=2 and Vi=3

As J>=Wi

table[1,2] = maximum { table[i-1,J], Vi + table[i-1,J-wi] }

= maximum { table[1-1,2], 3+table[1-1,2-2] }

= maximum { table[0,2], 3+0 }

= maximum { 0 , 3 }

= 3

Cont’d..

Sanjay Patel

Page 25: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

table[1,3]=3 , table[1,4]=3 , table[1,5]=3

table[2,1]=0 , table[2,2]=3 , table[2,3]=4

table[2,4]=4 , table[2,5]=7

table[3,1]=0 table[3,2]=3 , table[3,3]=4

table[3,4]=5 , table[3,5]=7

table[4,1]=0 , table[4,2]=3 , table[4,3]=4

table[4,4]=5 , table[4,5]=7

Cont’d..

Sanjay Patel

Page 26: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Finally, we can get the table given below

0 1 2 3 4 5

0

1

2

3

4

Cont’d..

Sanjay Patel

0 0 0 0 0 0

0 0 3 3 3 3

0 0 3 4 4 7

0 0 3 4 5 7

0 0 3 4 5 7

Page 27: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Let, i=n and k=W

While(i>0 and k>0)

{ if ( table[i , k]≠ table[i-1 , k] ) then

mark ith item as in kanpsack

i = i-1 and k= k - Wi // selection of ith item

else

i = i-1 // do not select ith item

}

How to find actual knapsack item?

Sanjay Patel

Page 28: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Thus we have selected item 1 and item 2 for the

knapsack.

So the solution can be represented in vector as below.

( 1 , 1 , 0 , 0 )

Solution

Sanjay Patel

Page 29: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

For( i 0 to n ) do

{

For ( J 0 to W ) do

{ table[i,0]=0

table[0,J]=0 // table initialization

}}

for ( i 0 to n) do

{

for (J 0 to W) do

{ if (J< W[i]) then

table[i,J] table [i-1,J]

else if (J>=W[i]) then

table[i,J] maximum { table[i-1,J], Vi + table[i-1,J-wi] }

}}

return table[n,W]

Algorithm

Sanjay Patel

Page 30: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

In this algorithm basic operation is if….else if

statement within two nested for loops.

So, n W

C(n)= ∑ ∑1i=0 J=0

n

= ∑ (W-0+1)

i=0

Analysis

Sanjay Patel

Page 31: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

n n

C(n)= w ∑ 1 + ∑ 1i=0 i=0

= W( n-0+1) +(n-0+1)

= Wn+W+n+1

C(n)=Wn

The time complexity of this algorithm is (Wn)

Cont’d..

Sanjay Patel

Page 32: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Solve the following 0/1 knapsack problem using dynamic

programming. There are five items whose weights and values are

given as below.

weight W[] = { 1 , 2 , 5 , 6 , 7 }

Value V[] = { 1 , 6 , 18 , 22 , 28 }

Show your equation and find out the optimal knapsack items for weight

and capacity of 11 units.

Another Example ( GTU 2010 )

Sanjay Patel

Item Weight Value

1 1 1

2 2 6

3 5 18

4 6 22

5 7 28

Page 33: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

The capacity W=11.

Initially table , table[0,J]=0 and table[i,0]=0.

There are 0 to n rows and 0 to W columns.

table[1,1] with i=1 , J=1 , Wi=1 and Vi=1 J>=Wi

table[1,1] = maximum { table[i-1,J], Vi + table[i-1,J-wi] }

= max { table[0,1] , 1 + table[0,0] }

= max{ 0,1}

= 1

Solution

Sanjay Patel

Page 34: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 0 0 0 0 0 0 0 0 0 0 0

0 1

0

0

0

0

Cont’d…

Sanjay Patel

0

1

2

3

4

5

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

Page 35: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 0 0 0 0 0 0 0 0 0 0 0

0 1 1 1 1 1 1 1 1 1 1 1

0 1 6 7 7 7 7 7 7 7 7 7

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

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

0 1 6 7 7 18 22 28 29 34 35 40

After Computing

Sanjay Patel

0

1

2

3

4

5

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

Page 36: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

After applying algorithm, we find final solution

which is ,we have selected item 3 and item 4 for

the knapsack.

The total profit = 18 + 22 =40

After applying algorithm

Sanjay Patel

Page 37: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Computing binomial co efficient is a typical example

of applying dynamic programming.

In mathematics, the binomial coefficient is denoted by

C(n , k).

The formula for computing binomial coefficient is,

C(n,k)= C(n-1, k-1) + C(n-1, k)

And C(n,0)=1

C(n,n)=1 where n > k > 0

Calculating the binomial coefficient

Sanjay Patel

Page 38: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Here n=4,k=2

C(4,2)= C(n-1, k-1) + C(n-1, k)

= C(3,1) + C(3,2)

There are two unknown C(3,1) and C(3,2).

Now, compute C(3,1) where n=3 and k=1

C(3,2)= C(n-1, k-1) + C(n-1, k)

= C(2,0) + C(2,1)

We can write C(n,0)=1

C(2,0)=1

Example (compute(4,2) )

Sanjay Patel

Page 39: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

C(3,1)= 1 + C(2,1)

Now let us compute C(2,1) where n=2, K=1

C(2,1)= C(n-1, k-1) + C(n-1, k)

= C(1,0) + C(1,1)

But as, C(n,0)=1 and C(n,n)=1

Then we can get, C(1,0)=1 and C(1,1)=1

C(2,1)= 1 + 1 =2 so we will get rest of the value,

C(3,1)=3 , C(3,2)=3 , C(4,2)=6

C(4,2)= 6 is the final Answer.

Cont’d..

Sanjay Patel

Page 40: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

While computing C(n,k) the smaller overlapping

sequence get generated by C(n-1, k-1) and C(n-1 ,k).

These overlapping smaller instance of problem need

to be solved first.

If we record binomial coefficients n and k values

ranging from 0 to n and 0 to k then.

How dynamic programming approach is used?

Sanjay Patel

Page 41: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1 2 3 4 …… ……. k-1 k

0 1

1 1 1

2 1 2 1

3 1 3 3 1

4 1 4 6 4 1

……

…..

(n-1)

n

Cont’d..

Sanjay Patel

Page 42: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1

1 1 1

2 1 2 1

3 1 3 3 1

4 1 4 6 4 1

5 1 5 10 10 5 1

6 1 6 15 20 15 6 1

Pascal’s triangle ( compute(6,3) )

Sanjay Patel

C(6,3) = 20

Page 43: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

for(i 0 to n) do

{ for (J 0 to k) do

{

if ((J=0 or (i=J)) then

C[i,J] 1

else

C[i,J] C[i-1,J-1] + C[i-1,J]

}

return C[n , k]

The time complexity of binomial coefficient is (nk)

Algorithm & complexity

Sanjay Patel

Page 44: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

The all pair shortest path is the problem of

determining shortest path distances between every

pair of vertices in a given graph.

The use of this algorithm is in routing problems.

We will discuss floyd-warshall’s algorithm.

Shortest path

Sanjay Patel

Page 45: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Floyd-warshall’s algorithm is for finding the shortest

path between every pair of vertices of a graph.

This algorithm is invented by R.Floyd and hence is

the name.

Floyd computes the distance matrix of a weighted

graph with n vertices through a series of n by n

matrices.

Floyd-warshall’s algorithm.

Sanjay Patel

Page 46: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Floyd-Warshall’s Algorithm

Sanjay Patel

The weight matrix and the graph

1 2 3 4 5

1 0 1 1 5

2 9 0 3 2

3 0 4

4 2 0 3

5 3 0

v1 v2

v3v4

v5

32

2

4

1

3

1

93

5

Page 47: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

for(k 1 to n) do

{

for(i 1 to n) do

{

for(J 1 to n) do

{

D[i,J] min { D[i,J] , (D[i,k] + D[k,J]) }

}}}

return D // computed D(n) is returned

Algorithm

Sanjay Patel

Page 48: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

In above algorithm the basic operation is

D[i,J] min { D[i,J] , (D[i,k] + D[k,J])

This operation is with in three nested for loops.

So, the time complexity of finding all pair shortest

path is ( n3 )

Analysis

Sanjay Patel

Page 49: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Example..

Sanjay Patel

W = D0 =

40 5

2 0

-3 0

1 2 3

1

2

3

1

2

3

5

-3

24

Page 50: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Cont’d

Sanjay Patel

D1 =40 5

2 0 7

-3 0

1 2 3

1

2

3

1

2

3

5

-3

24k = 1

Vertex 1 can be

intermediate node

D[i,J] = min { D[i,J] , (D[i,k] + D[k,J]) }

D1[2,3] = min( D0[2,3], D0[2,1]+D0[1,3] )

= min ( , 7)

= 7

40 5

2 0

-3 0

1 2 3

1

2

3

D0 =

Page 51: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Cont’d..

Sanjay Patel

D[i, J] = min { D[i,J] , (D[i,k] + D[k,J]) }

D1[3,2] = min( D0[3,2], D0[3,1]+D0[1,2] )

= min (-3, )

= -3

40 5

2 0 7

-3 0

1 2 3

1

2

3

D1 =

Page 52: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Cont’d

Sanjay Patel

D2 =

40 5

2 0 7

-1 -3 0

1 2 3

1

2

3

D[i, J] = min { D[i,J] , (D[i,k] + D[k,J]) }

D2[1,3] = min( D1[1,3], D1[1,2]+D1[2,3] )

= min (5, 4+7)

= 5

1

2

3

5

-3

24 D1 =

40 5

2 0 7

-3 0

1 2 3

1

2

3

k = 2

Vertices 1, 2 can

be intermediate

Page 53: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D[i, J] = min { D[i,J] , (D[i,k] + D[k,J]) }

D2[3,1] = min( D1[3,1], D1[3,2]+D1[2,1] )

= min ( , -3+2)

= -1

Cont’d..

Sanjay Patel

40 5

2 0 7

-1 -3 0

1 2 3

1

2

3

D2 =

Page 54: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Cont’d..

Sanjay Patel

D3 =

20 5

2 0 7

-1 -3 0

1 2 3

1

2

3

D[i, J] = min { D[i,J] , (D[i,k] + D[k,J]) }

D3[1,2] = min(D2[1,2], D2[1,3]+D2[3,2] )

= min (4, 5+(-3))

= 2

D2 =

40 5

2 0 7

-1 -3 0

1 2 3

1

2

3

1

2

3

5

-3

24k = 3

Vertices 1, 2, 3

can be

intermediate

Page 55: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D[i, J] = min { D[i,J] , (D[i,k] + D[k,J]) }

D3[2,1] = min(D2[2,1], D2[2,3]+D2[3,1] )

= min (2, 7+ (-1))

= 2

Cont’d..

Sanjay Patel

20 5

2 0 7

-1 -3 0

1 2 3

1

2

3

D3 =

Page 56: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Another Example

Sanjay Patel

Page 57: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D0

Sanjay Patel

Page 58: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D1

Sanjay Patel

Page 59: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D2

Sanjay Patel

Page 60: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D3

Sanjay Patel

Page 61: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D4

Sanjay Patel

Page 62: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

D5

Sanjay Patel

Page 63: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

efficiently multiply a chain of N matrices, i.e. calculate in the

fastest way

Example: consider the chain A1, A2, A3, A4 of 4 matrices

Let us compute the product A1A2A3A4

There are 5 possible ways:

1. (A1(A2(A3A4)))

2. (A1((A2A3)A4))

3. ((A1A2)(A3A4))

4. ((A1(A2A3))A4)

5. (((A1A2)A3)A4)

Matrix Chain Multiplication

Sanjay Patel

Page 64: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

For each i, which within range of 1 to n and for each J

which within a range of 1 to n the optimal cost m[i,J] is

computing using following formula.

m[i,i]=0 where 1<=i<=n for all i

m[i, J] = min {m[i, k] + m[k+1, J ] + pi-1* pk * pJ }

for i ≤ k < J

Recursive solution

Sanjay Patel

Page 65: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

We will construct table of m[i,J] using formula

1. m[i,i]=0 for i=1 to n

2. For length 2 to n compute

m[i, J] = min { m[i, k] + m[k+1, J ] + Pi-1* Pk * PJ }

for i ≤ k < J

Computing optimal cost

Sanjay Patel

Page 66: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Consider,

We have to compute matrix in chain order,

Here, P0=5 , P1=4 , P2=6 , P3=2 , P4=7

Example

Sanjay Patel

Matrix Dimension

A1 5 * 4

A2 4 * 6

A3 6 * 2

A4 2 * 7

Page 67: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Step-1 . For all i 1 <=i <=n

so, m[i,i]=0

Then , m[1,1]=0 , m[2,2]=0 , m[3,3]=0 , m[4,4]=0

Solution

Sanjay Patel

Page 68: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Sanjay Patel

Page 69: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Let i=1 , J=2 , k=1

Calculate m[1,2]

Sanjay Patel

Page 70: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Sanjay Patel

Page 71: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Sanjay Patel

Page 72: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Sanjay Patel

Page 73: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Sanjay Patel

Page 74: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Sanjay Patel

Page 75: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

m[1,4] determines the optimal cost =158.

Build up s table using s[i, J]=k

Sanjay Patel

Page 76: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

for (i 1 to n) do

m[i, i] 0

for (length 2 to n) do // is the chain length.

{ for (i 2 to (n − length + 1) ) do

{ J i + length − 1

m[i, J] Infinity

for (k i to J − 1) do

{ q m[i, k] + m[k + 1, j] + pi−1*pk* pJ

if (q < m[i, j]) then

{ m[i, J] q

s[i, J] k }}}}

return m[1,n] and s

Algorithm

Sanjay Patel

Page 77: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

The basic operation in this algorithm is computation

of m[i,J] and s[i,J] which is within three nested for

loops.

Hence the time complexity of the above algorithm is

(n3)

Analysis

Sanjay Patel

Page 78: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Let , B=<b1,b2….bn> and A=<a1,a2,…..an> be

string over an alphabet. Then B is a subsequence of

A if B can generated by striking out some element

from A.

For example, two string s1=abbacdcba,

s2=bcdbbcaaac

So, s1 is a subsequence of s2.

The longest common subsequence problem is the

problem of finding given two sequence A and Ba

maximum length common subsequence of A and B.

Let us apply dynamic programming steps.

Longest Common subsequence

Sanjay Patel

Page 79: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Let c[i,J] be the length of an LCS of Ai and Bi then we

get a recursive definition:

= 0 if i=0 or J=0

C[i, J] = C[i-1,J-1]+1 if i,J>0 and Ai=BJ

= max( c[i,J-1], c[i-1,J]) if i,J>0 and Ai ≠ BJ

Recursive Definition

Sanjay Patel

Page 80: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

m length (A)

n length (B)

for(i1 to m) do

{ c[i,0] 0

} for(J 0 to n) do

{ c[0,J] 0

}

// making first row and column zero.

Continue algorithm.

Computing LCS

Sanjay Patel

Page 81: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

for(i 1 to m) do

{ for(J 1 to n) do

{ if(ai ==bi) then

{ c[i,J] c[i-1,J-1]+1

d[i,J] “ “ } //Putting direction.

else if(c[i-1, J] >= c[ i , J-1]) then

{ c[i,J] c[i-1,J]

d[i,J] “ “ }

else { c[i, J] c[i , J-1]

d[i,J] “ “ }}}

return c and d.

Algorithm

S anjay Patel

Page 82: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

Let A =< X , Y , Z , Y , T , X , Y>

B =< Y , T , Z , X , Y , X>

1 2 3 4 5 6 7

A[i]

B[i]

Example

Sanjay Patel

X Y Z Y T X Y

Y T Z X Y X

Page 83: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

We will build c[i,J] and d[i, J].

Initially, c[i,0] 0 where i represent row and

c[0,J] 0 where J represent column

Solution

Sanjay Patel

0 0 0 0 0 0 0

0

0

0

0

0

0

0

Page 84: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

for(i1 to m) and

for(J1 to n)

We will filling up c[i,J] and d[i,J] horizontally,

Let i=1 , J=1

1 2 3 4 5 6 7

A[i]

B[J]

Cont’d..

Sanjay Patel

X Y Z Y T X Y

Y T Z X Y X

Page 85: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

As if( A[ i ] ≠ B [ J ] ) // first condition

else if(c[i-1, J] >= c[ i , J-1])

else if ( c[0,1] >= c [1,0] ) which is true //second cond.

0 >= 0

c[1,1] c[i-1 , J]

c[0,1]

0

And d[1,1] “ “

C[1,1] i=1, J=1

Sanjay Patel

Page 86: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1 2 3 4 5 6

0

1

2

3

4

5

6

7

Cont’d..

Sanjay Patel

0 0 0 0 0 0 0

0 0

0

0

0

0

0

0

Page 87: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

As if( A[ i ] ≠ B [ J ] ) // first condition

else if(c[i-1, J] >= c[ i , J-1])

else if ( c[0,2] >= c [1,1] ) which is true //second cond.

0 >= 0

c[1,1] c[i-1 , J]

c[0,1]

0

And d[1,1] “ “

C[1,2] i=1 , J=2

Sanjay Patel

Page 88: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1 2 3 4 5 6

0

1

2

3

4

5

6

7

Cont’d..

Sanjay Patel

0 0 0 0 0 0 0

0 0 0

0

0

0

0

0

0

Page 89: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

As if( A[ i ] ≠ B [ J ] ) // first condition

else if(c[i-1, J] >= c[ i , J-1])

else if ( c[0,3] >= c [1,2] ) which is true //second cond.

0 >= 0

c[1,1] c[i-1 , J]

c[0,1]

0

And d[1,1] “ “

C[1,3] i=1 , J=3

Sanjay Patel

Page 90: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1 2 3 4 5 6

0

1

2

3

4

5

6

7

Cont’d..

Sanjay Patel

0 0 0 0 0 0 0

0 0 0 0

0

0

0

0

0

0

Page 91: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

As if( A[ i ] = B [ J ] ) // first condition

which is true

c[i,J] c[i-1, J-1] + 1

c[1,4] c[1-1 , 4-1] + 1

c[0,3] + 1

1

And d[1,4] “ “

C[1,4] i=1 , J=4

Sanjay Patel

Page 92: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1 2 3 4 5 6

0

1

2

3

4

5

6

7

Cont’d..

Sanjay Patel

0 0 0 0 0 0 0

0 0 0 0 1

0

0

0

0

0

0

Page 93: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

0 1 2 3 4 5 6

0

1

2

3

4

5

6

7

Cont’d..

Sanjay Patel

0 0 0 0 0 0 0

0 0 0 0 1 1 1

0 1 1 1 1 2 2

0 1 1 2 2 2 2

0 1 1 2 2 3 3

0 1 2 2 2 3 3

0 1 2 2 3 3 4

0 1 2 2 3 4 4

Page 94: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

display_LCS ( d , A[] , i , J)

if ( i=0 !! J=0 ) then

return;

if( d[i,J] = “ “ ) then

{ display LCS(d , A , i-1 , J-1)

print(ai) }

else if ( d[i,J] = “ “ ) then

{ display LCS(d , A , i-1 , J) }

else {

display LCS(d , A , i , J-1) }

Constructing LCS

Sanjay Patel

Page 95: Chapter 5:- DYNAMIC PROGRAMMINGsvbitce2010.weebly.com/uploads/8/4/4/5/8445046/che_5.pdf · In dynamic programming many decision sequences are generated and all the overlapping sub

YZYX is your LCS.

Answer

Sanjay Patel