Beginner's Guide to Greedy

Revision en2, by dominique38, 2026-01-28 22:25:05

Introduction

I personally find greedy problems hard to derive. After struggling with proofs for quite a while, there are a few things I realized. Greedy proofs are very dependent on the rules of the problem. Loosely speaking each optimization problem gives you observations, from those observations, you realize that if you take certain choices while avoiding all others, then it will always be optimal. That realization is termed Greedy.

There are a lot of optimization problems, but only a small subset allows you to break the problem into subproblems where solving them optimally leads to the best global answer. This property, known as Optimal Substructure, is found in both DP and Greedy algorithms. Sadly, not every problem has this, for example, checking whether $$$N$$$ is prime, or solving a system of $$$N$$$ linear equations where all variables affect each other.

While DP and Greedy share the optimal substructure property, DP is essentially "smart brute force", where you define the subproblems and check all transitions to find the best one. In Greedy, however, you have to rigorously prove that ignoring all other choices and taking just one specific path works every single time. This takes the difficulty one notch up from DP.

Idea of the DP is the same everywhere, gather enough observations to that this is my subproblems, transitions and the base case, and this similar idea is explored a lot diverse places like, there dp on bitmasks, trees, digits, ranges and On and on.., but there are not many on Greedy. To fill this gap, I would like to discuss a lot of problems and proofs to capture the essence of how to prove Greedy solutions.

Idea

To prove greedy solution, their arguments are almost always Proof by Contradiction or Induction, where argument goes like lets assume a greedy solution $$$G$$$ and magical optimal solution $$$O$$$ that don't follow greedy choices, then given observations we prove that either $$$G$$$ performs no worse than $$$O$$$ , or given constraints the final solution given by both strategies will look exactly the same. And to prove that the strategy is not greedy, easiest way is to works out a counter-example.

There are a lot of problems i have referenced some of them are, Kanade's Perfect Multiples, Game on Array, Stay or Mirror, Ticket Hoarding, Maximum Running time of N computers, Group Increases, Xor factorization, Divine Tree, etc. Below i have mentioned the Greedy aspects, and some common greedy problems. Sometimes greedy seems obvious and intuitive but try to form a rigorous argument while proving the below mentioned problems.

Problems

Basic problems

Try to come up with formal arguments.

P1. I have $$$n$$$ items and knapsack of capacity $$$W$$$. The $$$i$$$th item has a value and weight $$$v_i$$$ and $$$w_i$$$, im allowed to take fractions of items. Find the strategy that maximizes the total value.

Hint
Solution

P2. I running along have infinitely long number line. I can run for $$$M$$$ units without refreshments. There are $$$n$$$ refreshment stalls where $$$i$$$th stall is located at $$$x_i$$$ on the number line for refreshment breaks. Find the strategy that minimizes number of refreshment breaks.

Hint
Solution

P3. Given 2 integer arrays $$$a$$$ and $$$b$$$ of size $$$n$$$ filled with positive integers. Reorder arrays to maximize $$$\prod_{i=1}^{n} a_{i}^{b_i}$$$. Too easy :P

Hint
Solution

P4. Given array of $$$n$$$ activities, $$$i$$$th activity takes $$$p_i$$$ time to complete. I want to schedule activities such activities dont overlap,

  1. Let $$$c_i$$$ be the completion time of the activities. Say $$$i$$$th activity starts at $$$x$$$th time, then its completion time is $$$c_i=x+p_i$$$. Minimize the $$$ \cdot \sum_{i=1}^{n} c_i$$$ , or divide by $$$n$$$ to call it minimum average completion time.
  2. Given another array $$$s$$$ that tells the starting time of the activities. Find the maximum set of activities you take.
Hint 1
Solution 1
Hint 2
Solution 2

P5. Given a undirected graph $$$G=(V,E)$$$ with positive weights.

  1. Find its Minimum spanning tree.
  2. Find strategy to find the 2nd best Minimum spanning tree.
Hint 1
Solution 1
Ideas used in above
  1. Kanade's Perfect Multiples,
  2. Game on Array,
  3. Stay or Mirror,
  4. Ticket Hoarding,
  5. Maximum Running time of N computers,
  6. Group Increases,
  7. Xor factorization,
  8. Divine Tree,
Tags greedy, mathematical induction

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en11 English dominique38 2026-02-12 19:00:11 183 Final Draft 2: Clarified the wording in P2.
en10 English dominique38 2026-02-10 15:19:29 2 (published)
en9 English dominique38 2026-02-10 15:04:08 23902 Final Draft 1
en8 English dominique38 2026-01-31 21:58:10 3923 Completed P1 to P5. Added little language correction.
en7 English dominique38 2026-01-31 17:18:09 5005 All P1 to P5 done. Remaining are the problem links.
en6 English dominique38 2026-01-29 14:50:47 2 Minor typo change
en5 English dominique38 2026-01-29 14:49:28 0 No change: Added Coauthors.
en4 English dominique38 2026-01-29 14:34:57 0 No change: Added Coauthors.
en3 English dominique38 2026-01-29 14:32:37 6118 Addition: P5 half done. P4 remaining. CF problems remaining.
en2 English dominique38 2026-01-28 22:25:05 8360 Addition: P1, P2 and P3 are done. P4 and P5 and onward are remaining.
en1 English dominique38 2026-01-28 17:16:48 4669 Initial revision: Added the basic structure, and rough content. (saved to drafts)