This module implements the functions pertaining to matching of undirected
graphs. A matching in a graph is a set of pairwise nonadjacent links
(nonloop edges). In other words, a matching in a graph is the edge set of an
1-regular subgraph. A matching is called a perfectmatching if it the
subgraph generated by a set of matching edges spans the graph, i.e. it’s the
edge set of an 1-regular spanning subgraph.
Return the vertices reachable from vertex via an even alternating path
starting with a non-matching edge.
This method implements the algorithm proposed in [LR2004]. Note that
the complexity of the algorithm is linear in number of edges.
INPUT:
vertex – a vertex of the graph
matching – a matching of the graph; it can be given using any
valid input format of Graph
OUTPUT:
even – the set of vertices each of which is reachable from the
provided vertex through a path starting with an edge not in the
matching and ending with an edge in the matching; note that a note that a
ValueError is returned if the graph is not simple
EXAMPLES:
Show the list of required vertices for a graph \(G\) with a matching \(M\)
for a vertex \(u\):
For a factor critical graph \(G\) (for instance, a wheel graph of an odd
order) with a near perfect matching \(M\) and \(u\) being the (unique)
\(M\)-exposed vertex, each vertex in \(G\) is reachable from \(u\) through an
even length \(M\)-alternating path as described above:
For a matching covered graph \(G\) (for instance, \(K_4 \odot K_{3,3}\)) with a
perfect matching \(M\) and for some vertex \(u\) with \(v\) being its \(M\)-matched
neighbor, each neighbor of \(v\) is reachable from \(u\) through an even length
\(M\)-alternating path as described above:
For a bicritical graph \(G\) (for instance, the Petersen graph) with a
perfect matching \(M\) and for some vertex \(u\) with its \(M\)-matched neighbor
being \(v\), each vertex of the graph distinct from \(v\) is reachable from \(u\)
through an even length \(M\)-alternating path as described above:
'Edmonds' uses Edmonds’ algorithm as implemented in NetworkX to
find a matching of maximal cardinality, then check whether this
cardinality is half the number of vertices of the graph.
'LP_matching' uses a Linear Program to find a matching of
maximal cardinality, then check whether this cardinality is half the
number of vertices of the graph.
'LP' uses a Linear Program formulation of the perfect matching
problem: put a binary variable b[e] on each edge \(e\), and for
each vertex \(v\), require that the sum of the values of the edges
incident to \(v\) is 1.
solver – string (default: None); specifies a Mixed Integer
Linear Programming (MILP) solver to be used. If set to None, the
default one is used. For more information on MILP solvers and which
default solver is used, see the method solve of the class
MixedIntegerLinearProgram.
verbose – integer (default: 0); sets the level of verbosity:
set to 0 by default, which means quiet (only useful when
algorithm=="LP_matching" or algorithm=="LP")
A nontrivial graph \(G\) is bicritical if \(G - u - v\) has a perfect
matching for any two distinct vertices \(u\) and \(v\) of \(G\). Bicritical
graphs are special kind of matching covered graphs. Each maximal barrier of
a bicritical graph is a singleton. Thus, for a bicritical graph, the
canonical partition of the vertex set is the set of sets where each set is
an indiviudal vertex. Three-connected bicritical graphs, aka bricks, play
an important role in the theory of matching covered graphs.
This method implements the algorithm proposed in [LZ2001] and we
assume that a connected graph of order two is bicritical, whereas a
disconnected graph of the same order is not. The time complexity of
the algorithm is \(\mathcal{O}(|V| \cdot |E|)\), if a perfect matching of
the graph is given, where \(|V|\) and \(|E|\) are the order and the size of
the graph respectively. Otherwise, time complexity may be dominated by
the time needed to compute a maximum matching of the graph.
Note that a ValueError is returned if the graph has loops or if
the graph is trivial, i.e., it has at most one vertex.
INPUT:
matching – (default: None); a perfect matching of the
graph, that can be given using any valid input format of
Graph.
If set to None, a matching is computed using the other parameters.
algorithm – string (default: 'Edmonds'); the algorithm to be
used to compute a maximum matching of the graph among
'Edmonds' selects Edmonds’ algorithm as implemented in NetworkX,
'LP' uses a Linear Program formulation of the matching problem.
coNP_certificate – boolean (default: False); if set to
True a set of pair of vertices (say \(u\) and \(v\)) is returned such
that \(G - u - v\) does not have a perfect matching if \(G\) is not
bicritical or otherwise None is returned.
solver – string (default: None); specify a Mixed Integer
Linear Programming (MILP) solver to be used. If set to None, the
default one is used. For more information on MILP solvers and which
default solver is used, see the method solve of the class
MixedIntegerLinearProgram.
verbose – integer (default: 0); sets the level of verbosity:
set to 0 by default, which means quiet (only useful when algorithm=='LP').
The graph obtained by splicing two bicritical graph is also bicritical.
For instance, \(K_4\) with one extra (multiple) edge (say \(G := K_4^+\)) is
bicritical. Let \(H := K_4^+ \odot K_4^+\) such that \(H\) is free of multiple
edge. The graph \(H\) is also bicritical:
A graph of order \(n\) is factor-critical if every subgraph of \(n-1\)
vertices have a perfect matching, hence \(n\) must be odd. See
Wikipedia article Factor-critical_graph for more details.
This method implements the algorithm proposed in [LR2004] and we assume
that a graph of order one is factor-critical. The time complexity of the
algorithm is linear if a near perfect matching is given as input (i.e.,
a matching such that all vertices but one are incident to an edge of the
matching). Otherwise, the time complexity is dominated by the time
needed to compute a maximum matching of the graph.
INPUT:
matching – (default: None); a near perfect matching of the
graph, that is a matching such that all vertices of the graph but one
are incident to an edge of the matching. It can be given using any
valid input format of Graph.
If set to None, a matching is computed using the other parameters.
algorithm – string (default: 'Edmonds'); the algorithm to use
to compute a maximum matching of the graph among
'Edmonds' selects Edmonds’ algorithm as implemented in NetworkX
'LP' uses a Linear Program formulation of the matching problem
solver – string (default: None); specifies a Mixed Integer
Linear Programming (MILP) solver to be used. If set to None, the
default one is used. For more information on MILP solvers and which
default solver is used, see the method solve of the class
MixedIntegerLinearProgram.
verbose – integer (default: 0); sets the level of verbosity:
set to 0 by default, which means quiet (only useful when algorithm=="LP")
A connected nontrivial graph wherein each edge participates in some
perfect matching is called a matchingcoveredgraph.
If a perfect matching of the graph is provided, for bipartite graph,
this method implements a linear time algorithm as proposed in [LM2024]
that is based on the following theorem:
Given a connected bipartite graph \(G[A, B]\) with a perfect matching
\(M\). Construct a directed graph \(D\) from \(G\) such that \(V(D) := V(G)\)
and for each edge in \(G\) direct the corresponding edge from \(A\) to \(B\)
in \(D\), if it is in \(M\) or otherwise direct it from \(B\) to \(A\). The
graph \(G\) is matching covered if and only if \(D\) is strongly connected.
For nonbipartite graph, if a perfect matching of the graph is provided,
this method implements an \(\mathcal{O}(|V| \cdot |E|)\) algorithm, where
\(|V|\) and \(|E|\) are the order and the size of the graph respectively.
This implementation is inspired by the \(M\)-\(alternating\)\(tree\)\(search\)
method explained in [LZ2001]. For nonbipartite graph, the
implementation is based on the following theorem:
Given a nonbipartite graph \(G\) with a perfect matching \(M\). The
graph \(G\) is matching covered if and only if for each edge \(uv\)
not in \(M\), there exists an \(M\)-\(alternating\) odd length \(uv\)-path
starting and ending with edges not in \(M\).
The time complexity may be dominated by the time needed to compute a
maximum matching of the graph, in case a perfect matching is not
provided. Also, note that for a disconnected or a trivial or a
graph with a loop, a ValueError is returned.
INPUT:
matching – (default: None); a perfect matching of the
graph, that can be given using any valid input format of
Graph.
If set to None, a matching is computed using the other parameters.
algorithm – string (default: 'Edmonds'); the algorithm to be
used to compute a maximum matching of the graph among
'Edmonds' selects Edmonds’ algorithm as implemented in NetworkX,
'LP' uses a Linear Program formulation of the matching problem.
coNP_certificate – boolean (default: False); if set to
True an edge of the graph, that does not participate in any
perfect matching, is returned if \(G\) is not matching covered or
otherwise None is returned.
solver – string (default: None); specify a Mixed Integer
Linear Programming (MILP) solver to be used. If set to None, the
default one is used. For more information on MILP solvers and which
default solver is used, see the method solve of the class
MixedIntegerLinearProgram.
verbose – integer (default: 0); sets the level of verbosity:
set to 0 by default, which means quiet (only useful when algorithm=='LP').
A corollary to Tutte’s fundamental result [Tut1947], as a
strengthening of Petersen’s Theorem, states that every 2-connected
cubic graph is matching covered:
A connected bipartite graph \(G[A, B]\), with \(|A| = |B| \geq 2\), is
matching covered if and only if \(|N(X)| \geq |X| + 1\), for all
\(X \subset A\) such that \(1 \leq |X| \leq |A| - 1\). For instance,
the Hexahedral graph is matching covered, but not the path graphs on
even number of vertices, even though they have a perfect matching:
A connected bipartite graph \(G[A, B]\) of order six or more is matching
covered if and only if \(G - a - b\) has a perfect matching for some
vertex \(a\) in \(A\) and some vertex \(b\) in \(B\):
Given a graph \(G\) such that each edge \(e\) has a weight \(w_e\), a maximum
matching is a subset \(S\) of the edges of \(G\) of maximum weight such that
no two edges of \(S\) are incident with each other.
As an optimization problem, it can be expressed as:
\[\begin{split}\mbox{Maximize : }&\sum_{e\in G.edges()} w_e b_e\\
\mbox{Such that : }&\forall v \in G,
\sum_{(u,v)\in G.edges()} b_{(u,v)}\leq 1\\
&\forall x\in G, b_x\mbox{ is a binary variable}\end{split}\]
INPUT:
value_only – boolean (default: False); when set to True,
only the cardinal (or the weight) of the matching is returned
algorithm – string (default: 'Edmonds')
'Edmonds' selects Edmonds’ algorithm as implemented in NetworkX
'LP' uses a Linear Program formulation of the matching problem
use_edge_labels – boolean (default: False)
when set to True, computes a weighted matching where each edge
is weighted by its label (if an edge has no label, \(1\) is assumed)
when set to False, each edge has weight \(1\)
solver – string (default: None); specifies a Mixed Integer
Linear Programming (MILP) solver to be used. If set to None, the
default one is used. For more information on MILP solvers and which
default solver is used, see the method solve of the class
MixedIntegerLinearProgram.
verbose – integer (default: 0); sets the level of verbosity:
set to 0 by default, which means quiet (only useful when algorithm=="LP")
When value_only=False (default), this method returns an
EdgesView containing the edges of a maximum matching of \(G\).
When value_only=True, this method returns the sum of the
weights (default: 1) of the edges of a maximum matching of \(G\).
The type of the output may vary according to the type of the edge
labels and the algorithm used.
ALGORITHM:
The problem is solved using Edmond’s algorithm implemented in NetworkX,
or using Linear Programming depending on the value of algorithm.
Return an iterator over all perfect matchings of the graph.
ALGORITHM:
Choose a vertex \(v\), then recurse through all edges incident to \(v\),
removing one edge at a time whenever an edge is added to a matching.
INPUT:
labels – boolean (default: False); when True, the edges
in each perfect matching are triples (containing the label as the
third element), otherwise the edges are pairs.