Greedy Algorithm Overview and Applications
Greedy Algorithm Overview and Applications
The greedy method is one of the strategies like Divide and conquer used to solve the problems. This
method is used for solving optimization problems. An optimization problem is a problem that
demands either maximum or minimum results. Let's understand through some terms.
The Greedy method is the simplest and straightforward approach. It is not an algorithm, but it is a
technique. The main function of this approach is that the decision is taken on the basis of the currently
available information. Whatever the current information is present, the decision is made without
worrying about the effect of the current decision in future.
This technique is basically used to determine the feasible solution that may or may not be optimal.
The feasible solution is a subset that satisfies the given criteria. The optimal solution is the solution
which is the best and the most favorable solution in the subset. In the case of feasible, if more than
one solution satisfies the given criteria then those solutions will be considered as the feasible, whereas
the optimal solution is the best solution among all the solutions.
The above is the greedy algorithm. Initially, the solution is assigned with zero value. We pass the
array and number of elements in the greedy algorithm. Inside the for loop, we select the element one
by one and checks whether the solution is feasible or not. If the solution is feasible, then we perform
the union.
P:A→B
The problem is that we have to travel this journey from A to B. There are various solutions to go from
A to B. We can go from A to B by walk, car, bike, train, aeroplane, etc. There is a constraint in the
journey that we have to travel this journey within 12 hrs. If I go by train or aeroplane then only, I can
cover this distance within 12 hrs. There are many solutions to this problem but there are only two
solutions that satisfy the constraint.
If we say that we have to cover the journey at the minimum cost. This means that we have to travel
this distance as minimum as possible, so this problem is known as a minimization problem. Till
Prepared By Kapil Dev Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
now, we have two feasible solutions, i.e., one by train and another one by air. Since travelling by train
will lead to the minimum cost so it is an optimal solution. An optimal solution is also the feasible
solution, but providing the best result so that solution is the optimal solution with the minimum
cost. There would be only one optimal solution.
The problem that requires either minimum or maximum result then that problem is known as an
optimization problem. Greedy method is one of the strategies used for solving the optimization
problems.
Where Greedy Approach Fails
In many problems, Greedy algorithm fails to find an optimal solution, moreover it may produce a
worst solution. Problems like Travelling Salesman and Knapsack cannot be solved using this
approach.
Advantages:
Finding solution is quite easy with a greedy algorithm for a problem.
Analyzing the run time for greedy algorithms will generally be much easier than for other
techniques (like Divide and conquer).
We have to travel from the source to the destination at the minimum cost. Since we have three feasible
solutions having cost paths as 10, 20, and 5. 5 is the minimum cost path so it is the optimal solution.
This is the local optimum, and in this way, we find the local optimum at each stage in order to
calculate the global optimal solution.
The above graph can be represented as G(V, E), where 'V' is the number of vertices, and 'E' is the
number of edges. The spanning tree of the above graph would be represented as G`(V`, E`). In this
case, V` = V means that the number of vertices in the spanning tree would be the same as the number
of vertices in the graph, but the number of edges would be different. The number of edges in the
spanning tree is the subset of the number of edges in the original graph. Therefore, the number of
edges can be written as:
E` € E
E` = |V| - 1
V` = V
o The number of edges in the spanning tree would be equal to the number of edges minus 1.
E` = |V| - 1
o The spanning tree should not contain any cycle.
Example : consider the below graph: for all possible spanning tree
o In a complete graph, we can remove maximum (e-n+1) edges to construct a spanning tree.
The number of spanning trees that can be made from the above complete graph equals to nn-2 = 44-2 =
16.
The maximum number of edges that can be removed to construct a spanning tree equals to e-n+1 = 6
-4+1=3
Application of Minimum Spanning Tree
1. Consider n stations are to be linked using a communication network & laying of communication links
between any two stations involves a cost.
The ideal solution would be to extract a subgraph termed as minimum cost spanning tree.
2. Suppose you want to construct highways or railroads spanning several cities then we can use the
concept of minimum spanning trees.
3. Designing Local Area Networks.
4. Laying pipelines connecting offshore drilling sites, refineries and consumer markets.
5. Suppose you want to apply a set of houses with
o Electric Power
o Water
o Telephone lines
o Sewage lines
To reduce cost, you can connect houses with minimum cost spanning trees.
Methods of Minimum Spanning Tree
The weight of the edges of the above graph is given in the below table –
Edge AB AC AD AE BC CD DE
Weight 1 7 10 5 3 4 2
Now, sort the edges given above in the ascending order of their weights.
Edge AB DE BC CD AE AC AD
Weight 1 2 3 4 5 7 10
Step 2 - Add the edge DE with weight 2 to the MST as it is not creating the cycle.
Step 3 - Add the edge BC with weight 3 to the MST, as it is not creating any cycle or loop.
Step 4 - Now, pick the edge CD with weight 4 to the MST, as it is not forming the cycle.
Step 5 - After that, pick the edge AE with weight 5. Including this edge will create the cycle, so
discard it.
Step 6 - Pick the edge AC with weight 7. Including this edge will create the cycle, so discard it.
Step 7 - Pick the edge AD with weight 10. Including this edge will also create the cycle, so discard
it.
So, the final minimum spanning tree obtained from the given weighted graph by using Kruskal's
algorithm is -
Step 1 - First, we have to choose a vertex from the above graph. Let's choose B.
Step 2 - Now, we have to choose and add the shortest edge from vertex B. There are two edges
from vertex B that are B to C with weight 10 and edge B to D with weight 4. Among the edges, the
edge BD has the minimum weight. So, add it to the MST.
Step 3 - Now, again, choose the edge with the minimum weight among all the other edges. In this
case, the edges DE and CD are such edges. Add them to MST and explore the adjacent of C, i.e., E
and A. So, select the edge DE and add it to the MST.
Step 4 - Now, select the edge CD, and add it to the MST.
Step 5 - Now, choose the edge CA. Here, we cannot select the edge CE as it would create a cycle to
the graph. So, choose the edge CA and add it to the MST.
Algorithm
1. Step 1: Select a starting vertex
2. Step 2: Repeat Steps 3 and 4 until there are fringe vertices
3. Step 3: Select an edge 'e' connecting the tree vertex and fringe vertex that has minimum weig
ht
4. Step 4: Add the selected edge and the vertex to the minimum spanning tree T
5. [END OF LOOP]
6. Step 5: EXIT
Examples 2: Find the minimum spanning tree using prims method (greedy approach) for the graph
given below with S as the arbitrary root.
Solution
Step 1
Create a visited array to store all the visited vertices into it.
V={}
The arbitrary root is mentioned to be S, so among all the edges that are connected to S we need to
find the least cost edge.
S → B = 8 V = {S, B}
Since B is the last visited, check for the least cost edge that is connected to the vertex B.
B→A=9
B → C = 16
B → E = 14
V = {S, B, A}
Step 3
Since A is the last visited, check for the least cost edge that is connected to the vertex A.
A → C = 22
A→B=9
A → E = 11
But A → B is already in the spanning tree, check for the next least cost edge. Hence, A → E is
added to the spanning tree.
V = {S, B, A, E}
Since E is the last visited, check for the least cost edge that is connected to the vertex E.
E → C = 18
E→D=3
V = {S, B, A, E, D}
Step 5
Since D is the last visited, check for the least cost edge that is connected to the vertex D.
D → C = 15
E→D=3
V = {S, B, A, E, D, C}
Huffman Codes
o (i) Data can be encoded efficiently using Huffman Codes.
o (iii) Huffman's greedy algorithm uses a table of the frequencies of occurrences of each character to
build up an optimal way of representing each character as a binary string.
Suppose we have 105 characters in a data file. Normal Storage: 8 bits per character (ASCII) - 8 x
105 bits in a file. But we want to compress the file and save it compactly. Suppose only six characters
appear in the file:
(i) Fixed length Code: Each letter represented by an equal number of bits. With a fixed length code,
at least 3 bits per character:
For example:
a 000
b 001
c 010
d 011
e 100
f 101
(ii) A variable-length code: It can do considerably better than a fixed-length code, by giving many
characters short code words and infrequent character long codewords.
b 101
c 100
d 111
e 1101
f 1100
Number of bits = (45 x 1 + 13 x 3 + 12 x 3 + 16 x 3 + 9 x 4 + 5 x 4) x 1000
= 2.24 x 105bits
Thus, 224,000 bits to represent the file, a saving of approximately 25%.This is an optimal character
code for this file.
Prefix Codes:
The prefixes of an encoding of one character must not be equal to complete encoding of another
character, e.g., 1100 and 11001 are not valid codes because 1100 is a prefix of some other code word
is called prefix codes.
Prefix codes are desirable because they clarify encoding and decoding. Encoding is always simple
for any binary character code; we concatenate the code words describing each character of the file.
Decoding is also quite comfortable with a prefix code. Since no codeword is a prefix of any other,
the codeword that starts with an encoded data is unambiguous.
The algorithm builds the tree T analogous to the optimal code in a bottom-up manner. It starts with
a set of |C| leaves (C is the number of characters) and performs |C| - 1 'merging' operations to create
Prepared By Kapil Dev Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
the final tree. In the Huffman algorithm 'n' denotes the quantity of a set of characters, z indicates the
parent node, and x & y are the left & right child of z respectively.
Huffman Coding Algorithm
Data may be compressed using the Huffman Coding technique to become smaller without losing
any of its information. After David Huffman, who created it in the beginning? Data that contains
frequently repeated characters is typically compressed using Huffman coding.
A well-known Greedy algorithm is Huffman Coding. The size of code allocated to a character relies
on the frequency of the character, which is why it is referred to be a greedy algorithm. The short-
length variable code is assigned to the character with the highest frequency, and vice versa for
characters with lower frequencies. It employs a variable-length encoding, which means that it gives
each character in the provided data stream a different variable-length code.
Prefix Rule
Essentially, this rule states that the code that is allocated to a character shall not be another code's
prefix. If this rule is broken, various ambiguities may appear when decoding the Huffman tree that
has been created.
Let's look at an illustration of this rule to better comprehend it: For each character, a code is
provided, such as:
1. a-0
2. b-1
3. c - 01
Assuming that the produced bit stream is 001, the code may be expressed as follows when decoded:
0 0 1 = aab
0 01 = ac
What is the Huffman Coding process?
The Huffman Code is obtained for each distinct character in primarily two steps:
o Create a Huffman Tree first using only the unique characters in the data stream provided.
o Second, we must proceed through the constructed Huffman Tree, assign codes to the
characters, and then use those codes to decode the provided text.
Steps to Take in Huffman Coding
The steps used to construct the Huffman tree using the characters provided
1. Input:
2. string str = "abbcdbccdaabbeeebeab"
If Huffman Coding is employed in this case for data compression, the following information must
be determined for decoding:
o For each character, the Huffman Code
o Huffman-encoded message length (in bits), average code length
o Utilizing the formulas covered below, the final two of them are discovered.
Step 2: Obtain two minimum frequency nodes from the min heap in step two. Add a third internal
node, frequency 2 + 3 = 5, which is created by joining the two extracted nodes.
o Now, there are 4 nodes in the min-heap, 3 of which are the roots of trees with a single element each,
and 1 of which is the root of a tree with two elements.
Step 3: Get the two minimum frequency nodes from the heap in a similar manner in step three.
Additionally, add a new internal node formed by joining the two extracted nodes; its frequency in
the tree should be 4 + 4 = 8.
o Now that the minimum heap has three nodes, one node serves as the root of trees with a single element
and two heap nodes serve as the root of trees with multiple nodes.
Step 4: Get the two minimum frequency nodes in step four. Additionally, add a new internal node
formed by joining the two extracted nodes; its frequency in the tree should be 5 + 7 = 12.
o When creating a Huffman tree, we must ensure that the minimum value is always on the left side and
that the second value is always on the right side. Currently, the image below shows the tree that has
formed:
Step 5: Get the following two minimum frequency nodes in step 5. Additionally, add a new internal
node formed by joining the two extracted nodes; its frequency in the tree should be 12 + 8 = 20.
Continue until all of the distinct characters have been added to the tree. The Huffman tree created for
the specified cast of characters is shown in the above image.
Now, for each non-leaf node, assign 0 to the left edge and 1 to the right edge to create the code for
each letter.
Rules to follow for determining edge weights:
o We should give the right edges weight 1 if you give the left edges weight 0.
o If the left edges are given weight 1, the right edges must be given weight 0.
o Any of the two aforementioned conventions may be used.
o However, follow the same protocol when decoding the tree as well.
Following the weighting, the modified tree is displayed as follows:
a 4 01
b 7 11
c 3 101
d 2 100
e 4 00
Where 𝑓𝑖 = frequency,
𝑙𝑖 =code length
Example: Find an optimal Huffman Code for the following set of frequencies
a: 50 b: 25 c: 15 d: 40 e: 75
Solution:
Example: 3
Huffman’s greedy algorithm uses a table giving how often each character occurs
(i.e., its frequency) to build up an optimal way of representing each character as a binary
string.
Suppose we have a 100,000-character data file that we wish to store compactly.
We observe that the characters in the file occur with the frequencies given by following table.
That is, only 6 different characters appear, and the character a occurs 45,000 times.
Characters a b c d e f
Frequency (in
thousand) 45 13 12 16 9 5
Solution:
The steps of Huffman’s algorithm for the frequencies given above as step no. 1 to 6.
Each step shows the contents of the queue sorted into increasing order by frequency.
At each step, the two trees with lowest frequencies are merged.
Leaves are shown as rectangles containing a character and its frequency.
Internal nodes are shown as circles containing the sum of the frequencies of their children.
An edge connecting an internal node with its children is labeled 0 if it is an edge to a left
child and 1 if it is an edge to a right child.
The code-word for a letter is the sequence of labels on the edges connecting the root to the leaf
Step-2: Step-3:
Step-4: Step-5:
Step-6:
Frequency (in a b c d e f
thousand) 45 13 12 16 9 5
Huffman code-
0 101 100 111 1101 1100
word
27
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Here, d[a] and d[b] denotes the shortest path estimate for vertices a and b respectively
from the source vertex ‘S’.
Now,
If d[a] + w < d[b]
then d[b] = d[a] + w and Π[b] = a
This is called as edge relaxation.
28
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Dijkstra Algorithm
Dijkstra algorithm is a single-source shortest path algorithm. Here, single-source means
that only one source is given, and we have to find the shortest path from the source to all
the nodes.
S ← ∅ // The set of vertices that have been visited 'S' is initially empty
do u ← mindistance (Q, dist) // A vertex from Q with the least distance is selected
S ← S ∪ {u} // Vertex 'u' is added to 'S' list of vertices that have been visited
for all v ∈ neighbors[u] // For all the neighboring vertices of vertex 'u'
then dist[v] ← dist[u] + w(u,v) // The new value of the shortest path is selected
return dist
Let's understand the working of Dijkstra's algorithm. Consider the below graph.
29
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
First, we have to consider any vertex as a source vertex. Suppose we consider vertex 0 as
a source vertex.
Here we assume that 0 as a source vertex, and distance to all the other vertices is infinity.
Initially, we do not know the distances. First, we will find out the vertices which are
directly connected to the vertex 0. As we can observe in the above graph that two vertices
are directly connected to vertex 0.
Let's assume that the vertex 0 is represented by 'x' and the vertex 1 is represented by 'y'.
The distance between the vertices can be calculated by using the below formula:
= (0 + 4) < ∞
=4<∞
30
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Therefore, we come to the conclusion that the formula for calculating the distance
between the vertices:
= (0 + 8) < ∞
=8<∞
Therefore, the value of d(y) is 8. We replace the infinity value of vertices 1 and 4 with
the values 4 and 8 respectively. Now, we have found the shortest path from the vertex 0
to 1 and 0 to 4. Therefore, vertex 0 is selected. Now, we will compare all the vertices
except the vertex 0. Since vertex 1 has the lowest value, i.e., 4; therefore, vertex 1 is
selected.
Since vertex 1 is selected, so we consider the path from 1 to 2, and 1 to 4. We will not
consider the path from 1 to 0 as the vertex 0 is already selected.
First, we calculate the distance between the vertex 1 and 2. Consider the vertex 1 as 'x',
and the vertex 2 as 'y'.
= (4 + 8) < ∞
= 12 < ∞
Now, we calculate the distance between the vertex 1 and vertex 4. Consider the vertex 1
as 'x' and the vertex 4 as 'y'.
= (4 + 11) < 8
= 15 < 8
Since 15 is not less than 8, we will not update the value d(4) from 8 to 12.
31
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Till now, two nodes have been selected, i.e., 0 and 1. Now we have to compare the nodes
except the node 0 and 1. The node 4 has the minimum distance, i.e., 8. Therefore, vertex
4 is selected.
Since vertex 4 is selected, so we will consider all the direct paths from the vertex 4. The
direct paths from vertex 4 are 4 to 0, 4 to 1, 4 to 8, and 4 to 5. Since the vertices 0 and 1
have already been selected so we will not consider the vertices 0 and 1. We will consider
only two vertices, i.e., 8 and 5.
First, we consider the vertex 8. First, we calculate the distance between the vertex 4 and
8. Consider the vertex 4 as 'x', and the vertex 8 as 'y'.
= (8 + 7) < ∞
= 15 < ∞
Since 15 is less than the infinity so we update d(8) from infinity to 15.
Now, we consider the vertex 5. First, we calculate the distance between the vertex 4 and
5. Consider the vertex 4 as 'x', and the vertex 5 as 'y'.
= (8 + 1) < ∞
=9<∞
Till now, three nodes have been selected, i.e., 0, 1, and 4. Now we have to compare the
nodes except the nodes 0, 1 and 4. The node 5 has the minimum value, i.e., 9. Therefore,
vertex 5 is selected.
Since the vertex 5 is selected, so we will consider all the direct paths from vertex 5. The
direct paths from vertex 5 are 5 to 8, and 5 to 6.
First, we consider the vertex 8. First, we calculate the distance between the vertex 5 and
8. Consider the vertex 5 as 'x', and the vertex 8 as 'y'.
= (9 + 15) < 15
= 24 < 15
32
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Since 24 is not less than 15 so we will not update the value d(8) from 15 to 24.
Now, we consider the vertex 6. First, we calculate the distance between the vertex 5 and
6. Consider the vertex 5 as 'x', and the vertex 6 as 'y'.
= (9 + 2) < ∞
= 11 < ∞
Till now, nodes 0, 1, 4 and 5 have been selected. We will compare the nodes except the
selected nodes. The node 6 has the lowest value as compared to other nodes. Therefore,
vertex 6 is selected.
Since vertex 6 is selected, we consider all the direct paths from vertex 6. The direct paths
from vertex 6 are 6 to 2, 6 to 3, and 6 to 7.
First, we consider the vertex 2. Consider the vertex 6 as 'x', and the vertex 2 as 'y'.
= (11 + 4) < 12
= 15 < 12
Since 15 is not less than 12, we will not update d(2) from 12 to 15
Now we consider the vertex 3. Consider the vertex 6 as 'x', and the vertex 3 as 'y'.
= 25 < ∞
Now we consider the vertex 7. Consider the vertex 6 as 'x', and the vertex 7 as 'y'.
= 22 < ∞
33
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Till now, nodes 0, 1, 4, 5, and 6 have been selected. Now we have to compare all the
unvisited nodes, i.e., 2, 3, 7, and 8. Since node 2 has the minimum value, i.e., 12 among
all the other unvisited nodes. Therefore, node 2 is selected.
Since node 2 is selected, so we consider all the direct paths from node 2. The direct paths
from node 2 are 2 to 8, 2 to 6, and 2 to 3.
First, we consider the vertex 8. Consider the vertex 2 as 'x' and 8 as 'y'.
= (12 + 2) < 15
= 14 < 15
Now, we consider the vertex 6. Consider the vertex 2 as 'x' and 6 as 'y'.
= (12 + 4) < 11
= 16 < 11
Since 16 is not less than 11 so we will not update d(6) from 11 to 16.
Now, we consider the vertex 3. Consider the vertex 2 as 'x' and 3 as 'y'.
= (12 + 7) < 25
= 19 < 25
Till now, nodes 0, 1, 2, 4, 5, and 6 have been selected. We compare all the unvisited
nodes, i.e., 3, 7, and 8. Among nodes 3, 7, and 8, node 8 has the minimum value. The
nodes which are directly connected to node 8 are 2, 4, and 5. Since all the directly
connected nodes are selected so we will not consider any node for the updation.
The unvisited nodes are 3 and 7. Among the nodes 3 and 7, node 3 has the minimum
value, i.e., 19. Therefore, the node 3 is selected. The nodes which are directly connected
34
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
to the node 3 are 2, 6, and 7. Since the nodes 2 and 6 have been selected so we will
consider these two nodes.
Now, we consider the vertex 7. Consider the vertex 3 as 'x' and 7 as 'y'.
= (19 + 9) < 21
= 28 < 21
Since 28 is not less than 21, so we will not update d(7) from 28 to 21.
Problem-
Using Dijkstra’s Algorithm, find the shortest distance from source vertex ‘S’ to remaining
vertices in the following graph-
Solution-
Step-01:
Unvisited set : {S , a , b , c , d , e}
Visited set : { }
35
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-02:
The two variables Π and d are created for each vertex and initialized as-
Π[S] = Π[a] = Π[b] = Π[c] = Π[d] = Π[e] = NIL
d[S] = 0
d[a] = d[b] = d[c] = d[d] = d[e] = ∞
Step-03:
Now,
d[S] + 1 = 0 + 1 = 1 < ∞
∴ d[a] = 1 and Π[a] = S
d[S] + 5 = 0 + 5 = 5 < ∞
∴ d[b] = 5 and Π[b] = S
Step-04:
36
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Now,
d[a] + 2 = 1 + 2 = 3 < ∞
∴ d[c] = 3 and Π[c] = a
d[a] + 1 = 1 + 1 = 2 < ∞
∴ d[d] = 2 and Π[d] = a
d[b] + 2 = 1 + 2 = 3 < 5
∴ d[b] = 3 and Π[b] = a
37
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-05:
Now,
d[d] + 2 = 2 + 2 = 4 < ∞
∴ d[e] = 4 and Π[e] = d
38
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-06:
Now,
d[b] + 2 = 3 + 2 = 5 > 2
∴ No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
Now, the sets are updated as-
Unvisited set : {c , e}
Visited set : {S , a , d , b}
Step-07:
Now,
d[c] + 1 = 3 + 1 = 4 = 4
∴ No change
After edge relaxation, our shortest path tree remains the same as in Step-05.
39
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-08:
Now,
All vertices of the graph are processed.
Our final shortest path tree is as shown below.
It represents the shortest path from source vertex ‘S’ to all other remaining vertices.
40
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Knapsack Problem-
The fractional knapsack problem is also one of the techniques which are used to solve the
knapsack problem. In fractional knapsack, the items are broken in order to maximize the
profit. The problem in which we break the item is known as a Fractional knapsack
problem.
This problem can be solved with the help of using two techniques:
o Brute-force approach: The brute-force approach tries all the possible solutions with
all the different fractions but it is a time-consuming approach.
Fractional knapsack problem is solved using greedy method in the following steps-
Step-01:
Step-02:
Arrange all the items in decreasing order of their value / weight ratio.
Step-03:
Start putting the items into the knapsack beginning from the item with the highest ratio.
42
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
4. Initialize a variable i to 1
6. Do the following
7. Return the array, which specifies the fraction of each item to include in the knapsack
while maximizing the total value.
Time Complexity-
The main time taking step is the sorting of all items in decreasing order of their value /
weight ratio.
If the items are already arranged in the required order, then while loop takes O(n) time.
The Fractional Knapsack problem assumes that items can be divided into fractions, which
may not always be practical or realistic. 15 kg gold powder is hard to get in the above
example.
4. Farmers can apply the algorithm to optimize crop planting and resource allocation for
maximum yield and profit.
Find the optimal solution for the fractional knapsack problem making use of greedy
approach. Consider-
n=5
w = 60 kg
(w1, w2, w3, w4, w5) = (5, 10, 15, 22, 25)
(b1, b2, b3, b4, b5) = (30, 40, 45, 77, 90)
OR
A thief enters a house for robbing it. He can carry a maximal weight of 60 kg into his bag.
There are 5 items in the house with the following weights and values. What items should
thief take if he can even take the fraction of any item with him?
45
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-02:
Sort all the items in decreasing order of their value / weight ratio-
I1 I2 I5 I4 I3
6 4 3.6 3.5 3
Step-03:
Start filling the knapsack by putting the items into it one by one.
60 Ø 0
55 I1 30
45 I1, I2 70
Now,
Knapsack weight left to be filled is 20 kg but item-4 has a weight of 22 kg.
Since in fractional knapsack problem, even the fraction of any item can be taken.
So, knapsack will contain the following items-
< I1 , I2 , I5 , (20/22) I4 >
46
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
I1 9 2 4.5
I7 3 1 3
I6 16 9 1.78
I2 5 5 1
I4 7 11 0.64
I3 2 6 0.33
Example 3
Suppose you have a knapsack with a weight capacity (w) of 15 Kgs and the following
items:
47
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Let’s use the Fractional Knapsack Algorithm to determine the best combination of items
for the knapsack.
Calculate value-to-weight ratios
It cannot be fully added to the knapsack, but a fraction of 1 kg is added and the
knapsack’s current weight becomes 14 + 1 = 15 Kgs.
1 kg value = 200/10 = 20
The total value is updated to 300 + 20 = 320.
Knapsack remaining weight = 1 – 1 = 0 Kg
The knapsack is now full, and the total value is 320.
The Optimal Merge Pattern (OMP) problem deals with minimizing the total
computational cost of merging multiple sorted files (or data lists) into a single sorted
file. It is a classic Greedy Algorithm problem where the optimal solution is built step
by step by always choosing the best (minimum-cost) pair to merge at each stage.
Optimal merge pattern is a pattern that relates to the merging of two or more sorted
files in a single sorted file. This type of merging can be done by the two-way merging
method.
If we have two sorted files containing n and m records respectively then they could be
merged together, to obtain one sorted file in time O (n+m).
There are many ways in which pairwise merge can be done to get a single sorted file.
Different pairings require a different amount of computing [Link] main thing is to
pairwise merge the n sorted files so that the number of comparisons will be less.
The formula of external merging cost is:
Where, f (i) represents the number of records in each file and d (i) represents the depth.
Problem Definition
Given n files with sizes:
f1, f2, f3, ..., fn
Each time we merge two files, say A and B, the cost of merging is:
Cost = size(𝐴) + size(𝐵)
49
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
After merging, the new file (of size A+B) replaces the two original files, and the process
continues until only one file remains.
The objective is to minimize the total cost of all merges.
Time Complexity
Insertion and extraction from a min-heap take 𝑂(log𝑛)time.
For n files, there are n-1 merge operations.
Total Time Complexity = 𝑂(𝑛log𝑛)
Applications
Data compression (Huffman Coding)
Optimal merge of sorted lists or database files
External sorting (multi-way merge sort)
Step 1: Insert 2, 3
Step 2:
Step 3: Insert 5
Step 4: Insert 13
Step 6:
51
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Examples 2 :
Let us consider the given files, f1, f2, f3, f4 and f5 with 20, 30, 10, 5 and 30 number of elements
respectively.
If merge operations are performed according to the provided sequence, then
M1 = merge f1 and f2 => 20 + 30 = 50
M2 = merge M1 and f3 => 50 + 10 = 60
M3 = merge M2 and f4 => 60 + 5 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Hence, the total number of operations is
50 + 60 + 65 + 95 = 270
Now, the question arises is there any better solution?
Sorting the numbers according to their size in an ascending order, we get the following sequence −
f4, f3, f1, f2, f5
Hence, merge operations can be performed on this sequence
M1 = merge f4 and f3 => 5 + 10 = 15
M2 = merge M1 and f1 => 15 + 20 = 35
M3 = merge M2 and f2 => 35 + 30 = 65
M4 = merge M3 and f5 => 65 + 30 = 95
Therefore, the total number of operations is
15 + 35 + 65 + 95 = 210
Obviously, this is better than the previous one.
52
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
In this context, we are now going to solve the problem using this algorithm.
Initial Set
Step 1
Step 2
Step 3
Step 4
Problem Statement
You are given n jobs, each having:
Ji → Job ID
di → Deadline (the latest time unit by which the job must be completed)
pi → Profit earned if the job is completed before or on its deadline
Each job takes one unit of time, and only one job can be scheduled at a time.
Goal:
Schedule the jobs so that:
Each job is completed before or on its deadline
The total profit is maximum
Characteristics
Each job takes unit time
No job can be executed after its deadline
If multiple jobs are available at the same time, pick the most profitable one
Approach to Solution-
A feasible solution would be a subset of jobs where each job of the subset gets completed within
its deadline.
Value of the feasible solution would be the sum of profit of all the jobs contained in the subset.
An optimal solution of the problem would be a feasible solution which gives the maximum
profit.
Greedy Strategy
Sort all jobs in decreasing order of profit, and assign each job to the latest available slot before
its deadline.
The greedy algorithm described below always gives an optimal solution to the job sequencing
problem-
Step-01:
Sort all the given jobs in decreasing order of their profit.
Step-02:
Step-03:
Algorithm
Step-by-Step (Greedy Approach):
1. Sort all jobs in decreasing order of profit.
2. Find the maximum deadline (D).
3. Create an array slots[D] to keep track of free time slots (initialized to -1).
4. For each job (in sorted order):
o Find the latest available slot before its deadline.
o If the slot is free, schedule the job there.
5. Calculate the total profit and print the job sequence.
Pseudocode
JOB_SEQUENCING(jobs[], n):
sort(jobs) by decreasing profit
max_deadline = maximum of all [Link]
create slot[1..max_deadline] and initialize to -1
total_profit = 0
for i = 1 to n:
for j = min(max_deadline, jobs[i].deadline) downto 1:
if slot[j] is empty:
slot[j] = jobs[i].id
total_profit += jobs[i].profit
break
Problem-
Jobs J1 J2 J3 J4 J5 J6
Deadlines 5 3 3 2 4 2
55
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-02:
Now,
We take each job one by one in the order they appear in Step-01.
We place the job on Gantt chart as far as possible from 0.
Step-03:
We take job J4.
Since its deadline is 2, so we place it in the first empty cell before deadline 2 as-
Step-04:
We take job J1.
Since its deadline is 5, so we place it in the first empty cell before deadline 5 as-
Step-05:
We take job J3.
Since its deadline is 3, so we place it in the first empty cell before deadline 3 as-
56
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step-06:
Step-07:
Now,
The only job left is job J6 whose deadline is 2.
All the slots before deadline 2 are already occupied.
Thus, job J6 can not be completed.
Part-02:
Part-03:
57
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Example 2 : Find the optimal solution for the instance n=4, (P1, P2, P3, P4) =(70,12,18,35), (d1, d2, d3,
d4)=(2,1,2,1) using Job Sequencing Algorithm.
Step 1: Sort all the given jobs in decreasing order based on their profit
Step 3: Let us take Job J4. If we look at job J4 it has a deadline 1. This means that we have to complete job
J4 on or before time slot 1 in order to earn its profit. Since its deadline is 1, so we place it in the first empty
cell before deadline 1 as shown in the figure given below:
The optimal schedule is- J4 , [Link] is the sequence in which the jobs must be completed in order to obtain
the maximum profit.
Maximum earned profit is 35+70=105
Example 2: Find the optimal solution for the instance n=6, (P 1, P2, P3, P4 ,P5, P6)
=(200,180,190,300,120,100), (d1, d2, d3, d4, d5, d6)=(5,3,3,2,4,2) using Job Sequencing Algorithm.
Step 1: Sort all the given jobs in decreasing order based on their profit
58
Prepared By Kapil Raghuwanshi
The ICFAI University Jaipur
Design and Analysis of Algorithm
First Semester: 2025–2026
Step 3: Let us take Job J1. If we look at job J1 it has a deadline 5. This means that we have to
complete job J1 on or before time slot 5 in order to earn its profit. Since its deadline is 5, so we
place it in the first empty cell before deadline 5 as shown in the figure given below:
Step 4: Let us take Job J3. If we look at job J3 it has a deadline 3. This means that we have to
complete job J3 on or before time slot 3 in order to earn its profit. Since its deadline is 3, so we
place it in the first empty cell before deadline 3 as shown in the figure given below:
Step 5: Let us take Job J2. If we look at job J2 it has a deadline 3. This means that we have to
complete job J2 on or before time slot 3 in order to earn its profit. Since its deadline is 3, so we
place it in the first empty cell before deadline 3 as shown in the figure given below:
Step 6: Let us take Job J5. If we look at job J5 it has a deadline 4. This means that we have to
complete job J5 on or before time slot 4 in order to earn its profit. Since its deadline is 4, so we
place it in the first empty cell before deadline 4 as shown in the figure given below:
The optimal schedule is- J2 , J4 J3 , J5 , J1 .This is the required order in which the jobs must be
completed in order to obtain the maximum profit.
Maximum earned profit is 180 + 300 + 190 + 120 + 200 = 990
59
Prepared By Kapil Raghuwanshi