You are on page 1of 18

Greedy Method

Dr. Arup Kumar Pal


Department of Computer Science & Engineering
Indian Institute of Technology (ISM), Dhanbad
Jharkhand-826004
E-mail: arupkrpal@iitism.ac.in

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 1
Outline
Introduction
Knapsack problem
Huffman Coding
Minimum Spanning Tree
Kruskal's Algorithm
Prim's Algorithm

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 2
Introduction
A greedy algorithm typically consists of an iterative procedure
that seeks to find a local optimal solution.
In some cases, these local optimal solutions translate into
global optimal solutions.
As a result, the greedy method suggests that one can develop
an algorithm that operates in stages, considering one input at
a time.
At each stage, a decision is made regarding whether a
particular input belongs to the optimal solution.
This decision is made by considering the inputs in an order
determined by a selection procedure.
In this lecture, we will explore some of the most prominent
problems for which the greedy strategy provides an optimal
solution.

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 3
Approach

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 4
Fractional Knapsack problem

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 5
Contd…

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 6
Algorithm
FractionalKnapsack(items, W):
{ Sort items based on value-to-weight ratio in
descending order
total_Value = 0
remaining_Weight = W

for each item in items:


if item.weight <= remaining_Weight:
total_Value += item.value
remaining_Weight -= item.weight
else:
fraction = remaining_Weight/item.weight
total_Value += fraction * item.value
break
return total_Value
}
Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 7
Huffman Codeing
Key idea: Assign fewer bits to symbols that
occur more frequently and more bits to
symbols appear less often.
Procedure:
 Step 1: Sort the probability of all source symbols in
descending order.
 Step 2: Merge the last two into new symbol, add their
probabilties
 Step 3: Repeat Step 1 and Step 2 until one symbol is left
 Step4: Code assignment, traverse the tree from the root to
each leaf node

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 8
Example

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 9
Binary Huffman Coding

Data Structures and Algorithms Department of CSE, ISM Dhanbad November 10, 2023 10
Contd…
Encoding Total No of bits:50

Total No of bits:22

Decoding

Data Structures and Algorithms Department of CSE, ISM Dhanbad November 10, 2023 11
Algorithm

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 12
Minimum Spanning Tree
Kruskal's algorithm:
Sort the edges in G by nondecreasing weight.
For each edge in the sorted list, include that edge
in the spanning tree T if it does not form a cycle
with the edges currently included in T; otherwise
discard it.
This process will terminate after adding exactly n-1
edges.

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 13
Contd…

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 14
Contd…
Prim's Algorithm:
The algorithm grows the spanning tree starting from an
arbitrary vertex.
The algorithm begins by creating two sets of vertices:
X={1} and Y = {2; 3; …; n}.
It then grows a spanning tree, one edge at a time. At
each step, it finds an edge (x; y) of minimum weight,
where x ⋵ X and y ⋵ Y and moves y from Y to X.
This edge is added to the current minimum spanning
tree edges in T.
This step is repeated until Y becomes empty.

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 15
Contd…

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 16
Conclusions
Greedy algorithms are often straightforward to implement and
understand, making them an attractive choice for many
problems.
Selecting the right criterion for making greedy choices is critical.
The criterion can vary from problem to problem and may involve
selecting the maximum or minimum value, profit-to-weight
ratios, or other relevant measures.
Greedy algorithms do not backtrack; once a decision is made, it
is final.
Greedy algorithms are widely used in various real-world
applications, including Huffman coding for data compression,
Dijkstra's algorithm for shortest paths, Kruskal's algorithm for
minimum spanning trees etc.

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 17
Thank You

Data Structures and Algorithms Dept. of CSE, IIT(ISM) Dhanbad November 10, 2023 18

You might also like