43 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
  • 159k
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,951
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
  • 39k
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
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
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

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
  • 159k
5 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
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
  • 39k
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
  • 39k
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
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

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,612
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
  • 14k
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

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
  • 39k
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,522
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
  • 39k
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
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
  • 39k
4 votes
Accepted

Expected behavior of an algorithm to minimize rankings

Finding the minimum matching is known as the assignment problem, and it has efficient solutions. Your exact problem has been considered by Parviainen, Random assignment with integer costs — it's his &...
Yuval Filmus's user avatar
4 votes
Accepted

Parition a multiset of numbers into two subsets, how to maximize the sum of their medians?

Your algorithm is correct. The following is its proof of correctness. Let $S_1$ and $S_2$ be the optimal partitions of $S$. Let their medians be $m_1$ and $m_2$. Let the maximum element is $M$ that ...
Inuyasha Yagami's user avatar
4 votes
Accepted

Greedy filling unit intervals

The greedy algorithm Let $result$ be an empty set and let $last\_interval$ be none. For each $num$ in sorted $X$: If $num$ is not covered by the last interval: let $last\_interval$ be the interval ...
John L.'s user avatar
  • 39k
4 votes

I want to start preparing for a job interviews for which i want to get a decent at solving DSA problems

I don't think this is entirely a matter of opinion like the other commentor has said (though they are right, that it can be a bit opinionated or subjective, everyone finds different things easier/...
Minko_Minkov's user avatar
3 votes
Accepted

How does "Greedy Stays Ahead" Prove an Optimal Greedy Algorithm?

What we are saying is that if A is not optimal, then the number of jobs in A (let it be k) should be less than the number of jobs in O ( let it be m). That means, there must be (k+1)-th job in O, ...
nepee's user avatar
  • 280

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