The matroid greedy algorithm returns a maximum-weight basis full
This proof shows: correctness, termination.
We use the pseudocode of the page. The input is a matroid M=(E,I) with ∣E∣=n, given by the test indep, and a weight function w:E→R. Let e1,…,en be the order of line L1, so w(e1)≥⋯≥w(en). Let A0=∅ and let Ai be the value of A after iteration i of the loop of line L3. The algorithm returns An.
Claim. The algorithm stops after n oracle calls, and An is a basis of M with w(An)≥w(B) for every basis B. Moreover, for every independent set X with elements x1,…,xs sorted so that w(x1)≥⋯≥w(xs), and for the elements g1,…,gr of An in the order in which they were added, s≤r and w(gk)≥w(xk) for k=1,…,s.
Step 1: termination. The sort of line L1 stops, and the loop of line L3 runs n times with one oracle call each.
Step 2: Ai is independent. A0=∅∈I by (I1). If Ai−1 is independent, then Ai is either Ai−1 or Ai−1∪{ei}, and line L5 runs only when line L4 found Ai−1∪{ei}∈I.
Step 3: An is a basis. Let x∈E∖An. Then x=ei for some i with Ai−1∪{ei}∈/I. Since Ai−1∪{ei}⊆An∪{ei}, the hereditary property (I2) gives An∪{x}∈/I. So no element can be added to An, and An is a maximal independent set, a basis. Write r=∣An∣.
Step 4: s≤r. Let X be independent. If ∣X∣>r, then (I3) applied to An and X gives x∈X∖An with An∪{x}∈I, against Step 3. So s=∣X∣≤r.
Step 5: w(gk)≥w(xk). The elements g1,…,gr were added in the order of line L1, so w(g1)≥⋯≥w(gr). Suppose that w(gk)<w(xk) for some k≤s, and take the least such k. Let P={g1,…,gk−1} and Q={x1,…,xk}. Both are independent by (I2), and ∣P∣=k−1<k=∣Q∣, so by (I3) there is xj∈Q∖P with P∪{xj}∈I. Since j≤k, w(xj)≥w(xk)>w(gk). Let xj=et. Because w(et)>w(gk), line L1 puts et before gk. At the start of iteration t, the set At−1 contains only elements added before gk, so At−1⊆P, and At−1∪{et}⊆P∪{xj} is independent by (I2). So line L4 accepted et, and et∈An was added before gk, which means et∈P. This contradicts xj∈/P. So w(gk)≥w(xk) for all k≤s.
Step 6: optimality. Let B be any basis. All bases have the same size, so ∣B∣=r, and by Step 5 with X=B and s=r,
w(B)=k=1∑rw(xk)≤k=1∑rw(gk)=w(An). The argument does not use the sign of the weights. If all weights are nonnegative, Step 5 also gives w(X)≤∑k≤sw(gk)≤w(An) for every independent set X, so An is a maximum-weight independent set. ■