Daa - Unit Ii 2020
Daa - Unit Ii 2020
Greed is good.
(Some of the time)
Refrence Horowitz & Sahani
• Demonstrate that, having made the greedy choice, what remains is a subproblem
with the property that if we combine an optimal solution to the subproblem with
the greedy choice we have made, we arrive at an optimal solution to the original
problem.
• Problem definition
• Given n objects and a knapsack where object i has a weight wi
and the knapsack has a capacity m
• If a fraction xi of object i placed into knapsack, a profit
pixi is earned
• The objective is to obtain a filling of knapsack maximizing the total profit
• Problem formulation (Formula 4.1-4.3)
• A feasible solution is any set satisfying (4.2) and (4.3)
• An optimal solution is a feasible solution for which (4.1) is maximized
maximize ∑ pi x i ( 4 . 1)
1≤i≤n
subject to ∑ w i x i ≤m ( 4 . 2)
1≤i ≤n
and 0≤ xi ≤1, 1≤i ≤n ( 4 .3 )
15 Kg Gold object i
15 Kg Silver object i1
2 Kg of Iron(2 Object which is Attractive) object i2
xi2
10 kg
9kg+1kg
9 kg+1kg
(x 1 ,x 2 ,x 3 ) ∑ w i xi ∑ p i xi
• 1. (1/2, 1/3, 1/4) 16.5 24.25
• 2. (1, 2/15, 0) 20 28.2
• 3. (0, 2/3, 1) 20 31
• 4. (0, 1, 1/2) 20 31.5
• Lemma 4.1
M=20
X1=18 P1=25
x2= 2/15 p2x2=24*2/15
x2=15 P2=24
X3=10 P3=15
p1x1 = 25
p2x2= 24*2/15
28.2
p1x1=0
p2x2=24*2/3=16
p3x3=15
31
p2x2=24
p3x3=1/2*15=7.5
31.5
Knapsack Problem
Sol: We know that greedy knapsack method using profit as its measures will at each step
choose an object that increases the profit most.
OBJECTS GREEDY BY
i wi pi pi / wi Profit weight pi / wi
1 10 20 2.0 1 1
2 20 30 1.5 1 1
3 30 66 2.2 1 1 1
4 40 40 1.0 0.5 1
5 50 60 1.2 1 0.8
TOTAL WEIGHT 100 100 100
TOTAL PROFIT EARNED 146 156 164
Greedy strategy : Knapsack Problem
Selection Criteria for selecting the best remaining objects … contd.
“If objects are selected in non-increasing order of pi / wi , then
algorithm knapsack finds an optimal solution”.
Digital or 0/1:
• O(2n)
• Solution space 2n
• Note: Time Complexity of real knapsack is linear logarithmic &
that of digital i.e. 0/1 knapsack is 2n. Therefore real knapsack
is used over 0/1.
• Digital knapsack i.e. 0/1 knapsack is NP-Hard.
P= 6+10+18+15+3+2*(5/3) = 55.33
Optimal solution: (1,2/3,0,1,1,1,1)
Note:
if p1/w1 >= p2/w2 >= p3/w3…>= pn/wn .i.e. inputs are already in this form then no
need of sorting.
Therefore best time complexity = n.
If inputs are not sorted as stated above then,
Time complexity = nlog2n
Therefore complexity of Greedy knapsack = nlogn
01/02/2025 Design and Analysis of Algorithms 23
Job Sequencing with Deadlines Page No :- 228
• Example
• n=4, (p1,p2,p3,p4)=(100,10,15,27),
(d1,d2,d3,d4)=(2,1,2,1)
Feasible processing
Solution sequence value
1. (1, 2) 2, 1 110
2. (1, 3) 1, 3 or 3, 1 115
3. (1, 4) 4, 1 127
4. (2, 3) 2, 3 25
5. (3, 4) 4, 3 42
6. (1) 1 100
7. (2) 2 10
8. (3) 3 15
9. (4) 4 27
01/02/2025 Design and Analysis of Algorithms
P2
P1
Job Sequencing
Max. deadline = 2
J4 J1
0 1 2
J1 & J3 are having same deadline i.e. 2. so therefore the
J={1,3,4} wont find a Optimal solution select one of them having
greater profit & put it from right in box.
P1= 100 & P3 = 10
Therefore P1 i.e. Job J1. Now select next job with higher profit.
Ie. Job J4. Therefore Max Profit = 100 + 27 = 127.
J1 J3 J2 J5
0 1 2 3 4
01/02/2025 Design and Analysis of Algorithms 26
Job Sequencing with Deadlines
• Theorem---Let J be a set of k jobs and a permutation of jobs in J such that Then J is a feasible solution
iff the jobs in J can be processed in the order without violating any deadline.
σ
d i1 ≤d i2 ≤. ..≤d ik . σ=i 1 ,i 2 , .. . ,i k
• Two sorted files containing n & m records respectively could be merged together
to obtain one sorted file in time O(n+m).
• When more than two sorted files are to be merged together, the merge can be
accomplished by repeatedly merging sorted files in pairs.
• Thus if x1,x2,x3 & x4 are to be merged then we could first merge x1 & x2 to
get file y1, then y1 & x3 to get y2 & finally y2 & x4 to get the desired sorted file.
• We could also merge y1 & y2 & get desired sorted file.
• Thus for n sorted files, different pairings require differing amounts of computing
time.
• Thus the problem we address here is that of determining an optimal way (one
requiring the fewest comparisons) to pairwise merge n sorted files.
E.g. Files x1,x2 & x3 are three sorted files of length 30,20 & 10 records each.
• Merging x1 & x2 requires 50 record moves.
• Merging the result with x3 requires another 60 moves.
• The total no. of record moves required to merge the three files is 110.
• Thus if we first merge x2 & x3 (taking 30 moves) & then x1 (taking 60 moves), the total record
moves made is only 90. Hence, the second merge pattern is faster than the first.
• Since merging an n-record file & m-record file requires possibly n+m record moves, the obvious
choice for a selection criteria is at each step merge the two smallest size files together.
55
25 30
10 15
1. x= 10 , y= 30 , z= 15
2. Sum of internal nodes:
OMP= 25+55 = 80
• Example
S--->A+A--->B
source
A---->M
A---->N
A--->B
C
D
T Target
Dynamic Programming
• It avoids calculating same thing more than once and it saves known results in a table which
gets populated as sub-instances are solved.
• D&C is a top-down method of dividing the instance into sub-instances and solving them
independently.
• DP on the other hand is a bottom-up technique. Which usually starts with the smallest, and
hence the simplest, sub-instances.
• In Greedy approach, at a time one element from the input is included in the solution which
always finds a place in final solution whereas in DP, solutions to earlier sub-instances may
not find a place in final solution.
Dynamic programming
Control Abstraction:
DP is usually applied to optimization problems to obtain optimal
solution to the given problem from the number of solutions available.
• Overlapping subproblems
• The optimal solution to a subproblem is
reused in solving more than one problem.
• The number of subproblems is usually a
polynomial of the input size.
Applications of Dynamic Programming:
Dynamic Programming
Hierarchical Data Structures
One-to-many relationship between elements
Tree
Single parent, multiple children
Binary tree
Tree with 0–2 children per node
Degenerate Balanced
binary tree binary tree
Binary Search Trees
Key property
Value at node
• Smaller values in left subtree
• Larger values in right subtree
Example
• X>Y
• X<Z X
Y Z
Binary Search Trees
Examples
5
10 10
2 45
5 30 5 45
30
2 25 45 2 25 30
10
25
Binary Non-binary
search trees search tree
Optimal binary search trees
e.g. binary search trees for 3, 7, 9, 12;
3 7 7 12
3 12 3 9 9
7
9 9 12 3
7
12
(a) (b) (c) (d)
8 -49
Optimal binary search trees
x < ai+1
n n
∑ P i +∑ Q i =1
i= 1 i= 1
8 -50
10 Identifiers : 4, 5, 8, 10, 11, 12, 14
Internal node : successful search, Pi
5 14
External node : unsuccessful search, Qi
4 8 11 E7
E0 E1 E2 E3 E4 12
E5 E6
8 -52
The dynamic programming approach
Let C(i, j) denote the cost of an optimal binary search tree containing ai … aj
The cost of the optimal binary search tree with ak as its root :
{ [ ][ ]}
k− 1 n
P k + Q 0 + ∑ ( P i +Q i ) +C ( 1,k−1 ) + Q k + ∑ ( P i +Qi ) +C ( k+1,n )
i= 1 i=k+ 1
ak
P 1 ...P k -1 P k +1 ...P n
Q 0 ...Q k -1 Q k ...Q n
a1...ak-1 ak+1...an
• Binary Search Trees (BSTs) are used to for search an identifier or a symbol in a
table. There are many practical applications of BST. Sometimes we need to
construct an Optimal BST (OBST) (with weights based on frequency of occurrence)
so that searching an identifier can be done in optimal time. For example any
language compiler or interpreter maintains symbol tables which are searched to
know whether the given identifier is present in the symbol table.
do
if
if while
do
if
while
do while
if
aK
a1.....ak-1 ak....aK+1
(i,j)=cost(L)+cost(R)+
Optimal Binary Search Tree (OBST) : Problem Definition
Problem Specifications :
• Let us assume given set of identifiers as {a1,a2,…,an} with a1<a2<, …
,<an.
• Let p(i) be the probability with which we search for ai and let q(i)be
the probability that the identifier x being searched for is such that ai < x <
ai+1.,0 ≤ i ≤ n.
• Further assume that a0 = - ∞ and an+1= +∞.
• Probability of unsuccessful searches
Σ q[i] 0<= i<= n
• Probability of successful searches
Σ p[i] 1<= i<= n
• Σ p[i]+Σ q[i] = 1
1<= i<= n 0<= i<= n
• With this data available now problem is to construct a BST with minimum
cost i.e. OBST.
Dynamic Programming approach to construct OBST :
• To apply DP approach for obtaining OBST, we need to view the construction of
such a tree as the result of sequence of decisions and observe that the principle
of optimality holds.
• A possible approach to this would be to decide which of the ai’s (with weights
pi’s) should be selected as the root node of the tree.
ai---ak-1 ak....aj
10,51,47,58,24,36,89
q(0)=false node
a1,a2
C(0,2)
C(0,0)+w(0,0)+ a1
C(0,1)+w(0,1)+
C(0,2+W(0,2) q0 q1
i<k<=j
C(i,j)=min{C(leftst)+C(rtsubt)}+w(i,j)
=min{c(i,k-1)+c(k,j)}+w(i,j)
C01=min{c(00)+c(1,1)}w(0,1)
i<k<=j
w(i,j)=p(j)+q(j) +w(i ,j-1) previous
W(0,0)=q(0)
w(11)=q1
w(22)=q2
W(0,1)=p(1)+q(1)+W(0,0)
W(0,2)=p(2)+q(2)+w(0,1)
0,1,2 a1
q0 q1
Dynamic Programming approach to construct OBST :
1<= k <= n
i< k <= j
i< k <= j
OBST a2 3
Ecost = 18
Icost = 14
a1 3 Total Cost = 32
if a3 1 Total Weight = 16
2 3 1
a4 1
do int
1 1
while
Dynamic Programming approach to construct OBST :
i → 0 1 2 3 4
j-i ↓
w01 = 8 w11 = 3
1 c01 = 8 c11 = 0
r01 = 1 r11 = 0
w23=p+q2+w11
(a1,c12=min{c11+c22}+w1,2
a2, a3, a4) = (do, if, int, while)
(p1, p2, p3, p4) = (3, 3, 1, 1)
C01=min{c(00)+c(1,1)}+w(0,1)
(q0, q1, q2, q3, q4) = (2, 3, 1, 1, 1)
i<k<=j
Dynamic Programming approach to construct OBST :
c(i, j) w(i, j) + min {c(i, k-1) + c(k, j)} Int. Values Min. Value k
i< k ≤ j
c(i, j) w(i, j) + min {c(i, k-1) + c(k, j)} Int. Values Min. Value k
i< k ≤ j
c(0, 4) w(0, 4) + mim {c(0, 0) + c(1, 4)} 16 + min{19, 16, 22, 25) 32 1
{c(0, 1) + c(2, 4)} 2
{c(0, 2) + c(3, 4)} 3
{c(0, 3) + c(4, 4)} 4
OBST
0 1 2 3 4
W00 =
0 C00 =
R00 =
W01 = W11 =
1 C01 = C11 =
R01 = R11 =
W01 = 8 W11 = 3
1 C01 = 8 C11 = 0
R01 = 1 R11 = 0
R ij = a R 04 = 2
R ia-1 R a. j R 01 =1 R 24 =3
R33 = 0 R44 = 0
if
do int
while
OBST
E.g. Find an OBST using Dynamic Programming for N=3 & keys
{a1,a2,a3} = { do , if , while}
Given that p(1:3) = (0.5 , 0.1 , 0.05)
q(0:3) = (0.15 , 0.1 , 0.05 , 0.05)
OBST
0 1 2 3
W00 = 0.15
0 C00 = 0
R00 = 0
Profits Wts 0 1 2 3 4 5 6 7 8
1 2 3 0 0 0 2 2 2 2 2 2
2 3 4 0 0 0 2 3 3 3 5 5
3 4 5 0 0 0 2 3 4 4 5 6
4 1 6 0 0 0 2 3 4 4 5 6
n=5 wt{1,2,5,6,7}
m=11 profit{1,6,18,22,28}
page no 267 Brassard
0/1 Knapsack Problem : Solution by DP strategy
... j →
i↓
Weight Limit 0 1 2 3 4 5 6
1 to n ->
w1 = 2, p1 = 1 0 0 1 1 1 1 1
w2 = 3, p1 = 2 0 0 1 2 2 3 3
w3 = 4, p1 = 5 0 0 1 2 5 5 6
Knapsack problem
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
5 8
W = 20
9 10
0-1 Knapsack problem
1 2 3 4 5
Knapsack
W:4 W:5 W:5 W:2 W:7
Capacity: 13
V:4 V:3 V:7 V:3 V:8
1 3 4 1 4 5
W:4 W:5 W:2 W:4 W:2 W:7
V:4 V:7 V:3 V:4 V:3 V:8
0/1 Knapsack: Optimal Substructure
• Let m[k, s] be the maximum value of objects i, for 1 i k, that can be fitted in a knapsack
with capacity s.
m[k, 0] = 0.
m[0, s] = 0.
If k,s 0:
m[k, 0] = 0.
m[0, s] = 0.
If k,s 0:
m[k, s] = max(m[k-1, s], m[k-1, s-wk]+vk) if s wk .
m[k, s] = m[k-1, s] if s < wk .
Multistage Graph
Principle of Optimality:
Whatever may be the initial decision & initial state, the remaining set of decisions including
the first one should lead to optimal solution.
• It is directed graph G = (V,E) in which the vertices are partitioned into k ≥ 2 disjoint sets
Vi, 1 ≤ i ≤ k.
• if (u,v) is an edge in E, then u € Vi, and v € Vi+1 for some i, 1 ≤ I < k.
• The vertex s is the source, and t the sink.
• Let c(i,j) be the cost of edge (I,j).
• The cost of path from s to t is the sum of the costs of the edges on the path.
• The multistage graph problem is to find a minimum-cost path from s to t.
• Each set Vi defines a stage in the graph, & due to constraints on E, every path
from s to t starts in stage 1, goes to stage 2, then to stage 3, then to stage 4, &
so on, & terminates in stage k.
• E.g. Five stage graph with a minimum cost s to t path indicated by the broken edges.
Multistage Graphs
cost(i,j)=min(cost(i, l)
+min(cost (i+1,l)
Cost Matrix
Solution using Backward Costs
The traveling salesperson
(TSP) problem
e.g. a directed graph :
2
1 2
2
10
4
6 5 9 3
8
7
4 3
4
1 2 3 4
Cost matrix: 1 2 10 5
2 2 9
3 4 3 4
4 6 8 7
The multistage graph solution
(1,2,3) 4 (1,2,3,4)
9
6
¡Û (1,2,4) 7 (1,2,4,3)
(1,2)
4
2
3 (1,3,2) ¡Û (1,3,2,4) 6
10
(1) (1,3) 1
2
4 (1,3,4) 8 (1,3,4,2)
5 4
(1,4) 9
8 (1,4,2) (1,4,2,3)
2
7
(1,4,3) 3 (1,4,3,2)
O1+O3, O2+O3
O=O1+O3+O4