Neeldhara
  • About
  • Research
    • Overview
    • People
    • Publications
    • Surveys
  • Teaching
    • Courses
    • Materials
  • Lists
    • Puzzles
    • Bookmarks
  • Exposition
    • Talks
    • Videos
  • Events
  • Blog

3. Greedy Works!

  • Advanced Algorithms
    • Flows
    • Intractability
    • Approximation Algorithms
    • Randomized Algorithms
    • Greedy Algorithms
      • 1. Storing Files on a Tape
      • 2. A Scheduling Problem
      • 3. Stable Matchings
    • Matroids
      • 1. The Motivation
      • 2. The Definition
      • 3. Greedy Works!
      • 4. Scheduling with Deadlines
    • Dynamic Programming
      • 1. Longest Increasing Subsequence
      • 2. Subset Sum
      • 3. Set Cover
      • 4. Optimal BSTs
      • 5. Maximum Independent Set on Trees

3. Greedy Works!

algonotes
lecturenotes
Published

June 7, 2023

Acknowledgements

This write up is borrowed (with minor adaptations) from Chapter 4 of Jeff Erickson’s textbook on Algorithms.

Recall that from last time, we were dealing with:

  • a finite universe of elements U={e1,e2,⋯ ,en}\cal U = \{e_1, e_2, \cdots, e_n\}U={e1​,e2​,⋯,en​}
  • a weight function w:U⟶Q⩾0w: \cal U \longrightarrow \mathbb{Q}^{\geqslant 0}w:U⟶Q⩾0 that assigns a non-negative weight with every universe element
  • oracle access to a family F={S1,…,Sm}\cal F = \{S_1, \ldots, S_m\}F={S1​,…,Sm​} of subsets of U\cal UU

and we wanted to determine the maximum weight attained by any set in F\cal FF, where the weight of a set is defined as the sum of the weights of the elements that belong to it.

To this end, we proposed the following greedy algorithm:

A Greedy Algorithm
  1. Sort the elements of UUU in descending order of weight; i.e, WLOG w(e1)⩾⋯⩾w(en)w(e_1) \geqslant \cdots \geqslant w(e_n)w(e1​)⩾⋯⩾w(en​).
  2. Initialize a solution: specifically, let X=∅X = \emptysetX=∅.
  3. For i∈[n]i \in [n]i∈[n]: if X∪{ei}∈FX \cup \{e_i\} \in \cal FX∪{ei​}∈F, X⟵X∪{ei}X \longleftarrow X \cup \{e_i\}X⟵X∪{ei​}.

Since the greedy algorithm did not work “at large”, we agreed that it might be interesting to impose some constraints on F\cal FF and see if we could argue that the greedy algorithm works for all families that enjoy some additional structure. After some back and forth, here is what we came up with:

Matroids

A matroid (U,F)(\cal U, \cal F)(U,F) consists of a finite ground set U\cal UU and a collection F\mathcal{F}F of subsets of U\cal UU that satisfies three axioms:

  • Non-emptiness. The empty set is in F\mathcal{F}F. (Thus, F\mathcal{F}F is not itself empty.)

  • Heredity. If F\mathcal{F}F contains a subset S⊆US \subseteq \cal US⊆U, then F\mathcal{F}F contains every subset of SSS.

  • Exchange. If SSS and TTT are two sets in F\cal FF where ∣S∣>∣T∣|S|>|T|∣S∣>∣T∣, then T∪{e}∈FT \cup\{e\} \in \mathcal{F}T∪{e}∈F for some element e∈S\Te \in S \backslash Te∈S\T.

All this sets the stage for the main result that we want to get to:

The Greedy Algorithm Works

Theorem. For any matroid M=(U,F)\mathcal{M}=(\cal U, \mathcal{F})M=(U,F) and any weight function www, the greedy algorithm that takes (U,F,w)(\cal U, \mathcal{F}, w)(U,F,w) as input returns a maximum-weight basis of M\mathcal{M}M.

Proof. As you might anticipate, we use an exchange argument.

Let T={f1,f2,…,fk}T=\left\{f_1, f_2, \ldots, f_k\right\}T={f1​,f2​,…,fk​} be the independent set returned by the greedy algorithm on input (U,F,w))(\cal U, \mathcal{F}, w))(U,F,w)).

If any other element of U\cal UU could be added to TTT to obtain a larger independent set, the greedy algorithm would have added it. Thus, TTT is a basis.

For purposes of deriving a contradiction, suppose there is an independent set S={g1,g2,…,gℓ}S=\left\{g_1, g_2, \ldots, g_{\ell}\right\}S={g1​,g2​,…,gℓ​} such that ∑i=1kw(fi)<∑j=1ℓw(gi). \sum_{i=1}^k w\left(f_i\right)<\sum_{j=1}^{\ell} w\left(g_i\right) . i=1∑k​w(fi​)<j=1∑ℓ​w(gi​). Without loss of generality, assume that SSS is a basis. The exchange property now implies that k=ℓk=\ellk=ℓ.

Now suppose the elements of TTT and SSS are indexed in order of decreasing weight. Let iii be the smallest index such that w(fi)<w(gi)w\left(f_i\right)<w\left(g_i\right)w(fi​)<w(gi​), and consider the independent sets Ti−1={f1,f2,…,fi−1} and Si={g1,g2,…,gi−1,gi} T_{i-1}=\left\{f_1, f_2, \ldots, f_{i-1}\right\} \quad \text { and } \quad S_i=\left\{g_1, g_2, \ldots, g_{i-1}, g_i\right\} Ti−1​={f1​,f2​,…,fi−1​} and Si​={g1​,g2​,…,gi−1​,gi​}

By the exchange property, there is some element gj∈Sig_j \in S_igj​∈Si​ such that Ti−1∪{gj}T_{i-1} \cup\left\{g_j\right\}Ti−1​∪{gj​} is an independent set. We have:

w(gj)⩾w(gi)>w(fi).w\left(g_j\right) \geqslant w\left(g_i\right)>w\left(f_i\right).w(gj​)⩾w(gi​)>w(fi​).

Thus, the greedy algorithm considers and rejects the heavier element gjg_jgj​ before it considers the lighter element fif_ifi​.

But this is impossible, since the greedy algorithm accepts elements in decreasing order of weight.

This result also has an interesting flip side, so to speak. When dealing with set systems that do not satisfy the exchange axiom, it turns out that we can adverserially choose a weight function that misleads the greedy algorithm into producing suboptimal output! The intuition is the following: a violation of the exchange axiom in a set system (U,F)(\cal U, \cal F)(U,F) amounts to saying the following:

∃S,T∈F\exists S, T \in \cal F∃S,T∈F such that ∣S∣>∣T∣|S| > |T|∣S∣>∣T∣ but for all e∈S∖Te \in S \setminus Te∈S∖T, T∪{e}∉FT \cup \{e\} \notin \cal FT∪{e}∈/F.

Now our plan is the following.

We “tempt” greedy into picking all elements in TTT before anything in SSS, by making the elements of TTT just a little heavier than elements in, say, S∖TS \setminus TS∖T. Note that once greedy chooses all of TTT, it cannot expand further into SSS because of the violation above combined with its own committment to navigating with F\cal FF. So it will skip over all elements of S∖TS \setminus TS∖T: which is an opportunity for us to create a better optimal solution.

In particular, our goal will be to assign weights to elements in S∖TS \setminus TS∖T in such a way that w(S)>w(T)w(S) > w(T)w(S)>w(T) in spite of the fact that elements in TTT are heavier than those in S∖TS \setminus TS∖T. Here, we can take advantage of the fact that ∣S∣>∣T∣|S| > |T|∣S∣>∣T∣.

With these ideas in place, can you come up with the adverserial weight function?

Adverserially Chosen Weights to Trip Greedy [Click to reveal.]

Using mmm to denote ∣T∣|T|∣T∣, suppose we use the following scheme:

  • Every element of TTT has weight m+2m+2m+2.
  • Every element of S\TS \backslash TS\T has weight m+1m+1m+1.
  • Every other element of the ground set has weight zero.

With these weights, the greedy algorithm will consider and accept every element of TTT, then consider and reject every element of SSS, and finally consider all the other elements. The algorithm returns a set with total weight m(m+2)=m2+2mm(m+2)=m^2+2 mm(m+2)=m2+2m. But the total weight of SSS is at least (m+1)2=m2+2m+1(m+1)^2=m^2+2 m+1(m+1)2=m2+2m+1. Thus, the output of the greedy algorithm is not the maximum-weight set in F\cal FF.

This completes the argument for the following fact:

Theorem. Greedy for set systems that do not satisfy the exchange axiom can be made to fail.

For any heriditary family (U,F)(\cal U, \cal F)(U,F) that is not a matroid, there is a weight function www such that the greedy algorithm fails to output the optimal answer.

Recall the Dance Classes problem considered in Chapter 1. There is a natural subset system associated with this problem: A set of classes is independent if and only if no two classes overlap. This subset system is not a matroid, because there can be maximal independent sets of different sizes, which violates the exchange property. If we consider a weighted version of the class scheduling problem, where each class is worth a different number of hours, our result above implies that the greedy algorithm will not always find the optimal schedule.

However, this result here does not contradict the correctness of the greedy algorithm for the original unweighted problem; that problem uses a particularly lucky choice of weights (all equal), whereas the claim here is simply that there exists particularly nasty choice of weights that is designed to mislead greedy into making suboptimal choices.


© 2022 • Neeldhara Misra • Credits •

 

Corrections? Please leave a comment here or a PR in this repository, thanks!

I’d rather be a failure at something I love than a success at something I hate.

George Burns

You live and you learn — at any rate, you live.

Douglas Adams

A problem worthy of attack proves its worth by fighting back.

Paul Erdos

×