pub struct Matroid {
pub ground_set_size: usize,
pub bases: BTreeSet<BTreeSet<usize>>,
pub rank: usize,
}Expand description
A matroid on a ground set {0, …, n-1}.
Represented by its collection of bases, which must satisfy the basis exchange axiom: for any two bases B₁, B₂ and any x ∈ B₁ \ B₂, there exists y ∈ B₂ \ B₁ such that (B₁ \ {x}) ∪ {y} is a basis.
Fields§
§ground_set_size: usizeSize of the ground set
bases: BTreeSet<BTreeSet<usize>>Collection of bases (each a k-element subset)
rank: usizeRank of the matroid (common size of all bases)
Implementations§
Source§impl Matroid
impl Matroid
Sourcepub fn uniform(k: usize, n: usize) -> Self
pub fn uniform(k: usize, n: usize) -> Self
Create the uniform matroid U_{k,n}: all k-subsets of [n] are bases.
§Contract
requires: k <= n
ensures: result.bases.len() == C(n, k)
ensures: result.rank == kSourcepub fn from_bases(
n: usize,
bases: BTreeSet<BTreeSet<usize>>,
) -> Result<Self, String>
pub fn from_bases( n: usize, bases: BTreeSet<BTreeSet<usize>>, ) -> Result<Self, String>
Create a matroid from an explicit collection of bases.
Validates the basis exchange axiom.
§Contract
requires: all bases have the same cardinality
requires: basis exchange axiom holds
ensures: result.rank == bases[0].len()Sourcepub fn from_plucker(
k: usize,
n: usize,
coords: &[f64],
tolerance: f64,
) -> Result<Self, String>
pub fn from_plucker( k: usize, n: usize, coords: &[f64], tolerance: f64, ) -> Result<Self, String>
Create a matroid from Plücker coordinates.
The matroid is defined by the nonvanishing pattern of the Plücker coordinates: a k-subset I is a basis iff p_I ≠ 0 (within tolerance).
§Contract
requires: coords.len() == C(n, k)
ensures: result represents the matroid of the point in Gr(k,n)Sourcepub fn rank_of(&self, subset: &BTreeSet<usize>) -> usize
pub fn rank_of(&self, subset: &BTreeSet<usize>) -> usize
Rank of a subset S: max |B ∩ S| over all bases B.
§Contract
ensures: result <= self.rank
ensures: result <= subset.len()Sourcepub fn circuits(&self) -> BTreeSet<BTreeSet<usize>>
pub fn circuits(&self) -> BTreeSet<BTreeSet<usize>>
Compute all circuits (minimal dependent sets).
A circuit is a minimal subset C such that rank(C) < |C|.
§Contract
ensures: forall C in result. rank_of(C) < |C|
ensures: forall C in result. forall e in C. rank_of(C \ {e}) == |C| - 1Sourcepub fn dual(&self) -> Self
pub fn dual(&self) -> Self
Dual matroid: bases are complements of original bases.
§Contract
ensures: result.rank == self.ground_set_size - self.rank
ensures: result.bases.len() == self.bases.len()Sourcepub fn direct_sum(&self, other: &Matroid) -> Self
pub fn direct_sum(&self, other: &Matroid) -> Self
Direct sum of two matroids (disjoint union of ground sets).
§Contract
ensures: result.rank == self.rank + other.rank
ensures: result.ground_set_size == self.ground_set_size + other.ground_set_sizeSourcepub fn matroid_union(&self, other: &Matroid) -> Self
pub fn matroid_union(&self, other: &Matroid) -> Self
Matroid union: bases are maximal sets in B₁ ∨ B₂.
This is NOT the same as the direct sum — it uses the same ground set. A set I is independent in M₁ ∨ M₂ iff I = I₁ ∪ I₂ where I_j is independent in M_j. The rank is min(|E|, r₁ + r₂).
§Contract
requires: self.ground_set_size == other.ground_set_size
ensures: result.rank <= self.rank + other.rank
ensures: result.ground_set_size == self.ground_set_sizeSourcepub fn is_coloop(&self, e: usize) -> bool
pub fn is_coloop(&self, e: usize) -> bool
Check if an element is a coloop (in every basis).
§Contract
ensures: result == forall B in bases. e in BSourcepub fn delete(&self, e: usize) -> Self
pub fn delete(&self, e: usize) -> Self
Delete element e: restrict to ground set \ {e}.
§Contract
requires: e < self.ground_set_size
ensures: result.ground_set_size == self.ground_set_size - 1Sourcepub fn contract(&self, e: usize) -> Self
pub fn contract(&self, e: usize) -> Self
Contract element e: bases containing e, with e removed.
§Contract
requires: e < self.ground_set_size
ensures: result.ground_set_size == self.ground_set_size - 1
ensures: result.rank == self.rank - 1 (when e is not a loop)Sourcepub fn tutte_polynomial(&self) -> BTreeMap<(usize, usize), i64>
pub fn tutte_polynomial(&self) -> BTreeMap<(usize, usize), i64>
Compute the Tutte polynomial T_M(x, y) via deletion-contraction.
Returns coefficients as a map (i, j) -> coefficient of x^i y^j.
§Contract
ensures: T_M(1, 1) == number of bases
ensures: T_M(2, 1) == number of independent setsSourcepub fn intersection_cardinality(&self, other: &Matroid) -> usize
pub fn intersection_cardinality(&self, other: &Matroid) -> usize
Matroid intersection cardinality via Edmonds’ augmenting path algorithm.
Finds the maximum cardinality common independent set of two matroids on the same ground set.
§Contract
requires: self.ground_set_size == other.ground_set_size
ensures: result <= min(self.rank, other.rank)Sourcepub fn schubert_matroid(
partition: &[usize],
k: usize,
n: usize,
) -> Result<Self, String>
pub fn schubert_matroid( partition: &[usize], k: usize, n: usize, ) -> Result<Self, String>
Schubert matroid: the matroid associated with a Schubert cell in Gr(k, n).
For a partition λ fitting in the k × (n-k) box, the Schubert matroid has bases corresponding to k-subsets I such that i_j ≤ λ_j + j (where λ is padded to length k).
§Contract
requires: partition fits in k × (n-k) box
ensures: result is a valid matroidSource§impl Matroid
Combinatorial extensions for matroid theory.
impl Matroid
Combinatorial extensions for matroid theory.
Sourcepub fn characteristic_polynomial(&self) -> Vec<i64>
pub fn characteristic_polynomial(&self) -> Vec<i64>
Characteristic polynomial χ_M(q) = Σ_{A ⊆ E} (−1)^|A| · q^(r − rank(A)).
Returns coefficients [c_0, c_1, …, c_r] where χ_M(q) = Σ cᵢ · q^i.
Sourcepub fn characteristic_polynomial_eval(&self, q: i64) -> i64
pub fn characteristic_polynomial_eval(&self, q: i64) -> i64
Evaluate the characteristic polynomial at an integer q.
Sourcepub fn beta_invariant(&self) -> i64
pub fn beta_invariant(&self) -> i64
Beta invariant β(M) = (−1)^r · χ’_M(1).
Sourcepub fn closure(&self, subset: &BTreeSet<usize>) -> BTreeSet<usize>
pub fn closure(&self, subset: &BTreeSet<usize>) -> BTreeSet<usize>
Closure operator: cl(A) = smallest flat containing A.
Sourcepub fn flats(&self) -> Vec<Vec<BTreeSet<usize>>>
pub fn flats(&self) -> Vec<Vec<BTreeSet<usize>>>
Lattice of flats grouped by rank: flats[i] = all flats of rank i.
Sourcepub fn mobius_values(&self) -> BTreeMap<BTreeSet<usize>, i64>
pub fn mobius_values(&self) -> BTreeMap<BTreeSet<usize>, i64>
Möbius function μ(∅, F) for each flat F.
Sourcepub fn is_connected(&self) -> bool
pub fn is_connected(&self) -> bool
Check if the matroid is connected (no direct sum decomposition).
Sourcepub fn connected_components(&self) -> Vec<Matroid>
pub fn connected_components(&self) -> Vec<Matroid>
Connected components of the matroid.
Sourcepub fn h_vector(&self) -> Vec<i64>
pub fn h_vector(&self) -> Vec<i64>
h-vector derived from f-vector.
h_i = Σ_j (-1)^{i-j} C(r-j, i-j) f_j
Sourcepub fn whitney_numbers_first_kind(&self) -> Vec<u64>
pub fn whitney_numbers_first_kind(&self) -> Vec<u64>
Whitney numbers of the first kind: |coefficient of q^i in χ_M(q)|.
Sourcepub fn whitney_numbers_second_kind(&self) -> Vec<u64>
pub fn whitney_numbers_second_kind(&self) -> Vec<u64>
Whitney numbers of the second kind: W_i = number of flats of rank i.
Sourcepub fn restrict(&self, subset: &BTreeSet<usize>) -> Matroid
pub fn restrict(&self, subset: &BTreeSet<usize>) -> Matroid
Restriction M|A: matroid on subset A with inherited independence.
Sourcepub fn contraction_deletion(&self, e: usize) -> (Matroid, Matroid)
pub fn contraction_deletion(&self, e: usize) -> (Matroid, Matroid)
Contraction-deletion pair: returns (M/e, M\e).
Sourcepub fn has_excluded_minor(&self, name: &str) -> bool
pub fn has_excluded_minor(&self, name: &str) -> bool
Check for a specific excluded minor by name.
Supported: “U24” (uniform 2,4), “F7” (Fano), “F7*” (dual Fano).