Greedy Algorithms

Distribute Education like Computer Geek

Greedy Algorithms for Problem Solving

.

“A Greedy algorithm is an approach to solving problems by making locally optimal (best) choices at each step with the hope of finding a global optimum (best) or near-best solution.”

In other words, it is a strategy that makes the best decision at each stage of a problem while not worrying about the consequences of those decisions on future steps.

Greedy Thief of Greedy Algorithms
Greedy Thief

.

Edsger W. Dijkstra, a computer scientist and mathematician who wanted to calculate a minimum spanning tree, introduced the term “Greedy algorithm”.

Edsger Dijkstra inventedDijkstra's algorithm of Greedy algorithms
Edsger Dijkstra

Dijkstra theorem (minimum spanning tree theorem) also was made by Edgser Dijkstra.

.

Characteristics of Greedy Algorithms
  1. Choice of the Best Option – At each step, the algorithm selects the best available option based on a specific criterion. This choice is made without considering the impact of future selections.
  2. Greedy Choice PropertyThe selected option should be the one that appears to be the most advantageous in the current context. This choice is based on some criteria or objective function.
  3. Optimal Substructure – The problem must exhibit the property of optimal substructure, which means that finding an optimal solution to the problem involves finding optimal solutions to its subproblems.
  4. Irrevocable Choices – Once a decision is made in a greedy algorithm, it is final and cannot be changed in subsequent steps.
  5. Short Sightedness – The term “short-sighted” emphasizes that this algorithm doesn’t have a long-term vision or strategy. It’s like making decisions in the moment without thinking about how they might affect the bigger picture. While this approach can work well in some situations, it may not always lead to the best overall solution.

.

Optimal Reliability Problem

Greedy algorithms are often used to solve optimization problems, where the goal is to find the best solution among a set of feasible solutions.

While they are relatively simple and efficient, it’s important to note that greedy algorithms may not always produce globally optimal solutions.

In some cases, they can provide approximate solutions that are close to the optimal solution, while in others, they may fail to find the correct solution.

The effectiveness of a greedy algorithm depends on the problem at hand and the choice of the greedy criterion. It is crucial to analyse the problem’s properties and constraints to determine whether a greedy approach is appropriate and to design the greedy criterion carefully to achieve the desired results.

.

Applications of Greedy Algorithm

1. Activity Selection Problem

In this problem, you are given a set of activities, each with a start time and an end time. The goal is to select a maximum-sized subset of non-overlapping activities that can be performed without conflicting with each other, i.e., no two selected activities overlap in time.

.

2. Fractional Knapsack Problem

You are given a set of items, each with a weight (w) and a value (v), and a knapsack with a maximum weight capacity (W). The goal is to determine the most valuable combination of items to include in the knapsack without exceeding its weight capacity.

.

3. Minimum Spanning Tree

It involves finding the subset of edges in an undirected, connected graph that connects all the vertices while minimizing the total edge weight. Kruskal’s Algorithm and Prim’s Algorithm are two greedy problems.

.

4. Huffman Code

The Huffman coding problem is a well-known problem in computer science and information theory. It involves the compression of data by creating a variable-length prefix-free binary code, known as the Huffman code, to represent characters in a way that minimizes the total number of bits required for encoding.

.

5. Job Sequencing

you are given a set of jobs, each with a specific deadline and a profit or weight associated with it. The goal is to schedule these jobs on a single machine (or processor) in such a way that you maximize the total profit or minimize the total penalty.

.

6. Coin Change Problem

Determines the minimum number of coins needed to make change for a given amount. Greedily selects the largest coin denominations until the target amount is reached.

Test Yourself

Q1- Explain a situation where a greedy algorithm fails to find the optimal solution. What characteristics of the problem contribute to this failure?

Greedy algorithms may fail in problems where making locally optimal choices at each step does not guarantee a globally optimal solution. An example is the “Traveling Salesman Problem,” where selecting the nearest city in each step may lead to a suboptimal tour.

Q2- Discuss a scenario where a greedy algorithm produces a globally optimal solution.

Greedy algorithms excel in problems with optimal substructure and where locally optimal choices lead to globally optimal solutions. An example is the “Minimum Spanning Tree” problem, where Kruskal’s and Prim’s algorithms consistently find the optimal solution.

Q3- How can a greedy algorithm be adapted to find an optimal solution in a problem where it typically fails to do so?

In some cases, a greedy algorithm can be modified to include additional criteria or constraints to ensure an optimal solution.

Q4- What is the significance of the “Short Sightedness” characteristic in greedy algorithms? Provide an example to illustrate its impact.

“Short Sightedness” in greedy algorithms emphasizes making decisions in the moment without considering their long-term consequences. While this approach simplifies problem-solving, it can lead to suboptimal solutions in some cases.

In the Knapsack problem, selecting the most valuable item at each step may not always result in the highest total value.

Q5- How does the “Optimal Substructure” property play a role in determining whether a problem is suitable for a greedy algorithm? Provide an example of a problem that exhibits optimal substructure.

The “Optimal Substructure” property helps us decide if a problem can be solved using a greedy algorithm. It means that to solve the big problem in the best way, we should also solve the smaller parts in the best way. It’s like building a big puzzle by putting the small pieces together.

 

For example, think about the “Huffman Coding” problem. In this problem, we want to find the best way to represent characters in a text, like ‘A,’ ‘B,’ or ‘C,’ using as few bits as possible. To do this, we need to find the best way to represent each individual character. Then, we can combine these small solutions to create the best solution for the whole text.

 

This property is like saying that to make a delicious pizza (the big problem), we need to make sure each ingredient (like the cheese, tomatoes, and dough) tastes good on its own (the small problems). When we put them all together, we get a great pizza! So, if a problem can be broken down into smaller problems like this, it might be a good fit for a greedy algorithm.

Q6- Discuss the “Irrevocable Choices” characteristic of greedy algorithms. Why is it essential in the decision-making process?

 

“Irrevocable Choices” mean that once a decision is made in a greedy algorithm, it cannot be changed in subsequent steps.

This feature enforces commitment to a choice, which is crucial for the algorithm’s simplicity and efficiency. It prevents constant reconsideration of past choices, reducing complexity.

Q7- What are the potential advantages of using a greedy algorithm in problem-solving? Can you name situations where a greedy approach is preferable?

Advantages of greedy algorithms include simplicity, efficiency, and speed. They work well in problems where optimal solutions can be approximated by making locally optimal choices, such as scheduling non-overlapping activities in the “Activity Selection Problem.”

Q8- What is the primary characteristic of a greedy algorithm?
  1. Long-term vision
  2. Short Sightedness
  3. Random decision-making
  4. Heuristic approach

Ans – (2)

Explanation – Greedy algorithms make decisions based on immediate optimization without considering long-term consequences.

Q9- The “Greedy Choice Property” suggests that a greedy algorithm should
  1. Choose the option with the most uncertain outcome
  2. Make decisions based on future selections
  3. Select the option most advantageous in the current context
  4. Randomly pick available options

Ans – (3)

Explanation – The Greedy Choice Property dictates choosing the best option based on the current context.

Q10- In which type of problems do greedy algorithms work well?
  1. Problems without optimal substructure
  2. Problems with highly uncertain outcomes
  3. Problems with optimal substructure
  4. Problems with random constraints

Ans – (3)

Explanation – Greedy algorithms are effective in problems exhibiting optimal substructure.

Q11- What does “Irrevocable Choices” mean in the context of greedy algorithms?
  1. Choices can be changed in subsequent steps
  2. Choices depend on future selections
  3. Choices are permanent and cannot be changed
  4. Choices are made randomly

Ans – (3)

Explanation – Once a decision is made in a greedy algorithm, it cannot be changed.

Q12- Which of the following problems is not typically solved using a greedy algorithm?
  1. Coin Change Problem
  2. Minimum Spanning Tree
  3. Activity Selection Problem
  4. Traveling Salesman Problem

Ans – (4)

Explanation – Greedy algorithms often fail to find the optimal solution in the Traveling Salesman Problem.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â