What is Greedy?

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 of the complete Activity Selection Problem.

An activity-selection is the problem of scheduling a resource among several competing activity.

Statement: Given a set S of n activities with and start time, Sand fi, finish time of an ith activity. Find the maximum size set of mutually compatible activities.

Compatible Activities
Activities i and j are compatible if the half-open internal [si, fi) and [sj, fj) do not overlap, that is, i and j are compatible if s fj  and sj  fi

activity-selection-problem

Greedy Algorithm for Selection Problem

I. Sort the input activities by increasing finishing time.
f1 ≤  f2 ≤ . . . ≤  fn

II Call GREEDY-ACTIVITY-SELECTOR (Sif)

  1. n = length [s]
  2. A={i}
  3. j = 1
  4. FOR i = 2 to n
  5.         do if  si ≥ fj
  6.             then A= AU{i}
  7.                     j = i
  8. Return A

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, Fractional Knapsack problem (See this) can be solved using Greedy, but 0-1 Knapsack cannot be solved using Greedy.

Here is a standard algorithms that are Greedy algorithms.

  • Kruskal’s Minimum Spanning Tree (MST): In Kruskal’s algorithm, we create a 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.
  • Prim’s Minimum Spanning Tree: In Prim’s algorithm also, we create a MST by picking edges one by one. We maintain two sets: 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 Shortest Path: The Dijkstra’s algorithm is very similar to Prim’s algorithm. The shortest path tree is built up, edge by edge. We maintain two sets: set of the vertices already included in the tree and the 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 source to the set that contains not yet included vertices.
  • Huffman Coding: Huffman Coding is a loss-less compression technique. It assigns variable length bit codes to different characters. The Greedy Choice is to assign least bit length code to the most frequent character.
[ad type=”banner”]

Why do we need Greedy Algorithms?

The greedy algorithms are sometimes also used to get an approximation for Hard optimization problems. For example, Traveling Salesman Problem is a NP Hard problem. A Greedy choice for this problem is to pick the nearest unvisited city from the current city at every step. This solutions doesn’t always produce the best optimal solution, but can be used to get an approximate optimal solution.

Let us consider the Activity Selection problem as our first example of Greedy algorithms. Following is the problem statement.

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.

Example:

Example 1 : Consider the following 3 activities sorted by
by finish time.
     start[]  =  {10, 12, 20};
     finish[] =  {20, 25, 30};
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[] ]

Example 2 : Consider the following 6 activities 
sorted by by finish time.
     start[]  =  {1, 3, 0, 5, 8, 5};
     finish[] =  {2, 4, 6, 7, 9, 9};
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[] ]

The greedy choice is to always pick the next activity whose finish time is least among the remaining activities and the start time is more than or equal to the finish time of previously selected activity. We can sort the activities according to their finishing time so that we always consider the next activity as minimum finishing time activity.

1) Sort the activities according to their finishing time

2) Select the first activity from the sorted array and print it.

3) Do following for remaining activities in the sorted array.

…….a) If the start time of this activity is greater than or equal to the finish time of previously selected activity then select this activity and print it.

In the following C implementation, it is assumed that the activities are already sorted according to their finish time.

[ad type=”banner”]

C++

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20activity%20selection%20problem.%0A%2F%2F%20The%20following%20implementation%20assumes%20that%20the%20activities%0A%2F%2F%20are%20already%20sorted%20according%20to%20their%20finish%20time%0A%23include%3Cstdio.h%3E%0A%0A%2F%2F%20Prints%20a%20maximum%20set%20of%20activities%20that%20can%20be%20done%20by%20a%20single%0A%2F%2F%20person%2C%20one%20at%20a%20time.%0A%2F%2F%20%20n%20%20%20–%3E%20%20Total%20number%20of%20activities%0A%2F%2F%20%20s%5B%5D%20–%3E%20%20An%20array%20that%20contains%20start%20time%20of%20all%20activities%0A%2F%2F%20%20f%5B%5D%20–%3E%20%20An%20array%20that%20contains%20finish%20time%20of%20all%20activities%0Avoid%20printMaxActivities(int%20s%5B%5D%2C%20int%20f%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20int%20i%2C%20j%3B%0A%0A%20%20%20%20printf%20(%22Following%20activities%20are%20selected%20%5Cn%22)%3B%0A%0A%20%20%20%20%2F%2F%20The%20first%20activity%20always%20gets%20selected%0A%20%20%20%20i%20%3D%200%3B%0A%20%20%20%20printf(%22%25d%20%22%2C%20i)%3B%0A%0A%20%20%20%20%2F%2F%20Consider%20rest%20of%20the%20activities%0A%20%20%20%20for%20(j%20%3D%201%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%2F%2F%20If%20this%20activity%20has%20start%20time%20greater%20than%20or%0A%20%20%20%20%20%20%2F%2F%20equal%20to%20the%20finish%20time%20of%20previously%20selected%0A%20%20%20%20%20%20%2F%2F%20activity%2C%20then%20select%20it%0A%20%20%20%20%20%20if%20(s%5Bj%5D%20%3E%3D%20f%5Bi%5D)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20printf%20(%22%25d%20%22%2C%20j)%3B%0A%20%20%20%20%20%20%20%20%20%20i%20%3D%20j%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20s%5B%5D%20%3D%20%20%7B1%2C%203%2C%200%2C%205%2C%208%2C%205%7D%3B%0A%20%20%20%20int%20f%5B%5D%20%3D%20%20%7B2%2C%204%2C%206%2C%207%2C%209%2C%209%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(s)%2Fsizeof(s%5B0%5D)%3B%0A%20%20%20%20printMaxActivities(s%2C%20f%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

Java

[pastacode lang=”java” manual=”%2F%2F%20The%20following%20implementation%20assumes%20that%20the%20activities%0A%2F%2F%20are%20already%20sorted%20according%20to%20their%20finish%20time%0Aimport%20java.util.*%3B%0Aimport%20java.lang.*%3B%0Aimport%20java.io.*%3B%0A%0Aclass%20ActivitySelection%0A%7B%0A%20%20%20%20%2F%2F%20Prints%20a%20maximum%20set%20of%20activities%20that%20can%20be%20done%20by%20a%20single%0A%20%20%20%20%2F%2F%20person%2C%20one%20at%20a%20time.%0A%20%20%20%20%2F%2F%20%20n%20%20%20–%3E%20%20Total%20number%20of%20activities%0A%20%20%20%20%2F%2F%20%20s%5B%5D%20–%3E%20%20An%20array%20that%20contains%20start%20time%20of%20all%20activities%0A%20%20%20%20%2F%2F%20%20f%5B%5D%20–%3E%20%20An%20array%20that%20contains%20finish%20time%20of%20all%20activities%0A%20%20%20%20public%20static%20void%20printMaxActivities(int%20s%5B%5D%2C%20int%20f%5B%5D%2C%20int%20n)%0A%20%20%20%20%7B%0A%09int%20i%2C%20j%3B%0A%09%20%0A%09System.out.print(%22Following%20activities%20are%20selected%20%3A%20%5Cn%22)%3B%0A%09%20%0A%09%2F%2F%20The%20first%20activity%20always%20gets%20selected%0A%09i%20%3D%200%3B%0A%09System.out.print(i%2B%22%20%22)%3B%0A%09%20%0A%09%2F%2F%20Consider%20rest%20of%20the%20activities%0A%09for%20(j%20%3D%201%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%09%7B%0A%09%20%20%20%20%20%2F%2F%20If%20this%20activity%20has%20start%20time%20greater%20than%20or%0A%09%20%20%20%20%20%2F%2F%20equal%20to%20the%20finish%20time%20of%20previously%20selected%0A%09%20%20%20%20%20%2F%2F%20activity%2C%20then%20select%20it%0A%09%20%20%20%20%20if%20(s%5Bj%5D%20%3E%3D%20f%5Bi%5D)%0A%09%20%20%20%20%20%7B%0A%09%20%20%20%20%20%20%20%20%20%20System.out.print(j%2B%22%20%22)%3B%0A%09%20%20%20%20%20%20%20%20%20%20i%20%3D%20j%3B%0A%09%20%20%20%20%20%20%7D%0A%09%20%7D%0A%20%20%20%20%7D%0A%09%20%0A%20%20%20%20%2F%2F%20driver%20program%20to%20test%20above%20function%0A%20%20%20%20public%20static%20void%20main(String%5B%5D%20args)%0A%20%20%20%20%7B%0A%09int%20s%5B%5D%20%3D%20%20%7B1%2C%203%2C%200%2C%205%2C%208%2C%205%7D%3B%0A%09int%20f%5B%5D%20%3D%20%20%7B2%2C%204%2C%206%2C%207%2C%209%2C%209%7D%3B%0A%09int%20n%20%3D%20s.length%3B%0A%09%20%20%20%0A%09printMaxActivities(s%2C%20f%2C%20n)%3B%0A%20%20%20%20%7D%0A%09%0A%7D” message=”Java” highlight=”” provider=”manual”/]

Python

[pastacode lang=”python” manual=”%22%22%22The%20following%20implementation%20assumes%20that%20the%20activities%0Aare%20already%20sorted%20according%20to%20their%20finish%20time%22%22%22%0A%0A%22%22%22Prints%20a%20maximum%20set%20of%20activities%20that%20can%20be%20done%20by%20a%0Asingle%20person%2C%20one%20at%20a%20time%22%22%22%0A%23%20n%20–%3E%20Total%20number%20of%20activities%0A%23%20s%5B%5D–%3E%20An%20array%20that%20contains%20start%20time%20of%20all%20activities%0A%23%20f%5B%5D%20–%3E%20An%20array%20that%20conatins%20finish%20time%20of%20all%20activities%0A%0Adef%20printMaxActivities(s%20%2C%20f%20)%3A%0A%20%20%20%20n%20%3D%20len(f)%0A%20%20%20%20print%20%22The%20following%20activities%20are%20selected%22%0A%0A%20%20%20%20%23%20The%20first%20activity%20is%20always%20selected%0A%20%20%20%20i%20%3D%200%0A%20%20%20%20print%20i%2C%0A%0A%20%20%20%20%23%20Consider%20rest%20of%20the%20activities%0A%20%20%20%20for%20j%20in%20xrange(n)%3A%0A%0A%20%20%20%20%20%20%20%20%23%20If%20this%20activity%20has%20start%20time%20greater%20than%0A%20%20%20%20%20%20%20%20%23%20or%20equal%20to%20the%20finish%20time%20of%20previously%0A%20%20%20%20%20%20%20%20%23%20selected%20activity%2C%20then%20select%20it%0A%20%20%20%20%20%20%20%20if%20s%5Bj%5D%20%3E%3D%20f%5Bi%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20print%20j%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20i%20%3D%20j%0A%0A%23%20Driver%20program%20to%20test%20above%20function%0As%20%3D%20%5B1%20%2C%203%20%2C%200%20%2C%205%20%2C%208%20%2C%205%5D%0Af%20%3D%20%5B2%20%2C%204%20%2C%206%20%2C%207%20%2C%209%20%2C%209%5D%0AprintMaxActivities(s%20%2C%20f)%0A%0A%23%20This%20code%20is%20contributed%20by%20Nikhil%20Kumar%20Singh” message=”” highlight=”” provider=”manual”/]

Output:

Following activities are selected
0 1 3 4
[ad type=”banner”]

How Greedy Choice work for Activities sorted according to finish time?

Let the give set of activities be S = {1, 2, 3, ..n} and activities be sorted by finish time. The greedy choice is to always pick activity 1. How come the activity 1 always provides one of the optimal solutions. We can prove it by showing that if there is another solution B with first activity other than 1, then there is also a solution A of same size with activity 1 as first activity. Let the first activity selected by B be k, then there always exist A = {B – {k}} U {1}.(Note that the activities in B are independent and k has 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 activities sorted, we apply same above algorithm.

[pastacode lang=”cpp” manual=”%2F%2F%20C%2B%2B%20program%20for%20activity%20selection%20problem%0A%2F%2F%20when%20input%20activities%20may%20not%20be%20sorted.%0A%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Ausing%20namespace%20std%3B%0A%0A%2F%2F%20A%20job%20has%20start%20time%2C%20finish%20time%20and%20profit.%0Astruct%20Activitiy%0A%7B%0A%20%20%20%20int%20start%2C%20finish%3B%0A%7D%3B%0A%0A%2F%2F%20A%20utility%20function%20that%20is%20used%20for%20sorting%0A%2F%2F%20activities%20according%20to%20finish%20time%0Abool%20activityCompare(Activitiy%20s1%2C%20Activitiy%20s2)%0A%7B%0A%20%20%20%20return%20(s1.finish%20%3C%20s2.finish)%3B%0A%7D%0A%0A%2F%2F%20Returns%20count%20of%20maximum%20set%20of%20activities%20that%20can%0A%2F%2F%20be%20done%20by%20a%20single%20person%2C%20one%20at%20a%20time.%0Avoid%20printMaxActivities(Activitiy%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20%2F%2F%20Sort%20jobs%20according%20to%20finish%20time%0A%20%20%20%20sort(arr%2C%20arr%2Bn%2C%20activityCompare)%3B%0A%0A%20%20%20%20cout%20%3C%3C%20%22Following%20activities%20are%20selected%20%5Cn%22%3B%0A%0A%20%20%20%20%2F%2F%20The%20first%20activity%20always%20gets%20selected%0A%20%20%20%20int%20i%20%3D%200%3B%0A%20%20%20%20cout%20%3C%3C%20%22(%22%20%3C%3C%20arr%5Bi%5D.start%20%3C%3C%20%22%2C%20%22%20%3C%3C%20arr%5Bi%5D.finish%20%3C%3C%20%22)%2C%20%22%3B%0A%0A%20%20%20%20%2F%2F%20Consider%20rest%20of%20the%20activities%0A%20%20%20%20for%20(int%20j%20%3D%201%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%2F%2F%20If%20this%20activity%20has%20start%20time%20greater%20than%20or%0A%20%20%20%20%20%20%2F%2F%20equal%20to%20the%20finish%20time%20of%20previously%20selected%0A%20%20%20%20%20%20%2F%2F%20activity%2C%20then%20select%20it%0A%20%20%20%20%20%20if%20(arr%5Bj%5D.start%20%3E%3D%20arr%5Bi%5D.finish)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20%22(%22%20%3C%3C%20arr%5Bj%5D.start%20%3C%3C%20%22%2C%20%22%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%3C%3C%20arr%5Bj%5D.finish%20%3C%3C%20%22)%2C%20%22%3B%0A%20%20%20%20%20%20%20%20%20%20i%20%3D%20j%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%0A%2F%2F%20Driver%20program%0Aint%20main()%0A%7B%0A%20%20%20%20Activitiy%20arr%5B%5D%20%3D%20%7B%7B5%2C%209%7D%2C%20%7B1%2C%202%7D%2C%20%7B3%2C%204%7D%2C%20%7B0%2C%206%7D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B5%2C%207%7D%2C%20%7B8%2C%209%7D%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20%20%20printMaxActivities(arr%2C%20n)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”C++” highlight=”” provider=”manual”/]

 Output:

Following activities are selected 
(1, 2), (3, 4), (5, 7), (8, 9), 

Time Complexity : It takes O(n log n) time if input activities may not be sorted. It takes O(n) time when it is given that input activities are always sorted.

Categorized in: