0% found this document useful (0 votes)
12 views8 pages

Understanding Greedy Algorithms Explained

A greedy algorithm is a problem-solving approach that makes a series of local optimal choices with the hope of achieving a global optimal solution. While they are efficient and simple to implement, greedy algorithms do not guarantee the best solution for all problems and can be limited by their inability to backtrack. Key applications include the fractional knapsack problem, activity selection, and constructing minimum spanning trees.

Uploaded by

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

Understanding Greedy Algorithms Explained

A greedy algorithm is a problem-solving approach that makes a series of local optimal choices with the hope of achieving a global optimal solution. While they are efficient and simple to implement, greedy algorithms do not guarantee the best solution for all problems and can be limited by their inability to backtrack. Key applications include the fractional knapsack problem, activity selection, and constructing minimum spanning trees.

Uploaded by

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

A greedy algorithm is a problem-solving approach that builds up a solution piece

by piece, always choosing the next piece that offers the most immediate benefit or
value. The key characteristic of a greedy algorithm is that it makes a series of
choices, each of which looks the best at the moment, with the hope that these local
optimal choices will lead to a global optimal solution.

Key Characteristics of Greedy Algorithms:


Local Optimal Choice: At each step, the algorithm makes a choice that seems
the best at that moment. This choice is made based on some criterion, such as the
highest value, lowest cost, or best ratio.

Irrevocable Decisions: Once a choice is made, it cannot be undone. The


algorithm does not backtrack or reconsider previous decisions.

Efficiency: Greedy algorithms are often more efficient in terms of time complexity
compared to other approaches like dynamic programming, as they do not explore
all possible solutions.

Optimality: Greedy algorithms do not always produce the optimal solution for
every problem. They work well for problems where a local optimal choice leads to
a global optimal solution, such as the fractional knapsack problem, but may not
work for others, like the 0/1 knapsack problem.

Examples of Problems Solved by Greedy Algorithms:


Fractional Knapsack Problem: Select items based on the highest value-to-weight
ratio until the knapsack is full.
Activity Selection Problem: Choose the maximum number of non-overlapping
activities by selecting the earliest finishing activity first.
Huffman Coding: Build an optimal prefix code for data compression by repeatedly
merging the two least frequent nodes.
Prim's and Kruskal's Algorithms: Find the minimum spanning tree of a graph by
selecting edges with the smallest weight.
Advantages and Disadvantages:
Advantages:
Simplicity: Greedy algorithms are often easier to understand and implement.
Speed: They are generally faster and require less computational resources.
Disadvantages:
Suboptimal Solutions: They may not always yield the best solution for every
problem.
Problem-Specific: They work well only for certain types of problems where local
optimal choices lead to a global optimum.

What Is Greedy Algorithm?


A Greedy algorithm is an approach to solving a problem that selects the most
appropriate option based on the current situation. This algorithm ignores the fact
that the current best result may not bring about the overall optimal result. Even if
the initial decision was incorrect, the algorithm never reverses it.

This simple, intuitive algorithm can be applied to solve any optimization problem
which requires the maximum or minimum optimum result. The best thing about this
algorithm is that it is easy to understand and implement.

The runtime complexity associated with a greedy solution is pretty reasonable.


However, you can implement a greedy solution only if the problem statement
follows two properties mentioned below:

Greedy Choice Property: Choosing the best option at each phase can lead to
a global (overall) optimal solution.

Optimal Substructure: If an optimal solution to the complete problem contains


the optimal solutions to the subproblems, the problem has an optimal
substructure.

Moving forward, we will learn how to create a greedy solution for a problem that
adheres to the principles listed above.
Steps for Creating a Greedy Algorithm
By following the steps given below, you will be able to formulate a greedy solution
for the given problem statement:

Step 1: In a given problem, find the best substructure or subproblem.

Step 2: Determine what the solution will include (e.g., largest sum, shortest
path).

Step 3: Create an iterative process for going over all subproblems and
creating an optimum solution.

Let’s take up a real-world problem and formulate a greedy solution for it.

Problem: Alex is a very busy person. He has set aside time T to accomplish some
interesting tasks. He wants to do as many tasks as possible in this allotted time T.
For that, he has created an array A of timestamps to complete a list of items on his
itinerary.

Now, here we need to figure out how many things Alex can complete in the T time
he has.

Approach to Build a Solution: This given problem is a straightforward greedy


problem. In each iteration, we will have to pick the items from array A that will take
the least amount of time to accomplish a task while keeping two variables in mind:
current_Time and number_Of_Things. To generate a solution, we will have to
carry out the following steps.

Sort the array A in ascending order.

Select one timestamp at a time.

After picking up the timestamp, add the timestamp value to current_Time.

Increase number_Of_Things by one.

Repeat steps 2 to 4 until the current_Time value reaches T.


Example of Greedy Algorithm

Problem Statement: Find the best route to reach the destination city from the
given starting point using a greedy method.

Greedy Solution: In order to tackle this problem, we need to maintain a graph


structure. And for that graph structure, we'll have to create a tree structure, which
will serve as the answer to this problem. The steps to generate this solution are
given below:

Start from the source vertex.

Pick one vertex at a time with a minimum edge weight (distance) from the
source vertex.

Add the selected vertex to a tree structure if the connecting edge does not
form a cycle.

Keep adding adjacent fringe vertices to the tree until you reach the destination
vertex.

The animation given below explains how paths will be picked up in order to reach
the destination city.
Limitations of Greedy Algorithm
Factors listed below are the limitations of a greedy algorithm:

[Link] greedy algorithm makes judgments based on the information at each


iteration without considering the broader problem; hence it does not produce the
best answer for every problem.

[Link] problematic part for a greedy algorithm is analyzing its accuracy. Even
with the proper solution, it is difficult to demonstrate why it is accurate.

[Link] problems (Dijkstra’s Algorithm) with negative graph edges


cannot be solved using a greedy algorithm.

Moving forward, let’s look at some applications of a greedy algorithm.

Applications of Greedy Algorithm

Following are few applications of the greedy algorithm:


Used for Constructing Minimum Spanning Trees: Prim’s and Kruskal’s
Algorithms used to construct minimum spanning trees are greedy algorithms.

Used to Implement Huffman Encoding: A greedy algorithm is utilized to build a


Huffman tree that compresses a given image, spreadsheet, or video into a
lossless compressed file.

Used to Solve Optimization Problems: Graph - Map Coloring, Graph - Vertex


Cover, Knapsack Problem, Job Scheduling Problem, and activity selection
problem are classic optimization problems solved using a greedy algorithmic
paradigm.

Characteristics of a Greedy Method

The greedy method is a simple and straightforward way to solve optimization


problems. It involves making the locally optimal choice at each stage with the hope
of finding the global optimum. The main advantage of the greedy method is that it
is easy to implement and understand. However, it is not always guaranteed to find
the best solution and can be quite slow.

The greedy method works by making the locally optimal choice at each stage in
the hope of finding the global optimum. This can be done by either minimizing or
maximizing the objective function at each step. The main advantage of the greedy
method is that it is relatively easy to implement and understand. However, there
are some disadvantages to using this method. First, the greedy method is not
guaranteed to find the best solution. Second, it can be quite slow. Finally, it is often
difficult to prove that the greedy method will indeed find the global optimum.

One of the most famous examples of the greedy method is the knapsack problem.
In this problem, we are given a set of items, each with a weight and a value. We
want to find the subset of items that maximizes the value while minimizing the
weight. The greedy method would simply take the item with the highest value at
each step. However, this might not be the best solution. For example, consider the
following set of items:

Item 1: Weight = 2, Value = 6


Item 2: Weight = 2, Value = 3

Item 3: Weight = 4, Value = 5

The greedy method would take Item 1 and Item 3, for a total value of 11. However,
the optimal solution would be to take Item 2 and Item 3, for a total value of 8. Thus,
the greedy method does not always find the best solution

Components of a Greedy Algorithm


There are four key components to any greedy algorithm:

1.A set of candidate solutions (typically represented as a graph)

2.A way of ranking the candidates according to some criteria

3.A selection function that picks the best candidate from the set, according to
the ranking

4.A way of "pruning" the set of candidates, so that it doesn't contain any
solutions that are worse than the one already chosen.

The first two components are straightforward - the candidate solutions can be
anything, and the ranking criteria can be anything as well. The selection function is
usually just a matter of picking the candidate with the highest ranking.

The pruning step is important, because it ensures that the algorithm doesn't waste
time considering candidates that are already known to be worse than the best one
found so far. Without this step, the algorithm would essentially be doing a brute-
force search of the entire solution space, which would be very inefficient.

Disadvantages of Using Greedy Algorithms


The main disadvantage of using a greedy algorithm is that it may not find the
optimal solution to a problem. In other words, it may not produce the best possible
outcome. Additionally, greedy algorithms can be very sensitive to changes in input
data — even a small change can cause the algorithm to produce a completely
different result. Finally, greedy algorithms can be difficult to implement and
understand.

You might also like