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. Greedy algorithms are used for optimization problems.
An optimization problem can be solved using Greedy if the problem has the following property:
- At every step, we can make a choice that looks best at the moment, and we get the optimal solution to the complete problem.
If a Greedy Algorithm can solve a problem, then it generally becomes the best method to solve that problem as the Greedy algorithms are in general more efficient than other techniques like Dynamic Programming. But Greedy algorithms cannot always be applied. For example, the Fractional Knapsack problem can be solved using Greedy, but 0-1 Knapsack cannot be solved using Greedy.
Following are some standard algorithms that are Greedy algorithms:
In Kruskal’s algorithm, we create an MST by picking edges one by one. The Greedy Choice is to pick the smallest weight edge that doesn’t cause a cycle in the MST constructed so far
In Prim’s algorithm also, we create a MST by picking edges one by one. We maintain two sets: a set of the vertices already included in MST and the set of the vertices not yet included. The Greedy Choice is to pick the smallest weight edge that connects the two sets
Dijkstra’s algorithm is very similar to Prim’s algorithm. The shortest-path tree is built up, edge by edge. We maintain two sets: a set of the vertices already included in the tree and a set of the vertices not yet included. The Greedy Choice is to pick the edge that connects the two sets and is on the smallest weight path from the source to the set that contains not yet included vertices
Huffman Coding is a loss-less compression technique. It assigns variable-length bit codes to different characters. The Greedy Choice is to assign the least bit length code to the most frequent character.
The greedy algorithms are sometimes also used to get an approximation for Hard optimization problems. For example, Traveling Salesman Problem is an NP-Hard problem. A Greedy choice for this problem is to pick the nearest unvisited city from the current city at every step. These solutions don’t always produce the best optimal solution but can be used to get an approximately optimal solution.
Here let us see one such problem that can be solved using Greedy algorithm
Problem:
You are given n activities with their start and finish times. Select the maximum number of activities that can be performed by a single person, assuming that a person can only work on a single activity at a time.
Examples:
Input: start[] = {10, 12, 20}, finish[] = {20, 25, 30}
Output: 0 2
Explanation: A person can perform at most two activities. The
maximum set of activities that can be executed
is {0, 2} [ These are indexes in start[] and finish[] ]
Input: start[] = {1, 3, 0, 5, 8, 5}, finish[] = {2, 4, 6, 7, 9, 9};
Output: 0 1 3 4
Explanation: A person can perform at most four activities. The
maximum set of activities that can be executed
is {0, 1, 3, 4} [ These are indexes in start[] and finish[]
Approach: To solve the problem follow the below idea:
The greedy choice is to always pick the next activity whose finish time is the least among the remaining activities and the start time is more than or equal to the finish time of the previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as the minimum finishing time activity
Follow the given steps to solve the problem:
- Sort the activities according to their finishing time
- Select the first activity from the sorted array and print it
- Do the following for the remaining activities in the sorted array
- If the start time of this activity is greater than or equal to the finish time of the previously selected activity then select this activity and print it
Note: In the implementation, it is assumed that the activities are already sorted according to their finish time, otherwise the time complexity will rise to O(N*log(N)) and Auxiliary Space will rise to O(N), as we have to create a 2-D array for storing the start and finish times together.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void printMaxActivities( int s[], int f[], int n)
{
int i, j;
cout << "Following activities are selected" << endl;
i = 0;
cout << i << " " ;
for (j = 1; j < n; j++) {
if (s[j] >= f[i]) {
cout << j << " " ;
i = j;
}
}
}
int main()
{
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
int n = sizeof (s) / sizeof (s[0]);
printMaxActivities(s, f, n);
return 0;
}
|
C
#include <stdio.h>
void printMaxActivities( int s[], int f[], int n)
{
int i, j;
printf ( "Following activities are selected\n" );
i = 0;
printf ( "%d " , i);
for (j = 1; j < n; j++) {
if (s[j] >= f[i]) {
printf ( "%d " , j);
i = j;
}
}
}
int main()
{
int s[] = { 1, 3, 0, 5, 8, 5 };
int f[] = { 2, 4, 6, 7, 9, 9 };
int n = sizeof (s) / sizeof (s[0]);
printMaxActivities(s, f, n);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class ActivitySelection {
public static void printMaxActivities( int s[], int f[],
int n)
{
int i, j;
System.out.println(
"Following activities are selected" );
i = 0 ;
System.out.print(i + " " );
for (j = 1 ; j < n; j++) {
if (s[j] >= f[i]) {
System.out.print(j + " " );
i = j;
}
}
}
public static void main(String[] args)
{
int s[] = { 1 , 3 , 0 , 5 , 8 , 5 };
int f[] = { 2 , 4 , 6 , 7 , 9 , 9 };
int n = s.length;
printMaxActivities(s, f, n);
}
}
|
Python3
def printMaxActivities(s, f):
n = len (f)
print ( "Following activities are selected" )
i = 0
print (i, end = ' ' )
for j in range ( 1 , n):
if s[j] > = f[i]:
print (j, end = ' ' )
i = j
if __name__ = = '__main__' :
s = [ 1 , 3 , 0 , 5 , 8 , 5 ]
f = [ 2 , 4 , 6 , 7 , 9 , 9 ]
printMaxActivities(s, f)
|
C#
using System;
class GFG {
public static void printMaxActivities( int [] s, int [] f,
int n)
{
int i, j;
Console.Write(
"Following activities are selected\n" );
i = 0;
Console.Write(i + " " );
for (j = 1; j < n; j++) {
if (s[j] >= f[i]) {
Console.Write(j + " " );
i = j;
}
}
}
public static void Main()
{
int [] s = { 1, 3, 0, 5, 8, 5 };
int [] f = { 2, 4, 6, 7, 9, 9 };
int n = s.Length;
printMaxActivities(s, f, n);
}
}
|
Javascript
<script>
function printMaxActivities(s,f,n)
{
let i, j;
document.write( "Following activities are selected : n" );
i = 0;
document.write(i+ " " );
for (j = 1; j < n; j++)
{
if (s[j] >= f[i])
{
document.write(j+ " " );
i = j;
}
}
}
let s = [1, 3, 0, 5, 8, 5]
let f = [2, 4, 6, 7, 9, 9]
let n = s.length;
printMaxActivities(s, f, n);
</script>
|
PHP
<?php
function printMaxActivities( $s , $f , $n )
{
echo "Following activities are selected " . "\n" ;
$i = 0;
echo $i . " " ;
for ( $j = 1; $j < $n ; $j ++)
{
if ( $s [ $j ] >= $f [ $i ])
{
echo $j . " " ;
$i = $j ;
}
}
}
$s = array (1, 3, 0, 5, 8, 5);
$f = array (2, 4, 6, 7, 9, 9);
$n = sizeof( $s );
printMaxActivities( $s , $f , $n );
?>
|
OutputFollowing activities are selected
0 1 3 4
Time Complexity: O(N)
Auxiliary Space: O(1)
How does Greedy Choice work for Activities sorted according to finish time?
Let the given set of activities be S = {1, 2, 3, …n}, and activities are sorted by finish time. The greedy choice is to always pick activity 1. How come activity 1 always provides one of the optimal solutions?
We can prove it by showing that if there is another solution B with the first activity other than 1, then there is also a solution A of the same size as activity 1 as the first activity. Let the first activity selected by B be k, then there always exist A = {B – {k}} U {1}.
Note: The activities in B are independent and k has the smallest finishing time among all. Since k is not 1, finish(k) >= finish(1))
How to implement when given activities are not sorted?
We create a structure/class for activities. We sort all activities by finish time (Refer sort in C++ STL). Once we have the activities sorted, we apply the same algorithm.
Below image is an illustration of the above approach:

Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Activitiy {
int start, finish;
};
bool activityCompare(Activitiy s1, Activitiy s2)
{
return (s1.finish < s2.finish);
}
void printMaxActivities(Activitiy arr[], int n)
{
sort(arr, arr + n, activityCompare);
cout << "Following activities are selected :\n" ;
int i = 0;
cout << "(" << arr[i].start << ", " << arr[i].finish
<< ")" ;
for ( int j = 1; j < n; j++) {
if (arr[j].start >= arr[i].finish) {
cout << ", (" << arr[j].start << ", "
<< arr[j].finish << ")" ;
i = j;
}
}
}
int main()
{
Activitiy arr[] = { { 5, 9 }, { 1, 2 }, { 3, 4 },
{ 0, 6 }, { 5, 7 }, { 8, 9 } };
int n = sizeof (arr) / sizeof (arr[0]);
printMaxActivities(arr, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Activity {
int start, finish;
public Activity( int start, int finish)
{
this .start = start;
this .finish = finish;
}
}
class Compare {
static void compare(Activity arr[], int n)
{
Arrays.sort(arr, new Comparator<Activity>() {
@Override
public int compare(Activity s1, Activity s2)
{
return s1.finish - s2.finish;
}
});
}
}
class GFG {
static void printMaxActivities(Activity arr[], int n)
{
Compare obj = new Compare();
obj.compare(arr, n);
System.out.println(
"Following activities are selected :" );
int i = 0 ;
System.out.print( "(" + arr[i].start + ", "
+ arr[i].finish + ")" );
for ( int j = 1 ; j < n; j++) {
if (arr[j].start >= arr[i].finish) {
System.out.print( ", (" + arr[j].start + ", "
+ arr[j].finish + ")" );
i = j;
}
}
}
public static void main(String[] args)
{
int n = 6 ;
Activity arr[] = new Activity[n];
arr[ 0 ] = new Activity( 5 , 9 );
arr[ 1 ] = new Activity( 1 , 2 );
arr[ 2 ] = new Activity( 3 , 4 );
arr[ 3 ] = new Activity( 0 , 6 );
arr[ 4 ] = new Activity( 5 , 7 );
arr[ 5 ] = new Activity( 8 , 9 );
printMaxActivities(arr, n);
}
}
|
Python3
def MaxActivities(arr, n):
selected = []
Activity.sort(key = lambda x: x[ 1 ])
i = 0
selected.append(arr[i])
for j in range ( 1 , n):
if arr[j][ 0 ] > = arr[i][ 1 ]:
selected.append(arr[j])
i = j
return selected
if __name__ = = '__main__' :
Activity = [[ 5 , 9 ], [ 1 , 2 ], [ 3 , 4 ], [ 0 , 6 ], [ 5 , 7 ], [ 8 , 9 ]]
n = len (Activity)
selected = MaxActivities(Activity, n)
print ( "Following activities are selected :" )
print (selected[ 0 ], end = "");
for i in range ( 1 , len (selected)):
print ( "," , end = " " )
print (selected[i], end = "")
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Activity {
public int start, finish;
public Activity( int start, int finish)
{
this .start = start;
this .finish = finish;
}
}
class GFG {
static void printMaxActivities(List<Activity> arr, int n)
{
arr = arr.OrderBy(a => a.finish).ToList();
Console.WriteLine(
"Following activities are selected :" );
int i = 0;
Console.Write( "(" + arr[i].start + ", "
+ arr[i].finish + ")" );
for ( int j = 1; j < n; j++) {
if (arr[j].start >= arr[i].finish) {
Console.Write( ", (" + arr[j].start + ", "
+ arr[j].finish + ")" );
i = j;
}
}
}
public static void Main( string [] args)
{
int n = 6;
List<Activity> arr = new List<Activity>();
arr.Add( new Activity(5, 9));
arr.Add( new Activity(1, 2));
arr.Add( new Activity(3, 4));
arr.Add( new Activity(0, 6));
arr.Add( new Activity(5, 7));
arr.Add( new Activity(8, 9));
printMaxActivities(arr, n);
}
}
|
Javascript
<script>
function MaxActivities(arr, n){
let selected = [];
Activity = Activity.sort( function (a,b) {
return a[1] - b[1];
});
let i = 0
selected.push(arr[i]);
for (let j=1;j<n;j++){
if ( arr[j][0] >= arr[i][1]){
selected.push(arr[j]);
i = j;
}
}
return selected;
}
Activity = [[5, 9], [1, 2], [3, 4], [0, 6],[5, 7], [8, 9]];
n = Activity.length;
selected = MaxActivities(Activity, n);
document.write( "Following activities are selected : <br>" )
console.log(selected)
for (let i = 0;i<selected.length;i++)
document.write( "(" +selected[i]+ "), " )
</script>
|
OutputFollowing activities are selected :
(1, 2), (3, 4), (5, 7), (8, 9)
Time Complexity: O(N log N), If input activities may not be sorted. It takes O(n) time when it is given that input activities are always sorted.
Auxiliary Space: O(1)
Activity Selection Problem using Priority-Queue:
We can use Min-Heap to get the activity with minimum finish time. Min-Heap can be implemented using priority-queue
Follow the given steps to solve the problem:
- Create a priority queue (Min-Heap) and push the activities into it.
- Push the top of the priority queue into the answer vector and set the variable start to the start time of the first activity and end to the finish time of the activity
- While priority is not empty do the following:
- Take the top of the priority queue and check
- If the start time of this activity is greater than or equal to the finish time of the last chosen activity then push this activity into the answer vector
- Else ignore it
- Print the activities chosen, stored in the answer vector
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void SelectActivities(vector< int > s, vector< int > f)
{
vector<pair< int , int > > ans;
priority_queue<pair< int , int >, vector<pair< int , int > >,
greater<pair< int , int > > >
p;
for ( int i = 0; i < s.size(); i++) {
p.push(make_pair(f[i], s[i]));
}
auto it = p.top();
int start = it.second;
int end = it.first;
p.pop();
ans.push_back(make_pair(start, end));
while (!p.empty()) {
auto itr = p.top();
p.pop();
if (itr.second >= end) {
start = itr.second;
end = itr.first;
ans.push_back(make_pair(start, end));
}
}
cout << "Following Activities should be selected. "
<< endl
<< endl;
for ( auto itr = ans.begin(); itr != ans.end(); itr++) {
cout << "Activity started at " << (*itr).first
<< " and ends at " << (*itr).second << endl;
}
}
int main()
{
vector< int > s = { 1, 3, 0, 5, 8, 5 };
vector< int > f = { 2, 4, 6, 7, 9, 9 };
SelectActivities(s, f);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static class Pair {
int first;
int second;
Pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static void SelectActivities( int s[], int f[])
{
ArrayList<Pair> ans = new ArrayList<>();
PriorityQueue<Pair> p = new PriorityQueue<>(
(p1, p2) -> p1.first - p2.first);
for ( int i = 0 ; i < s.length; i++) {
p.add( new Pair(f[i], s[i]));
}
Pair it = p.poll();
int start = it.second;
int end = it.first;
ans.add( new Pair(start, end));
while (!p.isEmpty()) {
Pair itr = p.poll();
if (itr.second >= end) {
start = itr.second;
end = itr.first;
ans.add( new Pair(start, end));
}
}
System.out.println(
"Following Activities should be selected. \n" );
for (Pair itr : ans) {
System.out.println(
"Activity started at " + itr.first
+ " and ends at " + itr.second);
}
}
public static void main(String[] args)
{
int s[] = { 1 , 3 , 0 , 5 , 8 , 5 };
int f[] = { 2 , 4 , 6 , 7 , 9 , 9 };
SelectActivities(s, f);
}
}
|
Python3
from heapq import heappop, heappush
def SelectActivities(s, f):
ans = []
p = []
for i, j in zip (s, f):
heappush(p, (j, i))
it = heappop(p)
start = it[ 1 ]
end = it[ 0 ]
ans.append(it)
while p:
it = heappop(p)
if it[ 1 ] > = end:
start = it[ 1 ]
end = it[ 0 ]
ans.append(it)
print ( "Following Activities should be selected.\n" )
for f, s in ans:
print (f "Activity started at {s} and ends at {f}" )
if __name__ = = "__main__" :
s = [ 1 , 3 , 0 , 5 , 8 , 5 ]
finish = [ 2 , 4 , 6 , 7 , 9 , 9 ]
SelectActivities(s, finish)
|
C#
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
static void SelectActivities(List< int > s, List< int > f)
{
List<Tuple< int , int > > ans = new List<Tuple< int , int > >();
var p = new List<Tuple< int , int >>();
for ( int i = 0; i < s.Count; i++) {
p.Add(Tuple.Create(f[i], s[i]));
}
p.Sort();
var it = p[0];
int start = it.Item2;
int end = it.Item1;
p.RemoveAt(0);
ans.Add(Tuple.Create(start, end));
while (p.Count > 0) {
var itr = p[0];
p.RemoveAt(0);
if (itr.Item2 >= end) {
start = itr.Item2;
end = itr.Item1;
ans.Add(Tuple.Create(start, end));
}
}
Console.Write( "Following Activities should be selected.\n\n" );
foreach ( var itr in ans)
Console.Write( "Activity started at " + itr.Item1
+ " and ends at " + itr.Item2 + "\n" );
}
public static void Main( string [] args)
{
List< int > s = new List< int > { 1, 3, 0, 5, 8, 5 };
List< int > f = new List< int > { 2, 4, 6, 7, 9, 9 };
SelectActivities(s, f);
}
}
|
Javascript
<script>
class Pair
{
constructor(first,second)
{
this .first = first;
this .second = second;
}
}
function SelectActivities(s,f)
{
let ans = [];
let p = [];
for (let i = 0; i < s.length; i++) {
p.push( new Pair(f[i], s[i]));
}
p.sort( function (a,b){ return a.first-b.first;});
let it = p.shift();
let start = it.second;
let end = it.first;
ans.push( new Pair(start, end));
while (p.length!=0) {
let itr = p.shift();
if (itr.second >= end) {
start = itr.second;
end = itr.first;
ans.push( new Pair(start, end));
}
}
document.write(
"Following Activities should be selected. <br>" );
for (let itr of ans.values()) {
document.write(
"Activity started at: " + itr.first
+ " and ends at " + itr.second+ "<br>" );
}
}
let s=[1, 3, 0, 5, 8, 5 ];
let f=[2, 4, 6, 7, 9, 9 ];
SelectActivities(s, f);
</script>
|
OutputFollowing Activities should be selected.
Activity started at: 1 and ends at 2
Activity started at: 3 and ends at 4
Activity started at: 5 and ends at 7
Activity started at: 8 and ends at 9
Time Complexity: O(N * log N)
Auxiliary Space: O(N)