Bipartite Edge-Colouring in O ( M) Time: Max Max Max
Bipartite Edge-Colouring in O ( M) Time: Max Max Max
Bipartite Edge-Colouring in O ( M) Time: Max Max Max
1
Alexander Schrijver
2
Abstract. We show that a minimum edge-colouring of a bipartite graph can be found in O(m)
time, where and m denote the maximum degree and the number of edges of G, respectively. It is
equivalent to nding a perfect matching in a k-regular bipartite graph in O(km) time.
By sharpening the methods, a minimum edge-colouring of a bipartite graph can be found in
O((p
max
() + log )m) time, where p
max
() is the largest prime factor of . Moreover, a perfect
matching in a k-regular bipartite graph can be found in O(p
max
(k)m) time.
1. Introduction
In a classical paper, K onig [9] showed that the edges of a bipartite graph G can be
coloured with (G) colours, where (G) is the maximum degree of G. (In this paper,
colouring edges presumes that edges that have a vertex in common obtain dierent colours.)
K onigs proof is essentially algorithmic, yielding an O(nm) time algorithm (n and m
denote the numbers of vertices and edges, respectively, of the graph). As was shown by
Gabow [4], the O(
nmlog ) bipartite edge-colouring algorithm. This was improved by Cole and Hopcroft
[1] to O(mlog m), by extending methods of Gabow and Kariv [5], [6].
Fixing the maximum degree , the methods found as yet are superlinear (albeit slightly).
In this paper we give a linear-time method for xed or bounded . More precisely, we give
an O(m) method for bipartite edge-colouring. It implies (in fact, is equivalent to) nding
a perfect matching in a k-regular bipartite graph in O(km) time.
Ultimately one would hope for an O(mlog k) (or even O(m)) algorithm nding a perfect
matching in a k-regular bipartite graph, and for an O(mlog ) algorithm for bipartite edge-
colouring (the rst algorithm implies the second, by a method of Gabow [4] see below).
We did not nd such algorithms, although our methods can be extended to obtain some
supporting results.
In particular, dene, for any natural number k,
(k) :=
t
i=1
p
i
i1
j=1
p
j
, (1)
where p
1
p
t
are primes with k = p
1
. . . p
t
. We give an O((() + log )m)
bipartite edge-colouring algorithm. Note that in () +log , the term () dominates if
is prime, while log dominates if is a power of 2. Note also that () 2p
max
(),
where p
max
() denotes the largest prime factor in . So xing the maximum prime factor
of , there is an O(mlog ) bipartite edge-colouring algorithm.
Moreover, we give an O((k)m) algorithm nding a perfect matching in a k-regular
bipartite graph. So bounding the maximum prime factor of k, there is a linear-time perfect
matching algorithm for k-regular bipartite graphs.
1
Key Words: bipartite, edge-colouring, complexity, timetabling, perfect, matching
Subject Classication: Primary: 68R10, 90C27, Secondary: 05C70, 05C85
2
CWI, Kruislaan 413, 1098 SJ Amsterdam, The Netherlands and Department of Mathematics, University
of Amsterdam, Plantage Muidergracht 24, 1018 TV Amsterdam, The Netherlands.
1
The proof idea is an extension of the following idea of Gabow [4] to nd a perfect
matching in a 2
t
-regular bipartite graph G in linear time. First nd an Eulerian orientation
of G (taking O(m) time), and consider those edges that are oriented from vertex-colour I
to vertex-colour II (in the 2-vertex-colouring of G). This gives a 2
t1
-regular subgraph of
G. Repeating this, we end up with a 1-regular subgraph of G, being a perfect matching in
G. The time is O(m+
1
2
m+
1
4
m+ ) = O(m).
One can similarly nd a 2
t
-edge-colouring in O(tm) time. In extending this method
to prime factors other than 2 we use some techniques of [10] for estimating the number of
perfect matchings and edge-colourings of bipartite graphs.
In this paper, all graphs may have multiple edges.
2. Some practical motivation
As is well-known, bipartite edge-colouring can be applied in timetabling. A pure instance
of timetabling consists of a set of teachers, a set of classes, and a list L of pairs (t, c) of a
teacher t and a class c, indicating that teacher t has to teach class c during a time-slot (say,
an hour) within the time-span of the schedule (say, a week). A pair (t, c) may occur several
times in the list, indicating the number of hours the pair t, c should meet weekly.
A timetable then is an assignment of the pairs in the list to hours, from a set H of
possible hours, in such a way that no teacher t and no class c occurs in two pairs that
are assigned to the same hour. This clearly is a bipartite edge-colouring problem, and by
K onigs theorem, there is a timetable if and only if [H[ is not smaller than the number of
times that any teacher t or any class c occurs in L. So by the result of Cole and Hopcroft
[1] a timetable can be found in O([L[ log [L[) time, and by our theorem, it can be found
also in O([H[ [L[) time. (In practice, several additional constraints are put on a timetable,
making the problem usually NP-complete cf. Even, Itai, and Shamir [3].)
In many countries, schools are merging, yielding an increase in size, including in numbers
of teachers and of classes. So the list L grows. However, the number of hours during a week
does not grow. This gives that, in this interpretation, the algorithm is linear in the size of
the school.
Moreover, often H is built up from smaller units (say, days), implying that [H[ does not
have large prime factors. ([H[ typically has prime factors 2,3, and 5 only, sometimes 7).
This gives that applying the O(([H[+log [H[)[L[)-time algorithm can be fruitful. Similarly,
the method is not very sensitive to doubling or tripling the time-span (say to 2 or 3 weeks).
3. An O(m) bipartite edge-colouring algorithm
Basic in the edge-colouring algorithm (as in [4]) is a subroutine nding a matching that
covers all maximum-degree vertices, and that hence can serve as our rst colour. To this
end we show:
Theorem 1. A perfect matching in a k-regular bipartite graph can be found in O(km)
time.
Proof. Let G = (V, E) be a k-regular bipartite graph. For any function w : E Z
+
, let
E
w
be the set of edges with w(e) > 0. For any F E, denote w(F) :=
eF
w(e).
Initially set w(e) := 1 for each edge e. Next apply the following iteratively:
2
Find a circuit C in E
w
. Let C = M N for matchings M and N with w(M)
w(N). Reset w := w +
M
N
.
(2)
Note that, at any iteration, the equality w((v)) = k is maintained for all v V (where
(v) is the set of edges incident with v).
To see that the process terminates, rst note that at any iteration the sum
eE
w(e)
2
(3)
increases by
eM
((w(e)+1)
2
w(e)
2
)+
eN
((w(e)1)
2
w(e)
2
) = 2w(M)+[M[2w(N)+[N[, (4)
which is at least [C[ (as w(M) w(N)). Moreover, (3) is bounded, since w(e) k for each
edge e. So the process terminates.
At termination, we have that the set E
w
is a forest, and hence is a perfect matching
(since w(e) = k for each end edge e of E
w
). This implies that at termination the sum (3)
is equal to
1
2
nk
2
= km.
Now by depth-rst we can nd a circuit C in (2) in O([C[) time on average. Indeed,
keep a path P of edges e with 0 < w(e) < k. Let v be the last vertex of P. Choose an edge
e = vu incident with v but not in P. If u does not occur in P, we reset P := P e and
iterate. If u does occur in P, let C be the circuit in P e, and apply (2) to C. Next reset
P := P ` C, and iterate.
If P = , choose any edge e with 0 < w(e) < k, and set P := e. If no such edge e
exists, we are done.
For k smaller than
log n, the O(km) bound is asymptotically better than the O(m+
nlog n(log k)
2
)) bound proved by Cole and Hopcroft [1]. (An algorithm related to, but
dierent from, the algorithm described in Theorem 1, was proposed by Csima and Lov asz
[2], giving an O(n
2
k log k) time bound.)
By applying a technique of Gabow [4], one can derive from Theorem 1 the following
stronger statement:
Corollary 1a. A k-edge-colouring of a k-regular bipartite graph can be found in O(km)
time.
Proof. If k is odd, rst nd a perfect matching M, remove M from G, and apply recursion
(M will serve as colour).
If k is even, nd an Eulerian orientation of G. Let k
=
1
2
k. Then split G into two
k
-regular graphs G
1
= (V, E
1
) (with E
1
the set of edges oriented from vertex-colour class
I to vertex-colour class II) and G
2
= (V, E
2
) (with E
2
:= E ` E
1
). Find recursively k
-
edge-colourings of G
1
and G
2
. The union of the two colourings is a k-edge-colouring of
G.
The time is bounded as follows. Starting with G, we can nd M (if k is odd), nd
the Eulerian orientation, and split G into G
1
and G
2
, in time ckm for some constant c.
Then the whole recursion takes time 2ckm. This can be shown inductively, as 2ckm =
3
ckm+ 2ck
+ 2ck
, where m
= [E(G
1
)[ = [E(G
2
)[ =
1
2
m.
This implies the sharper statement:
Corollary 1b. A (G)-edge-colouring of a bipartite graph G = (V, E) can be found in
O((G)m) time.
Proof. Let k := (G). First iteratively merge any two vertices in the same colour class
of G, if each has degree at most
1
2
k. The nal graph H will have at most two vertices of
degree at most
1
2
k, and moreover, (H) = k and any k-edge-colouring of H yields a k-edge-
colouring of G. Next make a copy H
in H
(where d
H
(v) is the degree of v in H). This gives the k-regular
graph G
, with [E(G
in O(k[E(G
i=1
p
i
i1
j=1
p
j
, (5)
where p
1
p
t
are primes with k = p
1
. . . p
t
.
Theorem 2. A k-edge-colouring of a k-regular bipartite graph G = (V, E) can be found in
O(((k) + log k)m) time.
Proof. Let k = pk
new vertices v
1
, . . . , v
k
, and
distribute the edges incident with v over v
1
, . . . , v
k
in such a way that each vertex v
i
is
incident with exactly p edges. This gives the p-regular graph
G. Find a p-edge-colouring of
-regular. Next nd a k
-edge-colouring of G
p
, yielding perfect matchings
M
1
, . . . , M
k
.
Now we apply the following iteratively. We have a partition of E into perfect matchings
M
1
, . . . , M
k
and k
-regular graphs E
1
, . . . , E
p
. (Initially, = 1.) Let q := min, p.
Choose r such that qk
. Let E
:= M
1
M
r
E
1
E
q
. Then G
:= (V, E
) is a qk
+ r-edge-
colour G
, yielding colours N
1
, . . . , N
qk
+r
. Now replace M
1
, . . . , M
r
by N
1
, . . . , N
qk
+r
and
E
1
, . . . , E
p
by E
q+1
, . . . , E
p
and iterate. We stop if = p.
So at any iteration, is replaced by + q. Moreover, at any iteration except possibly
the last iteration, we have q = . So at any iteration except possibly the last one, q is twice
as large as at the previous iteration.
4
By [4], the work in the iteration takes time O([E
[ log(qk
+ r)) = O([E
+ r k. Since [E
[ =
1
2
(qk
+ r)n qk
n, over all
iterations the work is O((1 + 2 + 2
2
+ + 2
log p
)k
nlog k) = O(pk
) + log k
)m
) time, where m
,
with p the smallest prime factor of k, then (k) = p +(k
)/p p
max
(k) +(2p
max
(k
)/p)
2p
max
(k). This implies:
Corollary 2b. A (G)-edge-colouring of a bipartite graph G can be found in O((p
max
((G))+
log (G))m) time.
Proof. Directly from Corollary 2a with (6).
Note that in performing this method one does not need to apply deep number-theoretic
algorithms to nd the prime-factorization of k. Indeed, the factors p
1
, . . . , p
t
can be found
in O((k)k) time, since the smallest prime factor p can be found in time O(pk), by trying
i = 2, 3, . . . as divisor of k (for each i taking O(k) time), until we reach p. Next we can
apply recursion to k
)k
)/p.
A sharpening can be obtained also for nding perfect matchings in k-regular bipartite
graphs.
Theorem 3. A perfect matching in a k-regular bipartite graph G can be found in time
O((k)m) time.
Proof. Write k = pk
-regular
subgraph G
= (V, E
) of G. In G
is k
and E
= (V, E
) and G
= (V, E
:=
1
2
|. We may assume that [E
[
1
2
m. Let t :=
+ log(
1)|. Then
t-edge-colour G
+s is a
power of 2. Next (
. With the
remaining M
s+1
, . . . , M
t
it gives an edge-colouring of G with
(
+s) + (t s) = 2
+ log(
is at most
1
2
m and since edge-colouring H takes
O(mlog(