Neeldhara
  • About
  • Research
    • Overview
    • People
    • Publications
    • Surveys
  • Teaching
    • Courses
    • Materials
  • Lists
    • Puzzles
    • Bookmarks
  • Exposition
    • Talks
    • Videos
  • Events
  • Blog
  1. Advanced Algorithms
  2. Matroids
  3. 1. The Motivation
  • Advanced 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
    • Flows
      • 1. Flow-Cut Duality
    • Intractability
      • 1. Independent Set
    • Approximation
      • 1. Vertex Cover
      • 2. Set Cover
    • Randomized Algorithms
      • 1. Randomized QuickSort

On this page

  • A Generic Optimization Problem
  • A Greedy Approach

1. The Motivation

algonotes
lecturenotes
Published

May 16, 2023

A Generic Optimization Problem

Suppose we have:

  • 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
  • a family F={S1,…,Sm}\cal F = \{S_1, \ldots, S_m\}F={S1​,…,Sm​} of subsets of U\cal UU

and we “lift” www to the sets in F\cal FF as follows:

w(S)=∑e∈Sw(e).w(S) = \sum_{e \in S} w(e).w(S)=e∈S∑​w(e).

Here is, then, a natural optimization question: what is the maximum weight attained by a set in F\cal FF?

Notice that this need not be the set of the largest cardinality (a large number of light elements may fall short of a small number of heavy ones).

Now, you may wonder why this is even an interesting question — do we not find the answer by computing the weights of all the sets, and tracking the maximum?

That would indeed work! However, the time we need to execute this is O(mn)O(mn)O(mn) in the worst case (accounting for the time we need to add O(n)O(n)O(n) weights), and note that mmm is potentially exponential in nnn, which — when it happens — will not be fun.

You can rebut this by suggesting that it does not matter how large mmm is as a function of nnn: it counts as input, and so our approach is polynomial in the size of the input.

Indeed! So to make this problem interesting, and one that captures problems that you will actually encounter, let’s add the constraint that F\cal FF is not given to us as explicit input, but rather via an oracle. What this means is that we do not have the luxury of direct access to the list of sets in F\cal FF, but we can poke at the list indirectly: if we have a set at hand, we can feed it to the oracle, which will tell us if the set belongs in F\cal FF or not.

So our goal is to now build a set that has the largest weight, with some help from the oracle. We want to do this as efficiently as possible, in particular using only polynomially-in-nnn many calls to the oracle. It is like navigating a room in the dark with a torch… we do not know most things, but we can get sneak peeks into the lay of the land using the torch, but we don’t want to overuse the torch or the battery will run out.

A Greedy Approach

If you had to build the heaviest set greedily, how would you go about it? A natural idea is to favor heavier elements over lighter ones. In particular, let us line up the elements from heaviest to lightest, and keep picking them up one by one. Well, this will always end with us choosing UUU, which is a set that is as heavy as it gets: however we have a problem — this set may not be in F\cal FF! We may have lost the plot well before reaching the end, so we need to introduce some sanity check here. How about ensuring, using the oracle, that we only choose elements that keep us rooted in F\cal FF? Here’s what we would do:

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​}.

Notice that we invoke the oracle nnn times in step 3. This is a natural greedy approach, and it might remind you of, say, the mechanics of Kruskal’s algorithm for finding spanning trees of maximum weight. Indeed, for a particular choice of U\cal UU and F\cal FF, the algorithm above is exactly Kruskal’s algorithm1! Fix a graph GGG, and let U=E(G)\cal U = E(G)U=E(G) and F\cal FF consisting of all acyclic subsets of E(G)E(G)E(G). The “oracle” in this case just has to check if, given S⊆E(G)S \subseteq E(G)S⊆E(G), the subgraph induced by SSS contains a cycle or not.

Now the question is if the algorithm always works. A moment’s reflection will reveal that it in fact, does not.

A Counterexample

Let:

  • U={p,q,r}\cal U = \{p,q,r\}U={p,q,r}
  • w(p)=40,w(q)=30,w(r)=20w(p) = 40, w(q) = 30, w(r) = 20w(p)=40,w(q)=30,w(r)=20
  • F={{p},{qr}}\cal F = \{\{p\},\{qr\}\}F={{p},{qr}}

The greedy output will be {p}\{p\}{p}, while the optimal output is {qr}\{qr\}{qr}.

Even knowing that the algorithm fails, let us try to anticipate how a typical greedy exchange argument would have gone-and-gotten-stuck. Here’s the cliche line of reasoning:

Let:

  • {f1,f2,…,fk}\{f_1,f_2,\ldots,f_k\}{f1​,f2​,…,fk​} be the output of the greedy algorithm, and
  • {g1,g2,…,gℓ}\{g_1,g_2,\ldots,g_\ell\}{g1​,g2​,…,gℓ​} be the optimal solution.

If it exists, let iii be the smallest index for which fi<gif_i < g_ifi​<gi​. If not, i.e, fi⩾gif_i \geqslant g_ifi​⩾gi​ for all 1≤i≤k1 \leq i \leq k1≤i≤k, then it must be the case that ℓ>k\ell > kℓ>k. In either case, we have an element in the optimal solution that was not chosen by the greedy algorithm, even though in terms of weight, it was favorable over the greedy choice (either fif_ifi​ or early termination). Let us call this element ggg and denote by SSS the greedy choices made up to (and excluding) the point where “OPT trumped Greedy”.

Note that the only reason the element ggg was not chosen by the greedy algorithm must have been that ggg was not compatible with the greedy choices we had made up to that point, i.e, S∪{g}∉FS \cup \{g\} \notin \cal FS∪{g}∈/F. Indeed, recall that this is what happened in the counterexample above as well!

So clearly, the greedy approach will not work in general for any input (U,F,w)(\cal U, \cal F, w)(U,F,w). However, what if F\cal FF had some additional structure, which allowed us to push the key step in the proof above? In particular, let’s make our wishlist explicit. If

  • TTT is the set of greedy choices up to the (i−1)th(i-1)^{th}(i−1)th iteration, and
  • SSS is the set of optimal choices up to the ithi^{th}ith iteration,

then we would like to say that there is some element in SSS that is:

  • not yet chosen by the greedy algorithm, and
  • can be legitimately appended to the greedy choices made so far.

In other words, we want that ∃e∈S∖T\exists e \in S \setminus T∃e∈S∖T such that T∪{e}∈FT \cup \{e\} \in \cal FT∪{e}∈F: why? Because such an element eee will be guaranteed to be better in terms of weight than greedy choice at the ithi^{th}ith iteration (which is either the element fif_ifi​ or no element at all due to termination). But this time, eee is also compatible with the greedy choices… so the greedy algorithm really had no good reason to ignore eee, and we have a contradiction!

Next up, we are going to define the notion of a matroid motivated by the need for a property like the one we described above.

Footnotes

  1. This is a remark you can safely ignore if you have not encountered Kruskal’s algorithm yet.↩︎


© 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

×