41 votes

How to prove greedy algorithm is correct

Ultimately, you'll need a mathematical proof of correctness. I'll get to some proof techniques for that below, but first, before diving into that, let me save you some time: before you look for a ...
D.W.'s user avatar
  • 156k
14 votes

How to prove greedy algorithm is correct

I will use the following simple sorting algorithm as an example: ...
adrianN's user avatar
  • 5,931
9 votes
Accepted

Matrix Chain Multiplication Greedy Approach

You don't state why you think that your algorithm is correct. In fact, it is incorrect. Here is an example. Consider the problem of computing the product of matrices of dimensions $2\times 1$, $1\...
Yuval Filmus's user avatar
8 votes
Accepted

Counterexample to this modified Dijkstra's

Your algorithm makes the wrong choice between the following two paths: 5 channels with a reliability of 50% (combined reliability 3.125%), weight $5 \cdot {1 \over 0.50} = 10$. A single channel with ...
orlp's user avatar
  • 12.7k
7 votes
Accepted

Is there a polynomial time algorithm to determine whether an 'up down' language is 'emptible'?

Reduction from 3-SAT: a variable in 3-SAT becomes a character in your problem and is paired with its negation. Each clause becomes a word. e.g. 3 SAT: (a,b,-c) && (-b,c) => pairs: (a,-a), (...
Albert Hendriks's user avatar
7 votes
Accepted

Find the lexicographically smallest order of N numbers such that the total of N numbers <= Threshold value

You are on the right track. It turns out the original question can be solved by a greedy algorithm. (A full blown solution by dynamic programming as I tried a while ago is both an overkill on coding ...
John L.'s user avatar
  • 38.6k
6 votes

greedy algorithm for Maximum directed cut

The starting point is the trivial random algorithm that chooses $S$ completely at random. Each directed edge is cut with probability $1/4$ (why?), and so in expectation, this random algorithm gives a $...
Yuval Filmus's user avatar
6 votes

A general algorithm for greedy algorithms

There is no such thing as the correct generalization of the greedy selection technique, because it's an informal technique. That said, there has been some effort at modeling the greedy heuristic, with ...
Yuval Filmus's user avatar
5 votes

Greedy Algorithms for Non-monotone Submodular Maximization with Cardinality Constraints

Edit: See my answer to the same question on math stackexchange here. I've copy and pasted the answer again for ease of use. There has been a recent line of work investigating algorithms for ...
Chris Harshaw's user avatar
5 votes
Accepted

set with maximum sum consisting of mutually co-prime numbers

Project Euler asks you to solve the problems yourself, without help. So dont read on if you want to submit a solution for Project Euler; that would be cheating. Since the numbers are mutually co-...
gnasher729's user avatar
  • 28.4k
5 votes
Accepted

Vertex cover algorithms for directed graphs?

Thanks for the edit! This isn't vertex cover; it's something different. There are simple algorithms for this problem. Decompose the graph into a dag of strongly connected components. (The dag is ...
D.W.'s user avatar
  • 156k
5 votes
Accepted

Greedy algorithms: Minimum sum number pairing

Suppose that numbers are $x_1, \ldots x_{2n}$, and let us rename them as $a_1, \ldots a_n, b_1, \ldots, b_n$, where $a_i \geq b_j$ for any $i, j$, $a_1 \geq a_2 \geq \ldots \geq a_n$, and $b_1 \leq ...
jwg's user avatar
  • 205
5 votes
Accepted

Prove that the greedy algorithm to remove k digits from a n-digit positive integer is optimal

The greedy algorithm is optimal. The simple observation is that any optimal $k$ digits to remove must contain the rightmost digit in the initial non-decreasing digits of A, or one of its equivalents....
John L.'s user avatar
  • 38.6k
5 votes
Accepted

N numbers, N/2 pairs. Minimizing the maximum sum of a pairing. Proving greedy algorithm

Can you see why $\max((-2)+5, 3+4) \lt \max(-2+3, 4+5)$? The reason is simple. Because on the right hand side, the maximum number 5 is not paired with the minimum number. Let the numbers are $a_1\...
John L.'s user avatar
  • 38.6k
5 votes
Accepted

Dynamic programming: optimal order to answer questions to score the maximum expected marks

First, if any $p_i=0$, then immediately throw it away since you're guaranteed to lose. If any probability is $1$ then immediately ask it! (after all why risk not getting the reward when you're ...
AspiringMat's user avatar
4 votes

Correctness of the greedy algorithm

The example provided by Valentin Lorentz can be slightly modified to break your solution for one order of traversal: 1 0 0 1 0 0 0 0 0 0 0 0 You can build a ...
Mihai's user avatar
  • 416
4 votes

Coin Change Problem(Greedy Algorithm)

For the set of coins (2,3,11). $\frac{3}{2}<\frac{11}{3}$ so by your assumption we can be greedy here. Consider the value of 23. The greedy strategy would involve first taking 2 11 cent coins to ...
lPlant's user avatar
  • 1,602
4 votes
Accepted

Information about ε-greedy algorithms

ε-greedy is just a way to promote exploration in Reinforcement Learning. I would not classify SARSA or Q-Learning as ε-greedy algorithms. The latter are very common reinforcement learning algorithms ...
Juan Leni's user avatar
  • 325
4 votes

How to prove greedy algorithm is correct

Jeff Ericson in his "Algorithms" states three conditions: Greedy choice: There is an optimal solution that includes the choice the algorithm makes. Inductive structure: The smaller ...
vonbrand's user avatar
  • 13.9k
4 votes
Accepted

Dynamic Programming vs Greedy - coin change problem

A dynamic approach would say that "x$ can be made out of change using, as the first coin, v1 or v2 or v3 ... or vn" and then build a table so that the second coin would be v1 or v2 or v3 ... or vn + ...
EvHi's user avatar
  • 56
4 votes
Accepted

Maximum cut using a 1/2 approximation greedy algorithm

Let us say that an edge $(v,w)$ belongs to $v$ if when $v$ is processed, $w \in A \cup B$, and that $(v_1,v_2)$ belongs to $v_2$. Denote by $N_v$ the number of edges belonging to $v$, and by $C_v$ the ...
Yuval Filmus's user avatar
4 votes
Accepted

Correctness proof for greedy algorithm based on ratio

The strategy to prove your ratio greedy algorithm is what I called "unimprovable solution by exchange of elements". Instead of proving that an algorithm produces the optimal solution, this strategy ...
John L.'s user avatar
  • 38.6k
4 votes
Accepted

Merging balls interview problem

Keep all the values $\frac{D_i - D_{i+1}}{V_i - V_{i+1}}$ in a min-heap. At each step, remove the minimum value, say $T = \frac{D_i - D_{i+1}}{V_i - V_{i+1}}$. We would first like to update all ...
Yuval Filmus's user avatar
4 votes
Accepted

Gas Station problem : Fixed path variation

The greedy strategy works in this case (fill up as much as you need to reach the next cheaper city, or fill up to K if no cheaper city is reachable with a full tank). The proof is pretty much the ...
Tassle's user avatar
  • 2,322
4 votes
Accepted

Why this greedy algorithm fails in rod cutting problem?

Because a local maximum is not always a global maximum. The length 3 rod is the most price-for-length effective single rod piece. But the optimization goal isn't to find the best price-to-length ...
Aaron Rotenberg's user avatar
4 votes

Greedy heuristic for buying fewest fridges of set temperature for products that can be kept in some temp. ranges?

The formalized problem here is selecting the minimum number of points such that for each temperature interval $\ell_i\in L$ we have at least one point that covers it. Let each interval $\ell_i$ be ...
Throckmorton's user avatar
4 votes
Accepted

Difficulty in understanding the proof of the lemma : "Matroids exhibit the optimal-substructure property"

The adjective "maximum-weight" should not appear in item (1) in that proof of the lemma. This is a minor bug of that famous book. To be fully clear, item (1) should have been the following. ...
John L.'s user avatar
  • 38.6k
4 votes
Accepted

Why this problem has such a simple solution? How would you avoid looking for more complex solutions first?

In this case: It is obvious that you start with some task immediately, start a second task as soon as the first task is finished, then start a third task immediately after this etc. It is obviously ...
gnasher729's user avatar
  • 28.4k
4 votes
Accepted

Hamiltonian path greedy and anti-greedy algorithms

No. In fact, we can prove the following stronger proposition. Claim. Given a complete weighted undirected graph, a run of the greedy algorithm on it and a run of the anti-greedy algorithm on it, the $...
John L.'s user avatar
  • 38.6k

Only top scored, non community-wiki answers of a minimum length are eligible