PrevNext

Matroid Intersection

Given two matroids M1,M2M_1, M_2 over common ground set EE, the matroid intersection algorithm finds a set XEX \subseteq E of maximum size such that XI(M1)I(M2)X \in \mathcal{I}(M_1) \cap \mathcal{I}(M_2). The algorithm works by starting with the set S=S = \emptyset, and repeatedly augmenting SS using the corresponding exchange graph GM1,M2(S)\mathcal{G}_{M_1, M_2}(S). The vertices of GM1,M2(S)\mathcal{G}_{M_1, M_2}(S) are the elements of EE, and for ySy \in S and xESx \in E \setminus S:

  1. There is a directed edge (y,x)(y, x) if Sy+xI(M1)S - y + x \in \mathcal{I}(M_1).
  2. There is a directed edge (x,y)(x, y) if Sy+xI(M2)S - y + x \in \mathcal{I}(M_2).

Note that GM1,M2(S)\mathcal{G}_{M_1, M_2}(S) is bipartite. We also label certain vertices xESx \in E \setminus S:

  1. xx is a source if S+xI(M1)S + x \in \mathcal{I}(M_1).
  2. xx is a sink if S+xI(M2)S + x \in \mathcal{I}(M_2).

To augment SS, the algorithm finds the shortest path x0,y1,x1,,yn,xnx_0, y_1, x_1, \dots, y_n, x_n from a source to a sink. Then, it redefines SS to

S=S{y1,y2,,yn}{x0,x1,,xn}.\begin{equation*} S = S \setminus \{y_1, y_2, \dots, y_n\} \cup \{x_0, x_1, \dots, x_n\}. \end{equation*}

There are two facts that together explain the correctness of this algorithm:

  1. Augmenting by the shortest source-sink path preserves SS's independence in both matroids.
  2. If there are no augmenting paths, then SS has maximum size among all XEX \subseteq E such that XX is independent in both matroids.
Optional: Proof of the above facts

Example - SERVERS

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

Explanation

Let EE be the set of all provided edges. The problem asks us to find the size of a largest subset SES \subseteq E where:

  1. SS is acyclic.
  2. For each frequency ff, there are at most c(f)c(f) edges in SS with frequency ff.

We can directly apply matroid intersection. Let EE be the ground set. Then, condition 1 is asking for SS to be independent in the graphic matroid, and condition 2 is asking for SS to be independent in the partition matroid where the categories are frequencies and c(f)c(f) is the capacity of category ff.

Implementing the Matroid Intersection Algorithm

We'll implement the algorithm with a general interface so that it can be used for any matroids M1,M2M_1, M_2 we want. Then, in the next section, we'll give the specific operations needed for our matroids.

Let N=EN=|E| be the size of the ground set and RR be the size of a maximum common independent set. Then, our algorithm does RR iterations of the following:

  1. Construct the exchange graph between SS and ESE \setminus S.
  2. Update SS using a shortest augmenting path from a source to a sink.

Step 2 can be done in O(RN)\mathcal O(RN) time using BFS. The hard part is step 1, where we need to maintain a matroid structure that allows us to efficiently check when a set is independent.

It turns out that supporting the following operations (for both matroids) is enough:

  • clear(): set S:=S := \emptyset.
  • check(x): return true if S+xS + x is independent and false otherwise.
  • add(x): set S:=S+xS := S + x, assuming S+xS + x is independent.
  • precomp(): (optional) precomputation to speed up a batch of check(x) queries.

Recall that we need to know for each ySy \in S and xESx \in E \setminus S if Sy+xI(M1)S - y + x \in \mathcal I(M_1) and Sy+xI(M2)S - y + x \in \mathcal I(M_2). The trick is to batch queries by yy. For a fixed yy, we use clear() and add(x) to obtain SyS - y, then use check(x) to find all xx where Sy+xS - y + x is independent. Before making the check(x) calls, we also make a call to precomp() to potentially speed up the batch of O(N)\mathcal O(N) check(x) calls. In case precomputation is unnecessary, precomp() can be defined as an empty function.

This builds the graph with O(R)\mathcal O(R) calls to clear() and precomp(), O(R2)\mathcal O(R^2) calls to add(x), and O(RN)\mathcal O(RN) calls to add(x). Observe that we sidestepped the need to efficiently remove arbitrary elements from SS, which would have been a big problem for matroids like the graphic matroid.

Also see KACTL's implementation, which only checks edges as needed rather than building the entire graph upfront.

Time Complexity: O(R2)\mathcal O(R^2) calls to clear() and precomp(), O(R3)\mathcal O(R^3) to add(x), and O(R2N)\mathcal O(R^2N) calls to check(x). Note that in our implementation, the complexity is always dominated by these oracle calls. In KACTL's implementation, backE loops over all NN elements instead of just those in SS, which adds an extra O(RN2)O(RN^2) (though the number of oracle calls stays the same).

template <class M1, class M2> vector<bool> MatroidIsect(M1 m1, M2 m2, int n) {
vector<bool> in_S(n);
auto augment = [&] {
vector<int> S, E_S; // E_S has indices of all edges not in S
for (int i = 0; i < n; i++) {
if (in_S[i]) S.push_back(i);
else E_S.push_back(i);
}
// build exchange graph
vector<vector<int>> adj(n);
Optional: Theoretically faster matroid intersection

Section 4 of this paper achieves O(r1.5n)\mathcal O(r^{1.5}n) independence queries by extending the ideas used in the Hopcroft-Karp algorithm for bipartite matching. In particular, the algorithm augments on a maximal set of shortest paths in one phase with O(rn)\mathcal O(rn) queries and bounds the number of phases by O(r)\mathcal O(\sqrt r).

Unfortunately, since each phase modifies SS and revisits vertices multiple times, the trick of batching queries no longer works and we need to support fast removals to see benefits. Because the constraints for matroid intersection problems are small, we believe the extra effort is not worth it (in all problems below, the original algorithm is fast enough).

Implementing the Matroid Structure

We implement the graphic matroid with union-find and the partition matroid by maintaining a count of the number of used elements of each frequency.

In our case, the maximum size of a common independent set is n1n-1 and the size of the ground set is mm. We get an extra O(α(n))\mathcal O(\alpha(n)) factor from union-find, but this is still well within the time limit.

Time complexity: O(n2mα(n))\mathcal O(n^2m \cdot \alpha(n))

#include <bits/stdc++.h>
using namespace std;
Code Snippet: Union-find Data Structure (Click to expand)
struct GraphicMatroid {
int n; // nodes of the graph must be in [0..n-1]
DSU dsu;
vector<pair<int, int>> edges;
GraphicMatroid(int n, const vector<pair<int, int>> &edges)

Modifying Existing Matroids

We now consider some ways of making new matroids from existing ones. This expands the kinds of conditions we can handle.

Matroid Dual

In many problems, it is useful to consider the dual of some well-known matroid. The dual MM^\star of a matroid MM is defined on the same ground set E(M)E(M) with the independent sets defined as follows:

I(M)={XE:EX contains a basis of M}.\mathcal{I}(M^\star) = \{X \subseteq E : E \setminus X \text{ contains a basis of } M\}.

As an example, consider the graphic matroid MM corresponding to a connected graph G=(V,E)G = (V, E). Then, the bases of MM are spanning trees, and a set of edges XEX \subseteq E is independent in MM^\star if and only if GG remains connected after removing all of the edges in XX.

Claim: The dual MM^\star of a matroid MM is itself a matroid.

Proof: Clearly, the non-emptiness and heredity axioms hold, so it remains to show that the exchange axiom also holds. Let X,YI(M)X, Y \in I(M^\star) such that X<Y|X| < |Y|. For convenience, we will reframe the exchange axiom in terms of Xc=EXX^c = E \setminus X and Yc=EYY^c = E \setminus Y. Our goal is to show that there exists eXce \in X^c such that eYce \notin Y^c and XceX^c - e contains a basis of MM.

Partition XcX^c into C\mathcal{C} and A\mathcal{A} where

C={eXc:e is in some circuit CXc}\begin{equation*} \mathcal{C} = \{e \in X^c : e \text{ is in some circuit } C \subseteq X^c\} \end{equation*}

and A=XcC\mathcal{A} = X^c \setminus \mathcal{C}. If there exists eCe \in \mathcal{C} such that eYce \notin Y^c, then we are immediately done. Suppose that CYc\mathcal{C} \subseteq Y^c. It follows that

YcC=YcC<XcC=A\begin{equation*} |Y^c \setminus \mathcal{C}| = |Y^c| - |\mathcal{C}| < |X^c| - |\mathcal{C}| = |\mathcal{A}| \end{equation*}

so we get

r(Yc)r(C)+r(YcC)<r(C)+A\begin{equation*} r(Y^c) \leq r(\mathcal{C}) + r(Y^c \setminus \mathcal{C}) < r(\mathcal{C}) + |\mathcal{A}| \end{equation*}

Furthermore, it can be shown that

r(Xc)=r(C)+A\begin{equation*} r(X^c) = r(\mathcal{C}) + |\mathcal{A}| \end{equation*}

by taking any maximum independent set SCS \subseteq \mathcal{C} and noting that SAS \cup \mathcal{A} is independent by choice of A\mathcal{A}. Therefore, r(Xc)r(Yc)r(X^c) \neq r(Y^c), which is a contradiction because XcX^c and YcY^c both contain bases of MM.

Contraction of a Matroid

Suppose we have an existing matroid M=E,IM=\langle E, \mathcal I\rangle, but there are some elements AEA \subseteq E we are forced to include. If AIA \in \mathcal I, we can effectively mod out by AA with matroid contraction: the contraction of MM by AA, written M/AM / A, is the matroid on the ground set EAE \setminus A where BB is independent in M/AM / A if ABIA \cup B \in \mathcal I.

Warning: Independence of the empty set

If AA is not independent in MM, then M/AM / A fails the non-emptiness axiom. Some problems don't guarantee AIA \in \mathcal I, so this is always a condition that should be checked.

Problems

StatusSourceProblem NameDifficultyTags
SPOJEasy
Show TagsDSU, Euler Tour, Matroid Intersection
CFEasy
Show TagsMatroid Intersection, XOR Basis
CCEasy
Show TagsMatroid Intersection
CFHard
Show TagsMatroid Intersection
CCHard
Show TagsMatroid Intersection

Additional Problems

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!

PrevNext