J. Appl. Math. & Computing Vol. 17(2005), No. 1 - 2, pp. 391 - 399
SOLVING A COMBINATORIAL PROBLEM WITH NETWORK
FLOWS
FLORIN MANEA∗ AND CĂLINA PLOSCARU
Abstract. In this paper we present an algorithm based on network flow
techniques which provides a solution for a combinatorial problem. Then,
in order to provide all the solutions of this problem, we make use of an
algorithm that given the bipartite graph G = (V1 ∪ V2 , E, w) outputs the
enumeration of all bipartite matchings of given cardinality v and cost c.
AMS Mathematics Subject Classification : 05C50, 05C85, 68W40, 90C35.
Key words and phrases : Bipartite graph, network flow, bipartite matching,
assignment problem.
1. Introduction
We consider the following problem: given a matrix M with n rows and k
columns, n ≥ k, M = (mij )1≤i≤n,1≤j≤k , such that mij ∈ IN, ∀i ∈ {1, . . . , n} and
j ∈ {1, . . . , k}, find a set
X
A = (x1 , y1 ), . . . , (xk , yk ) : ∀i 6= j, xi 6= xj , yi 6= yj and
mxi yi is minimal .
i=1,k
When n = k the problem coincides with the assignment problem solved by the
Hungarian Algorithm (presented by Kuhn in [8]) and, as it is known, this is
the same to the problem of finding a minimal weighted perfect matching in a
bipartite graph. We present in this paper an algorithm that solves the problem
in the general case, namely the restriction n = k is discarded. Here we assume
that n ≥ k (this does not restrict the generality of the problem, since if k > n,
then we can use the transposed matrix M T ).
Received November 27, 2003. Revised March 28, 2004. ∗ Corresponding author.
c 2005 Korean Society for Computational & Applied Mathematics and Korean SIGCAM.
391
392
Florin Manea and Călina Ploscaru
Next we are interested in finding all the solutions for the above-mentioned
problem. An algorithm for n = k is provided by Fukuda and Matsui (see [6]),
and it actually solves the problem of finding all the perfect matchings of minimal
weight in a given bipartite graph. We propose an algorithm that generalizes the
one designed by Fukuda and Matsui, in order to find all the matchings of minimal
weight that saturate V1 in the complete bipartite graph G = (V1 ∪ V2 , E, w),
which turns out to be equivalent with our task.
Also in this paper we will use a graph theory framework, using network flows
as a tool in constructing our algorithms.
2. Some basic facts
In this section we give some basic concepts and notations used throughout
the paper. For all unexplained notions, we refer to [1] and [3].
A bipartite weighted graph can be seen as a triple G = (V1 ∪ V2 , E, w) such
that
V1 ∩ V2 = ∅, E ⊂ {(i, j) : i ∈ V1 , j ∈ V2 }
is the set of edges and w : E → IN is the weight function. Note that by w(e)
we denote the weight of the edge e. A matching in such a graph is a subset
E 0 ∈ E such that if (i, j) and (k, l) are edges from E 0 we have i 6= k and j 6= l. A
maximal matching is a matching of maximal cardinality (|E| is used for denoting
for the cardinality of E).P
A maximal matching of minimal weight is a maximal
matching E 0 such that
w(e) is minimal. A matching that saturates V1 is
e∈E 0
a matching E 0 such that for every i ∈ V1 there exists (i, j) ∈ E 0 . In the case
|V1 | = |V2 | a matching that saturates V1 is called a perfect matching. It is not
hard to see that if |V1 | ≤ |V2 | then every matching that saturates V1 is a maximal
matching.
To find the maximal bipartite matching in G = (V1 ∪ V2 , E) we use the
following strategy. First we construct a directed network (associated with G)
N = (VN , EN , cN ) where VN = {s, d} ∪ V1 ∪ V2 ,
EN = {[s, v] : v ∈ V1 )} ∪ {[v, d] : v ∈ V2 } ∪ {[v, u] : v ∈ V1 , u ∈ V2 , (u, v) ∈ E}
(by [v, u] we denote the directed edge from v to u), and cN : EN → IN, cN (e) = 1
is the capacity function. It is not hard to prove that in this network a maximal
flow, of source s and destination d, is equivalent to a maximal bipartite matching
in G. Therefore applying the Ford-Fulkerson method to find this maximal flow
leads to an answer to our problem. The time complexity of this strategy is (if we
take |VN | = n and |EN | = m): O(nm) due to the fact that the above constructed
network is very particular). A better strategy requires O(n5/2 ) computational
time. For more details [3] and [1] are good references.
Solving a combinatorial problem with network flows
393
To find the maximal bipartite matching of minimal weight in G = (V1 ∪
V2 , E, w), we also construct the directed network associated with G, and we
apply an algorithm for maximal flow of minimal cost (see [1]). Using such
an algorithm the complexity of solving this problem is (with |VN | = n and
|EN | = m): O(n(m + n log n)) (see [1]). We will denote as M inBip(G) the
algorithm that finds the maximal bipartite matching of minimal weight in the
weighted graph G = (V1 ∪ V2 , E, w). Note that if we want to compute a bipartite
matching of given size (not necessary maximal) and minimal cost, the same
computational time is obtained.
Next we present the way we make use of these facts in solving the problem
mentioned in the beginning.
3. Computing one solution
We recall that we want to solve the following problem: let M be a matrix
with n rows and k columns, M = (mij )1≤i≤n,1≤j≤k , such that we have mij ∈
IN, ∀i ∈ {1, . . . , n} and j ∈ {1, . . . , k}. Find a set
X
A = (x1 , y1 ), . . . , (xk , yk ) : ∀i 6= j, xi 6= xj , yi 6= yj and
mxi yi is minimal .
i=1,k
First we reformulate this problem in a graph theory framework.
Let G = (V1 ∪ V2 , E, w) be a weighted bipartite complete graph such that:
• V1 = {(1, j) : j ∈ {1, . . . , k}} and V2 = {(i, 2) : i ∈ {1, . . . , n}}
• E = {((1, j), (i, 2)) : j ∈ {1, . . . , k}, i ∈ {1, . . . , n}}
• w : E → IN, w(e) = mij for e = ((1, j), (i, 2)) and w(e) = 0, otherwise.
We observe that in this graph, every node v from V1 is connected to every
node from V2 . So, we can state that a maximal bipartite matching saturates V1 .
Indeed, if E ∗ is a maximal bipartite matching and there is a node v ∈ V1 with no
edge from the matching incident in it, taking into account that |V1 | ≤ |V2 |, we
find a node u ∈ V2 such that (v, u) ∈ E − E ∗ and u is not saturated by E ∗ (no
edge from the matching is incident to it). So E ∗ ∪ (v, u) is a bipartite matching
with greater cardinality than E ∗ , a contradiction.
A maximal bipartite matching of minimal weight in G is a set of edges:
0
A = ((1, y1 ), (x1 , 2)), . . . , ((1, yk ), (xk , 2)) : ∀i 6= j, yi 6= yj , xi 6= xj ,
X
w(((1, yl ), (xl , 2))) is minimal .
l=1,k
394
Florin Manea and Călina Ploscaru
The members of the set A0 can be seen as the members of the set:
X
A = (x1 , y1 ), . . . , (xk , yk ) : ∀i 6= j, xi 6= xj , yi 6= yj and
mxi yi is minimal ,
i=1,k
which is the solution of the problem we try to solve.
In the aim of finding only one solution for our problem we propose an algorithm that computes a bipartite matching of minimal weight that saturates V1
in the graph G, namely M inBip(G). Consequently, in order to find all the solutions, we need an algorithm that outputs all the bipartite matchings of minimal
weight that saturate V1 .
4. Computing all the solutions
In this section we will show how we can compute all the bipartite matchings
of minimal weight that saturate V1 in the graph G designed in the last section.
First, remember that a maximal bipartite matching saturates V1 , hence a maximal bipartite matching E ∗ satisfies: |E ∗ | = |V1 |. The algorithms described
before not only find a bipartite matching of minimal weight, but they also provide this minimal weight; let Cmin be a denotation for it. So we are looking for
the bipartite matchings of cardinality |V1 | and weight Cmin .
We denote by P (G, M, v, c) a function that, given a weighted bipartite graph
G = (V ∪ U, E, w), a bipartite matching M of cardinality v and cost c, returns a
weighted bipartite matching in G, namely M 0 , such that |M 0 | = v and the weight
of this matching is c. In the case that no such matching exists, the algorithm will
highlight this situation by returning a special value denoted by NIL. In order
to find a maximal bipartite matchings of minimal weight in G, different from
M , we need to compute the functions P (G, M, min(|V |, |U |), Cmin ) (where Cmin
is the minimal cost of a matching). The function P (G, M, v, c) implements the
next simple algorithm:
1: Algorithm A
2. begin
3. for every edge e in M
4.
if exists M 0 a bipartite matching of cardinality v and cost c
in G − {e}
5.
then return M 0 and exit the function
6. return NIL
7. end
Note that in the algorithm, the step 6 is reached only if no matching with the
stated properties was found, and, consequently step 5 was never executed. The
computational time needed by this function is vT (|V |, |U |, |E| − 1, v, c) where
G = (V ∪ U, E, w) and T (|V |, |U |, |E|, v, c) is the time needed to compute in G a
Solving a combinatorial problem with network flows
395
bipartite matching of cardinality v and weight c. We can state that in the case
of a bipartite matching of minimal weight (denoted by Cmin ) and cardinality
v, the computational time for P (G, M, v, Cmin ) is O(vn(m + n log n)) (where
|V ∪ U | = n and |E| = m).
We continue by showing how this function can help us in computing all the
solutions to our problem. We denote by M (G) the number of bipartite matchings
in G of cardinality v and cost c. For some M1 and M2 in M (G) and e ∈ M1 −M2 ,
we partition the set of matchings in:
M1 = {M ∈ M (G) : e ∈ M },
M2 = {M ∈ M (G) : e ∈
/ M }.
Note that M1 ∈ M1 and M2 ∈ M2 . The two sets of matchings can be easily
characterized in the following way. Let e = (i, j) be an edge in G, and let
E 0 = {(i0 , j 0 ) ∈ E : i0 6= i, j 0 6= j} ∪ {(i, j)}, and G1 = (V ∪ U, E 0 ).
It is not hard to see that M1 = M (G1 ). We will denote by G/e the graph G1 .
Let G2 = (V ∪ U, E − {e}), and we obtain M2 = M (G2 ). This graph will be
denoted by G − e.
By solving the problems P (G/e, M1 , v, c) and P (G − e, M2 , v, c)) we obtain
two more bipartite matchings from M (G), different from M1 and M2 , such that
one of them is in M1 and the other in M2 . By applying recursively this algorithm
we construct a binary tree of subproblems. We solve the subproblems in this
tree by traversing it using a depth first strategy.
In the aim of a better understanding of the algorithm, note that a stack is a
list organized in the Last In First Out manner, and the operations that can be
performed on this data structure are: push- that introduces an element on top
of the list, and pop that extracts the element on top of the stack. Our algorithm
is, consequently, the next:
1. Algorithm B
2. input: A bipartite weighted graph G = (V ∪ U, E, w), a cost c and a
number v, the cardinality of the matching.
3. output: The set of bipartite matchings of G, of cardinality v and cost
c.
4. begin
5. compute a bipartite matching in G, of cardinality v and cost c,
6. if there exists such a matching then:
7.
begin
8.
let M be a bipartite matching in G, of cardinality v and cost
c.
9.
output M
10.
let P = ∅ be the stack of unsolved problems
11.
push P (G, M, v, c) in P
396
Florin Manea and Călina Ploscaru
12.
while
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
end
25. end
P 6= ∅ do
begin
pop P (G0 , M 0 , v, c) from the stack P
let M 00 = P (G0 , M 0 , v, c)
if M 00 6= ∅ then
begin
output M 00
let e ∈ M 0 − M 00
push P (G0 /e, M 0 , v, c) in P
push P (G0 − e, M 00 , v, c) in P
end
end
The corectness of the algorithm just presented comes from the next observations. First: if a new matching is obtained by solving the problem P (G0 , M 0 , v, c),
then M (G0 ) is partitioned in M (G0 − e) and M (G0 /e) for some e ∈ M 0 , and all
the matchings from M (G0 ) that will be returned from that moment will be from
these sets, so M 0 will not be returned again. Therefore the algorithm returns
the matchings without repetitions.
Next, we want to show that the algorithm outputs all the matchings.
We will prove that if at the step 13 of the algorithm the stack of unsolved
problems is [P (G, M, v, c) α] (where α = P (G1 , M1 , v1 , c1 ) . . . P (Gp , Mp , vp , cp )),
then when the stack contains only [α] all the matchings of G, with cardinality v
and weight c have been returned. This property is equivalent the fact that all the
matchings that we are looking for are returned by applying the Algorithm B,
since the stack is initialized in line 10 of the algorithm as the empty stack, and
then P (G, M, v, c) is pushed in it.
The proof will be done by induction on the number of edges of G. If the
graph G has less than min(|V |, |U |) edges then no matching is found in G, so no
more problems are inserted in the stack. Consequently, M is the only matching
in G with cardinality v and cost c, and it was already returned (just before the
problem P (G, M, v, c) was inserted in the stack).
We suppose now that G = (V ∪ U, E) has the number of edges |E| ≥
min(|V |, |U |) and suppose that we can compute, by solving the problem on top
of the stack, a matching M 0 different from M (otherwise the only matching with
the stated properties is M , which was already returned. It also follows that no
other problems are inserted in the stack, and this concludes our proof).
Let e ∈ M − M 0 be an edge. All the other matchings of G, that satisfy the
cardinality and cost conditions, can contain e or not, so they are in M (G/e) −
{M } or respectively in M (G − e) − {M 0 }. First the algorithm outputs M 0 ,
and then it inserts in the stack two new problems: P (G/e, M, v, c) and P (G −
Solving a combinatorial problem with network flows
397
e, M 0 , v, c). So the stack of unsolved problems will be:
[P (G/e, M, v, c) P (G − e, M 0 , v, c) α].
But in both the new problems the number of edges of the graphs G/e and G − e
is less than |E|, so we can apply twice our inductive assumption. Therefore we
state first that when the stack will be [P (G − e, M 0 , v, c) α] all the matchings
from G/e have been returned, and, after this, when the stack will be [α] all
the matchings from G − e have been also returned. But in this moment all the
matchings of value v and cost c from G have been returned, which concludes our
proof.
Also we observe that the usage of the depth-first rule in traversing the subproblems tree imposes the restriction that the length of the list of unsolved
problems P is less than 2|E|. To store one problem in the stack we use polynomial space, so the entire used space is polynomial.
The time complexity obtained by this algorithm is:
O((2x + 1)vT (|V |, |U |, |E| − 1, v, c) + vT (|V |, |U |, |E| − 1, v, c)),
for a graph G = (V ∪ U, E, w), where x is the number of solutions that verify
the conditions (of weight and cost). This is justified by the fact that every time
a matching that verifies the conditions is found, two more problems are inserted
in the stack, so we will solve maximum 2x + 1 problems.
In the case of finding all the bipartite matchings of minimal weight and maximal cardinality, if we take |V | = n1 , |V ∪ U | = n and |E| = m we obtain, we
obtain the time complexity
O((2x + 2)n1 n(m + n log n)).
We observe that this is a polynomial function in n, m, n1 and a linear function in
x, the number of solutions. Consequently, for the combinatorial problem stated
in the first section of the paper, we obtain all the x solutions in time:
O((2x + 2)k(k + n)(kn + (k + n) log (k + n))).
We can state the following theorem:
Theorem 1. The algorithm proposed above outputs all the matchings of given
cardinality and weight in time
O((2x + 2)vT (n1 , n2 , m − 1, v, c)),
for a graph G = (V ∪U, E, w), |V | = n1 , |U | = n2 , |E| = m (where x is the number
of matchings of weight equal to v and cost equal to c, and T (|V |, |U |, |E|, v, c) is
the time needed to compute in G a bipartite matching of cardinality v and weight
c).
398
Florin Manea and Călina Ploscaru
Note that this algorithm is a generalization for the algorithms proposed in [6]
and [7].
Remark 1. If we are looking for matchings of given cardinality v (not necessarily
maximal) and minimal cost, we obtain the same complexity as in the case of
maximal bipartite matchings with minimal cost.
Remark 2. Note that in sections 3 and 4 we solved the problem of finding a
solution to the maximum bipartite matching of minimal weight, and respectively,
the problem of finding all such matchings for weighted bipartite complete graphs.
Due to the general form of the algorithms presented in these sections, we can
address in the same way to the case of uncomplete graphs. Concluding, we
proposed a solution for the problem of finding all the bipartite matchings of
given cardinality and weight for all bipartite weighted graphs, as well.
It is interesting to see the relation between this general problem and the
combinatorial problem that was stated in the first chapter. In order to do this,
for a given bipartite weighted graph G = (V ∪ U, E, w), let V = {v1 , . . . , vk } and
U = {u1 , . . . , un }. We construct the next matrix M = (mij )1≤i≤k,1≤j≤n and
mij = w(vi , uj ) if the edge (vi , uj ) exists, and undefined otherwise (we denote
this by mij =↑). Now it is not hard to see that a bipartite matching of cardinality
v and weight c defines a set {(iP
l , jl ) : 1 ≤ l ≤ k} such that mil jl is defined for
all l, such that 1 ≤ l ≤ k, and l=1,k mil jl is minimal. Consequently, the task
of finding all such sets is the same to that of finding all the bipartite matchings
of cardinality v and weight c in the weighted bipartite graph G.
In this section we have proposed an efficient algorithm for computing all the
matchings of maximal cardinality and minimal weight, which leads to a complete
solution to the combinatorial problem presented in the beginning of our paper.
Of course, due to the special graph associated with this problem even better
time bounds could be obtained (in the maximal flow algorithms, we could use
the fact that the arcs in the network have capacity 1). However, our purpose,
to provide a good algorithm for solving this problem, was accomplished.
5. Conclusions
We have presented a complete solution for one combinatorial problem (a generalization of the assignment problem) using network flow techniques. This
approach using graph theory tools offers the chance of pointing out efficient
algorithms for computing weighted bipartite matchings.
A solution for the problem discussed in this paper is necessary also as a step in
providing efficient new algorithms for document classification, as it is presented
in some recent papers ([4], [5] and [9]).
Solving a combinatorial problem with network flows
399
6. Acknowledgements
The authors would like to thank to the anonymous referees for their valuable
comments, which improved the quality of the presentation of this paper.
References
1. R. Ahuja, T. Magnanti and J. Orlin, Network Flows, Prentice Hall, Inc., 1993.
2. E. Ciurea, An algorithm for minimal dynamic flow, Korean J. Comput. & Appl. Math.
7(2) (2000), 259-271.
3. T. H. Cormen, C. E. Leiserson and R. R. Rivest, Introduction to Algorithms, MIT Press,
1990.
4. L. P. Dinu, Aspecte Cantitative ı̂n Studiul Limbajelor, Ph. D. Thesis, Bucharest University,
2003.
5. L. P. Dinu, On the classification and aggreation of hierarchies with different constitutive
elements, Fundamenta Informaticae 55(1) (2003), 39-50.
6. K. Fukuda, T. Matsui, Finding all minimum cost perfect matchings in bipartite graphs,
Networks 22 (1992), 461-468.
7. K. Fukuda, T. Matsui, Finding all the perfect matchings in bipartite graphs, Appl. Math.
Lett. 7(1) (1994), 15-18.
8. H. W. Kuhn, The hungarian method for assignment problem, Naval Research Logistics
Quarterly 2 (1955), 83-97.
9. C. Ploscaru, Problema fluxului ı̂n reţele şi aplicaţii, Degree Thesis, Bucharest University,
2003.
10. Yong Yeon Shin and Jai Shang Koh, An algorithm for generating minimal cutsets of
undirected graphs, Korean J. Comput. & Appl. Math. 5(3) (1998), 681-695.
Florin Manea graduated from the Faculty of Mathematics and Computer Science,
Bucharest University in the summer of 2003, with a degree in Computer Science. Now
he is a Master student at the same faculty in the Department of Theoretical Computer
Science. The main research interests of Florin Manea are Formal Languages, Complexity,
Non-conventional Computational Paradigms, and Graphs Theory.
Faculty of Mathematics and Computer Science, Bucharest University, Academiei str., no.
14, 70109, Bucharest, Romania
e-mail: [email protected]
Călina Ploscaru graduated from the Faculty of Mathematics and Computer Science,
Bucharest University in the summer of 2003, with a degree in Computer Science. Now she
is a Master student at the Faculty of Letters, Bucharest University, where she is studying
Computational Linguistics. Her main research interests are: Computational Linguistics,
Graphs Theory, Artificial Intelligence and Databases Theory.
Faculty of Mathematics and Computer Science, Bucharest University, Academiei str., no.
14, 70109, Bucharest, Romania
e-mail: [email protected]
© Copyright 2026 Paperzz