Dynamic Programming
Dynamic Programming
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)
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
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.
assumption.
Proof: If A is any maxwt independent subset of M
-{x} is an independent
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