0% found this document useful (0 votes)
8 views

Greedy Algorithms

ADA

Uploaded by

parmar darshan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Greedy Algorithms

ADA

Uploaded by

parmar darshan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

GOVERNMENT ENGINEERING COLLEGE , BHARUCH

UNDER DIRECTED OF TECHNICAL EDUCATION APPROVED BY AICTE


AFFILATED TO GUJART TECHNOLOGICAL UNIVERSITY

Introduction to Greedy Algorithms


Understanding the Basics and Characteristics

Prepared By : Twisha A. Patel (220140107052)


Darshan A. Parmar (220140107027)
Sem/Branch : Computer Engg. Sem-05
Subject : Analysis & Design of Algorithms
Course Coordinator : Prof. K. G. Yadav

Analysis & Design of Algorithms


INDEX
Introduction to Greedy Algorithms
Basic Structure of Greedy Algorithms
Problems That Can Be Solved Using Greedy Algorithm
Example 1: Fractional Knapsack Problem
Example 2: Activity Selection Problem
Characteristics of Greedy Algorithm
Huffman Coding Algorithm
Dijkstra’s Algorithm for Shortest Path
Comparison with Dynamic Programming
Limitations of Greedy Algorithms
Summary and Conclusion
WHAT ARE GREEDY ALGORITHMS? 1
Greedy algorithms work by making a sequence of choices, each of which looks
best at the moment. The greedy algorithm finds the optimum solution by:
Proceeding step-by-step.
And picking the most favorable option at each step.

Analysis & Design of Algorithms


BASIC STRUCTURE 2

Initialization: Start with an empty


solution.
Iterative Selection: At each step, choose PROBLEM
the best option according to a
predefined criterion (local optimal
SUB-
choice). Greedy Choice
PROBLEM
Feasibility Check: Ensure that the chosen
option leads to a feasible solution. SUB-
Termination: Stop when no further Greedy Choice
PROBLEM
choices can be made.
Final Solution: The final result is reached
by combining all selected options.

Analysis & Design of Algorithms


PROBLEMS THAT CAN BE SOLVED USING 3
GREEDY ALGORITHM

If both of the properties below are true, a greedy algorithm can be used to solve the
problem.
Greedy choice property:
A global (overall) optimal solution can be reached by choosing the optimal choice
at each step.

Optimal substructure:
An optimal solution to the entire problem contains the optimal solutions to the
sub-problems.

Analysis & Design of Algorithms


EXAMPLE 1
FRACTIONAL KNAPSACK PROBLEM.
4
PROBLEM. You are a traveler with a backpack that can hold a maximum weight of 50 kg.
There are three treasures, each having a specific weight and value. The total weight of all
items is 60 kg, but the knapsack can only hold 50 kg. So, you must maximize the value
while keeping the total weight ≤ 50 kg.
Weight = 10 kg
Value = ₹60

Greedy Choice Property: A


A problem exhibits the greedy choice Value-to-weight ratio = ₹6/kg

property if a global optimal solution can be


Weight = 20 kg

arrived at by making a locally optimal


B +
Value = ₹100

choice at each stage. Consider the


Fractional Knapsack Problem, where at each
Value-to-weight ratio = ₹5/kg
Capacity- 50kg

step, the item with the highest value-to-


weight ratio is chosen. c Weight = 30 kg
Value = ₹120

Value-to-weight ratio = ₹4/kg

Analysis & Design of Algorithms


.1
EXAMPLE 1 4*
FRACTIONAL KNAPSACK PROBLEM.

SOLUTION.
Step 1: Calculate the value-to-weight ratio
Step 2: Sort treasures by value-to-weight ratio in descending order:
A→B→C
Step 3: Select items:
Take all of Treasure A (10 kg) → Value = ₹60
Take all of Treasure B (20 kg) → Value = ₹100
The remaining capacity is 20 kg.
→ Take 20/30 = 2/3 of Treasure C (20 kg of 30 kg) → Value = 2/3 × ₹120 = ₹80

Result :
Total Weight: 50 kg Total Value: ₹240

Analysis & Design of Algorithms


EXAMPLE 2
ACTIVITY SELECTION PROBLEM
5
PROBLEM. Given a set of activities, each with a start and finish time, select the maximum
number of non-overlapping activities that can be performed by a single person or
machine.
Activity Start Finish

Optimal Substructure: A1 1 4
The problem can be broken
into smaller subproblems. A2 3 5

Greedy algorithms rely on this A3 0 6


property to ensure
correctness. Consider the A4 5 7

following activities with start


A5 3 6
and finish times.
A6 6 11

Analysis & Design of Algorithms


EXAMPLE 2 5*
ACTIVITY SELECTION PROBLEM.
SOLUTION.
Step 1: Sort Activities by Finish Time: A1 → A2 → A5 → A3 → A4 → A6
Step 2: Select Activities
Select A1: The first activity that finishes the earliest (Finish: 4).
Check A2: Start time (3) overlaps with A1 (Finish: 4), so skip it.
Check A5: Start time (3) overlaps with A1 (Finish: 4), so skip it.
Check A3: Start time (0) overlaps with A1 (Finish: 4), so skip it.
Select A4: Start time (5) is greater than the finish time of A1 (4), so select A4.
Select A6: Start time (6) is greater than the finish time of A4 (7), so select A6.
Result :
The activities selected are: A1, A4, and A6.
A1: Start = 1, Finish = 4
A4: Start = 5, Finish = 7
A6: Start = 6, Finish = 1

Analysis & Design of Algorithms


CHARACTERISTICS OF GREEDY ALGORITHM
6

Locally Optimal Choice: Always chooses the option that seems the best at the
moment.
Irrevocability: Once a choice is made, it cannot be changed.
Non-backtracking: Does not reconsider previous decisions.
Efficiency: Generally has a time complexity of O(n) or O(n log n), making it
efficient for some problems.
Not Always Globally Optimal: May not always produce the best overall solution
(global optimum), but works well for specific problems (e.g., fractional knapsack,
activity selection).
Problem-Specific: Works best when the problem exhibits greedy choice
property and optimal substructure.

Analysis & Design of Algorithms


HUFFMAN CODING
7
PROBLEM.
Build an optimal binary, prefix-free code for a given set of characters based on their frequencies.

Huffman Coding Algorithm (Greedy Approach):


At each step, combine the two characters with the smallest frequencies and build a tree from the
bottom up.

Steps:

Create a priority queue: Insert all characters with


their frequencies into a min-heap (priority queue).
Extract two smallest elements: Remove the two
nodes with the smallest frequencies from the
queue.
Merge and reinsert: Create a new internal node by
combining the two nodes, summing their
frequencies, and insert it back into the queue.

Analysis & Design of Algorithms


Repeat: 7*
Continue extracting, merging, and reinserting nodes until only one element remains in
the priority queue.
This node will be the root of the Huffman tree.

Generate Huffman Codes:


Assign binary codes to each character by traversing the tree

Result:
The left and right edge assignments can follow either
of two conventions:
Convention 1: Left edge = 0, Right edge = 1.
Convention 2: Left edge = 1, Right edge = 0.

Ensure the same convention is followed when


encoding and decoding.

Analysis & Design of Algorithms


DIJKSTRA’S ALGORITHM FOR SHORTEST PATH
8
Problem:
Find the shortest path from a source node
to all other nodes in a weighted graph.
Greedy Approach: Always extend the
shortest known path from the source
node.

Steps:
Initialize distances to all nodes as infinity,
except the source node.
Pick the node with the smallest distance.
Update distances for its neighbors.
Repeat until all nodes are processed.

Analysis & Design of Algorithms


COMPARISON WITH DYNAMIC PROGRAMMING 9
Greedy Approach Dynamic Programming

Guarantees an optimal solution if the problem exhibits the


May not always provide an optimal solution.
principle of optimality.

Does not reuse solutions to subproblems. Reuses solutions to overlapping subproblems.

May involve backtracking, especially in top-down


Does not involve backtracking.
implementations.

Typically simpler and faster to implement. May be more complex and slower to implement.

Suitable for problems where local optimization leads to Suitable for problems with overlapping subproblems and
global optimization. optimal substructure.

Minimum Spanning Tree, Shortest Path algorithms. Fibonacci sequence, Longest Common Subsequence.

Analysis & Design of Algorithms


LIMITATIONS OF GREEDY ALGORITHMS 10
Not Always Globally Optimal
Problem-Specific
Lack of Backtracking
Inefficiency with Complex Structures

Analysis & Design of Algorithms


CONCLUSION AND KEY TAKEAWAYS 11
Summary:
Greedy algorithms are simple and efficient for certain types of problems,
especially optimization problems.
They rely on properties like the greedy choice property and optimal
substructure.
Greedy algorithms are not always optimal, but when they are, they are
often faster and easier to implement than other methods.

References:
Introduction to Algorithms, Cormen et al.
Javatpoint.com
Geeksforgeeks.org
Brilliant.org

Analysis & Design of Algorithms


THANK YOU
Made with dedication by:
Twisha and Darshan

You might also like