I have a simple graph $G = (V,E)$ and each vertex has a range $[a,b]$. Every two vertices are connected only if $[a_1, b_1]$ and $[a_2, b_2]$ have a common subrange.

Each range of vertex is rV1 = [0,5] rV2 = [1,3] rV3 = [2,10] rV4 = [4,9] rV5 = [6,7] rV6 = [8,12] rV7 = [11,13]

Graph Created based on above ranges.

Based on the algorithm below i have to color the graph.

ranges = rV1,rV2,rV3,rV4,rV5,rV6,rV7...

if(number of rVi > 0){
 C_m  = MAXIMAL_COLOR_CLASS(rV1,rV2,rV3,rV4,rV5,rV6,rV7...);
 //paint C_m vertices with color m.
 //new_ranges <- remove C_m from rV1,rV2,rV3,rV4,rV5,rV6,rV7...
 return {C_m} U COLOR_INTERVAL_GRAPH(new_ranges)
   return []

C = []
i = 1
while(i <= new_range.size()){
  C = C U {Vi}
  j = i+1
  while(j <= new_range.size() AND rVi (not common subrange with rVj)){
  j = j+1
return C

How i know if the above algorithm uses the greedy strategy?

My work so far:

The algorithm has the 'greedy choice property' since it paints the most each turn, by choosing the best solution to the current subproblem without caring about future problems or backtracking.(can this be improved and how)?

Graph Colored (Is the graph colored correctly?).

  • $\begingroup$ Please ask only one question per post. If you have multiple questions, you can post them separately. We are a question-and-answer site, so we require you to articulate a specific question. A question usually ends with a "?". $\endgroup$
    – D.W.
    Jan 11, 2021 at 18:04
  • $\begingroup$ We discourage "please check whether my answer is correct" questions, as only "yes/no" answers are possible, which won't help you or future visitors. See here and here. Can you edit your post to ask about a specific conceptual issue you're uncertain about? As a rule of thumb, a good conceptual question should be useful even to someone who isn't looking at the problem you happen to be working on. If you just need someone to check your work, you might seek out a friend, classmate, or teacher. $\endgroup$
    – D.W.
    Jan 11, 2021 at 18:05

3 Answers 3


Many problems can be written in the form "You start with an initial state. You make a choice what your first action should be and perform the action, getting into a new state. You make another choice for your second action and perform that action, and so on. After a limited number of actions, you will arrive at the final state. You never undo any action that you have taken, and the effort determining the next action is limited". That's a greedy algorithm.

For example, the travelling salesman problem can be handled with a greedy algorithm. You just pick the first, second, third etc. place to visit, and your last action is to return to the starting point. The "limited effort" requirement is there so you don't solve the whole problem, take the first step, and call it a "greedy algorithm".

If you're lucky, you can prove that a greedy algorithm will find the optimal solution. Or you can prove that a greedy algorithm that isn't much worse than the optimal solution. You can vary the effort to determine the next step, and that may make your greedy algorithm slower but with better results.

A "Greedy" algorithm is often not optimal when making one choice that seems a good choice now makes it impossible to make a good choice later.


A greedy algorithm looks for an optimal solution in a restricted neighbourhood of your starting point.

Say you want to find the shortest path from a root to a leaf in a rooted tree (where the edges are marked with a positive integer indicating distance).

Start at the root. Check all the edges adjacent to the vertex (leading to the children of the root). This is your restricted neighbourhood. A greedy algorithm would select a shortest edge (randomly picking one if there are several shortest choices). It then moves to the next vertex at the other end of the selected edge and repeats, looking again locally at the edges adjacent on the new vertex. Once again it selects a shortest edge.

It is easy to see that this does not always leads to an optimal solution for the shortest distance from the root to a leaf.

Some greedy algorithms will however obtain an optimal solution (if not, you can often get by with dynamic programming). The main point is that the algorithm is greedy in the sense that it looks for the best optimal solution in an immediate local and limited range. The algorithm never reaches a global overview of the tree, i.e. its computation proceeds purely locally. Needless to say, greed is not always a great strategy.

A case where a greedy approach works very well is the coin change problem.

For this problem a financial amount to be returned in coins. The greedy algorithm produces change consisting of the fewest coins so the cash register is not depleted too soon. It does this by picking the largest coin that fits the initial amount to be returned and finds out how many of these coins fit that initial amount. It subtracts the total of these coins from the initial amount and repeats the strategy on the remaining amount. Again, it looks for the largest coin fitting this amount (a local situation, determined by the amount reached).

Existing currencies are designed to make this greedy approach work, i.e. the greedy algorithm will return change using the least amount of coins. You can design imaginary currencies for which the greedy algorithm will not work.

There are more formal approaches to the greedy algorithm strategy, but I'd merely repeat standard text books on the greedy choice property etc.


"A greedy algorithm is any algorithm that follows the problem-solving heuristic of making the locally optimal choice at each stage." (from Wikipedia) In other words, it does not backtrack. It builds up the solution incrementally, and when it adds something to the solution-so-far, it never later removes it (or, when it makes a decision, it never later undoes that decision).

Does that describe the algorithm you are looking at?

If you want to justify that it satisfies the greedy choice property, it might help to precisely state what is the "subproblem" that is solved at each step.

  • $\begingroup$ So my answer is correct, the algorithm does not backtrack and also makes the optimal choice at each stage since the criterion is to choose all non overlapping vertices (the ones that don't share an edge) with one another. $\endgroup$
    – Demokles
    Jan 11, 2021 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.