Matroid Intersection
Given two matroids over common ground set , the matroid intersection algorithm finds a set of maximum size such that . The algorithm works by starting with the set , and repeatedly augmenting using the corresponding exchange graph . The vertices of are the elements of , and for and :
- There is a directed edge if .
- There is a directed edge if .
Note that is bipartite. We also label certain vertices :
- is a source if .
- is a sink if .
To augment , the algorithm finds the shortest path from a source to a sink. Then, it redefines to
There are two facts that together explain the correctness of this algorithm:
- Augmenting by the shortest source-sink path preserves 's independence in both matroids.
- If there are no augmenting paths, then has maximum size among all such that is independent in both matroids.
Example - SERVERS
Focus Problem – try your best to solve this problem before continuing!
Explanation
Let be the set of all provided edges. The problem asks us to find the size of a largest subset where:
- is acyclic.
- For each frequency , there are at most edges in with frequency .
We can directly apply matroid intersection. Let be the ground set. Then, condition 1 is asking for to be independent in the graphic matroid, and condition 2 is asking for to be independent in the partition matroid where the categories are frequencies and is the capacity of category .
Implementing the Matroid Intersection Algorithm
We'll implement the algorithm with a general interface so that it can be used for any matroids we want. Then, in the next section, we'll give the specific operations needed for our matroids.
Let be the size of the ground set and be the size of a maximum common independent set. Then, our algorithm does iterations of the following:
- Construct the exchange graph between and .
- Update using a shortest augmenting path from a source to a sink.
Step 2 can be done in 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 .check(x): return true if is independent and false otherwise.add(x): set , assuming is independent.precomp(): (optional) precomputation to speed up a batch ofcheck(x)queries.
Recall that we need to know for each and if
and .
The trick is to batch queries by . For a fixed , we use clear() and
add(x) to obtain , then use check(x) to find all where
is independent. Before making the check(x) calls, we also make
a call to precomp() to potentially speed up the batch of
check(x) calls. In case precomputation is unnecessary, precomp() can be
defined as an empty function.
This builds the graph with calls to clear() and precomp(),
calls to add(x), and calls to add(x).
Observe that we sidestepped the need to efficiently remove arbitrary elements
from , 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: calls to clear() and precomp(),
to add(x), and 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 elements
instead of just those in , which adds an extra (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 Sfor (int i = 0; i < n; i++) {if (in_S[i]) S.push_back(i);else E_S.push_back(i);}// build exchange graphvector<vector<int>> adj(n);
Section 4 of this paper achieves 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 queries and bounds the number of phases by .
Unfortunately, since each phase modifies 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 and the size of the ground set is . We get an extra factor from union-find, but this is still well within the time limit.
Time complexity:
#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 of a matroid is defined on the same ground set with the independent sets defined as follows:
As an example, consider the graphic matroid corresponding to a connected graph . Then, the bases of are spanning trees, and a set of edges is independent in if and only if remains connected after removing all of the edges in .
Claim: The dual of a matroid 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 such that . For convenience, we will reframe the exchange axiom in terms of and . Our goal is to show that there exists such that and contains a basis of .
Partition into and where
and . If there exists such that , then we are immediately done. Suppose that . It follows that
so we get
Furthermore, it can be shown that
by taking any maximum independent set and noting that is independent by choice of . Therefore, , which is a contradiction because and both contain bases of .
Contraction of a Matroid
Suppose we have an existing matroid , but there are some elements we are forced to include. If , we can effectively mod out by with matroid contraction: the contraction of by , written , is the matroid on the ground set where is independent in if .
If is not independent in , then fails the non-emptiness axiom. Some problems don't guarantee , so this is always a condition that should be checked.
Problems
| Status | Source | Problem Name | Difficulty | Tags | ||
|---|---|---|---|---|---|---|
| SPOJ | Easy | Show TagsDSU, Euler Tour, Matroid Intersection | ||||
| CF | Easy | Show TagsMatroid Intersection, XOR Basis | ||||
| CC | Easy | Show TagsMatroid Intersection | ||||
| CF | Hard | Show TagsMatroid Intersection | ||||
| CC | Hard | Show TagsMatroid Intersection | ||||
Additional Problems
- Honesty
- Max Size Graphic + Colorful
- Ambiguous Forest
- Max Size Graphic + Graphic
- Rainbow Graph
- Min Weight Graphic + Colorful
- TST
- Partition into 3 MST's
- Almost Rainbow Tree (Easy)
- Can be solved exactly using Matroid Intersection, or via a -approximation using greedy.
- Almost Rainbow Tree (Hard):
- Can be solved exactly using Matroid Intersection, or via a -approximation using local search.
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!