Matroids
Creating Matroids
In this implementation of matroids, the ground set, S, is always of the form {1,2,...,m} where m is a nonnegative integer.
Matroid from a Matrix
Given a matrix A, use Matroid(A) to create a matroid based on the column vectors in A.
Matroid from a Graph or Multigraph
Given a graph g, use Matroid(g) to create the cycle matroid of g. Here, g is an undirected graph from the Graphs module. The graph may have loops, but multiple edges are not supported by Graphs.
We also provide a basic implementation of multigraphs, EasyMultiGraph, that allows multiple edges and loops. [See the documentation.] If g is an EasyMultiGraph, then Matroid(g) creates its cycle matroid.
Uniform Matroids
Use UniformMatroid(m,k) to create a matroid whose ground set is {1,2,...,m} in which all sets of size k or smaller are independent.
Matroid Properties
Basic Properties
Let M be a matroid.
The number of elements in the ground set is given by
ne(M).The rank of
Mis given byrank(M).If
Sis a subset of the elements ofM, the rank of that set is given byrank(M,S). This may be called on a list of elements (e.g.,rank(M,1,2,3)) or a vector of elements (e.g.,rank(M,[1,2,3])).Use
isindependent(M,S)to check ifSis an independent subset of the elements ofM.isloop(M,x)checks ifxis a loop element inM.
Bases
A basis of a matroid is a maximum-size independent set. To find a basis of a matroid M, use basis(M). Note that matroid typically has many bases. This function returns one of them with no guarantee as to which.
Given weights wt (specified as a Dict) for the elements of a matroid M, use min_weight_basis(M, wt) to return a basis the sum of whose weights is smallest.
The function random_basis(M) returns a random basis of M by the following algorithm: Assign random weights to the elements of M and then apply min_weight_basis.
Finally, all_bases(M) returns an iterator that generates all the bases of M. Note that the number of bases may be enormous.
Equality Testing (Randomized)
We provide the function fuzzy_equal that performs a randomized equality check of a pair of matroids. Two matroids are equal if their ground sets are equal and, for any subset X of the ground set, the rank of X is the same in both matroids.
If two matroids have, say, 20 elements each, testing that the rank functions give identical results would entail calculating the ranks of over a million subsets.
The function fuzzy_equal tests equality by repeatedly generating a random subset X of the ground set and checking that the rank of X is the same in both matroids.
To use this function, simply call fuzzy_equal(M1,M2). One thousand random sets X will be generated and their ranks compared. If the function returns false, the matroids are definitely not equal. If the function returns true, they probably are equal.
Options
- The number of tests can be modified by calling
fuzzy_equal(M1,M2,reps)with a different value forreps. - A random subset of the ground set is created by choosing each element of the ground set with probability
0.5. A different probability may be used by callingfuzzy_equal(M1,M2,reps,p)and providing a different value forp.
Operations
These operations create new matroids from previously created matroids.
Matroids are immutable; operations do not modify existing matroids.
Dual: $M^*$
For a matroid M, use dual(M) to create the dual of M.
The resulting matroid has the same ground set as M and the labels in the new matroid are the same as the labels in M.
Deletion: $M \backslash X$
Given a matroid M and a subset X of the ground set of M, the function delete(M,X) forms a new matroid by deleting the members of X from M. Here X may be either a Set or a Vector of integer values. In addition, delete(M,x), where x is an integer, deletes the single element from M. In all cases, the \ operator may be used: M\X or M\x.
Recall our convention that the ground set of a Matroid must be of the form {1,2,...,m}. The implication of this is that an element of the new matroid may correspond to a higher number element of the original.
For example, define a Matroid using the following 2x7 matrix:
julia> A = [1 2 3 4 5 6 7; 8 9 10 11 12 13 14]
2×7 Matrix{Int64}:
1 2 3 4 5 6 7
8 9 10 11 12 13 14
julia> M = Matroid(A)
{7, 2} matroidFrom this matroid, we delete elements 2 and 5.
julia> MM = delete(M, [2,5])
{5, 2} matroidThe deletion of element 2 from M makes element 3 in M move to position 2 in MM. Likewise, element 4 moves to position 3 in MM. We skip element 5 (it has been deleted) and so element 6 goes to position 4 in MM. Likewise element 7 in M becomes element 5 in MM.
This can be illustrated by examining the labels. Consider element 3 of M which is now at index 2 in MM:
julia> get_label(M,3)
2-element Vector{Int64}:
3
10
julia> get_label(MM,2)
2-element Vector{Int64}:
3
10Likewise, element 7 of M moves to position 5 in MM:
julia> get_label(M,7)
2-element Vector{Int64}:
7
14
julia> get_label(MM,5)
2-element Vector{Int64}:
7
14Contraction: $M / X$
Given a matroid M and a subset of its ground set X, use contract(M,X) to produced a new matroid formed by contracting the elements in X. Here, X may be either a Set or a Vector of integer values. In addition, contract(M,x), where x is an integer, contracts the single element x. In all cases the / operator may be used: M/X or M/x.
As in the case of deletion, the elements of X are eliminated from the matroid by the contraction operation, and the remaining elements are renumbered so that the resulting ground set is of the usual form, {1,2,...,m}.
Labels carry forward from the original matroid to the contracted result.
Element contraction in a matroid corresponds to edge contraction in a graph. For example, if we delete an edge from a cycle, we get a path, whereas if we contract an edge in a cycle we get a smaller cycle. This is reflected in the corresponding matroids:
julia> g = cycle_graph(8)
{8, 8} undirected simple Int64 graph
julia> M = Matroid(g)
{8, 7} matroid
julia> delete(M,1)
{7, 7} matroid
julia> contract(M,1)
{7, 6} matroidDisjoint Union: $M_1 + M_2$
Given matroids M1 and M2, the result of disjoint_union(M1,M2) is a new matroid defined as follows:
- Let
m1andm2be the number of elements ofM1andM2, respectively. - Form a copy of
M2(call itM2a) by shifting its elements from1tom2to be fromm1+1tom1+m2. - Let
S1andS2abe the ground sets ofM1andM2a, respectively. - The rank of a set
Xis calculated as the sum of the rank (inM1) ofX ∩ S1and the rank (inM2a) ofX ∩ S2.
This is analogous to the direct sum of matrices and the disjoint union of graphs. For example, suppose $A_1 = \begin{bmatrix} 1&2&3\\4&5&6 \end{bmatrix}$ and $A_2 = \begin{bmatrix} 7 & 8 \\ 9& 10 \end{bmatrix}$ and let M1 and M2 be their corresponding matroids. Then the disjoint union of M1 and M2 is the matroid derived from this matrix: $A_1 \oplus A_2 = \begin{bmatrix} 1 & 2 & 3 & 0 & 0 \\
4 & 5 & 6 & 0 & 0 \\
0 & 0 & 0 & 7 & 8 \\
0 & 0 & 0 & 9 & 10 \end{bmatrix}.$
The operations disjoint_union(M1, M2) may alternatively be invoked as M1 + M2.
To Do List
- Create a simple
MultiGraphtype to include multiple edges. - Other ways to create matroids (e.g., from a finite projective plane).