0% found this document useful (0 votes)
4 views45 pages

Dynamic Programming

The document discusses dynamic programming techniques, particularly focusing on problems like matrix chain multiplication and polygon triangulation, emphasizing optimal substructures and overlapping subproblems. It also covers greedy algorithms, such as the activity selection problem and the knapsack problem, explaining their properties and applications. Additionally, it introduces matroid theory and task scheduling problems, illustrating how greedy strategies can be used to find optimal solutions in these contexts.

Uploaded by

Turjo Sarker
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)
4 views45 pages

Dynamic Programming

The document discusses dynamic programming techniques, particularly focusing on problems like matrix chain multiplication and polygon triangulation, emphasizing optimal substructures and overlapping subproblems. It also covers greedy algorithms, such as the activity selection problem and the knapsack problem, explaining their properties and applications. Additionally, it introduces matroid theory and task scheduling problems, illustrating how greedy strategies can be used to find optimal solutions in these contexts.

Uploaded by

Turjo Sarker
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/ 45

Dynamic Programming

Characterize structure of optimal solution


Recursively define value of optimal solution
Compute value of optimal solution bottom-up
Construct an optimal solution from this

Problems that need such solution:


Matrix chain multiplication
Optimal polygon triangulation
Longest common subsequence

Key ingredients to look for:


Optimal substructures
Overlapping subproblems
Matrix multiplication problem
Multiply A1 x A2 x A3
Dimensions: (pXq) x (qXr) x (rXs)
Count of Operations:
(A1 X A2) X A3 -> option-1 = pqr + prs
A1 X (A2 X A3) -> option-2 = qrs + pqs

The order of the matrices in a matrix chain is


such that the multiplication is compatible.
Polygon triangulation Problem
Cost of triangulation is considered to be the
sum of side lengths of the triangles
Substructures - The polygon can be split into
two subpolygons by joining two vertices
Overlapping - A pentagon can be split into
triangle and quadrilateral and the resulting
quadrilateral splits further into two triangles,
which two can as well be the starting point
Matrix Chain Multiplication Problem
Let no of alternative parenthesizations be denoted by
P(n)
A sequence of n matrices can be split between k-th and
(k+1)-th matrices for any k=1,2,..,n-1 and then
parenthesize the two resulting subsequences
independently.
-
Solution is C(n) = (1/n+1) 2nCn = (4n/n 3/2),
This is called Catalan number and is exponential.
Hence no of solutions is exponential and exhaustive
search is a poor strategy
Catalan number
Balanced parentheses
Mountain ranges
Diagonal avoiding paths
Polygon triangulation
Hands across a table
Rooted Binary trees
Planar trees
Skewed polyominos
Multiplication ordering
Balanced parentheses
Mountain ranges
Diagonal avoiding paths
Polygon triangulation
Hands across a table
Rooted binary trees
(internal nodes those which connect to two nodes below)
Plane rooted trees
Skew polyominos
(with perimeter 2n+2)
Matrix chain multiplication
Generating function - Fibonacci
Consider the generating function to be power
series with Fibonacci numbers as coefficients
F(z)= Fi zi with Fi = Fi-1 + Fi-2
F(z)= (Fi-1 + Fi-2 ) zi
= z + z Fi-1 zi-1 + z2 Fi-2 zi-2
= z + z F(z) + z2 F(z)
= z / (1 z - z2)
Generating function Catalan number
C(n) = C(k)C(n-k) summed over all k=1 to n-1
C(n)=Cn-1C0+Cn-2C1+ ... + C1Cn-2+C0Cn-1
Using generating function, f(z) = C(n) zn
Next, [f(z]2= C0C0+(C1C0 + C0C1)z+ ...= C1 + C2z+C3 z2+..
Multiplying by z on both sides, f(z)= C0+z[f(z]2
This quadratic in f(z) solves to (1 - -4z))/2z
Solution for Catalan number
Solution for Catalan number
Solution for Catalan number use
Stirling approximation on factorials

Overall expression will become

This implies that Catalan number grows


exponentially
Matrix Chain Order
MATRIX-CHAIN-ORDER (p)
n=length[p]-1
for i=1 to n
m[i,i]=0
for l=2 to n
for i=1 to n-l+1
j=i+l-1
m[i,j]=INF
for k=i to j-1
q=m[i,k]+m[k+1,j]+pi-1pkpj
if q< m[i,j]
m[i,j]=q
s[i,j]=k
Return m and s
Multiplication of the chain
Matrix-Chain-Multiply (A,s,i,j)
if j>i
X=M-C-M(A,s,i,s[i,j])
Y=M-C-M(A,s,s[i,j]+1,j)
return MATRIX-MULTIPLY(X,Y)
else
return Ai
Memoized Matrix Chain
MMC(p)
n=length[p]-1
for i= 1 to n
for j= i to n
m[i,j]=INF
return LOOK-UP-CHAIN(p,1,n)

LookUpChain(p,i,j)
if m[i,j]<INF return m[i,j]
if i==j
m[i,j]=0
else
for k=i to j-1
q=LUC(p,i,k)+ LUC(p,k+1,j)+ pi-1pkpj
if q < m[I,j]
m[I,j]=q
Return m[i,j]
Example of Matrix Chain
m[i,j]=min{m[i,k]+m[k+1,j]+pi-1pkpj} for all <j
Suppose, the best parentheses of 2-5 is needed.
m[2,5]= m[2,2]+m[3,5]+p1p2p5
m[2,5]= m[2,3]+m[4,5]+p1p3p5
m[2,5]= m[2,4]+m[5,5]+p1p4p5
Here, all smaller chain matrix results are stored.
The minimum among the three is stored in m[][]
The corresponding index k is stored in s[][]
Recursive Matrix Chain
RMC(p,i,j)
if i==j then return zero
m[i,j]=INF
for k=i to j-1
q=RMC(p,i,k)+RMC(p,k+1,j)+pi-1pkpj
if q<m[i,j]
m[i,j]=q
Return m[i,j]

- i n-1
Greedy Algorithm for Activity Selection
Locally optimum choice leading to globally
optimum solution
Greedy choice property & Optimal
substructure
-{1} is also optimal
si 1
Induction at every step - top down approach
iteratively solves a smaller subproblem
Greedy Activity Selector
To accommodate maximum number of activities with set of start
times and finish times
Sort the activities first on finish times in order to maximize the
amount of unscheduled time remaining

n=length[S]
A={1}
j=1
for i=2 to n
if si fj //Compatibility check
A=A U {i}
j=i // most recent addition
return A
Knapsack problem
Thief robs a store having n items with value vi and
weight wi with total carrying capacity of W.
Optimal substructure if a portion or whole of
the most valuable is taken out, the remaining
load is to be selected from remaining items
In Fractional knapsack, use top-down greedy
strategy on unit price of items ui =vi /wi
In 0/1 knapsack, the strategy fails as there can be
empty space left out use dynamic programming
to solve the resulting overlapping subproblems
bottom-up
Data compression - Huffman coding
Variable length encoding based on frequency of
occurrence of the symbols
Collapse two least occurring symbols into
compound symbol
Continue the process until two symbols are left
Heap based construction yields the coding tree
Minimum overhead on average code word length
ensured by collapsing of least probable symbols
No other code that uses any other strategy is
capable of better compression
Greedy Algorithm on Matroids
Matroid is an ordered pair M= [S,I]
To find Maximal weight independent subset I
of a set S having elements of weight w
Graphic Matroid theory
Generic Matroid Graphic Matroid
S is a finite non-empty set Set of edges E of a graph (V, E)

Independent: I is non-empty A is I iff A is acyclic, Set of edges


family of subsets of S are independent iff it is forest

Depletion of an edge retains the


Hereditary: If B I and A is independent property subset
contained in B then A I of forest is a forest

Exchange: If A,B I and |A| < |B| Extension to AU{x} possible till
then some x B-A exists such formation of cycle
that A U {x} I
Maximal Independent Subset
When no more extensions are possible
All maximal subsets are of same size

Find maximum weight independent subset A of weighted matroid

GREEDY(M,w)
A = NULL
sort S[M] into non-decreasing order by weight
for each x S[M] taken in order of w
if A U {x} I[M]
A = A U {x}
Return A
Exchange property of graphic matroid
Suppose A and B are forests of G and that |B| > |A|,
i.e. A, B are acyclic sets of edges and B contains more
edges.
Now, forest with k edges contains exactly |V| - k
trees. Begin with |V| trees and no edges. As and
when an edge is introduced, no of trees reduce by 1.
So forest A contains |V|-|A| trees and forest B has
|V|-|B| trees.
Since forest B has fewer trees, it must be having
some tree T whose vertices are in two different trees
in forest A.
Exchange property of graphic matroid
Now, T being connected, it must have an edge (u,v)
connecting vertices in two different trees in forest A.
Thus edge (u,v) can be added to A without creating a
cycle. This satisfies exchange property.

An edge e is an extension of A iff e is not in A and


addition of e does not create a cycle. When no more
extension is possible, A is maximal. For this A should
not be contained in any larger independent subset of
M.
Size of independent subsets
- All maximal independent subsets in a
matroid have the same size.
Suppose on the contrary, B is a maximal
independent subset that is larger than
another one A. In that case, exchange
property implies that A is extendable to A U
-A. which contradicts the
assumption that A is maximal.
Correctness of greedy algorithm

Lemma 1: Let x be the first element of S (sorted


on weight) such that {x} is independent. If such x
exists, then there exists an optimal subset A of S
that contains x. (greedy choice property)
Lemma 2: If x is not an extension of NULL, it is
not an extension of any independent subset A of
S.
Lemma 3: Let x be first element chosen. The
x,y -{x}: B U
Proof for Lemma 1
If no x exists, we have empty set independent.
Otherwise, B is any non-empty optimal subset. Assume that x
does not belong to B. otherwise A=B and we are done. No

Now construct set A as follows. Begin with A={x} which by


choice of x makes A independent.
Using exchange property repeatedly, find a new element of B
that can be added to A until |A|=|B| while preserving
independence of A.
Then A= B-
optimal and since x , lemma is proven.
Proof for Lemma 2 and 3
Proof: Assume that x is an extension of A but not

assumption.
Proof: If A is any maxwt independent subset of M
-{x} is an independent

w(x), a maxwt solution in M containing x yields


maxwt
Proof for matroid greedy theorem
By Lemma 2, any element that is not an initial extension of
NULL, can be forgotten.
Once first element x is selected, Lemma 1 implies that the
greedy algorithm does not err by adding x to A, since there
exists an optimal subset containing x.
Finally Lemma 3 implies that the problem gets reduced to one
x.

iff B U {x} is independent in M,

Thus subsequent operations of greedy algorithm finds maxwt


maxwt independent subset for M.
Task scheduling problem
-time tasks with deadlines
{d1,d2 dn} and penalty (non ve) {w1,w2 wn} for
missing the deadlines. To find a schedule for S that
minimizes the total penalty.
Early-deadline-first schedule is possible by swapping
tasks while constructing a schedule starting from NULL
set. Then we are left with a subset A of early tasks
where tasks meet deadlines sorted in order of non-
decreasing deadlines and another subset {S-A} of late
tasks where tasks miss deadlines and these may appear
in any order.
Early and late task sets
Set A of tasks is independent if there exists a schedule
where none is late, the set of early tasks for a schedule
forms an independent set of tasks.
Let I denote the set of all independent sets. For
t=1,2,..,n; let Nt(A) denote no of tasks in A whose di
i.e. deadline is t or earlier.
Clearly, if Nt(A) > t for some t, then there is no way to
make a schedule with no late tasks for set A, because
there are more than t tasks to finish before time t.
Hence set A is independent implies that Nt
t=1,2,..,n.
Minimizing penalty on early tasks
Then there is no late task if those in A are
scheduled in order of non-decreasing di. The i-th
largest deadline is at most i. Given these
properties it is easy to compute whether a given
set of tasks is independent.
Minimizing sum of penalties on late tasks is
equivalent to maximizing the penalty on early
tasks. So we can use greedy strategy to find an
independent set A of tasks that maximizes the
total penalty. Then this system must be shown to
be a matroid.
Independent task sets
Every subset of an independent set of tasks is
certainly independent.
Suppose B, A are independent with |B| > |A|.
Let k be the largest t so that Nt Nt(A).
Since Nn(B)=|B| and Nn(A)=|A|, but |B| > |A|,
we must have k < n and Nj(B) > Nj(A) for all j in

Therefore B contains more tasks with deadline


k+1 than A does.
Exchange property of tasks
Let x be a task in B-
U {x}.
To show exchange property, we need to show

property.
Nt Nt
independent.
Nt Nt
Task scheduling - example
Task 1 2 3 4 5 6 7
Deadline 4 2 4 3 1 4 6
Penalty 70 60 50 40 30 20 10
Augment A Deadline N1(A) N2(A) N3(A) N4(A) N5(A) N6(A) Remarks

{1} 4 0 0 0 1 - - Independent
{1,2} 4,2 0 1 1 2 - - -do-
{1,2,3} 4,2,4 0 1 1 3 - - -do-
{1,2,3,4} 4,2,4,3 0 1 2 4 - - -do-
{1,2,3,4,5} 4,2,4,3,1 1 2 3 5 - - N4(A) > 4
{1,2,3,4,6} 4,2,4,3,4 0 1 2 5 - - N4(A) > 4
{1,2,3,4,7} 4,2,4,3,6 0 1 2 4 4 5 Independent
Result of scheduling example
The set S={1,2,3,4,5,6,7} of tasks is sorted on
the penalty.
Next, we sort A on deadlines so that schedule
is early tasks <2,4,1,3,7> followed by late
tasks <5,6>
Final schedule <2,4,1,3,7,5,6> with penalty=50

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy