Design and Analysis of Algorithms: Unit - III
Design and Analysis of Algorithms: Unit - III
Algorithms
Unit - III
Periyar Govt. Arts College
Cuddalore
Dr. R. Bhuvaneswari
Assistant Professor
Department of Computer Science
Periyar Govt. Arts College, Cuddalore.
1 Dr. R. Bhuvaneswari
Greedy Method
Syllabus
UNIT - III: THE GREEDY METHOD
The General Method - Knapsack Problem – Tree Vertex Splitting -
Job Sequencing with Deadlines - Minimum Cost Spanning Trees -
Optimal Storage on Tapes - Optimal Merge Pattern - Single Source
Shortest Paths.
TEXT BOOK
Fundamentals of Computer Algorithms, Ellis Horowitz, Sartaj
Sahni, Sanguthevar Rajasekaran, Galgotia Publications, 2015.
General Method:
• In the method, problems have n inputs and requires to obtain a
subset that satisfies some constraints.
• Any subset that satisfies these constraints is called feasible
solution.
• A feasible solution should either maximizes or minimizes a given
objective function is called an optimal solution.
• The greedy technique in which selection of input leads to optimal
solution is called subset paradigm.
• If the selection does not lead to optimal subset, then the decisions
are made by considering the inputs in some order. This type of
greedy method is called ordering paradigm.
• Given a set of items, each with a weight and a profit, determine the
number of each item to include in a collection so that the total weight is
less than or equal to a given limit and the total profit is as large as
possible.
• Items are divisible; you can take any fraction of an item.
• And it is solved using greedy method.
subject to wi x i ≤ m − − − − − ②
1≤ i ≤n
0 ≤ xi ≤ 1, 1 ≤ i ≤ n − − − − −③
• A feasible solution is any set (x1 …. xn) satisfying equations ② and ③.
• An optimal solution is a feasible solution for which equation ① is
maximized.
Periyar Govt. Arts College
6 Dr. R. Bhuvaneswari Cuddalore
Knapsack Problem
Example: n = 3, m = 20
Weight wi 18 15 10
Profits pi 25 24 15
Algorithm GreedyKnapsack(m, n)
//n objects are ordered such that p[i]/w[i] ≥ p[i+1]/w[i+1].
{
for i:= 1 to n do x[i] := 0.0; Weight wi 15 10 18
U := m; Profits pi 24 15 25
for i := 1 to n do
{ x[1] = 0.0 m = 20, n = 3
if (w[i] > U) then break; x[2] = 0.0
x[3] = 0.0
x[i] :=1.0;
U = 20
U := U-w[i];
i=1
}
x[1] = 1; U = 5
if (i ≤ n) then i = 2, 10 > 5
x[i] = U/w[i]; x[2] = 5/10 = 1/2
} x[1] = 1, x[2] = 1/2, x[3] = 0
=5
d(4) = 4.
since, d(4) + w(2,4) = 6 > , node 4
is split and d(4) = 0.
since, d(2) + w(1,2) = 6 > , node 2
is split and d(2) = 0.
since, d(6) + w(3,6) = 6 > , node 6
is split and d(6) = 0.
10 2 10 2
14 16 14 16
6 7 3 6 7 3
24
25 18 25
12 12
5 5
22 4 22 4
Periyar Govt. Arts College
21 Dr. R. Bhuvaneswari Cuddalore
MST – Prim’s Algorithm
for i = 2 to n-1 do
{
// find n-2 additional edges for t. Let j be an index such that near[j] 0
//and cost[j, near[j]] is minimum;
t[i, 1] = j;
t[i, 2] = near[j];
mincost = mincost + cost[j, near[j]];
near[j] = 0;
for k = 1 to n do
{
if ((near[k] 0) and (cost[k, near[k]] > cost[k, j])) then
near[k] = j;
}
}
return mincost;
}
Periyar Govt. Arts College
23 Dr. R. Bhuvaneswari Cuddalore
Optimal Storage on tapes
• If all the programs are retrieved equally often, then the Mean Retrieval
Time (MRT) is 1 𝑡 𝑗
𝑛
1 ≤𝑗 ≤𝑛
Example:
n = 3,
(l1, l2, l3) = (5, 10, 3)
n! = 6 possible ordering
Ordering I d(I)
1, 2, 3 5+5+10+5+10+3 = 38
1, 3, 2 5+5+3+5+3+5+10 = 31
2, 1, 3 10+10+5+10+5+3 = 43
2, 3, 1 10+10+3+10+3+5 = 41
3, 1, 2 3+3+5+3+5+10 = 29
3, 2, 1 3+3+10+3+10+5 = 34
Optimal ordering is 3, 1, 2
Thus the greedy method implies to store the programs in nondecreasing
order of their length.
Algorithm Store(n, m)
// n is the number of programs and m the number of tapes.
{
j = 0;
for i = 1 to n do
{
write(“append program “, i, “to permutation for tape “, j);
j = (j+1) mod m;
} Tape 0 11 34 45 73
} Tape 1 12 34 56 78
Tape 2 24 34 56 91 Periyar Govt. Arts College
26 Dr. R. Bhuvaneswari Cuddalore
Optimal Merge patterns
• Merge a set of sorted files of different length into a single sorted file.
• We need to find an optimal solution, where the resultant file will be
generated in minimum time.
• If the number of sorted files are given, there are many ways to merge
them into a single sorted file. This merge can be performed pair wise.
Hence, this type of merging is called as 2-way merge patterns.
• As, different pairings require different amounts of time, in this strategy we
want to determine an optimal way of merging many files together. At each
step, two shortest sequences are merged.
• To merge a m-record file and a n-record file requires possibly m + n
record moves
• Merge the two smallest files together at each step.
• Two-way merge patterns can be represented by binary merge trees.
• Initially, each element is considered as a single node binary tree.
Periyar Govt. Arts College
27 Dr. R. Bhuvaneswari Cuddalore
Optimal Merge patterns
File/list A B C D
sizes 6 5 2 3
(a) (b) (c)
6 5 2 3
6 5 2 3
6 5 2 3
5
11
11 5
10
13
16 16
16
11+5+16 = 32 5+10+16 = 31
11+13+16 = 40
Periyar Govt. Arts College
28 Dr. R. Bhuvaneswari Cuddalore
Optimal Merge patterns
lists x1 x2 x3 x4 x5 lists x1 x2 x3 x4 x5
sizes 20 30 10 5 30 sizes 2 3 5 7 9
• During the course of the algorithm candidate paths are examined and the
tentative distances are modified.
• Initially dv = for all v V such that v ≠ v0, while dv0 = 0.
• The predecessor of the vertex v on the shortest path from v0 to v is pv.
Initially, pv is unknown for all v V.
• The following steps are performed in each pass:
1. From the set of vertices for with kv = false, select the vertex v
having the smallest tentative distance dv.
2. Set kv true.
3. For each vertex w adjacent to v for which kv ≠ true, test whether
the tentative distance dv is greater than dv + C(v,w). If it is, set
dw dv + C(v,w) and set pw v.
• In each pass exactly one vertex has its kv set to true. The algorithm
terminates after |V| passes are completed at which time all the shortest
paths are known.
Periyar Govt. Arts College
33 Dr. R. Bhuvaneswari Cuddalore
Single-source shortest path
Initially:
S = {1}; D[2] = 10; D[3] = ; D[4] = 30; D[5] = 100
Iteration 1
Select w = 2, so that S = {1, 2}
D[3] = min(, D[2] + C[2, 3]) = 60
D[4] = min(30, D[2] + C[2, 4]) = 30
D[5] = min(100, D[2] + C[2, 5]) = 100
Iteration 2
Select w = 4, so that S = {1, 2, 4}
D[3] = min(60, D[4] + C[4, 3]) = 50
D[5] = min(100, D[4] + C[4, 5]) = 90
Iteration 3
Select w = 3, so that S = {1, 2, 4, 3}
D[5] = min(90, D[3] + C[3, 5]) = 60
Iteration 4
Select w = 5, so that S = {1, 2, 4, 3, 5}
D[2] = 10; D[3] = 50; D[4] = 30; D[5] = 60
Periyar Govt. Arts College
34 Dr. R. Bhuvaneswari Cuddalore