SlideShare a Scribd company logo
Design & Analysis of Algorithms
Greedy Algorithms
Presented By
22,24,25,26,27,28,29,31,32,34,37,38,40
BSCS-E122
What Are Greedy Techqniques?
Greedy Techniques
● Definition: Greedy algorithms make
locally optimal choices at each step,
aiming to reach a global optimum
solution.
● In simple words, a greedy algorithm is
a simple technique that solves a
problem by selecting the best option at
each step without considering long-
term implication..
Characteristics of Greedy Algorithm
• Locally optimal choices: Decisions made based on immediate
information.
• No backtracking: Once a decision is made, it's never
reconsidered.
• Greedy choice property: Optimal solution can be reached by
making a series of greedy choices.
Greedy Choice Property – (Core of G.A)
Explanation of the Greedy Choice Property:
• At each step, the optimal choice is made without considering
future consequences.
• This choice doesn't depend on previous decisions.
“It means Greedy algorithm always selecting the best option
available at each phase, even if the final decision is wrong.”
• Also, it does not guarantee the best overall solution.
Real Life Example
One real-life example of a greedy algorithm is the process of
making change.
Vending Machine Change Dispensing:
“Vending Machine Change Dispensing is the process by
which a vending machine provides the correct amount of
change”
Working of Vending Machine
Problem Statement:
Scenario: Making change for 73 rupees.
Coin available: 1 rupee, 5 rupees, 10 rupees, 25 rupees.
Objective: Minimize the number of coins issued while providing
correct change.
Greedy Algorithm Approach:
Prioritize dispensing the highest denomination coin that is smaller than or
equal to the required change amount.
25
25 1
5
10
Coins
25 25 = 50
Greedy Algorithm Approach:
25 25 = 60
10
25 25 = 70
10 10
25 25 = 73
10 10 1 1 1
•Total coins provided: 7.
•Achieved optimal solution with minimum coins.
Algorithm Of Greedy Technique
• Fractional Knapsack
• Dijkstra’s algorithm
• Kruskal’s algorithm
• Huffman coding
Fractional Knapsack
The basic idea of the greedy approach is to calculate the ratio profit/weight for each item
and sort the item on the basis of this ratio. Then take the item with the highest ratio and
add them as much as we can (can be the whole element or a fraction of it).
This will always give the maximum profit because, in each step it adds an element such
that this is the maximum possible profit for that much weight.
Problem
Given the weights and profits of N items, in the form of {profit, weight} put these items in a
knapsack of capacity W to get the maximum total profit in the knapsack. In Fractional
Knapsack, we can break items for maximizing the total value of the knapsack.
For Example, in this example we have to determine the best combination we can take to
maximize both profit and wieght whereas the max load of the knapsack is 20.
Keep this in mind we are to make such a combination which allows us to optimize our
solution.
Fractional Knapsack Cont.
Greedy About Weight
Greedy About Profit
OBJ1
18
Objects OBJ1 OBJ2 OBJ3
Profit 25 24 15
Weight 18 15 10
25+2/15*24
=28.2
OBJ3
10
15+10/15*24
=31
P/W Ratio
OBJ2
15
24+5/10*15
=31.5
Dijkstra’s algorithm
Dijkstra's algorithm is a graph search algorithm that finds the shortest path
between nodes in a weighted graph. It starts from a source node and
iteratively explores the neighboring nodes, updating the shortest distance to
each node as it goes.
1 2 3
20 10
40
Dijkstra’s algorithm
The real-life examples of the Dijkstra Algorithm are Google Maps, Social networking, and
DNA mapping. All these things are represented in the form of graphs using Nodes and
edges. To reach from one single source to multiple sources ( Nodes ) using the shortest
path, then we use Dijkstra Algorithm. It is also known as the minimization algorithm and it
uses a greedy approach and provides optimal solution.
Relaxation:
If d(u) + d(u+v) < d(v)
d(v) = d(u) + c(u+v)
Huffman Coding
Huffman coding is a method used in computer science and information theory to
compress data efficiently. It works by assigning variable-length codes to input
characters based on their frequency of occurrence in the data.
Steps Of Huffman Algorithm are following.
1. Frequency Count: Calculate the frequency of each character in the input
data.
2. Priority Queue: Create a priority queue or min-heap based on the
frequencies, where nodes with lower frequencies have higher priority.
3. Build Huffman Tree:
While there is more than one node in the queue:
• Remove the two nodes with the lowest frequency from the queue.
• Create a new internal node with a frequency equal to the sum of the
frequencies of the two nodes.
• Set the two removed nodes as children of the new node.
• Add the new node back to the queue.
• The remaining node in the queue is the root of the Huffman tree.
Huffman Coding Cont
4.Generate Huffman Codes:
Traverse the Huffman tree from the root.
Assign '0' to the left child edge and '1' to the right child edge.
When reaching a leaf node (character node), the path from the root to that node represents
the Huffman code for that character.
5.Encode Data:
Replace each input character with its corresponding Huffman code to create the
compressed data.
6.Decoding:
Start from the root of the Huffman tree.
Read each bit from the compressed data.
Traverse down the tree following the '0' path for 'left' and '1' path for 'right' until reaching a
leaf node.
Output the character represented by the leaf node and go back to the root to continue
decoding the next bits.
Huffman Coding Cont 2
a=50, c=30, b=10, d=5,
e=3, f=2
2
f e
3
5
5
d
10
10
b
20 30
50
100
c
M is the message and consist of 100 characters
with the alphabets given above.
We know messages are coded in 8-bit ASCII codes.
For 100 characters the code will be of 7*100 bits =
700 bits.
Our Goal is to minimize this using huffman coding.
Solution
a = 0 1*50 = 50
b=100 3*10 = 30
c=11 2*30 = 60
d=1010 4*5 = 20
e=10111 5*3 = 15
f=10110 5*2 = 10
TOTAL 185bits
50
a
0
0
0
0
0 1
1
1
1
1
Kruskal’s ALgorithm :
• Construct Min Heap with edges .
• Take one by one edge and add in spinning tree .
( Cycle should not be created )
Best Case ( n - 1 ) edges
Worst Case ‘ e ’ edges
A
B
C
D
F
G
E
5
4
3
6 6
6
5
6
2
4
4
3
Example
Solution :
A
B
C
D
F
G
E
4
3
5
2
4
3
2
1
n - 1 => 7 -
1 => 6
Time Complexity of Kruskal’s ALgorithm :
• In Best Case , ( n - 1 ) log e .
• In Worst Case , e log e .
Real Life Applications
Airline Ticket Booking Systems: Systems used by airlines to optimize seat
allocation and pricing often use greedy algorithms. For example, when allocating
seats, the system may prioritize filling higher-priced seats first before moving on
to lower-priced ones.
Data Compression: Greedy algorithms play a role in data compression techniques
such as Huffman coding. This technique assigns shorter codes to more frequent
symbols, reducing the overall size of the data.
Google Maps: Google Maps uses greedy algorithms, particularly Dijkstra's
algorithm, to find the shortest routes between locations. This is crucial for
providing accurate and efficient navigation directions to users.
CREDITS: This presentation template was
created by Slidesgo, and includes icons by
Flaticon, and infographics & images by Freepik
Thanks!
Do you have any questions?

More Related Content

PDF
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
PDF
A greedy algorithms
PPTX
Greedyalgorithm
PPT
36 greedy
PPT
Greedy Algorihm
PPT
Greedy Algorithm
PPTX
Comparitive Analysis of Algorithm strategies
PPT
Greedy algorithm pptxe file for computer
Lec07-Greedy Algorithms.pdf Lec07-Greedy Algorithms.pdf
A greedy algorithms
Greedyalgorithm
36 greedy
Greedy Algorihm
Greedy Algorithm
Comparitive Analysis of Algorithm strategies
Greedy algorithm pptxe file for computer

Similar to greedy algorithm.pptx good for understanding (20)

PPT
Chapter 17
PPTX
Introduction to the Greedy Algorithms - primer
PPTX
Presentation on Greedy Algorithm
PDF
Daa chapter4
PDF
Analysis and Design of Algorithms notes
PPS
Greedy Algorithms with examples' b-18298
PPTX
7. Algorithm Design and analysis ppt.pptx
PPTX
Applied Algorithms Introduction to Algorithms.pptx
PPT
16_Greedy_Algorithms presentation paradigm
PDF
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
PPT
16_Greedy_Algorithms.ppt
PPT
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
PPT
16_Greedy_Algorithms.ppt
PDF
Module 2 - Greedy Algorithm Data structures and algorithm
PDF
heuristic search Techniques and game playing.pdf
PPTX
Huffman analysis
PDF
Greedy Algorithms in Algorithms and Design
PPTX
Greedy Method unit-2(Design and analysis of algorithms).pptx
PDF
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
PPTX
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Chapter 17
Introduction to the Greedy Algorithms - primer
Presentation on Greedy Algorithm
Daa chapter4
Analysis and Design of Algorithms notes
Greedy Algorithms with examples' b-18298
7. Algorithm Design and analysis ppt.pptx
Applied Algorithms Introduction to Algorithms.pptx
16_Greedy_Algorithms presentation paradigm
Mastering Greedy Algorithms: Optimizing Solutions for Efficiency"
16_Greedy_Algorithms.ppt
16_Greedy_Algorithms Greedy_AlgorithmsGreedy_Algorithms
16_Greedy_Algorithms.ppt
Module 2 - Greedy Algorithm Data structures and algorithm
heuristic search Techniques and game playing.pdf
Huffman analysis
Greedy Algorithms in Algorithms and Design
Greedy Method unit-2(Design and analysis of algorithms).pptx
Unit-3 greedy method, Prim's algorithm, Kruskal's algorithm.pdf
Greedy algorithms -Making change-Knapsack-Prim's-Kruskal's
Ad

More from HUSNAINAHMAD39 (7)

PPTX
GO compiler.pptx download for learning gi
PPTX
Web_Engineering_Assignment_Clean.pptxfor web
PPTX
Web_Engineering_Assignment_Clean.pptxfor college
PPTX
Dynamic Programing.pptx good for understanding
PPTX
USE CASE rabia_032117.pptx very good for understanding
PPTX
Lecture-20 Kleene’s Theorem-1.pptx best for understanding the automata
PDF
Nosql Presentation.pdf for DBMS understanding
GO compiler.pptx download for learning gi
Web_Engineering_Assignment_Clean.pptxfor web
Web_Engineering_Assignment_Clean.pptxfor college
Dynamic Programing.pptx good for understanding
USE CASE rabia_032117.pptx very good for understanding
Lecture-20 Kleene’s Theorem-1.pptx best for understanding the automata
Nosql Presentation.pdf for DBMS understanding
Ad

Recently uploaded (20)

PPTX
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
PPT
Lecture 3344;;,,(,(((((((((((((((((((((((
DOCX
unit 1 COST ACCOUNTING AND COST SHEET
PDF
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
PDF
How to Get Funding for Your Trucking Business
PPTX
Probability Distribution, binomial distribution, poisson distribution
PDF
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
PPTX
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
PDF
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
PDF
Nidhal Samdaie CV - International Business Consultant
PPTX
2025 Product Deck V1.0.pptxCATALOGTCLCIA
PDF
COST SHEET- Tender and Quotation unit 2.pdf
PDF
Laughter Yoga Basic Learning Workshop Manual
PDF
IFRS Notes in your pocket for study all the time
PDF
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
DOCX
Business Management - unit 1 and 2
PPT
Chapter four Project-Preparation material
PPTX
New Microsoft PowerPoint Presentation - Copy.pptx
PPTX
3. HISTORICAL PERSPECTIVE UNIIT 3^..pptx
PPTX
Lecture (1)-Introduction.pptx business communication
Dragon_Fruit_Cultivation_in Nepal ppt.pptx
Lecture 3344;;,,(,(((((((((((((((((((((((
unit 1 COST ACCOUNTING AND COST SHEET
Solara Labs: Empowering Health through Innovative Nutraceutical Solutions
How to Get Funding for Your Trucking Business
Probability Distribution, binomial distribution, poisson distribution
kom-180-proposal-for-a-directive-amending-directive-2014-45-eu-and-directive-...
CkgxkgxydkydyldylydlydyldlyddolydyoyyU2.pptx
BsN 7th Sem Course GridNNNNNNNN CCN.pdf
Nidhal Samdaie CV - International Business Consultant
2025 Product Deck V1.0.pptxCATALOGTCLCIA
COST SHEET- Tender and Quotation unit 2.pdf
Laughter Yoga Basic Learning Workshop Manual
IFRS Notes in your pocket for study all the time
Elevate Cleaning Efficiency Using Tallfly Hair Remover Roller Factory Expertise
Business Management - unit 1 and 2
Chapter four Project-Preparation material
New Microsoft PowerPoint Presentation - Copy.pptx
3. HISTORICAL PERSPECTIVE UNIIT 3^..pptx
Lecture (1)-Introduction.pptx business communication

greedy algorithm.pptx good for understanding

  • 1. Design & Analysis of Algorithms Greedy Algorithms Presented By 22,24,25,26,27,28,29,31,32,34,37,38,40 BSCS-E122
  • 2. What Are Greedy Techqniques?
  • 3. Greedy Techniques ● Definition: Greedy algorithms make locally optimal choices at each step, aiming to reach a global optimum solution. ● In simple words, a greedy algorithm is a simple technique that solves a problem by selecting the best option at each step without considering long- term implication..
  • 4. Characteristics of Greedy Algorithm • Locally optimal choices: Decisions made based on immediate information. • No backtracking: Once a decision is made, it's never reconsidered. • Greedy choice property: Optimal solution can be reached by making a series of greedy choices.
  • 5. Greedy Choice Property – (Core of G.A) Explanation of the Greedy Choice Property: • At each step, the optimal choice is made without considering future consequences. • This choice doesn't depend on previous decisions. “It means Greedy algorithm always selecting the best option available at each phase, even if the final decision is wrong.” • Also, it does not guarantee the best overall solution.
  • 6. Real Life Example One real-life example of a greedy algorithm is the process of making change. Vending Machine Change Dispensing: “Vending Machine Change Dispensing is the process by which a vending machine provides the correct amount of change”
  • 7. Working of Vending Machine Problem Statement: Scenario: Making change for 73 rupees. Coin available: 1 rupee, 5 rupees, 10 rupees, 25 rupees. Objective: Minimize the number of coins issued while providing correct change.
  • 8. Greedy Algorithm Approach: Prioritize dispensing the highest denomination coin that is smaller than or equal to the required change amount. 25 25 1 5 10 Coins 25 25 = 50
  • 9. Greedy Algorithm Approach: 25 25 = 60 10 25 25 = 70 10 10 25 25 = 73 10 10 1 1 1 •Total coins provided: 7. •Achieved optimal solution with minimum coins.
  • 10. Algorithm Of Greedy Technique • Fractional Knapsack • Dijkstra’s algorithm • Kruskal’s algorithm • Huffman coding
  • 11. Fractional Knapsack The basic idea of the greedy approach is to calculate the ratio profit/weight for each item and sort the item on the basis of this ratio. Then take the item with the highest ratio and add them as much as we can (can be the whole element or a fraction of it). This will always give the maximum profit because, in each step it adds an element such that this is the maximum possible profit for that much weight. Problem Given the weights and profits of N items, in the form of {profit, weight} put these items in a knapsack of capacity W to get the maximum total profit in the knapsack. In Fractional Knapsack, we can break items for maximizing the total value of the knapsack. For Example, in this example we have to determine the best combination we can take to maximize both profit and wieght whereas the max load of the knapsack is 20. Keep this in mind we are to make such a combination which allows us to optimize our solution.
  • 12. Fractional Knapsack Cont. Greedy About Weight Greedy About Profit OBJ1 18 Objects OBJ1 OBJ2 OBJ3 Profit 25 24 15 Weight 18 15 10 25+2/15*24 =28.2 OBJ3 10 15+10/15*24 =31 P/W Ratio OBJ2 15 24+5/10*15 =31.5
  • 13. Dijkstra’s algorithm Dijkstra's algorithm is a graph search algorithm that finds the shortest path between nodes in a weighted graph. It starts from a source node and iteratively explores the neighboring nodes, updating the shortest distance to each node as it goes. 1 2 3 20 10 40
  • 14. Dijkstra’s algorithm The real-life examples of the Dijkstra Algorithm are Google Maps, Social networking, and DNA mapping. All these things are represented in the form of graphs using Nodes and edges. To reach from one single source to multiple sources ( Nodes ) using the shortest path, then we use Dijkstra Algorithm. It is also known as the minimization algorithm and it uses a greedy approach and provides optimal solution. Relaxation: If d(u) + d(u+v) < d(v) d(v) = d(u) + c(u+v)
  • 15. Huffman Coding Huffman coding is a method used in computer science and information theory to compress data efficiently. It works by assigning variable-length codes to input characters based on their frequency of occurrence in the data. Steps Of Huffman Algorithm are following. 1. Frequency Count: Calculate the frequency of each character in the input data. 2. Priority Queue: Create a priority queue or min-heap based on the frequencies, where nodes with lower frequencies have higher priority. 3. Build Huffman Tree: While there is more than one node in the queue: • Remove the two nodes with the lowest frequency from the queue. • Create a new internal node with a frequency equal to the sum of the frequencies of the two nodes. • Set the two removed nodes as children of the new node. • Add the new node back to the queue. • The remaining node in the queue is the root of the Huffman tree.
  • 16. Huffman Coding Cont 4.Generate Huffman Codes: Traverse the Huffman tree from the root. Assign '0' to the left child edge and '1' to the right child edge. When reaching a leaf node (character node), the path from the root to that node represents the Huffman code for that character. 5.Encode Data: Replace each input character with its corresponding Huffman code to create the compressed data. 6.Decoding: Start from the root of the Huffman tree. Read each bit from the compressed data. Traverse down the tree following the '0' path for 'left' and '1' path for 'right' until reaching a leaf node. Output the character represented by the leaf node and go back to the root to continue decoding the next bits.
  • 17. Huffman Coding Cont 2 a=50, c=30, b=10, d=5, e=3, f=2 2 f e 3 5 5 d 10 10 b 20 30 50 100 c M is the message and consist of 100 characters with the alphabets given above. We know messages are coded in 8-bit ASCII codes. For 100 characters the code will be of 7*100 bits = 700 bits. Our Goal is to minimize this using huffman coding. Solution a = 0 1*50 = 50 b=100 3*10 = 30 c=11 2*30 = 60 d=1010 4*5 = 20 e=10111 5*3 = 15 f=10110 5*2 = 10 TOTAL 185bits 50 a 0 0 0 0 0 1 1 1 1 1
  • 18. Kruskal’s ALgorithm : • Construct Min Heap with edges . • Take one by one edge and add in spinning tree . ( Cycle should not be created ) Best Case ( n - 1 ) edges Worst Case ‘ e ’ edges A B C D F G E 5 4 3 6 6 6 5 6 2 4 4 3 Example
  • 19. Solution : A B C D F G E 4 3 5 2 4 3 2 1 n - 1 => 7 - 1 => 6 Time Complexity of Kruskal’s ALgorithm : • In Best Case , ( n - 1 ) log e . • In Worst Case , e log e .
  • 20. Real Life Applications Airline Ticket Booking Systems: Systems used by airlines to optimize seat allocation and pricing often use greedy algorithms. For example, when allocating seats, the system may prioritize filling higher-priced seats first before moving on to lower-priced ones. Data Compression: Greedy algorithms play a role in data compression techniques such as Huffman coding. This technique assigns shorter codes to more frequent symbols, reducing the overall size of the data. Google Maps: Google Maps uses greedy algorithms, particularly Dijkstra's algorithm, to find the shortest routes between locations. This is crucial for providing accurate and efficient navigation directions to users.
  • 21. CREDITS: This presentation template was created by Slidesgo, and includes icons by Flaticon, and infographics & images by Freepik Thanks! Do you have any questions?