Resources
MT

Introduction

A matroid is a combinatorial structure that expands on the notion of independence from linear algebra. It will be useful for a couple of reasons. In this module, we will explore how its axioms allow a natural greedy algorithm to be applied to it (which generalizes Kruskal's algorithm). This allows us to discover greedy strategies for problems where it would otherwise be difficult to find (or at least prove) a greedy algorithm. As our focus problem, we will study an example of how this type of matroid thinking can be used to solve a Codeforces problem.

This module is a prerequisite for the next module on matroid intersection. In some problems, we want to find some largest cardinality set of elements subject to multiple constraints. If we can capture those constraints using (at most two) matroids, then it's possible to efficiently find such a set using this theory.

Matroid Definition

We will be following the notation from: A First Course in Combinatorial Optimization.

A matroid MM consists of a finite ground set E(M)E(M) along with a collection of subsets I(M)\mathcal I(M) that satisfy the following axioms:

  1. Non-emptiness: I(M)\emptyset\in\mathcal I(M)
  2. Heredity: If XYX\subseteq Y and YI(M)Y\in\mathcal I(M), then XI(M)X\in\mathcal I(M)
  3. Exchange: If X,YI(M)X,Y\in\mathcal I(M) and X>Y|X|>|Y|, then there exists some xXYx\in X\setminus Y such that Y+xI(M)Y+x\in\mathcal I(M)

We often call the subsets in I(M)\mathcal I(M) independent sets and the remaining subsets dependent sets.

Borrowing more terminology from linear algebra, a basis of a matroid is any independent set that is maximal in terms of containment (meaning it's not a proper subset of any other independent set).

A consequence of the exchange axiom is that all bases have the same cardinality. If we had two bases with distinct cardinalities, then one of them must be larger and then by exchange, we can add an element to it. Hence, for a matroid MM, we may define its rank r(M)r(M) to be the cardinality of any of its bases. The rank function can also be extended to any set XEX \subseteq E. In particular,

r(X):=max{I:IX is independent}r(X) := \max\left\{|I| : I \subseteq X \text{ is independent}\right\}

Another useful concept is that of a circuit, which is analogous to a cycle in a graph. A dependent set XEX \subseteq E is called a circuit if XeX - e is independent for any eXe \in X. Observe that a set XEX \subseteq E is dependent if and only if it contains a circuit CC. The if direction is trivial, and to show the only if direction, take CC to be the smallest dependent set contained in XX.

More facts

See Section 3. Matroid theory for some more definitions and facts on matroids.

Examples

Studying some examples will make these definitions feel more inspired.

Uniform Matroid

A uniform matroid is a matroid MM such that a subset XE(M)X\subseteq E(M) is independent if Xr|X|\leq r for some fixed r0r\geq 0.

It's pretty clear that all of the matroid axioms hold here.

  1. We have non-emptiness because =0r|\emptyset|=0\leq r.
  2. Heredity holds because XYr    Xr|X|\leq |Y|\leq r\implies |X|\leq r. The first inequality is because XYX\subseteq Y and the second is because YI(M)Y\in\mathcal I(M).
  3. To see exchange, we may pick any xXYx\in X\setminus Y because Y+xXr|Y+x|\leq |X|\leq r. The first inequality holds because X>Y|X|>|Y| and the second because XI(M)X\in\mathcal I(M).

Colourful Matroid

A more interesting example of a matroid is the colourful matroid. In this matroid, every element is assigned one of kk colours. A subset XE(M)X\subseteq E(M) is considered independent if no two elements in it share the same colour.

  1. Non-emptiness is clear because it's impossible to have two elements sharing the same colour in \emptyset.
  2. Heredity is also clear because if no two elements share the same colour in YY, then no two elements will share the same colour in any subset of YY as well.
  3. For exchange, notice that if X>Y|X|>|Y| and they're both independent, then XX contains strictly more colours than YY. Hence, by picking any element in XX that has a colour not present in YY, we can extend YY.

Henceforth, we will omit proofs that our examples are indeed matroids.

Partition Matroid

We can generalize the colourful matroid to get the partition matroid. The elements are partitioned into disjoint categories C1,,CkC_1, \dots, C_k, and each category has some capacity cic_i. A subset XX is independent if for each category CiC_i, there are at most cic_i elements of that category contained in XX.

Graphic Matroid

This matroid gives another perspective on Kruskal's algorithm for minimum spanning trees! The elements of a graphic matroid are edges in an undirected graph, and a subset of edges is independent if they don't contain a cycle.

Linear Matroid

The idea of linear independence naturally leads to the definition of a matroid. In the linear matroid, the elements are vectors in a vector space, and a subset of elements is independent in the matroid if they are linearly independent in the vector space.

Notice that the three matroid axioms come directly from properties of linear independence. Most notably, the exchange property is the Steinitz exchange lemma.

Transversal Matroid

Consider a ground set EE, and let A\mathcal A be a collection of subsets of EE. We can create a transversal matroid by defining X={x1,,xn}EX=\{x_1,\dots,x_n\}\subseteq E to be independent iff there exist distinct subsets: S1,,SnAS_1,\dots,S_n\in\mathcal A such that: i[1,n]:xiSi\forall i\in[1,n]:x_i\in S_i.

We can interpret the independence condition by constructing a bipartite graph with bipartition: (E,A)(E,\mathcal A), and we have an edge (e,S)(e,S) iff eSe\in S. Then, XEX\subseteq E is independent iff there exists a matching covering all vertices in XX.

Minimum Weight Independent Sets

The beauty of matroids lies in the fact that there is a very simple algorithm to find minimum weight independent sets of size 1kr(M)1 \leq k \leq r(M). The algorithm is as follows:

sort E by weight increasingSfor eEif S+eISS+eif S=kreturn SReturn \begin{align*} &\textbf{sort } E \text{ by weight increasing} \\ &S \leftarrow \emptyset\\ &\textbf{for } e \in E \\ &\qquad \textbf{if } S + e \in \mathcal{I} \\ &\qquad\qquad S \leftarrow S + e \\ &\qquad \textbf{if } |S| = k \\ &\qquad\qquad \textbf{return } S \\ &\text{Return } \bot &\end{align*}

Note that Kruskal's algorithm is a special case of this algorithm on the graphic matroid, where the independence checks are done by a DSU.

To prove correctness, fix an ordering of the elements e1,e2,,enEe_1, e_2, \dots, e_n \in E so that w(e1)w(e2)w(en)w(e_1) \leq w(e_2) \leq \dots \leq w(e_n). Let the elements picked by the greedy algorithm have indices p1<p2<<pkp_1 < p_2 < \dots < p_k. We claim that for any other independent set of size kk with indices q1<q2<<qkq_1 < q_2 < \dots < q_k, the following holds:

p1q1p2q2pkqk\begin{gather*} p_1 \leq q_1 \\ p_2 \leq q_2 \\ \vdots \\ p_k \leq q_k \end{gather*}

Suppose for the sake of contradiction that pi>qip_i > q_i for some 1ik1 \leq i \leq k. Let A={ep1,ep2,,epi1}A = \{e_{p_1}, e_{p_2}, \dots, e_{p_{i - 1}}\} and B={eq1,eq2,,eqi}B = \{e_{q_1}, e_{q_2}, \dots, e_{q_i}\}. By the exchange axiom, there exists eBAe \in B \setminus A such that A+eIA + e \in \mathcal{I}. This is an immediate contradiction, as ee should have been added to SS.

Remark: This shows that if the exchange axiom holds in an independence system (i.e., a system in which the non-emptiness and heredity axioms hold), the natural greedy works. We can further show that the greedy works only if the exchange axiom holds (see the attached document). As a result, if you ever suspect that the greedy works, it is both necessary and sufficient to prove that you have a matroid.

Optional: Greedy is equivalent to the exchange axiom

Focus Problem

Focus Problem – try your best to solve this problem before continuing!

Let's take a look at how you might think about matroid structure in an actual problem. This codeforces problem can be solved (after some reductions) with our optimal weighted basis algorithm.

Let's say that we pick index i[1,n]i\in[1,n] if we decide to take sushi at this index. To uncover the matroid structure, we must understand the condition for when a subset of indices SS is valid (can be picked without violating the problem constraints). It's easiest to think about the necessary condition first. Notice that the maximum number of indices we can pick is nk+1\lfloor\frac{n}{k+1}\rfloor, so we know S|S| cannot be larger. Furthermore, we can see that this bound applies to all suffixes of [1,n][1,n] as well. For clarity, let's define Ti:=T[i,n]T_i:=T\cap [i,n] for any set TT. Our necessary condition is now: i[1,n]:Sini+1k+1\forall i\in[1,n]:|S_i|\leq\lfloor\frac{n-i+1}{k+1}\rfloor.

This condition is rather restrictive, so it's probably not a huge surprise that it's also sufficient. To see why, you can imagine trying to select the corresponding kk indices required to eat sushi picked at each location. For an arbitrary picked index iSi\in S, we know that Sini+1k+1|S_i|\leq\lfloor\frac{n-i+1}{k+1}\rfloor by our necessary condition. Unpacking this, it means that there are (at least) kk unpicked indices in [i,n][i,n] for every picked index in that interval. Hence, no matter how we assigned eating indices for the future picked indices, there exists at least kk indices for ii, so we eat at those. Therefore, by iterating the indices back-to-front, we can produce a valid assignment. It's worth noting than in some sense, we created this condition so that this argument works.

We can now show that this forms a matroid whose ground set is the integers in [1,n][1,n]. Non-emptiness and heredity are clear. Let X>Y|X|>|Y| be as in the exchange axiom. Observe that Xn=Yn=0|X_n|=|Y_n|=0, so we can let j:=min{i:XiYi}j:=\min\{i:|X_i|\leq|Y_i|\}. Since X1>Y1|X_1|>|Y_1|, we know that j11j-1\geq 1. It turns out that j1XYj-1\in X\setminus Y will be the element we need to show the exchange axiom. We know that j1XYj-1\in X\setminus Y because Xj1>Yj1|X_{j-1}|>|Y_{j-1}| while XjYj|X_j|\leq|Y_j|. We can see that Y+(j1)Y+(j-1) is independent by examining (Y+(j1))i|(Y+(j-1))_i| for ij1i\leq j-1 (when i>j1i>j-1, the cardinality doesn't change). Observe that if ij1i\leq j-1, then: (Y+(j1))i=Yi+1Xi|(Y+(j-1))_i|=|Y_i|+1\leq |X_i| where the last inequality holds because Yi<Xi|Y_i|<|X_i| for ij1i\leq j-1. Finally, we use the fact that XX is independent, so Xi|X_i| satisfies the upper bound on the cardinality, as wanted.

Now that we know we're dealing with a matroid, it's possible to solve this problem using the optimal weighted basis algorithm. The independence checks can be handled by a lazy segment tree which supports addition on ranges, and querying for the max of a range. It works by maintaining the number of elements chosen in the suffix starting at ii (for each i[1,n]i\in[1,n]) minus the capacity of the suffix. Then, to add ii, we need to add 11 to the prefix [1,i][1,i]. We check that the max over [1,n][1,n] is 0\leq0 to see if the condition holds.

This is cool, however if we take the time to recognize which matroid we're dealing with (out of the examples we've seen), it's possible to give a much shorter implementation. It turns out that we have a transversal matroid.

Transversal Matroid Representation

As in the above diagram, the sets in the transversal matroid are given by: A:={[1,i]:k+1ni+1}\mathcal A:=\{[1,i]:k+1\mid n-i+1\}. Independent sets in the transversal matroid are independent in our original matroid because of the property that the number of sets in A\mathcal A covering the suffix starting at ii is the same as our bound: ni+1k+1\lfloor\frac{n-i+1}{k+1}\rfloor (by construction). The converse can be verified using Hall's condition. Suppose SS is independent in our original matroid and let XS\emptyset\neq X\subseteq S. We would like to prove that N(X)X|N(X)|\geq|X| (in the transversal bipartite graph). This is true because N(X)={AA:minXA}N(X)=\{A\in\mathcal A:\min X\in A\} (because all sets are prefixes). Hence, N(X)=nminX+1k+1|N(X)|=\lfloor\frac{n-\min X+1}{k+1}\rfloor. But, we know that this is greater than or equal to XminX=X|X_{\min X}|=|X| by heredity, as wanted.

Now, we can implement the independence checks by maintaining the right endpoints of all sets in A\mathcal A. To add ii, we greedily take the set with the smallest right endpoint that's greater than or equal to ii and remove it. If such a right endpoint doesn't exist, then we can't add ii.

#include <bits/stdc++.h>
using namespace std;
long long solve() {
int n, k;
cin >> n >> k;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];

Problems

StatusSourceProblem NameDifficultyTags
DMOJHard
Show TagsMST, Matroids, Trees
CFHard
Show TagsMatroids
KattisHard
Show TagsMST, Matroids

Problem Clarification

  • Distribution Channel: The problem asks for a spanning tree whose total weight is strictly greater than the weight of a minimum spanning tree.
  • Do It Yourself?: The hyperlink to the problem statement on Codeforces is broken. The problem statement can be found under the "Contest materials" section in the bottom-right sidebar of the problem page.

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!