UNO1997 Algorithms For Enumerating All Perfect Maximum and Maximal Matchings in Bipartite Graphs
UNO1997 Algorithms For Enumerating All Perfect Maximum and Maximal Matchings in Bipartite Graphs
Abstract: For a bipartite graph G = (V, E), (1) perfect, (2) maximum and (3) max-
imal matchings are matchings (1) such that all vertices are incident to some matching
edges, (2) whose cardinalities are maximum among all matchings, (3) which are con-
tained in no other matching. In this paper, we present three algorithms for enumerating
these three types of matchings. Their time complexities are O(|V |) per a matching.
Introduction
Let G = (V 1 ∪ V2 , E) be an undirected bipartite graph with vertex sets V1 and
V2 and edge set E ⊆ V1 × V2 . We denote the number of vertices and edges by n and
m. A matching M of the graph G is an edge set such that no two edges of M share
their endpoints. Edges of a matching are called matching edges. If a vertex is incident
to a matching edge, we say that the vertex is covered by the matching, and otherwise
uncovered. We call a matching with the maximum cardinality among all matchings
in G a maximum matching, and a matching which is properly contained in no other
matching a maximal matching. If all vertices of G are covered by a matching M , i.e.
M covers all vertices, then we say that M is a perfect matching. In this paper, we
consider the problems of enumerating all those matchings for a given bipartite graph.
In 1977, Tsukiyama et al. proposed an algorithm for enumerating all maximal
stable sets of a general graph [4]. It can be applied for the problem of maximal match-
ings in bipartite graphs within O(nm2 ) time per a maximal matching. For perfect
matchings in a bipartite graph, K. Fukuda and T. Matsui proposed an enumeration
algorithm [1]. Their algorithm takes O(n1/2 m + mNp) time where Np is the number of
perfect matchings in the given graph. Our main result is to speed up this algorithm.
We also obtain algorithms for maximum and maximal matchings by modifying the
improved algorithm. They take only O(n) time per a matching.
V1
V2
G D(G,M)
Fig. 1. The graph D(G, M ) and a cycle and a path
corresponding alternating cycle and feasible path in G.
them. We can obtain a new matching different from M by exchanging matching edges
with the other edges along an alternating cycle or a feasible path. On the other hand,
the symmetric difference between M and a different matching is composed of some
alternating cycles and paths, which correspond to cycles and feasible paths in D(G, M ).
A matching generated by an alternating cycle has cardinality equal to M . Since
a perfect matching covers all vertices, the symmetric difference between two perfect
matchings is composed only of alternating cycles. Hence there is another perfect match-
ing iff there exists an alternating cycle.
The algorithm of [1] utilizes this property. Firstly it checks whether there is a per-
fect matching in the given graph or not by finding a maximum matching. We spend
O(n1/2 m) time to do this [2]. After finding the perfect matching M , the algorithm
starts enumerating all perfect matchings taking as input the graph G and the match-
ing M . Firstly, it checks whether there is another perfect matching or not. This is done
by finding a cycle in D(G, M ) by depth-first search with O(m + n) time [3]. If there
is no other perfect matching, the algorithm stops. Otherwise, it constructs a perfect
matching M by using the cycle and outputs M . Next it chooses an edge e included
in both M and the cycle. That is, e is included in M but not in the generated per-
fect matching M . The algorithm generates two subproblems, to enumerate all perfect
matchings including e and not including e. For these subproblems, the algorithm con-
structs two subgraphs G+ (e) and G− (e) of G. G+ (e) is the graph obtained by deleting
e and both endpoints and all edges adjacent to e. G− (e) is the graph obtained by
deleting e from G. We have one-to-one corresponding between all perfect matchings in
G+ (e) and all perfect matchings in G including e, and between all perfect matchings
in G− (e) and all perfect matchings in G not including e. The algorithm solves the
subproblems by two recursive calls. One of them inputs a graph G+ (e) and a perfect
matching M \ e of G+ (e), and the other inputs G− (e) and M ( see Fig. 2).
Since all of these operations take O(n) time, the bottle neck part of the algorithm
on the time complexity is depth-first search with O(m + n) time. Thus, the algorithm
takes O(m) time per a perfect matching. The memory complexity is bounded by
O(m). In each recursive call, we store the given graph. This requires O(m) space, but
by storing only deleted edges when a recursive call occurs, we can reduce the size of
the required memory space. Since the number of these deleted edges does not exceed
m, the accumulating storing space is bounded by O(m).
Our algorithm is speeded up by adding some improvements to the algorithm. One of
these improvements is to trim unnecessary edges which are included in no cycle before
generating a subproblem. Since it treats only arcs included in cycles of D(G, M ), we
can delete arcs included in no cycle. The arcs included in no cycle of a directed
e e
G+(e) G\e
Fig. 2. A perfect matching M is generated from M by exchanging edges along an
alternating cycle. Two subproblems with the graph G+ (e) and M , and G− (e) and
M are generated. Dotted lines are deleted edges from the graph G.
Since one iteration still spends O(m) time, the algorithm seems to take O(n1 /2m+
mNp) time. In the next section, we analyze the time complexity more carefully, and
bound it by O(n1 /2m + nNp ).
Properties for Bounding the Time Complexity
In this section, we bound the time complexity by proving some properties. To
analyze the time complexity, we introduce the enumeration tree T expressing the
movement of the algorithm. It is defined for the algorithm and an input, which is the
given graph. Each vertex of T corresponds to a recursive call of the algorithm, and if a
recursive call occurs in another one, an edge connects the corresponding vertices. The
root of the tree corresponds to the start of the algorithm. The algorithm generates
two subproblems if it outputs a perfect matching, thus all internal vertices of T have
exactly two children and have a one-to-one correspondence to all perfect matchings.
Each recursive call corresponding to a vertex x of T inputs a graph. We denote
the input graph by Gx . The time complexity of the algorithm is given by the sum of
|E(Gx )| + |V (Gx )| over all vertices of T where E(Gx ) and V (Gx ) are the edge set and
the vertex set of the graph Gx . To bound it, we will show that if the input graph G has
more than 4|V (G)| edges, then there is an edge e such that at least |E(G)|/4 arcs
are included in some cycle of D(G+ (e), M ). We will propose an algorithm running in
O(m+n) time for finding such edges later. We also show that if all arcs in D(G, M ) are
included in some cycles, then G has at least m − n perfect matchings. From these facts,
for any internal vertex x of T , the child of x corresponding to the recursive call with
G+x (e) has at least (|E(Gx )| − |V (Gx )|)/4 descendants. For each vertex x of T with
|E(Gx )| ≥ 4|V (Gx )|, we assign its O(|E(Gx )|) computing time to the descendants of
its child with G+ (e) uniformly. Since the subproblem with G+ x (e) exactly less vertices
than the original problem, a vertex is assigned O(1) time by at most n ancestors. Thus
the sum of assigned time for a vertex is O(n) and the total time complexity is O(nNp ).
We next show proofs of these statements. Through the section, we assume that G
contains a perfect matching M and all arcs in D(G, M ) are included in some cycles.
Lemma 1. G contains at least m − n perfect matchings.
Fig. 3. Dotted lines compose the generated cycle for an arc e ∈ T by using T .
Step 1: Find a depth-first search tree T in D (G, M ) with the root vertex r.
Step 2: Apply a strongly connected component decomposition algorithm for the
graph obtained by removing the last visited vertex v. If m/2 − n edges
are included in some cycles, output v and stop.
Step 3: Find u1 and u2 .
Fig. 4. Exchanging along a length 2 feasible path and generating graph G+ (e).
All edges changed their directions are eliminated
and no edge of G+ (e) is not changed its direction.
Fig. 5: A maximal matching and a vertex v. The subgraph circled by dotted line is Ĝ.
the other which does not. All maximal matchings covering v include exactly one edge
incident to v. All maximal matchings not covering v include no such edge, and from
the maximality, all vertices adjacent to v are covered by these maximal matchings.
Enumerating all maximal matching covering v is easy. For all edges e incident to v,
we enumerate all maximal matchings in G+ (e). For an edge e, a maximal matching in
G+ (e) and e forms a maximal matching in G, and conversely all maximal matchings
including e contain maximal matchings in G+ (e). Thus we can enumerate all maximal
matchings covering v by enumerating all maximal matchings including edges incident
to v. For matchings not covering v, let Ĝ be the subgraph composed of edges incident
to vertices adjacent to v, except for edges incident to v ( see Fig. 5). Any maximal
matching not covering v includes a matching of Ĝ with the cardinality d(v) where
d(v) is the degree of v, the number of vertices adjacent to v. Conversely, for any
matching M̂ of Ĝ with cardinality d(v), some maximal matchings in G must include
it. Thus, by finding all matchings having cardinality d(v) and enumerating maximal
matchings including them, we can enumerate all maximal matchings not including
v. To enumerate maximal matchings including M̂ , we remove all edges and vertices
adjacent and incident to edges of M̂ from G and enumerate all maximal matchings in
the obtained graph.
Now we can enumerate all maximal matchings by finding all maximal matchings
covering v and not. We show the details of the algorithm.
The algorithm spends O(n) time in Step 1 and 2, and O(n) time in Step 3 and 5 for
a recursive call. We spend O(d(v)m) time to find a maximum matching of Ĝ in Step
4. Therefore it seems to be O(d(v)m) time algorithm for an output, but we will show
that it terminates in shorter time. To bound the time complexity of the algorithm,
we introduce the enumeration tree of the algorithm. Each vertex corresponds to each
recursive call, and each leaf of the tree corresponds to each maximal matching. Since we
choose a vertex with degree at least 2 in Step 2, any internal vertex of the enumeration
tree has at least two children. Therefore the number of internal vertices is less than Nl .
where Nl is the number of maximal matchings. Now we show the following property.
Lemma 3. G contains at least (m − n)/2 maximal matchings.
Proof. Let M be a maximum matching of G. For each edge e not in M , we generate
a maximal matching by exchanging e and the matching edges adjacent to e. If the
generated matching is not maximal, we add some edge so that it is maximal. The
removed and added edges compose an alternating path or an alternating cycle. Since
M is maximum, the length of the generated cycles and paths are at most 4. Thus,
each matching is generated by at most two edges. Thus there are at least (m − n)/2
maximal matchings.
From the lemma, for each edge e incident to v, we have at least (|G+ (e)|− n)/2 ≥
(m − 2n)/2 maximal matchings in G+ (e). Thus we have at least d(v)(m − 2n)/2
maximal matchings in G. For a vertex x of the enumeration tree corresponding to a
recursive call, d(vx )(|E(Gx )| − 2|V (Gx )|)/2 ≥ 12 d(vx )|E(Gx )| if |E(Gx )| ≥ 4|V (Gx )|
where vx is the vertex chosen at Step 2 in the corresponding recursive call. In this
case, we assign O(d(vx )|E(Gx )|) time to all descendants of it. Each descendant is
assigned only O(1) time. In the case that |E(Gx )| < 4|V (Gx )|, the algorithm takes
O(d(vx )|V (Gx )|) time in Step 4, and we assign their time to all its children. Since the
number of children is at least d(vx ), each children is assigned at most O(n) time by
its parent. Since each vertex of the enumeration tree is assigned at most O(n) time by
its parent and O(1) time by its at most n ancestors, the total time spent by Step 4 is
bounded by O(nNl ) time. We now obtain the following theorem.
Acknowledgment
We greatly thank Professor Akihisa Tamura of University of Electro-Communications
for his kindly advice. We would like to express my sincere thanks to Professor Yoshiko
Tamura Ikebe.
References
[1] K. Fukuda and T. Matsui, “Finding All the Perfect Matchings in Bipartite Graphs,”
Appl. Math. Lett. 7 1 (1994) 15-18
[2] J. E. Hopcroft and R. M. Karp, “An n5/2 algorithm for maximum matching in
bipartite graphs,” SIAM J. on Comp., Vol. 2: 225-231, 1973.
[3] R. E. Tarjan, “Depth-First Search and Linear Graph Algorithm,” SIAM J. Comp.
1, 146-169, 1972.
[4] S.Tsukiyama, M.Ide, H.Ariyoshi and I.Shirakawa, “A New Algorithm for Generat-
ing All the Maximum Independent Sets,” SIAM J. Comp.,Vol.6, No.3: 505-517,1977.