0% found this document useful (0 votes)
13 views13 pages

Greedy Algorithms

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

Greedy Algorithms

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Greedy Approach

KHURAM CHATTHA
What is Greedy Algorithm?
Greedy is an algorithmic paradigm that builds up a solution piece by
piece, always choosing the next piece that offers the most obvious
and immediate benefit. So the problems where choosing locally
optimal also leads to global solution are the best fit for Greedy.
For example consider the Fractional Knapsack Problem. The local
optimal strategy is to choose the item that has maximum value vs
weight ratio. This strategy also leads to a globally optimal solution
because we are allowed to take fractions of an item.
 A greedy algorithm is an algorithmic paradigm that follows the
problem-solving heuristic of making the locally optimal choice at
each stage with the hope of finding a global optimum. In other
words, a greedy algorithm chooses the best possible option at
each step, without considering the consequences of that choice
on future steps.
 The greedy algorithm is useful when a problem can be divided
into smaller subproblems, and the solution of each subproblem
can be combined to solve the overall problem. The greedy
algorithm can be used to solve optimization problems that
involve finding the best solution among many possible solutions.
Example
A classic example of a problem that can be solved using a greedy
algorithm is the “coin change” problem. The problem is to make
change for a given amount of money using the least possible
number of coins. For example, if the amount is 25 cents and the
available coins are 1 cent, 5 cents, and 10 cents, then the greedy
algorithm would choose the largest coin possible at each step. It
would start by selecting a 10-cent coin, then another 10-cent coin,
and finally a 5-cent coin, for a total of three coins.
Drawback

However, the greedy algorithm may not always find the optimal
solution. For example, if the available coins are 1 cent, 3 cents, and
4 cents, and the amount is 6 cents, the greedy algorithm would
select one 4-cent coin and two 1-cent coins , while the optimal
solution is to use two 3-cent coins.
The greedy algorithm can be applied in many contexts, including
scheduling, graph theory, and dynamic programming.
More Detailed Definition of
Greedy Algorithm
Greedy Algorithm is defined as a method for solving optimization
problems by taking decisions that result in the most evident and
immediate benefit irrespective of the final outcome. It works for cases
where minimization or maximization leads to the required solution.
Characteristics of Greedy algorithm
 For a problem to be solved using the Greedy approach, it must follow
a few major characteristics:
 There is an ordered list of resources(profit, cost, value, etc.)
 Maximum of all the resources(max profit, max value, etc.) are taken.
 For example, in the fractional knapsack problem, the maximum
value/weight is taken first according to available capacity.
Uses

 Some common use cases for the greedy algorithm include:


 Scheduling and Resource Allocation: The greedy algorithm can be used
to schedule jobs or allocate resources in an efficient manner.
 Minimum Spanning Trees: The greedy algorithm can be used to find the
minimum spanning tree of a graph, which is the subgraph that
connects all vertices with the minimum total edge weight.
 Coin Change Problem: The greedy algorithm can be used to make
change for a given amount with the minimum number of coins, by
always choosing the coin with the highest value that is less than the
remaining amount to be changed.
 Huffman Coding: The greedy algorithm can be used to generate a
prefix-free code for data compression, by constructing a binary tree in
a way that the frequency of each character is taken into consideration.
All greedy algorithms follow a
basic structure:
 Declare an empty result = 0.
 We make a greedy choice to select, If the choice is feasible add it
to the final result.
 return the result.
Why choose Greedy
Approach?
 The greedy approach has a few tradeoffs, which may make it
suitable for optimization. One prominent reason is to achieve the
most feasible solution immediately. In the activity selection
problem (Explained below), if more activities can be done before
finishing the current activity, these activities can be performed
within the same time. Another reason is to divide a problem
recursively based on a condition, with no need to combine all the
solutions. In the activity selection problem, the “recursive
division” step is achieved by scanning a list of items only once
and considering certain activities.
Greedy Technique

 Follows local optimal choice at each step to find global optimum


solution
 Feasible solution

Min. Cost
Optimal solution Max. Profit
Applications: Min. Risk
 Knapsack Problem
 Minimum Spanning Tree
 Optimal Merge Pattern
 Huffman Coding
 Dijkstra’s Method
Knapsack Problem
Objects Obj1 Obj2 Obj3
Profit 25 24 15
Weight 18 15 10

Greedy about Profit:


Obj1
25 + 2/15 * 24 = 28.2
Knapsack
Greedy about Weight: capacity
= 20
15 + 10/15 * 24 = 31 Obj3

Greedy about Weight:


P/W
24 + 5/10*15 = 31.5 Obj2
O(n)

O(nlogn)

O(n)

Time Complexity = O(n) +


O(nlogn) + O(n)

Hence neglecting lower


order complexities the final
time complexity for the
algorithm is = O(nlogn)

You might also like