dominique38's blog

By dominique38, history, 3 months ago, In English

This blog post is a submission for the Codeforces Month of Blog Posts Pt. III challenge. Thank you cadmiumky for the initiative!

This is how I wish I had been introduced to Greedy. Learning how to prove is the truly valuable skill, one that I believe good Greedy problems test rigorously.

Introduction

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 smaller independent 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. We won't go into those details here, instead we will generally assume our problems have the Optimal Substructure property.

Short Note

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.

The idea behind DP is the same everywhere, gather enough observations to define your subproblems, transitions, and base cases. This same idea is explored in a lot of diverse places—there is DP on Bitmasks, Trees, Graphs, Digits, Ranges, Weird DP Sorcery? and On and on.., but there are not many discussions 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 a greedy solution, the arguments are almost always Proof by Contradiction or Induction. The argument goes like this: let's assume a greedy solution $$$G$$$ and a magical optimal solution $$$O$$$ that doesn't follow the greedy choices. Then, given our observations, we prove that either $$$G$$$ performs no worse than $$$O$$$, or that given the constraints, the final solution produced by both strategies will look exactly the same. On the flip side, among many greedy choices, to disprove one, the easiest way is to find a counter-example.

Below, I have listed links to various problems and discussed the greedy ideas they use, along with some common greedy examples. Sometimes Greedy seems obvious and intuitive, but I encourage you to try forming a rigorous argument while proving the problems mentioned below.

Problems

I. Basic problems (without links)

Try to come up with formal arguments to get the hang of it. Basic doesn't always mean easy.

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

Hint
Solution

P2.I am running along an infinitely long number line. I can run for at most $$$M$$$ units without refreshments. There are $$$n$$$ refreshment stalls, where the $$$i$$$-th stall is located at $$$x_i$$$. Find the strategy that minimizes the number of refreshment breaks. There is a target $$$T$$$ located somewhere on the number line. All stalls are placed such that it is always possible to reach the target.

Hint
Solution

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

Hint
Solution

P4. Given an array of $$$n$$$ activities, where the $$$i$$$-th activity takes $$$p_i$$$ time to complete. I want to schedule activities such that they don't overlap.

  • Let $$$c_i$$$ be the completion time of the $$$i$$$-th activity in the schedule. If the $$$i$$$-th activity starts at time $$$x$$$, then its completion time is $$$c_i = x + p_i$$$. Goal is to minimize $$$\sum_{i=1}^{n} c_i$$$ (which is equivalent to minimizing the average completion time).
Hint
Solution
  • Each activity has a start time $$$s_i$$$ and a finish time $$$f_i$$$. Find the maximum number of non-overlapping activities you can perform.
Hint
Hint
Solution

Note: The graph is denoted by $$$H$$$, as the letter $$$G$$$ represents the Greedy solution.

By now, we must have gotten some idea about how to find properties that connect the Greedy solution $$$G$$$ and the Optimal solution $$$O$$$, eventually proving they are the same.The same logic applies to problems involving graphs or trees. We often assume the existence of a specific optimal tree $$$T_{O}$$$, a subgraph $$$H_{O} \subset H$$$, or even a partially constructed subtree $$$T_{O}' \subset T_{O} \subset H$$$ , basically anything concrete that we can "play with" to formulate rigorous arguments about their behavior.

P5. Given an undirected graph $$$H=(V,E)$$$ with non-negative weights.

  • Find its Minimum Spanning Tree (MST).
Hint
Hint
Solution
  • Find a strategy to find the Second Best Minimum Spanning Tree.
Hint
Solution
Bonus
II. Basic Problems (with Links)
Random Thought
  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,
  9. Tracks in the Snow
  10. Find the k-sum of an array
  11. Erasing Vertices 2
  12. Path and Subsequence

Outro

I hope this blog helps both beginners and intermediate problem solvers.

These are just a few of the interesting Greedy problems I've shared. If you know of other problems with elegant or tricky Greedy arguments, please share them! Also, if you spot any mistakes or if any part of the explanation wasn't clear, don't hesitate to point it out.

Happy Solving! ^_^

Update 1: Clarified the wording of P2.

  • Vote: I like it
  • +48
  • Vote: I do not like it

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by dominique38 (previous revision, new revision, compare).

»
2 months ago, hide # |
 
Vote: I like it +4 Vote: I do not like it

Brilliant way to think about greedy — Thank You So Much

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Hi I loved this blog, it was very useful for me! I also don't feel comfortable until I have a concrete enough proof, but I have no formal math training, so your basic examples were very helpful, I spent the whole evening proving through all of them lol.

I will certainly be working through your list of recommended greedy problems.

Some suggestions, I think the wording of the refreshment break run problem can be clarified. It says infinitely long line, so initially I thought we were trying to run forever. Then I thought the best answer was just to run for a distance of 0 and take no refreshments lol. Maybe specify that you have a fixed starting point, and must run to a fixed ending point, and refreshment stops are put in between in a way that guarantees it is possible to get from start to end.

Also here is a greedy/observation problem i think is cool: https://mirror.codeforces.com/contest/2122/problem/C

why i think is cool (spoilers)
»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

https://oj.uz/problem/view/JOI18_candies this problem is mind blowing, but its pretty hard

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by dominique38 (previous revision, new revision, compare).

»
2 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

W

»
2 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

Could I also suggest this greedy problem, so many exchange arguments in the editorial: https://mirror.codeforces.com/contest/2164/problem/C