Finding All Minimum-Cost Perfect Matchings
in Bipartite Graphs
Komei Fukuda
Graduate School of Systems Management
The University of Tsukuba
3-29-1 Otsuka, Bun kyo-ku
Tokyo 7 12, Japan
Tomomi Matsui
Department of Industrial Administration
Science University of Tokyo
Noda, Chiba 278, Japan
The Hungarian method is an efficient algorithm for finding a minimal-cost perfect
matching in a weighted bipartite graph. This paper describes an efficient algorithm for
finding all minimal-cost perfect matchings. The computational time required to generate
+
each additional perfect matching is O(n(n m ) ) , and it requires O(n + m ) memory
storage. This problem can be solved by algorithms for finding the Kth-best solution of
assignment problems. However, the memory storage required by the known algorithms
grows in proportion to K , and, hence, it may grow exponentially in n . So, our special-
ized algorithm has a considerable advantage in memory requirement over the precious
more general algorithms for Kth-best assignment problems. Here we will show that the
enumeration of all minimal-cost perfect matchings can be reduced to the enumeration of
all perfect matchings in some bipartite graph. Therefore, our algorithm can be seen as an
algorithm for enumerating all perfect matchings in a given bipartite graph.
1. INTRODUCTION
Let us consider a bipartite graph B = ( U , V, E ) with row vertex set I!/ and
column vertex set V satisfying IUI = IVI = n , and edge set E CJ x V with \El =
m. A matching M is a subset of edges, no two of which are incident with a
common vertex. A matching M is perfect if each vertex is incident with exactly
one member of M . Given a cost function w : E H Q, where Q denotes the set of
rational numbers, we define the assignment problem (AP) as the problem of
finding an optimal matching, i.e., a perfect matching M that minimizes the total
cost EeEMw(e).
NETWORKS, Vol. 22 (1992) 461-468
0 1992 by John Wiley B Sons, Inc. CCC 0028-3045/92/050461-08$04.00
462 FUKUDA AND MATSUI
It is well known that an optimal solution to assignment problems can be
computed efficiently by the Hungarian method [3]. In this paper, we introduce
an algorithm for finding all optimal solutions of the assignment problem. The
enumeration problem may arise quite naturally, for instance, when there exists
some additional complex conditions that are too difficult to incorporate directly
into the constraints of assignment problems.
In our algorithm, we have to solve the assignment problem by the Hungar-
ian method at first and we then generate each additional perfect matching in
O ( n ( n + m))computational time. By using the notion of lexicographic ordering,
it saves the memory storage to O(n + m). This problem can be solved by
algorithms for finding the Kth-best solution of assignment problems [ 1, 41. The
algorithm developed by Murty [4] requires O ( n ( n log n + m))computational
time to generate each additional perfect matching and O ( K n 2 )memory storage,
and the algorithm developed by Chegireddy and Hamacher [ 11 requires O(n(n
log n + ~n))computational time to generate each additional perfect matching
and O(Kn) memory storage. In the worst case, the number K becomes n ! , and
the space requirements become prohibitive. So, our algorithm has a significant
advantage in memory requirement over the previous algorithms.
It will become clear below that the problem of finding all optimal solutions
can be reduced to the problem of finding all perfect matchings in a certain
subgraph of a given bipartite graph. The major part of our algorithm is essen-
tially the enumeration of all perfect matchings in a bipartite graph.
2. FUNDAMENTAL PROPERTIES
In this section, we describe a good characterization of optimal solutions to
assignment problems.
For any finite set S , we identify the function f : S H R with the corresponding
vector in Rs.For s E S , we abbreviate {s} by s. For any u E U U V, 6 ( u )
denotes the set of edges incident to the vertex u. Now we consider the follow-
ing problem:
P:
subject to x(e) = I , Vu E U ,
<,tfi(Il)
x(e) 2 0, e E E,
where x is a real vector in R E .A feasible extreme point to this linear program-
ming problem is integral if and only if it is the characteristic vector of a perfect
matching of the graph B = ( U , V , E ) .
MINIMUM-COST PERFECT MATCHINGS IN BIPARTITE GRAPHS 463
The dual of this linear program is
D: maximize C y(ir) + 2 y ( u )
ll€O UE L'
subject to y(ir) + y ( u ) 5 w(e), Ve = (11, u) E E,
where y is a real vector in R u u v .
Given a dual solution y E R L r U(not
v necessarily feasible), the udniissihle set
is defined to be the subset of edges E ( y ) = { P I y(ir) + y(u) = w(e), e = ( 1 1 , u ) E
E } and the admissible sirbgrcrph is the subgraph B ( y ) = ( U , V , E ( y ) )of B. The
well-known complementary slackness implies the following:
Claim 1. Let y* E R""" he an optimal solution to the diral linear program D.
Then, a perfect niatching M of B is optimal to AP if uric/ ordy if M C E ( y " ) .
It is obvious that the problem to find all optimal solutions of an assignment
problem is equivalent to the problem to find all perfect matchings in the admis-
sible graph B ( y * ) ,induced from a dual optimal solution y * . Since a dual optimal
solution y* can be obtained easily by the Hungarian method, we simply have to
describe an algorithm for finding all perfect matchings in a bipartite graph.
3. MAIN FRAMEWORK FOR THE ALGORITHM
In this section, we describe an algorithm for finding all perfect matchings in a
given bipartite graph B = ( U , V , E ) .
First, we give some notations and definitions. In this section, we set E = { 1 ,
2, . . . . in}. Given two vectors x, X I E R E , we say x is 1exic.ographic.alIy less
than x ' , denoted by x <lcl x ' , or X I >lpl x, if there exists an edge i E E satisfying
that x(j) = x ' ( j ) for j = 1 , . . . , i - I and x ( i ) < x ' ( i ) . For any subset of edges
E' C E , the characteristic vector of E' in {O, 1}€ is denoted by x E ' . Given a pair
of two-edge subset E l , E;, E E , we say El is Iexicogrciphicnlly lrss thon E 2 ,
denoted by E l E z , or El >/(,,E l , if x F 1 <lrr x E 2 . Given a set of perfect
matchings 4. in B , a perfect matching M in 4. is called lexicographicully maxi-
mum in Ju if each perfect matching in A4 different from M is lexicographically
less than M . Since the lexicographic order is a total order, the lexicographically
maximum perfect matching in Ju exists uniquely if .ht # 0.
The main idea of our algorithm is to generate all perfect matchings in a given
bipartite graph in lexicographic order. Then, we have to find the lexicographi-
cally maximum perfect matching in a given bipartite graph at first. Thus, it is
very natural to consider the following subproblem:
Subproblem P(B)
Input: A bipartite graph B and a linear ordering of the edge set of B .
Output: The lexicographically maximum perfect matching in B, if exists; else,
say none exists.
464 FUKUDA AND MATSUI
In the algorithm described in this section, we solve a sequence of the sub-
problems iteratively, and in the next section, we give a simple trick that re-
duces the computational effort for solving them.
When a perfect matching is obtained by solving the above subproblem, we
have to find the second maximum perfect matching in the given lexicographic
ordering.
For any perfect matching M in a given bipartite graph B , 2 ( M ) denotes the
set of all perfect matchings in B that are lexicographically less than M . Now we
consider a procedure to find the lexicographically maximum perfect matching
in X ( M ) . From the definition of lexicographic .ordering, it is clear that for any
perfect matching M ' such that M ' </e.r M there exists an edge e satisfying e E
M.e $! M ' , and x M ( i )= x M ' ( i )for any i < e . Given a perfect matching M and an
edge e E M , let
M ' is a perfect matching in B
e 4 M ' and x M ( i ) = x M ' ( i )for any i < e
From the above definition, it is clear that X ( M , e ) 2 ( M ) for any e E M . Then,
we have the following lemma:
Lemma 2. For any perfect matching M in a given bipartite graph B , the
following statements hold:
( i ) For any pair ofdistincr edges e , e' E M , 2 ( M , e ) n 2 ( M , e ' ) = 0.
(ii) UeEMX ( M , e ) = X ( M ) .
(iii) Let e , e' be a pair of distinct edges in M with e < e ' . Then, MI M z , for
any M I E Y ( M , e ) , M ? E 2 ( M , e ' ) .
Proof. It is clear from the definitions of X ( M , e ) and lexicographic ordering.
The properties (i) and (ii) of Lemma 2 imply that the n sets of perfect match-
ings Y ( M , e l ) , X ( M , e z ) , . . . , X(M,e n ) are a partition of Y(M),
where M =
{ e l , e z , . . . , e n } . Thus, the lexicographically maximum perfect matching in
X ( M ) is contained in exactly one of the sets of perfect matchings in the above
family, if it exists. Here, we note that X ( M , g ) = 0 for g = max{e 1 e E M } . The
above partition is also used in the algorithm for the k-best matching problem
developed by Murty 141.
The following lemma contains a simple but essential idea for finding the
lexicographically maximum perfect matching in Y ( M ) efficiently.
Lemma 3. Let Y ( M ) # 0, and let f = max{e I X ( M , e ) # 0,e E M } . Then,
the lexicographically maximum perfect matching in X ( M ) is in 2 ( M , f ) .
Proof. It is clear from the property (iii) of Lemma 2.
Then, the above lemma implies that by checking whether 2 ( M , e ) is empty or
MINIMUM-COST PERFECT MATCHINGS IN BIPARTITE GRAPHS 465
not for each e E M in the decreasing order we can find the edge e E M such that
Y(M,e) contains the lexicographically maximum perfect matching in 2 ( M ) .
Now we define two operations that are used to check whether 9 ( M , e) is
empty or not for each e E M in our algorithm. For any edge e = ( I ! , u ) , we
denote the bipartite graph ( U , V, (E\(S(u) U W u ) ) U e) by BIe (note that Ble is
different from the much used "contraction" operation). Clearly, from the defi-
nition, each perfect matching in BIe contains the edge e. Given a matching E' =
{ e l , e 2 , . . . , e i } (not necessarily perfect), the bipartite graph (. . . ((B/el)Iez)
. . . ) / e i is denoted by BIE'. Since the edge subset E' is a matching. the order of
the edges in the operation (. . . ( ( B / e l ) / e z.) . . ) I f i is irrelevant. For any edge
subset E' E , the bipartite graph ( U , V , E\E') is denoted by B\E'.
By using the above operations, we can define a special bipartite graph that
characterizes the set of perfect matchings 9 ( M , e). Given a perfect matching M
and an edge e E M , the bipartite graph (BIE')\E" is denoted by B ( M , P ) , where
E' = { f E M I f < c } and E" = { f E E\M I f < e} U e. Then, each perfect
matching in B ( M , e) contains the edge subset E' and contains no edge in En.
Thus, it is clear that the set of all perfect matchings in B ( M , e ) is equal to Y(M,
e). It implies that for each edge e E M we can obtain the lexicographically
maximum perfect matching in 2 ( M , e) by solving the subproblem P ( B ( M , e)).
Therefore, by solving (at most n - 1 ) subproblems P ( B ( M , en-l)), P ( B ( M ,
e,-?)), . . . , P ( B ( M , el)) in this order, we can easily find the lexicographically
maximum perfect matching in 2 ( M ) . where M = { P I , e ? , . . . , e,,} and e l <
ez < . . . < e l , .
Now we describe our algorithm. Here we assume that a given graph B con-
tains at least one perfect matching. In the following algorithm, if a perfect
matching M is obtained by solving the subproblem P ( B ' ) for some graph B ' ,
then we denote the perfect matching M by P,,,,,(B');else, P,,,,,(B')returns the
empty set.
Algorithm A
input: A bipartite graph B and a linear ordering of the edge set of B .
output: All the perfect matchings in B .
begin
solve P ( B ) and let M := P,,,,,(B);output M ; Q := M ;
while Q # 0 do
begin
let e be the last edge in Q;
if Y(M, e) # 0, then
begin
solve P ( B ( M , e)) and let M ' : = P,,,,,(B(M,e));
Q := M ' ;
set M := M ' ; output M ;
end
else Q := Q\e;
end
end
466 FUKUDA AND MATSUI
Lemma 4. Algorithm A oiitprrts all perfect mutchings in u given bipartite
gruph.
Proof. It is clear that Algorithm A outputs all perfect matchings in lexico-
graphic order.
In Algorithm A, we have to check whether 2 ( M , e ) is empty or not. From the
definition of B ( M , e ) , it can be reduced to the problem of finding a maximum
cardinality matching in the bipartite graph B ( M , e). The maximum cardinality
matching problem can be solved in O(n’ ‘) time [2]. However, we can further
reduce the computational complexity as follows: Let M be the current perfect
matching. Then, M\e is a matching in B ( M , e). By using this matching as an
initial solution of the maximum cardinality matching algorithm, it is enough to
find an alternating path connecting the two vertices incident to the edge e and
this requires O(n + m ) time [ 5 ] . Between the occurrences of consecutive out-
puts, we have to check whether Y ( M , e ) is empty or not at most n times. Now
assume that we can solve the subproblems P ( B ’ )in O ( T )time for any subgraph
B’ of a given bipartite graph B . Then, Algorithm A requires O ( n ( n + m ) + T )
computational time to generate each additional perfect matching.
Here we note that by defining w to be a weight function on the edge set
E satisfying w ( j ) = (a)’ the subproblem can be reduced to the maximum
weight perfect matching problem. Thus, by applying the Hungarian method,
Algorithm A generates each additional perfect matching in O(n(n + m ) +
n(n log n + m ) ) = O ( n ( n log n + m ) ) time.
4. IMPROVEMENT OF THE ALGORITHM
In the previous section, we assumed that there exists an algorithm for solving
the subproblem P ( B ) for any bipartite graph B , and in Algorithm A, a sequence
of subproblems are solved iteratively. In this section, we describe a new algo-
rithm that reduces the effort to solve the subproblems.
Let M = { e l , e 2 , . . . , e,,} be a perfect matching in B. Then, as shown in the
previous section, 2 ( M ) = 2 ( M , e l ) U 2 ( M , e?) U . . . U 2 ( M , e n ) .The key
edge of a perfect matching M is the edge f € E satisfying f = max{e I e E M .
Y ( M , e ) # 021) . Lemma 3 , the lexicographically maximum perfect match-
From
ing in 2 ( M ) is contained in 2 ( M , f ) . In the previous section, we identified the
set of integer numbers { I , 2, . . . , m} with the edge set E. The main idea of the
improvement of Algorithm A is to permute the integer numbers assigned on the
edge set E without sacrificing the correctness of the algorithm. Now we con-
sider a permutation of the integer numbers {f + I , f + 2, . . . , m} assigned on
the edges. After a permutation, we can also define the sets of perfect matchings
2 ( M , e) in the same way with the previous section, i.e.,
M ’ is a perfect matching in B
e @ M ’ and x M ( i ) = x M ’ ( i ) for any i < e
MINIMUM-COST PERFECT MATCHINGS IN BIPARTITE GRAPHS 467
Then, from the above definition, it is clear that for any edge e E M satisfying
e If a n y permutation of the numbers {f + I , f + 2 , . . . , m} assigned on the
edges does not affect the sets of perfect matchings Y ( M , e). Thus, in Algorithm
A, we can permute the integer numbers { f + 1 , f + 2, . . . , m } when the key
edge f of the current perfect matching M is found without changing the sets of
perfect matchings 2 ( M , e) for any e E M satisfying e 5 f.
In Algorithm A, we determine the key edge by finding a maximum cardinality
matching in the bipartite graph B ( M , e ) for each e E M . So, when the key edge
f of the current perfect matching is found, a perfect matching M ' E X ( M , f ) is
obtained. In our second algorithm described below, we permute the integer
numbers {.f + I , f + 2 , . . . , m } so that the perfect matching M ' becomes the
lexicographically maximum in Y ( M , f).This can be achieved easily by a per-
mutation satisfying the condition that {e E M ' I e > f} = { f + 1, f + 2 , . . . ,
f + j } , where j = I{e E M ' I P > f } l after the permutation.
Then, we can modify Algorithm A as follows:
Algorithm B
input: A bipartite graph B .
output: All the perfect matchings in B .
begin
find a perfect matching M in B ;
output M ;
let Q be a sequence of edges in M ;
while the length of Q is greater than 0 do
begin
let e be the last edge in the sequence Q;
if e E M and Y ( M , e) f 0, then
begin
find a perfect matching M ' E 2 ( M , e ) ;
add the edges M'\Q to the last of the sequence Q in an arbitrary order;
set M := M ' ; output M ;
end
else Q := Q\e;
end
end
Now we discuss the computational bound of the above algorithm. In Algo-
rithm B, we can check whether 2 ( M , e ) is empty or not, in the same way as in
Algorithm A, and it requires U ( n + m) time [ 5 ] . Between the occurrences of
consecutive outputs, we have to check at most n times. Thus, it requires O(n(n
+ m ) ) computational time to generate each additional perfect matching. The
length of the edge sequence Q in Algorithm B is less than or equal to m and
Algorithm B requires U ( n + m) memory storage.
To generate all minimum-cost perfect matchings, we have to solve the ordi-
nary minimum-cost perfect matching problem at first. The Hungarian method
solves it in O(n(n log n + m)) time and we can obtain the admissible set
468 FUKUDA AND MATSUI
simultaneously. By using an admissible set and an optimal perfect matching
obtained by the Hungarian method as an input data and an initial perfect match-
ing of Algorithm B , we can generate all minimum-cost perfect matchings. Thus,
the overall complexity to generate all minimum-cost perfect matchings is O(n(n
log n + m ) + Kn(n + m)),where K denotes the number of minimum-cost
perfect matchings.
REFERENCES
[l] C. R. Chegireddy and H. W. Hamacher, Algorithms for finding k-best perfect
matchings. Discrete Appl. Mark. 18 (1987) 155-165.
[2] J . E. Hopcroft and R. M. Karp, A n S i 2algorithm for maximum matching in bipartite
graphs. SIAM J . Comput. 2 (1973) 225-23 I .
[3] H. W. Kuhn, The Hungarian method for the assignment problem. Naual R e s . L o g .
Q. 2 (1955) 83-97.
[4] K. G. Murty, An algorithm for ranking all the assignments in order of increasing
cost. Operations Res. 16 (1968) 682-687.
[ S ] R . Tarjan, Depth-first search and linear graph algorithms. SIAM J . C o m p ~ r 1
.
(1972) 146-160.
Received January 1989
Accepted January 1992