Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
107 views

Basics of Greedy Algorithms

A greedy algorithm makes locally optimal choices at each step in the hope of finding a global optimum. It builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. The document provides examples of problems that can be solved using greedy algorithms, such as filling bottles of water or finding the minimum spanning tree of a graph. It also discusses advantages like ease of implementation but difficulties proving correctness for all cases.

Uploaded by

Iqra Raja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

Basics of Greedy Algorithms

A greedy algorithm makes locally optimal choices at each step in the hope of finding a global optimum. It builds up a solution piece by piece, always choosing the next piece that offers the most obvious and immediate benefit. The document provides examples of problems that can be solved using greedy algorithms, such as filling bottles of water or finding the minimum spanning tree of a graph. It also discusses advantages like ease of implementation but difficulties proving correctness for all cases.

Uploaded by

Iqra Raja
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Basics of Greedy Algorithms

Introduction

In an algorithm design there is no one 'silver bullet' that is a cure for all computation problems.
Different problems require the use of different kinds of techniques. A good programmer uses all
these techniques based on the type of problem. Some commonly-used techniques are:

1. Divide and conquer


2. Randomized algorithms
3. Greedy algorithms (This is not an algorithm, it is a technique.)
4. Dynamic programming

What is a 'Greedy algorithm'?

A greedy algorithm, as the name suggests, always makes the choice that seems to be the
best at that moment. This means that it makes a locally-optimal choice in the hope that this
choice will lead to a globally-optimal solution.

How do you decide which choice is optimal?

Assume that you have an objective function that needs to be optimized (either maximized or
minimized) at a given point. A Greedy algorithm makes greedy choices at each step to ensure
that the objective function is optimized. The Greedy algorithm has only one shot to compute the
optimal solution so that it never goes back and reverses the decision.

Greedy algorithms have some advantages and disadvantages:

1. It is quite easy to come up with a greedy algorithm (or even multiple greedy


algorithms) for a problem.
2. Analyzing the run time for greedy algorithms will generally be much easier than for
other techniques (like Divide and conquer). For the Divide and conquer technique, it is
not clear whether the technique is fast or slow. This is because at each level of recursion
the size of gets smaller and the number of sub-problems increases.
3. The difficult part is that for greedy algorithms you have to work much harder to
understand correctness issues. Even with the correct algorithm, it is hard to prove
why it is correct. Proving that a greedy algorithm is correct is more of an art than a
science. It involves a lot of creativity.

Note: Most greedy algorithms are not correct. An example is described later in this article.

C. How to create a Greedy Algorithm?

Being a very busy person, you have exactly T time to do some interesting things and you want
to do maximum such things.
You are given an array A of integers, where each element indicates the time a thing takes for
completion. You want to calculate the maximum number of things that you can do in the limited
time that you have.

This is a simple Greedy-algorithm problem. In each iteration, you have to greedily select the
things which will take the minimum amount of time to complete while maintaining two
variables currentTime and numberOfThings. To complete the calculation, you must:

1. Sort the array A in a non-decreasing order.


2. Select each to-do item one-by-one.
3. Add the time that it will take to complete that to-do item into currentTime.
4. Add one to numberOfThings.

Repeat this as long as the currentTime is less than or equal to T.

Let A = {5, 3, 4, 2, 1} and T = 6

After sorting, A = {1, 2, 3, 4, 5}

After the 1st iteration:

 currentTime = 1
 numberOfThings = 1

After the 2nd iteration:

 currentTime is 1 + 2 = 3
 numberOfThings = 2

After the 3rd iteration:

 currentTime is 3 + 3 = 6
 numberOfThings = 3

After the 4th iteration, currentTime is 6 + 4 = 10, which is greater than T. Therefore, the answer
is 3.

Implementation

#include <iostream>
#include <algorithm>

using namespace std;


const int MAX = 105;
int A[MAX];

int main()
{
int T, N, numberOfThings = 0, currentTime = 0;
cin >> N >> T;
for(int i = 0;i < N;++i)
cin >> A[i];
sort(A, A + N);
for(int i = 0;i < N;++i)
{
currentTime += A[i];
if(currentTime > T)
break;
numberOfThings++;
}
cout << numberOfThings << endl;
return 0;
}

This example is very trivial and as soon as you read the problem, it is apparent that you can
apply the Greedy algorithm to it.

Examples

The greedy method is quite powerful and works well for a wide range of problems. Many
algorithms can be viewed as applications of the Greedy algorithms, such as (includes but is not
limited to):

1. Minimum Spanning Tree


2. Dijkstra’s algorithm for shortest paths from a single source
3. Huffman codes ( data-compression codes )

Being greedy for Water


You are given container full of water. Container can have limited amount of water. You
also have N bottles to fill. You need to find the maximum numbers of bottles you can fill.
4. Input:
First line contains one integer, T, number of test cases.
First line of each test case contains two integer, N and X, number of bottles and capacity
of the container.
Second line of each test case contains N space separated integers, capacities of bottles.
5. Output:
For each test case print the maximum number of bottles you can fill.
6. Constraints:
1≤T≤100
1≤N≤104
1≤X≤109
1≤capacitiesofbottles≤106
7. SAMPLE INPUT
8.  
9. 1
10. 5 10
11. 8 5 4 3 2
12. SAMPLE OUTPUT
13.  
14. 3

You might also like