0% found this document useful (0 votes)
6 views165 pages

Lec 12 16 Dynamic Programming

The document provides an introduction to dynamic programming, focusing on inductive definitions and recursive programs for calculating factorials and Fibonacci numbers. It discusses the optimal substructure property, overlapping subproblems, and the importance of memoization to avoid redundant calculations. The content is structured with examples and code snippets to illustrate the concepts clearly.

Uploaded by

fakertoolzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views165 pages

Lec 12 16 Dynamic Programming

The document provides an introduction to dynamic programming, focusing on inductive definitions and recursive programs for calculating factorials and Fibonacci numbers. It discusses the optimal substructure property, overlapping subproblems, and the importance of memoization to avoid redundant calculations. The content is structured with examples and code snippets to illustrate the concepts clearly.

Uploaded by

fakertoolzz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 165

Dynamic Programming

Introduction

Prepared by Pawan K. Mishra


Inductive definition

Recursive program

Prepared by Pawan K. Mishra


Inductive definition
Fact(n)
Fact(0)=1
Fact(n)= n-1 * Fact(n)

sort(A)
sort([])=[]
sort(a,b,c,d)= fit( d, sort[a,b,c])

Prepared by Pawan K. Mishra


Recursive program

factorial(n):
if (n <= 0) return(1)
else
return (n*factorial(n-1))

Prepared by Pawan K. Mishra


Optimal substructure property
Solution to original problem can be derived by combining
solutions to subproblems

factorial(n-1) is a subproblem of factorial(n)


So are factorial(n-2), factorial(n-3), …,
factorial(0)

sort([b,c,d]) is a subproblem of sort([a,b,c,d])


So are sort([c,d]), sort([b,c])….
Prepared by Pawan K. Mishra
Fibonacci numbers
Inductive definition Recursive Program

fib(0) = 0
function fib(n):
if (n == 0) or (n == 1)
fib(1) = 1
value = n
fib(n) = fib(n-1) + fib(n-2) else
value = fib(n-1) + fib(n-2)

return(value)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value) fib(3) fib(2)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value) fib(3) fib(2)

fib(2) fib(1)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value) fib(3) fib(2)

fib(2) fib(1)

fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value) fib(3) fib(2)

fib(2)
fib(1)
1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value) fib(3) fib(2)
1

fib(2) fib(1)
1 0
fib(1) fib(0)
fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2)
return(value) fib(3) fib(2)
1 1

fib(2) fib(1)
1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2
return(value) fib(3) fib(2)
1 1

fib(2) fib(1)
1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2
return(value) fib(3) fib(2)
1 1

fib(2) fib(1) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2
return(value) fib(3) fib(2)
1 1 1 0

fib(2) fib(1) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1
return(value) fib(3) fib(2)
1 1 1 0

fib(2) fib(1) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1
return(value) fib(3) fib(2)
1 1 1 0

fib(2) fib(1) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0

fib(2) fib(1) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1 1 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
if n == 0 or n == 1 fib(5)
value = n 3 2
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1 1 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
5
if n == 0 or n == 1 fib(5)
value = n 3 2
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1 1 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
5
if n == 0 or n == 1 fib(5)
value = n 3 2
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1 1 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0

fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)


1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Computing fib(5)
function fib(n):
5
if n == 0 or n == 1 fib(5)
value = n 3 2
else
fib(4) fib(3)
value = fib(n-1) +
fib(n-2) 2 1 1 1
return(value) fib(3) fib(2) fib(2) fib(1)
1 1 1 0 1 0
Time complexity ?? fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


OBSERVATIONS
Overlapping 5
subproblems 3
fib(5)
2

Wasteful fib(4) fib(3)

recomputation 2 1 1 1

fib(3) fib(2) fib(2) fib(1)


Computation 1 1 1 0 1 0
tree grows fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
exponentially 1 0
fib(1) fib(0)

Prepared by Pawan K. Mishra


Never re-evaluate a subproblem
Build a table of values already computed
Memory table

Memoization

Remind yourself that this value has already been seen


before

Prepared by Pawan K. Mishra


Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
table
-Look up table Kk Fib(k)
fib(k)
before starting a
recursive
computation
-Computation
tree is linear

Prepared by Pawan K. Mishra


Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table Kk Fib(k)
fib(k)
before starting a
recursive
computation
-Computation
tree is linear

Prepared by Pawan K. Mishra


Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table Kk Fib(k)
fib(k)
before starting a fib(3) fib(2)
recursive
computation
-Computation
tree is linear

Prepared by Pawan K. Mishra


Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table Kk Fib(k)
fib(k)
before starting a fib(3) fib(2)
recursive
computation
fib(2) fib(1)
-Computation
tree is linear

Prepared by Pawan K. Mishra


Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table Kk Fib(k)
fib(k)
before starting a fib(3) fib(2)
recursive
computation
fib(2) fib(1)
-Computation
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive
computation
fib(2) fib(1)
-Computation 1
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 0 0
computation
fib(2) fib(1)
-Computation 1 0
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 1 0
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 1 0
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
2
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 3 2
0
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly
computed value in a
fib(4) fib(3)
table
2 1
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 3 2
0
tree is linear
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly 3
computed value in a
fib(4) fib(3)
table
2 1
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 3 2
0
tree is linear 4 3
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization
fib(5)
-Store each newly 3 2
computed value in a
fib(4) fib(3)
table
2 1
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 3 2
0
tree is linear 4 3
fib(1) fib(0)
Prepared by Pawan K. Mishra
Memoized fib(5)
Memoization 5
fib(5)
-Store each newly 3 2
computed value in a
fib(4) fib(3)
table
2 1
-Look up table K k Fib(k)
fib(k)
before starting a fib(3) fib(2) 1 1
recursive 1 1 0 0
computation 2 1
fib(2) fib(1)
-Computation 3 2
0
tree is linear 4 3
fib(1) fib(0) 5 5
Prepared by Pawan K. Mishra
Memoized fibonacci
function fib(n):
if fibtable[n]
Extra
return(fibtable[n])
if n == 0 or n == 1
value = n
else
value = fib(n-1) + fib(n-2)
fibtable[n] = value Extra

return(value)
Prepared by Pawan K. Mishra
In general
function f(x,y,z):

if ftable[x][y][z]
return(ftable[x][y][z])

value = expression in terms of subproblems

ftable[x][y][z] = value return(value)

Prepared by Pawan K. Mishra


In general
function f(x,y,z):

if ftable[x][y][z]
return(ftable[x][y][z])

value = expression in terms of subproblems


Need to solve recursively
exploiting the inductive
ftable[x][y][z] = value return(value) definition.

Prepared by Pawan K. Mishra


Dynamic programming

Anticipate what the


memory table looks like

Subproblems are
known from problem
structure

Dependencies form a
dag

Solve subproblems
in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the fib(5)


memory table looks like

Subproblems are
known from problem
structure

Dependencies form a
dag

Solve subproblems
in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the fib(5)


memory table looks like
fib(4)
Subproblems are
known from problem fib(3)
structure
fib(2)
Dependencies form a
dag fib(1)

Solve subproblems fib(0)


in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the fib(5)


memory table looks like
fib(4)
Subproblems are
known from problem fib(3)
structure
fib(2)
Dependencies form a
dag fib(1)

Solve subproblems fib(0)


in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the fib(5)


memory table looks like
fib(4)
Subproblems are
known from problem fib(3)
structure
fib(2)
Dependencies form a
dag fib(1)

Solve subproblems fib(0)


in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the fib(5)


memory table looks like
fib(4)
Subproblems are
known from problem fib(3)
structure
fib(2)
Dependencies form a
dag fib(1)

Solve subproblems fib(0)


in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the fib(5)


memory table looks like
fib(4)
Subproblems are
known from problem fib(3)
structure
fib(2)
Dependencies form a
dag fib(1)

Solve subproblems fib(0)


in topological order
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k)
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k) 0
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k) 0 1
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k) 0 1 1
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k) 0 1 1 2
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k) 0 1 1 2 3
Prepared by Pawan K. Mishra
Dynamic programming

Anticipate what the memory table looks like fib(5)

Subproblems are known from problem fib(4)


structure
fib(3)
Dependencies form a dag
fib(2)
Solve subproblems in topological order
fib(1)

Kk 0 1 2 3 4 5 fib(0)
fib(k)
fib(k) 0 1 1 2 3 5
Prepared by Pawan K. Mishra
Dynamic programming fibonacci

function fib(n):
fibtable[0] = 0
fibtable[1] = 1
for i = 2,3,..n
fibtable[i] = fibtable[i-1] + fibtable[i-2]
return(fibtable[n])

Prepared by Pawan K. Mishra


Memoization

Store values of subproblems in a table


Look up the table before making a recursive
call

Dynamic programming

Solve subproblems in topological order


of dependency

Dependencies must form a dag (why?)


Iterative evaluation

Prepared by Pawan K. Mishra


Takeaway
Overlapping sub problem
Tabular Method
Recursive formulation– Don’t solve recursively
Compute opt for smaller sub problem
Optimal substructure
Computing Optimal value vs Optimal solution

Prepared by Pawan K. Mishra


P1. Knapsack-without profit

Input
n objects o1, o2,…, on
Size s1, s2, ..., sn
Bag with capacity W

Find a subset of objects with largest


total size ≤ W
Prepared by Pawan K. Mishra
P1. Knapsack-without profit

Input
n objects o1, o2,…, on
Size s1, s2, ..., sn
Bag with capacity W

Find a subset of objects with largest


total size ≤ W
Prepared by Pawan K. Mishra
Brute force

Check all subsets --- 2 n

Prepared by Pawan K. Mishra


OPTIMAL SUBSTRUCTURE PROPERTY
Suppose object o1 is in the optimal solution, what
we need to solve now ??
Then the remaining subset of objects
{o2, o3, …, on} of largest size whose total
size is atmost W – s1.
Suppose object o1 is not in the optimal solution,
what we need to solve now ??
Then the remaining subset of objects
{o2, o3, …, on} of largest size whose total
Prepared by Pawan K. Mishra size is atmost W.
Recusrive Formulation
OPT ( {o2 ,…, on}, W )

OPT({o1, o2 ,…, on}, W)=max


OPT( { o1, o2 ,…, on }, W – s1 ) +s1

Prepared by Pawan K. Mishra


Recusrive Formulation
OPT ( {o2 ,…, on}, W )

OPT({o1, o2 ,…, on}, W)=max


OPT( { o1, o2 ,…, on }, W – s1 ) +s1

OPT ( i+1, w )

OPT(i, w)=max
OPT( i+1, w – si ) + si
Largest sized subset {i,….,n} with total size at most w
Prepared by Pawan K. Mishra
Prepared by Pawan K. Mishra
Prepared by Pawan K. Mishra
Prepared by Pawan K. Mishra
Prepared by Pawan K. Mishra
This basically tells you
the direction via which
you fill the table, thus
we get the base case
too

OPT(n, w)= 0 sn >w


= sn otherwise

For all w \in {1,2,…,W}

Prepared by Pawan K. Mishra


This basically tells you
the direction via which
you fill the table, thus
we get the base case
too

OPT(n, w)= 0 sn > w


= sn otherwise

For all w \in {1,2,…,W}

Prepared by Pawan K. Mishra


Total Time

How many entries we are computing ?? nW

Time to compute each entry – Constant time..


By using max of two entries..

O(nW)
Prepared by Pawan K. Mishra
Cut-and-Paste Arguments
To show optimal substructure, assume that
some piece of the optimal solution S* is not an
optimal solution to a smaller subproblem
Show that replacing that piece with the
optimal solution to the smaller subproblem
improves the allegedly optimal solution S*.
Conclude, therefore, that S* must include
an optimal solution to a smaller subproblem.
Prepared by Pawan K. Mishra
P2. Coin Change Problem
If we want to make change for Rs. T, and we have
infinite supply of each coin in the set
Coins = {v1, v2,. . . , vn}, where vi is
the value of the i-th coin.

What is the minimum number of coins required to


reach the value S?

Prepared by Pawan K. Mishra


Greedy Algorithm

Prepared by Pawan K. Mishra


Greedy Algorithm

Coins = {6, 4, 1} and T = 8

Counter Example

Prepared by Pawan K. Mishra


Greedy Algorithm

Coins = {6, 4, 1} and T = 8

Coins={1,5,10,25}-USA

Coins={1,2,5,10} -Indian
Prepared by Pawan K. Mishra
Dynamic Programming
OPT( T, n-1) First coin with value v1 is not used

OPT(T,n)= min
OPT(T- v1, n) + 1 First coin with value v1 is used

Prepared by Pawan K. Mishra


Dynamic Programming
OPT( T, n-1) First coin with value v1 is not used

OPT(T,n)= min
OPT(T- v1, n) + 1 First coin with value v1 is used

OPT( S, i-1) i-th coin with value vi is not used

OPT(S,i)= min
OPT(S- vi, i) + 1 i-th coin with value vi is used

the minimum number of coins required to reach sum S ≤ T with the first i coins, i.e.,
coins selected from the subset {v1, v2,. . . , vi} (where 0 ≤ i ≤ n).
Prepared by Pawan K. Mishra
Dynamic Programming

OPT( S, i-1) i-th coin with value vi is not used

OPT(S,i)= min
OPT(S- vi, i) + 1 i-th coin with value vi is used
SEMANTICS

the minimum number of coins required to reach sum S ≤ T with the first i coins, i.e.,
coins selected from the subset {v1, v2,. . . , vi} (where 0 ≤ i ≤ n).

Prepared by Pawan K. Mishra


Dynamic Programming

OPT( S, i-1) i-th coin with value vi is not used

OPT(S,i)= min
OPT(S- vi, i) + 1 i-th coin with value vi is used
SEMANTICS

the minimum number of coins required to reach sum S ≤ T with the first i coins, i.e.,
coins selected from the subset {v1, v2,. . . , vi} (where 0 ≤ i ≤ n).

∞ ∞∞ ∞ ∞ ∞ ∞ ∞
Prepared by Pawan K. Mishra
P3. Set Cover Problem
Given a universe U={e1, e2, e3, …, en} of n elements,

And a family of subsets F = {S1, S2, S3,…, Sm }.

find a minimum number of subcollection C of F such that C covers all elements of U.

U = {1, 2, 3, 4, 5}

F= { S1 = {1, 2, 3}, S2 = {2,3}, S3 = {4, 5}, S4 = {1, 2, 4} }

C = {S1, S3}

Prepared by Pawan K. Mishra


P3. Set Cover Problem
Given a universe U={e1 , e2, e3, …, en} of n elements,
Brute Force = O(2m poly(n))
And a family of subsets F = {S1, S2, S3,…, Sm }.

find a minimum number of subcollection C of F such that C covers all elements of U.

U = {1, 2, 3, 4, 5}

F= { S1 = {1, 2, 3}, S2 = {2,3}, S3 = {4, 5}, S4 = {1, 2, 4} }

C = {S1, S3}

Prepared by Pawan K. Mishra


Greedy Algorithm ??

Prepared by Pawan K. Mishra


Dynamic Programming
Can we improve to O*(2n) ??

Prepared by Pawan K. Mishra


Dynamic Programming
Can we improve to O*(2n) ??

Given : F = {S1, S2, S3,…, Sm } , U

To find subproblem, but how ?--- need recurrence formulation …

Prepared by Pawan K. Mishra


Dynamic Programming
Can we improve to O*(2n) ??

Given : F = {S1, S2, S3,…, Sm } , U

To find subproblem, but how ?--- need recurrence formulation …

How to proceed?

Is S1 in optimal solution, then what to cover and by what ?

Is S1 in not in optimal solution, then what to cover and by what ?

Prepared by Pawan K. Mishra


Dynamic Programming
Can we improve to O*(2n) ??

Given : F = {S1, S2, S3,…, Sm } , U

To find subproblem, but how ?--- need recurrence formulation …

How to proceed?

Is S1 in optimal solution, then what to cover and by what ? U \ S1 {S2, S3,…, Sm}

Is S1 in not in optimal solution, then what to cover and by what ? U {S2, S3,…, Sm}

Prepared by Pawan K. Mishra


Dynamic Programming
OPT( {S2, S3,…, Sn} , U) S1 is not used
OPT({S1,S2, S3,…, Sn} ,U)= min
OPT( {S2, S3,…, Sn} , U \ S1) + 1 S1 is used

Prepared by Pawan K. Mishra


Dynamic Programming
OPT( {S2, S3,…, Sn} , U) S1 is not used
OPT({S1,S2, S3,…, Sn} ,U)= min
OPT( {S2, S3,…, Sn} , U \ S1) + 1 S1 is used

OPT( Si+1, A) Si is not used

OPT(i,A)= min
OPT(Si+1, A \ Si) + 1 Si is used

To cover a subset A of U, the minimum number of subset required of F from


Si to Sm.

Prepared by Pawan K. Mishra


Dynamic Programming
OPT( Si+1, A) Si is not used

OPT(i,A)= min
OPT(Si+1, A \ Si) + 1 Si is used

To cover a subset A of U, the minimum number of subset required of F from


Si to Sm.

Goal : OPT(1, U)

Prepared by Pawan K. Mishra


Dynamic Programming
OPT( Si+1, A) Si is not used

OPT(i,A)= min
OPT(Si+1, A \ Si) + 1 Si is used

To cover a subset A of U, the minimum number of subset required of F from


Si to Sm.
Base Case: We need i=1 as a goal, so basically we need in decreasing order,
so m will play the role in defining the base case.

1 If A is subset of Sm
OPT(m,A)= min
∞ Otherwise

Goal : OPT(1, U)

Prepared by Pawan K. Mishra


Dynamic Programming
OPT( Si+1, A) Si is not used

OPT(i,A)= min
OPT(Si+1, A \ Si) + 1 Si is used

To cover a subset A of U, the minimum number of subset required of F from


Si to Sm.
Base Case: We need i=1 as a goal, so basically we need in decreasing order,
so m will play the role in defining the base case.

1 If A is subset of Sm Time Complexity=O(2nm)


OPT(m,A)= min
∞ Otherwise

Goal : OPT(1, U)

Prepared by Pawan K. Mishra


P4. Maximum Contiguous Subsequence in Array

IP: Sequence S={e1, e2, e3, …, en} of n integers

OP: pair (i,j) ei + ei+1 + ei+2 + … + ej is maximum

Prepared by Pawan K. Mishra


P4. Maximum Contiguous Subsequence in Array

IP: Sequence S={e1, e2, e3, …, en} of n integers Brute Force: For every
pair, find sum: O(n3) or
OP: pair (i,j) ei + ei+1 + ei+2 + … + ej is maximum O(n2)

DnC: O(nlogn), Tut-2,Q 8

Prepared by Pawan K. Mishra


Dynamic Programming
How to proceed?
1. OPT(i-1) + ei
Optimal solution contains element ei ----
2. ei

Optimal element does not contain element ei.


This means that ei

OPT(i-1) + ei
OPT(i)= max
ei

Optimal value if sub sequence ends at ei.


Prepared by Pawan K. Mishra
P5. Longest Common Subword
Given two strings, find the (length of the) longest
common subword
“secret”, “secretary” — “secret”, length 6

“bisect”, “trisect” — “sect”, length 4

Prepared by Pawan K. Mishra


More formally …

Let u = a0 a1…am and v = b0 b1… bn be two sequences

If we can find i, j such that


ai ai+1 …ai+k-1 = bj bj+1… bj+k-1,
u and v have a common subword of length k

Aim is to find the length of the longest common


subword of u and v

Prepared by Pawan K. Mishra


Brute force
Let u = a0 a1… am and v = b0 b1… bn
Try every pair of starting positions i in u, j in v

Match (ai, bi), (ai+1,bi+1),… as far as possible

Keep track of the length of the longest match

Assuming m > n, this is O(mn2)

mn pairs of positions

From each starting point, scan can be O(n)

Prepared by Pawan K. Mishra


Inductive structure
Let u = a0 a1 … am and v = b0 b1 …bn

aiai+1…ai+k-1 = bjbj+1…bj+k-1 is a common subword of length k at (i,j),

iff ai+1…ai+k-1 = bj+1…bj+k-1 is a common sub word of length k-1 at (i+1,j+1).

LCW(i,j): length of the longest common subword


starting at ai and b j

If ai ≠ b j, LCW(i,j) is 0,

otherwise 1+LCW(i+1,j+1)

Boundary condition: when we have reached the end


of one of the words
Prepared by Pawan K. Mishra
Inductive structure
Consider positions 0 to m+1 in u, 0 to n+1 in v
To know the end of the sequence
m+1, n+1 means we have reached the end of the word

LCW(m+1,j) = 0 for all j

LCW(i,n+1) = 0 for all i

LCW(i,j) = 0, if ai ≠ b j

= 1+ LCW(i+1,j+1),

if ai= b j
Prepared by Pawan K. Mishra
Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b
Last row and column have no 1 i
dependencies
2 s
Start at bottom right 3 e
corner and fill by row or by 4 c
column 5 t
6 •

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 33 4 5 66
LCW(i,j) depends on
LCW(i+1,j+1) s e c rr e t ••
0 b
Last row and column have no 1 i
dependencies
2 s
Start at bottom right 3 e
corner and fill by row or by 4 c
column 5 t
6 •

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b 0
Last row and column have no 1 i 0
dependencies
2 s 0
Start at bottom right 3 e 0
corner and fill by row or by 4 c 0
column 5 t 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b 0 0
Last row and column have no 1 i 0 0
dependencies
2 s 0 0
Start at bottom right 3 e 0 0
corner and fill by row or by 4 c 0 0
column 5 t 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b 0 0 0
Last row and column have no 1 i 0 0 0
dependencies 2 s 0 0 0
Start at bottom right 3 e 1 0 0
corner and fill by row or by 4 c 0 0 0
column 5 t 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b 0 0 0 0
Last row and column have no 1 i 0 0 0 0
dependencies 2 s 0 0 0 0
Start at bottom right 3 e 0 1 0 0
corner and fill by row or by 4 c 0 0 0 0
column 5 t 0 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b 0 0 0 0 0
Last row and column have no 1 i 0 0 0 0 0
dependencies 2 s 0 0 0 0 0
Start at bottom right 3 e 0 0 1 0 0
corner and fill by row or by 4 c 1 0 0 0 0
column 5 t 0 0 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 b 0 0 0 0 0 0
Last row and column have no 1 i 0 0 0 0 0 0
dependencies 0 0 0
2 s 0 0 0
Start at bottom right 3 e 2 0 0 1 0 0
corner and fill by row or by 4 c 0 1 0 0 0 0
column 5 t 0 0 0 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
0 1 2 3 4 5 6
LCW(i,j) depends on
LCW(i+1,j+1) s e c r e t •
0 0 0 0
b 0 0 0 0
Last row and column have no 1 i 0 0 0 0 0 0 0
dependencies 2 s 3 0 0 0 0 0 0
Start at bottom right 3 e 0 2 0 0 1 0 0
corner and fill by row or by 4 c 0 0 1 0 0 0 0
column 5 t 0 0 0 0 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Reading off the solution
Find (i,j) with largest
entry 0 1 2 3 4 5 6
s e c r e t •
LCW(2,0) = 3 0 0 0 0
b 0 0 0 0
Read offthe actual
1 i 0 0 0 0 0 0 0
subword diagonally 2 s 3 0 0 0 0 0 0
3 e 0 2 0 0 1 0 0
4 c 0 0 1 0 0 0 0
5 t 0 0 0 0 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Complexity

Recall that the brute force approach was


O(mn2)

The inductive solution is O(mn) if we use


dynamic programming (or memoization)

Need to fill an O(mn) size table

Each table entry takes constant time to


compute
Prepared by Pawan K. Mishra
P6. Longest common subsequence
Subsequence: can drop some letters in between

Given two strings, find the (length of the) longest


common subsequence
“secret”, “secretary” — “secret”, length 6

“bisect”, “trisect” — “isect”, length 5

“bisect”, “secret” — “sect”, length 4

“director”, “secretary” —“ectr”, “retr”, length 4

Prepared by Pawan K. Mishra


Applications
Analyzing genes of two species (in bionforamtics..)

DNA is a long string over 4 proteins A,T,G,C

Two species are closer if their DNA has longer


common subsequence

Unix Command diff


Compares text files
Find longest matching subsequence of lines

Prepared by Pawan K. Mishra


LCS
0 1 2 3 4 5 6
LCS is longest path we
s e c r e t •
can find between non-
zero LCW entries, 0 b 0 0 0 0 0 0 0
moving right and down 1 i 0 0 0 0 0 0 0
2 s 3 0 0 0 0 0 0
3 e 0 2 0 0 1 0 0
“bisect”, “secret” — “sect”, length 4
4 c 0 0 1 0 0 0 0
5 t 0 0 0 0 0 1 0
6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Inductive structure
u a0 a1 a2 … … am-1 am
v b0 b1 b2 … bn-1 bn
If a0 = b0,
LCS(a0a1…am, b0b1…bn) = 1 + LCS(a1a2…am, b1b2…bn)

Can force (a0,b0) to be part of LCS


If not, a0 and b0 cannot both be part of LCS

Not sure which one to drop

Solve both subproblems

LCS(a1 a2…am, b0 b1…bn) and LCS(a0 a1…am, b1 b2…bn), and


Prepared by Pawan K.take
Mishra the maximum
Inductive structure
u a0 a1 a2 … … am-1 am
v b0 b1 b2 … bn-1 bn

LCS(i,j) stands for LCS(ai ai+1… am, bj bj+1… bn)

If ai = bj, LCS(i,j) = 1 + LCS(i+1,j+1)


If ai ≠ b j, LCS(i,j) = max(LCS(i+1,j), LCS(i,j+1))

As with LCW, extend positions to m+1, n+1

LCS(m+1,j) = 0 for all j

LCS(i,n+1) = 0 for all i

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b
1 i
Dependencies for 2 s
LCS(m,n) are known 3 e

Start at LCS(m,n) and 4 c


fill by row, column or 5 t
diagonal 6 •

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 33 4 5 66
LCS(i+1,j+1) as well as s e c rr e t ••
LCS(i+1,j) and
0 b
LCS(i,j+1)
1 i
Dependencies for 2 s
LCS(m,n) are known 3 e

Start at LCS(m,n) and 4 c


fill by row, column or 5 t
diagonal 6 •

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 0
1 i 0
Dependencies for 2 s 0
LCS(m,n) are known 3 e 0
Start at LCS(m,n) and 4 c 0
fill by row, column or 5 t 0
diagonal 6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 0 0
1 i 0 0
Dependencies for 2 s 0 0
LCS(m,n) are known 3 e 0 0
Start at LCS(m,n) and 4 c 0 0
fill by row, column or 5 t 1 0
diagonal 6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 1 0 0
1 i 1 0 0
Dependencies for 2 s 1 0 0
LCS(m,n) are known 3 e 1 0 0
Start at LCS(m,n) and 4 c 1 0 0
fill by row, column or 5 t 1 1 0
diagonal 6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 1 1 0 0
1 i 1 1 0 0
Dependencies for 2 s 1 1 0 0
LCS(m,n) are known 3 e 1 1 0 0
Start at LCS(m,n) and 4 c 1 1 0 0
fill by row, column or 5 t 1 1 1 0
diagonal 6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 2 1 1 0 0
1 i 2 1 1 0 0
Dependencies for 2 s 2 1 1 0 0
LCS(m,n) are known 3 e 2 1 1 0 0
Start at LCS(m,n) and 4 c 2 1 1 0 0
fill by row, column or 5 t 1 1 1 1 0
diagonal 6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 3 2 1 1 0 0
1 i 3 2 1 1 0 0
Dependencies for 2 s 3 2 1 1 0 0
LCS(m,n) are known 3 e 3 2 1 1 0 0
Start at LCS(m,n) and 4 c 2 2 1 1 0 0
fill by row, column or 5 t 1 1 1 1 1 0
diagonal 6 • 0 0 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 4 3 2 1 1 0 0
1 i 43 2 1 1 0 0
Dependencies for 2 s 43 2 1 1 0 0
LCS(m,n) are known 3 e 33 2 1 1 0 0
Start at LCS(m,n) and 4 c 22 2 1 1 0 0
fill by row, column or 5 t 11 1 1 1 1 0
diagonal 6 • 00 0 0 0 0 0

Prepared by Pawan K. Mishra


Subproblem dependency
LCS(i,j) depends on 0 1 2 3 4 5 6
LCS(i+1,j+1) as well as s e c r e t •
LCS(i+1,j) and
LCS(i,j+1)
0 b 4 3 2 1 1 0 0
1 i 43 2 1 1 0 0
Dependencies for 2 s 43 2 1 1 0 0
LCS(m,n) are known 3 e 33 2 1 1 0 0
Start at LCS(m,n) and 4 c 22 2 1 1 0 0
fill by row, column or 5 t 11 1 1 1 1 0
diagonal 6 • 00 0 0 0 0 0
LCS(0,0) =4 is the ans
Prepared by Pawan K. Mishra
Recovering the sequence
Trace back the
0 1 2 3 4 5 6

path by which s e c r e t •
each entry was 0 b 4 3 2 1 1 0 0
filled 1 i 43 2 1 1 0 0
2 s 43 2 1 1 0 0
Each diagonal step
is an element of the 3 e 33 2 1 1 0 0
LCS 4 c 22 2 1 1 0 0
5 t 11 1 1 1 1 0
“sect”
6 • 00 0 0 0 0 0

Prepared by Pawan K. Mishra


Recovering the sequence
Trace back the
0 1 2 3 4 5 6

path by which s e c r e t •
each entry was 0 b 4 3 2 1 1 0 0
filled 1 i 43 2 1 1 0 0
2 s 43 2 1 1 0 0
Each diagonal step
is an element of the 3 e 33 2 1 1 0 0
LCS 4 c 22 2 1 1 0 0
5 t 11 1 1 1 1 0
“sect”
6 • 00 0 0 0 0 0

Prepared by Pawan K. Mishra


Complexity

Again O(mn) using dynamic programming


(or memoization)

Need to fill an O(mn) size table

Each table entry takes constant time to


compute

Prepared by Pawan K. Mishra


P7. Multiplying matrices
To multiply matrices A and B, need compatible
dimensions
A of dimension m x n, B of dimension n x p AB

has dimension mp

Each entry in AB take O(n) steps to compute AB[i,j]

is A[i,1]B[1,j] + A[i,2]B[2,j] +…+ A[i,n]B[n,j]

Overall, computing AB is O(mnp)

Prepared by Pawan K. Mishra


Multiplying matrices
Matrix multiplication is associative

ABC = (AB)C = A(BC)

Bracketing does not change the answer …

… but can affect the complexity of computing it!

Prepared by Pawan K. Mishra


Multiplying matrices
Suppose dimensions are A[1,100], B[100,1], C[1,100]

Computing A(BC)

BC is [100,100], 100 x 1 x 100 = 10000 steps

A(BC) is [1,100],1 x 100 x 100 = 10000 steps

Computing (AB)C

AB is [1,1], 1 x 100 x 1 = 100 steps

(AB)C is [1,100], 1 x 1 x 100 = 100 steps

A(BC) takes 20000 steps, (AB)C takes 200 steps!

Prepared by Pawan K. Mishra


Multiplying matrices
Given matrices M1, M2,…, Mn of dimensions [r1,c1], [r2,c2], …, [rn,cn]

Dimensions match, so M1 x M2 x …x Mn can be computed

ci= ri+1 for 1 ≤ i < n


Find an optimal order to compute the product

That is, bracket the expression optimally

Prepared by Pawan K. Mishra


Inductive structure
Product to be computed: M1 x M2 x …x Mn

Final step would have combined two subproducts

(M1 x M2 x …x Mk) x (Mk+1 x Mk+2 x …x Mn), for some 1 ≤ k < n

First factor has dimension (r1,ck), second (rk+1,cn)

Final multiplication step costs =r1ckcn

Add cost of computing the two factors

Prepared by Pawan K. Mishra


Subproblems
Final step is
(M1 x M2 x …x Mk) x (Mk+1 x Mk+2 x …x Mn)

Subproblems are (M1 x M2 x …x Mk) and (Mk+1 x Mk+2x …x Mn)

Total cost is Cost(M1 x M2x …x Mk) + Cost(Mk+1 x Mk+2x …x Mn)


+ r1ckcn

Which k should we choose?

No idea! Try them all and choose the minimum!

Prepared by Pawan K. Mishra


Inductive formulation
Cost(M1 x M2 x …x Mn) =
minimum value, for 1 ≤ k < n, of
Cost(M1 x M2 x …x Mk) +
Cost(Mk+1 x Mk+2 x …x Mn) +
r1ckcn

When we compute Cost(M1 x M2 x …x Mk) we will


get subproblems of the form Mj x Mj+1x …x Mk

Prepared by Pawan K. Mishra


In general …
Cost(Mi x Mi+1 x …x Mj) =
minimum value, for i ≤ k < j, of
Cost(Mi x Mi+1 x …x Mk) + Cost(Mk+1x Mk+2x …x Mj) + rickcj

Write Cost(i,j) to denote Cost(Mi x Mi+1x …x Mj)

Prepared by Pawan K. Mishra


Final equation

Cost(i,i) = 0 — No multiplication to be done

Cost(i,j) = min over i ≤ k < j


[ Cost(i,k) + Cost(k+1,j) + rickcj ]

Note that we only require Cost(i,j) when i ≤ j

Prepared by Pawan K. Mishra


Subproblem dependency
Cost(i,j) depends on 1 … i … j … n
Cost(i,k), Cost(k+1,j) for all i ≤ k < j
1
Can have O(n) dependent values, …
unlike LCS, LCW, ED
i
Start with main diagonal and fill …
matrix by columns, bottom to top,
left to right j

n

Prepared by Pawan K. Mishra


Subproblem dependency
Cost(i,j) depends on 1 … i … j … n
Cost(i,k), Cost(k+1,j) for all i ≤ k < j
1
Can have O(n) dependent values, …
unlike LCS, LCW, ED
i
Start with main diagonal and fill …
matrix by columns, bottom to top,
left to right j

n

Prepared by Pawan K. Mishra


Subproblem dependency
Cost(i,j) depends on 1 … i … j … n
Cost(i,k), Cost(k+1,j) for all i ≤ k < j
1
Can have O(n) dependent values, …
unlike LCS, LCW, ED
i
Start with main diagonal and fill …
matrix by columns, bottom to top,
left to right j

n

Prepared by Pawan K. Mishra


Subproblem dependency
Cost(i,j) depends on 1 … i … j … n
Cost(i,k), Cost(k+1,j) for all i ≤ k < j
1
Can have O(n) dependent values, …
unlike LCS, LCW, ED
i
Start with main diagonal and fill …
matrix by columns, bottom to top,
left to right j

n

Prepared by Pawan K. Mishra


Complexity

As with LCS, we to fill an O(n2) size table

However, filling MMC[i][j] could require


examining O(n) intermediate values

Hence, overall complexity is O(n3)

Prepared by Pawan K. Mishra


P8. Maximum Independent Set on
tree

Prepared by Pawan K. Mishra


P8. Minimum Vertex Cover on tree

Exercise : Based on the previous idea

Prepared by Pawan K. Mishra


P9. Diameter of a rooted tree
Excercise

Prepared by Pawan K. Mishra


P10. Weighted tree, find a maximum
sum path between any two nodes
Excercise

Prepared by Pawan K. Mishra


1
2
3
4
1
2
1
1
2
3
4
5

You might also like