Eedy Method
Eedy Method
Some problems like Knapsack, Job sequencing with deadlines and minimum cost
spanning trees are based on subset paradigm. For the problems that make decisions by
considering the inputs in some order, each decision is made using an optimization
criterion that can be computed using decisions already made. This version of greedy
method is ordering paradigm. Some problems like optimal storage on tapes, optimal
merge patterns and single source shortest path are based on ordering paradigm.
The Greedy method is used for solving optimization problems. Every greedy method
based problem will be given a set of inputs and constraints. The objective is to find a
solution vector, which satisfies the constraint. Some of the important concepts of
greedy method are:
Feasible solution: Any subset of the original input that satisfies a given set of
constraints is called a feasible solution.
Note: Greedy technique works by making the decision that seems most promising at
any moment .This technique will never reconsider this decision, irrespective of
situation later.
3.1Huffman Codes
File compression is another application of Greedy algorithm .If we have a file only
with characters a, e, i, s, t, spaces and new lines, the frequency of appearance of A= 10,
E= 15, I=12, I=3, T=4, Space=13 and Newline=1. By using a standard coding scheme,
for 58 characters using 3 bits for each character, the file requires 174 bits to represent,
as shown below
Using a binary tree representation the binary code for the alphabets are as follows:
The representation of each character can be found by starting at the root and recording
the path. Use a 0 to indicate the left branch and a 1 to indicate the right branch. If the
character ci is at depth di and occurs fi times, the cost of the code is equal to ∑
difi.With this representation the total number of bits is 3x10 + 3x15 + 3x12 + 3x3 +
3x4 + 3x13 + 3x1 = 174 .A better code can be obtained by with the following
representation.
The basic problem is to find the full binary tree of minimal total cost. This can be
done by using Huffman coding (1952).
Huffman's Algorithm:
In this algorithm a forest of trees is maintained. The weights of a tree is equal to the
sum of the frequencies of its leaves. If the number of characters is 'c'. c - 1 times,
select the two trees T1 and T2, of smallest weight, and form a new tree with sub-trees
T1 and T2. Repeating the process we will get an optimal Huffman coding tree.
Example:
The initial forest with the weight of each tree is as follows:
Step1: The two trees with the lowest weight are merged together, creating the forest,
the Huffman algorithm after the first merge with new root T1 is as follows:
The total weight of the new tree is the sum of the weights of the old trees,S=3 and
nl=1,total for T1=4
Step 2: Next select the two trees of smallest weight, T1 and t, which are merged into a
new tree with root T2 and weight 8.
Step 3: In next step we merge T2 and a creating T3, with weight 10+8=18.
Step 4: Now the two trees of lowest weight are the single node trees representing i and
the blank space. These two trees merged into the new tree with root T4 with weight 25.
Step 5: Again merge the trees with roots e and T3 of lowest weights resulting in a tree
with total weight 33.
Step 6: Lastly the optimal tree is obtained by merging the two remaining trees T4 and
T5. The optimal trees with root T6 is
Now traverse the tree from the root and find the code representation of each
character.For example for the character ‘a’ the code is 001,for space it is 11 , for
newline it is 00001 and so on.The full binary tree of minimal total cost, where all
characters are obtained in the leaves, uses only 146 bits as shown below
Example 2:
Construct a Huffman tree by using the below frequency table.
Value A B C D E F
Frequency 5 25 7 15 4 12
Step 1: According to the Huffman coding we arrange the values in ascending order of the
frequencies.
Value E A C F D B
Frequency 4 5 7 12 15 25
Step 2: Insert first two elements which have smaller frequency character E with frequency 4
and character A with frequency 5.
Value C EA F D B
Frequency 7 9 12 15 25
Value F D CEA B
Frequency 12 15 16 25
Step 4: Next elements are F and D so we construct another subtree for F and D.
Value CEA B FD
Frequency 16 25 27
Step 5: Taking next value having smaller frequency then add it with CEA and insert it
at correct
place.
Value FD CEAB
Frequency 27 41
Step 6: We have only two values hence we can combined by adding them.
Now traverse the above Huffman tree from the root and find the code representation of each
character and the total number of bits required
Value A B C D E F
Frequency 5 25 7 15 4 12
Code 1011 11 100 01 1010 00
Total bits 20 50 21 30 16 24
Total bits required is =161
1. 0-1 Knapsack Problem → In this type of knapsack problem, there is only one
item of each kind (or we can pick only one). So, we are available with only two
options for each item, either pick it (1) or leave it (0) i.e., xi∈{0,1}xi∈{0,1}.
2. Bounded Knapsack Problem (BKP) → In this case, the quantity of each item
can exceed 1 but can't be infinitely present i.e., there is an upper bound on it.
So, 0≤xi≤c0≤xi≤c, where c is the maximum quantity of ii we can take.
3. Unbounded Knapsack Problem (UKP) → Here, there is no limitation on the
quantity of a specific item we can take i.e., xi≥0xi≥0.
4. Integer Knapsack Problem → When we are not available to just pick a part
of an item i.e., we either take the entire item or not and can't just break the item
and take some fraction of it, then it is called integer knapsack problem.
Max ∑ p i xi
1<= i <=n
Subject to ∑ wi xi <= m
1<= i <=n
The main objective is to place the objects into the knapsack so that maximum profit is
obtained and the weight of the object should not exceed the capacity of the knapsack.
The knapsack problem is explained in 2 ways here:
1. Continuous of fractional
2. Discrete or 0/1
Example1:
For the knapsack problem n=3,m=20 (p1,p2,p3) = (25,24,15) and (w1,w2,w3) =
(18,15,10).Find the feasible solutions and optimal solution using greedy method.
Solution:
Strategy 1: Arrange the objects in the increasing order of weights.
Weights 10 15 18
Profits 15 24 25
Objects 3 2 1
20 3 10 1
20 -10 =10 2 15 10/15=2/3
0 1 18 Cannot add as the knapsack is
full
Profits 25 24 15
Weights 18 15 10
Objects 1 2 3
20 1 18 1
20 -18 =2 2 15 2/15
0 3 10 Cannot add as the knapsack is
full
20 2 15 1
20 -15 =5 3 10 5/10=1/2
0 1 18 Cannot add as the knapsack is
full
20 1 18 1
20 -18 =2 3 10 2/10=1/5
0 1 18 Cannot add as the knapsack is
full
Example 2:
Solve the greedy knapsack problem using fractional approach for earning the maximum
profit.
N=4, Knapsack capacity M=40
Object A B C D
Weight 20 10 15 12
Profit 2 8 4 6
Weights 10 12 15 20
Profits 8 6 4 2
Objects B D C A
40 B 10 1
40 -10 =30 D 12 1
30-12=18 C 15 1
18-15=3 A 20(The 3/20
full
object
cannot be
added)
40 B 10 1
40 -10 =30 D 12 1
30-12=18 C 15 1
18-15=3 A 20(The 3/20
full
object
cannot be
added)
40 B 10 1
40 -10 =30 D 12 1
30-12=18 C 15 1
18-15=3 A 20(The 3/20
full
object
cannot be
added)
20 3 10 1
20 -10 =10 2 15 Cannot add as the knapsack
doesn’t have space for the full
object
0 1 18 Cannot add as the knapsack is
full
Profits 25 24 15
Weights 18 15 10
Objects 1 2 3
20 1 18 1
20 -18 =2 2 15 Cannot add as the knapsack
doesn’t have space for the full
object
0 3 10 Cannot add as the knapsack is
full
20 2 15 1
20 -15 =5 3 10 Cannot add as the knapsack
doesn’t have space for the full
object
0 1 18 Cannot add as the knapsack is
full
20 1 18 1
20 -18 =2 3 10 Cannot add as the knapsack
doesn’t have space for the full
object
0 1 18 Cannot add as the knapsack is
full
Example 2:
Solve the greedy knapsack problem using fractional approach for earning the
maximum profit.
N=4, Knapsack capacity M=40
Object A B C D
Weight 20 10 15 12
Profit 2 8 4 6
Weights 10 12 15 20
Profits 8 6 4 2
Objects B D C A
40 B 10 1
40 -10 =30 D 12 1
30-12=18 C 15 1
18-15=3 A 20(The 0
object
cannot be
added)
40 B 10 1
40 -10 =30 D 12 1
30-12=18 C 15 1
18-15=3 A 20(The 0
object
cannot be
added)
40 B 10 1
40 -10 =30 D 12 1
30-12=18 C 15 1
18-15=3 A 20(The 0
object
cannot be
added)
Algorithm: Greedyknapsack0/1(M, n, w, P, x)
Assume the objects to be arranged in decreasing order of pi / wi. Let M be the
knapsack capacity, n is the number of edges, w [1 : n] is the array of weights, P[1 :
n]is the array of profits ,x is a solution vector.
graph is the sum of the edges in the sub graph. A minimum spanning tree for a
weighted graph is spanning tree with minimum weight.
In a minimum spanning tree problem we are given with a connected undirected
graph G =(V, E) and a length function W such that W (e) is the length of edge
e,we are asked to find the subset of edges that connects all vertices together and
has a minimum total length.The two popular algorithms to find the minimum
cost spanning tree are,
Kruskal’s Algorithm
Prim’s Algorithm
1. [Initialization]
count = 0 //initial no of edges
k=0 //points of the first selected edge of MST
sum=0 //initial cost of MST
4.[Finished]
1 28
10
14 2
6 16
24 7
3
25 18
5
12
22 4
Step1: The cost of the edge (6, 1) is least with weight 10 and hence it is selected
1
10
Step 2: The next least cost edge (3, 4) with weight 12 is selected
1
10
12
Design and analysis of Algorithms - A Beginners Guide 4 Page 20
Unit III : Greedy Method
Step 3: The next least cost edge (3, 4) with weight 12 is selected
1
10
14 2
6
7
3
12
4
Step3: Edge (2, 3) with weight 16 is the next least cost edge.
1
10
14 2
6 16
7
3
4 12
Step4: Edge (7, 4) with weight 18 is least cost edge but including it results in a cycle.
Hence it is rejected.
Step 5: Edge (4, 5) with weight 22 is the next least cost edge.
1
10
14 2
6 16
7
3
5
12
22 4
Step 6: The next least edge to be selected is (5, 7) with weight 24 but cannot be
selected because it results in a cycle.
Step 7: Edge (5, 6) with weight 25 is the next least cost edge.
1
10
14 2
6 16
7
3
25
5
12
22 4
Result: The cost of minimum spanning tree is given by the sum of weights of the
selected edges.
i.e., 10 +12 + 14 +16+22+25=99
Example 2:
Find the minimum spanning tree for the below graph using Kruskal’s Algorithm.
Step 5 : Including the edge b-c will result in cycle so do not include
Step 7 : Considering the edge b-e also is not possible because of cycle formation
Therefore the minimum spanning tree is shown below and the minimum cost is 7
Algorithm :Prim’s(n, c)
//Assume G is connected, undirected and weighted graph.
//Input : The cost adjacency matrix C and number of vertices n.
//Output: Minimum weight spanning tree.
for i=1 to n do
visited [ i ]=0
u=1
visited [ u ] = 1
while( there is still an unchosen vertex ) do
Let <u, v> be the lightest edge between any chosen vertex u and unchosen vertex
v
visited [ v ] =1
T=union (T, <u,v>)
end while
return T
1 28
10
14 2
6 16
24 7
3
25 18
5
12
22 4
Step1:
Initially S=0 and G=[1, 2, 3, 4, 5, 6, 7]
Now assume the starting node is 1.S=[1] and G= [2, 3, 4, 5, 6, 7].Looking into the cost
adjacency matrix ,the minimum edge is determined( the vertex from 1 to which the
cost is minimum)
Min{<1,2>, <1,3>, <1,4>, <1,5>, <1,6>, <1,7>}
There is an edge to vertex 2 and 6 from 1.
=min{28,10}=10
So draw an edge from vertex 1 to vertex 6
1
10
6
Step 2: Now S=[1,6] and G=[ 2, 3, 4, 5, 7]
Find Min{<1,2>, <1,3>, <1,4>, <1,5>, <1,7>,<6,2>, <6,3>,<6,4>,<6,5>, <6,7>}
There is an edge from vertex 1 to vertex 2 and an edge from vertex 6 to 5.
=min{28,25}=25
1
1
25
5
4
22
Step 4: Now S=[1,6,5,4] and G=[ 2, 3, 7]
Find Min{<1,2>, <1,3>, <1,7>,<6,2>, <6,3>,
<6,7>,<5,2>,<5,3>,<5,7>,<4,2>,<4,3>,<4,7>}
There is an edge from vertex 5 to vertex 7 and an edge from vertex 4 to 3, 7.
=min{28,24,12,18}=12
1
10
6
3
25
12
5
4
22
1 2
10
16
6
3
25
12
5
4
22
=min{24,18,14}=14
So draw an edge from vertex 2 to vertex 7.
1 2
10 14
16
6 7
3
25
12
5
4
22
Result: The cost of minimum spanning tree is given by the sum of weights of the
selected edges.
i.e., 10 +25 + 22 + 12 +16 + 14 =99
Example 2:
Find the minimum spanning tree for the below graph using Prim’s Algorithm.
C a b c d e f
b - - 5 - 9 1
c - 5 - 3 1 2
d - - 3 - 14 7
e - 9 1 14 - -
f - 1 2 7 - -
Step1:
Initially S=0 and G=[b,c,d,e,f ]
Now assume the starting node is 1.S=[b] and G= [c,d,e,f].Looking into the cost
adjacency matrix ,the minimum edge is determined( the vertex from b to which the
cost is minimum)
Min{<b,c>, <b,d>, <b,e>, <b,f>}=Min{5, -, 9, 1}=1
There is an edge to vertex b and f with minimum cost of 1.
So draw an edge from vertex b to vertex f
Result: The cost of minimum spanning tree is given by the sum of weights of the
selected edges. Equal to 1+1+2+3=7
**********************