Title: 10. Matchings
110. Matchings
- Max. size matchings in bipartite graphs
- Max. weight matchings in bipartite graphs
- Max. size matchings in general graphs
- Max. weight matchings in general graphs
Read Tarjan 113-123, GMG Paper
2Matchings in Graphs
- Let G (V,E) be an undirected graph. A matching
M of G is a set of edges, no two of which are
incident to a common vertex. - The object of the unweighted matching problem is
to find a matching of maximum size. - In a weighted graph, the weight of a matching M
is defined to be the sum of the weights of the
edges in M. Note that we need consider only
positive weights. The object of the weighted
matching problem is to find a matching of maximum
weight. - In a bipartite graph, the vertex set can be
partitioned into two sets X and Y such that all
the edges have one endpoint in X and the other in
Y. Bipartite matching is the special case where
the given graph is bipartite. - The weighted bipartite matching problem is often
called the assignment problem.
3Unweighted Bipartite Matching
- If G is bipartite and there are no edge weights,
then the matching problem can be reduced to a
maximum flow problem as illustrated below.
- Any flow algorithm that maintains integrality of
flows when given integral capacities will yield a
flow in which the saturated edges in the center
constitute a matching. - Because the resulting network is a unit network,
Dinics algorithm can be used, giving a running
time of O(mn1/2).
4Weighted Bipartite Matching
- Weighted bipartite matching can be solved using a
variation of min cost, maximum flow.
- If we assign zero cost to the source/sink edges
and cost(u,v) -w(u,v) for the others, we can
find a maximum weighted matching by finding a
minimum cost flow (but not necessarily maximum
value flow). Applying minimum cost augmentation,
we obtain a sequence of augmenting paths of
non-decreasing cost. If we halt when we get an
augmenting path of positive cost, the current
flow defines a maximum weighted matching. - The running time is O(n(m n log n)).
5Alternating Paths
- Let M be a matching of G (V,E).
- A matching edge is any edge in M all other edges
are free - A vertex is matched if it is incident to a
matching edge and free otherwise - An alternating path or cycle is a simple path or
cycle in which every other edge is matching - an alternating path is an augmenting path if its
end points are free - If G contains an augmenting path P (with respect
to M), then we can obtain a larger matching by
removing from M the matching edges in P and
adding to M the non-matching edges.
6Augmenting Path Method
- Theorem 9.1. Let M be a matching of G, M ? be a
maximum matching and k M ? - M . Then G has
k vertex disjoint augmenting paths with respect
to M. - Proof. Let N be the set of edges in M or in M ?
but not in both. Every vertex is incident to at
most two edges of N, so the subgraph induced by N
consists of paths and even length cycles that are
alternating with respect to M. N contains exactly
k more edges from M ? than from M. So at least k
paths in N must begin and end with edges from M
?. These paths are vertex disjoint and augmenting
(with respect to M). ? - The following algorithm is called the augmenting
path method for finding a matching. Given a graph
G, let M and repeat the following step
until there are no more augmenting paths. - Augmenting Step. Let P be an augmenting path with
respect to M. Remove from M the matching edges in
P and add to M the non-matching edges in P. - Complexity n times the time required to find an
augmenting path.
7Augmenting Paths in Bipartite Graphs
- The following algorithm finds augmenting paths in
bipartite graphs. - Each vertex is assigned one of three states,
unreached, odd, or even. For each matched vertex
v, mate(v) is the vertex connected to v by a
matching edge. The algorithm builds a forest,
defined by parent pointers p(v) for each vertex
v. Initially every matched vertex is unreached,
every free vertex is even and every free vertex v
has p(v) null. The algorithm repeats the
following step until a path is found or every
edge has been examined. - Choose an unexamined edge v,w with v even and
examine it. - If w is even, stop the path from the root of the
tree containing v to the root of the tree
containing w forms an augmenting path. - If w is unreached and matched, make w odd,
mate(w) even, p(w) v and p(mate(w)) w. - This can be implemented to run in O(m) time. This
gives an overall running time of O(mn) for
finding a maximum matching.
8- function path augpath(graph G, matching M)
- vertex u,v,w,x,y
- mapping statevertex?unreached,even,odd
- mapping matchedvertex?true,false
- mapping p, mate vertex?vertex
- for u ? V ? state(u) even matched(u)
false rof - for u,v ? M ?
- state(u), state(v) unreached
- matched(u), matched(v) true mate(u) v
mate(v) u - rof
- queue
- for u ? 1,n ?
- for u,v ? edges(u) ?
- if vgtu and (state(u) even or state(v)
even) ? - queue queue u,v
- fi
- rof
- rof
9- do queue ? ?
- v,w queue(1) queue queue2..
- if not even(v) ? v ? w fi
- if state(w) unreached and matched(w) ?
- x mate(w)
- state(w) odd p(w) v state(x) even
p(x) w - for x,y ? edges(x) ?
- if x,y ? queue ? queue queue x,y
fi - rof
- state(w) even ?
- path x v
- do p(x) ? null ? path p(x),x path x
p(x) od - path path v,w x w
- do p(x) ? null ? path path x,p(x) x
p(x) od - return path
- fi
- od
- end
10Blossoms
- The algorithm outlined above will fail if an edge
is selected which joins two even vertices in the
same tree. This can't happen in bipartite graphs,
but can in general graphs. - If v,w is an edge joining two even vertices in
the same tree, the cycle induced by the tree path
connecting v and w together with v,w is called
a blossom.
- This problem is handled by shrinking the blossom,
that is, condensing all the vertices of the
blossom into a single vertex. - Note that a blossom consists of an odd-length
cycle and that the one vertex in the cycle that
is incident to two non-matching edges in the
cycle is an ancestor of all other vertices in the
cycle (in the forest defined by the parent
pointers). - this vertex is called the base of the blossom
11- Theorem 9.3. If G? is formed from G by shrinking
a blossom b, then G? contains an augmenting path
if and only if G does. - For the moment, we prove only that if G?
contains an augmenting path P, then G also
contains an augmenting path. If b is not on P,
then P is an augmenting path in G. If b is on P,
then we can construct an augmenting path in G by
expanding b and reconnecting the now separated
parts of P by going around the blossom in the
appropriate direction.
- The proof of the other direction follows from
Theorem 9.4, below.
12Edmonds-Karp Algorithm
- Edmonds algorithm uses blossom shrinking. It
builds a forest much like the earlier algorithm,
using the following step. - Choose an unexamined edge v,w with v even and
examine it. - If w is unreached and matched, make w odd,
mate(w) even, p(w) v and p(mate(w)) w. - If w is even and v and w are in different trees,
stop the path from the root of the tree
containing v to the root of the tree containing w
forms an augmenting path. - If w is even and v and w are in the same tree,
the edge v,w together with the tree path
joining v and w form a blossom. Let u be the
nearest common ancestor of v and w (in the tree)
and condense all the vertices that are
descendants of u and ancestors of either v or w
into a shrunken blossom b, and let p(b) p(u).
Make b even. - A vertex is called shrunken at some point in the
algorithm if it has been included in a blossom.
Both original vertices and blossoms may become
shrunken.
13Validity of Blossom Shrinking
- Theorem 9.4. The blossom shrinking algorithm
finds an augmenting path ? there is an augmenting
path in the original graph. - Proof. (?) If the algorithm succeeds then there
is an augmenting path in the original graph,
which can be found by expanding the blossoms in
reverse order as discussed earlier. - (?) Note the following points.
- (1) If v is reached and matched then mate(v) is
also reached and in the same tree. - (2) If v is a free vertex contained in a blossom
b, then b is free. - (3) If the algorithm fails then any two adjacent
vertices that are either shrunken or even are
contained in a common blossom when the algorithm
halts. - Suppose the algorithm fails, but there is an
augmenting path p x0, . . . ,x2r 1 and
consider the situation just after the algorithm
halts. - Note that x0 and x2r1 are in different trees,
and let k be any integer such that xk1 is in a
different tree from x0 and xk1 is in a different
tree from xk. - By point (3) above, either xk or xk1 is odd and
unshrunken. Assume (w.l.o.g.) that xk is odd and
unshrunken. Mate(xk) xk-1, so k must be even.
Let j be the smallest integer such that j is even
and xj is odd and unshrunken.
14last odd unshrunken vertex on path, has odd
index
matching edge implies k even
odd unshrunken
unmatched edge since endpoints in different trees
first odd unshrunken vertex with even index
- There must be an odd, unshrunken vertex among
x0, . . ,xj-1 since otherwise (by point 3) x0, .
. ,xj-1 would all be contained in a common
blossom. Since this blossom contains a free
vertex x0, it must (by point 2) be a free
blossom. This implies that the matching edge to
xj must also be part of the blossom,
contradicting the assumption that xj is
unshrunken. - Let i be the largest integer such that i lt j and
xi is odd and unshrunken. Then i is odd which
means mate(xi) xi1. - This in turn implies that xi1, . . . ,xj-1 are
contained in a single blossom that is incident to
two matched edges, which is impossible. ?
15Implementing Edmonds-Karp Algorithm
- Replace each edge u,v with directed edges
u,v, v,u. - Current shrunken graph is represented implicitly
using partition data structure. Each set
corresponds to a vertex in current shrunken graph
and contains the original vertices contained in
current vertex. Define
- Origin(v) is initialized to v for all vertices.
Whenever a blossom forms, the canonical vertex v
of the set representing the blossom has origin(v)
set to the origin of the blossom. Hence,
origin(find(v)) is always equal to the origin of
the largest blossom containing v and represents
the vertex containing v in the current shrunken
graph. - If v?origin(find(v)) for all vertices v then the
edge x?,y? is the edge in the current graph
corresponding to the original edge x,y.
Whenever a blossom is shrunk, the algorithm
computes supplementary information needed to
construct an augmenting path.
16- For each odd vertex in the blossom, we define a
bridge. If examination of original edge v,w
causes x to be condensed into a blossom,
bridge(x)v,w if x is an ancestor of v? before
condensation and bridge(x) w,v if x is an
ancestor of w? before condensation. - The algorithm is implemented as follows. For each
v let origin(v) v, and make v even if v is
free and unreached otherwise. Then repeat the
following step. - Choose an unexamined original edge v,w such
that v? is even and examine it. - If w? is unreached and matched, make w' odd,
mate(w?) even, p(w?)v and p(mate(w?))w?. - If w? is even and v? , w? are in different trees,
stop and recover augmenting path. - If w? is even and v? and w? are in the same tree,
a blossom has been formed. Let u be the nearest
common ancestor of v? w?. For every x that is a
descendant of u and ancestor of v?, perform
link(find(u),find(x)) and if x is odd let
bridge(x)v,w. For every x that is a descendant
of u and an ancester of w?, perform
link(find(u),find(x)) and if x is odd let
bridge(x)w,v. Let origin(find(u))u.
17- The set of edges eligible for selection is
maintained in a list. - Suppose v,w is examined by the algorithm and v'
and w' are found to be in different trees with
roots x and y. The augmenting path from x to y is
reverse(path(v,x)) path(w,y) where reverse
reverses a list and path is defined by
- Using a reversible list data structure, this can
be implemented to run in O(n) time.
18Implementing Edmonds-Karp
- In the following, G is assumed to have been
converted to digraph form - (with two directed edges in place of each or the
original undirected - edges) before augpath is called.
- function path augpath(graph G, matching M)
- vertex u,v,w,x,y
- mapping statevertex?unreached,even,odd
- mapping matchedvertex?true,false
- mapping mate, origin, pvertex?vertex
- mapping bridgevertex?edge
- partition(V)
- list Q
- for u ? V ? state(u) even matched(u)
false rof - for u,v ? M ?
- state(u),state(v) unreached
- matched(u), matched(v) true
- mate(u) v mate(v) u
- rof
19- Q
- for u ? 1,n ?
- for u,v ? out(u) ? if state(u) even ? Q
Q u,v fi rof - rof
- do Q ? ?
- v,w Q(1) Q Q2..
- v? origin(find(v)) w? origin(find(w))
- if v? ? w? and state(w?) unreached and
matched(w?) ? - x mate(w?)
- state(w?) odd p(w?) v?
- state(x) even p(x) w?
- for x,y ? out(x) x,y ? Q ? Q Q
x,y rof - v? ? w? and state(w?) even and not
sametree(v?,w?) ? - x v? do p(x) ? null ? x
origin(find(p(x))) od - y w? do p(y) ? null ? y
origin(find(p(y))) od - return reverse(path(v,x)) path(w,y)
20- v? ? w? and state(w?) even and
sametree(v?,w?) ? - u nca(v?,w?) x v?
- do x ? u ?
- link(find(u),find(x))
- if state(x) odd ?
- bridge(x) v,w
- for x,y ? out(x) x,y ? Q ? Q Q
x,y rof - fi
- x origin(find(p(x)))
- od
- x w?
- do x ? u ?
- link(find(u),find(x))
- if state(x) odd ?
- bridge(x) w,v
- for x,y ? out(x) x,y ? Q ? Q Q
x,y rof - fi
- x origin(find(p(x)))
- od
21Analysis of Edmond-Karp Algorithm
- The main loop is executed at most 2m times since
each (directed) edge is put on the queue at most
once. Similarly, the for-loops that put edges on
the queue contribute a total of O(m) to the
running time. - The function call sametree(v',w') returns true if
v? and w? are in the same tree, otherwise it
returns false. The function call nca(v?,w?)
returns the nearest common ancestor of v' and w'
in the current shrunken graph. - Sametree and nca can be implemented by moving up
the tree paths (in the current shrunken graph)
from v? and w? in parallel. We stop either when
we reach different free vertices or when we reach
an ancestor of v? that was visited previously on
the search from w? or vice versa. - This process can be implemented to run in time
proportional to the number of vertices visited.
22- If v? and w? are in different trees we spend at
most O(n) time (also, note that this case is used
only once). If v? and w? are in the same tree
then the number of vertices visited is at most
twice the number on the cycle defining the
blossom that is about to be condensed. - There are at most n/2 blossoms, since the
formation of a blossom reduces the number of
vertices by at least two. Hence, the number of
vertices on all blossom cycles is at most 3n/2
and the total time spent executing sametree and
nca is O(n). - By the same argument, the total time spent
shrinking blossoms is O(n), excluding the
partition operations. - Hence, the number of partition operations is O(m)
and the total running time for finding an
augmenting path is O(ma(m,n)). The time for
finding a maximum matching is thus O(mna(m,n)).
23Maximum Weight Augmentation
- Given a graph G(V,E) and a matching M, define
the weight of a path p to be the sum of the
weights of its free edges minus the sum of the
weights of its matched edges. - Theorem 9.2. Let M be a matching of maximum
weight among matchings of size M, let p be an
augmenting path for M of maximum weight, and let
M ? be the matching formed by augmenting M using
p. Then M ? is of maximum weight among matchings
of size M1. - Proof. Let M ? be a matching of maximum weight
among matchings of size M1. Let N be the set
of edges in M or M ? but not both. - Define the weight of a path or cycle in N with
respect to M. - Any cycle or even length path in N must have
weight zero, since otherwise we could increase
the weight of either M or M ? without changing
their size, by exchanging the edges on the cycle
or path.
Read Galil, Micali and Gabow
24- Since N contains exactly one more edge in M ?
than in M, we can pair all but one of the
odd-length paths so that each pair has an equal
number of edges in M and in M ?. Each such pair
of paths must have zero total weight by the same
reasoning as before. - Augmenting M using the remaining path gives a
matching of size M1 with the same weight as M
?. This must be a maximum weight augmenting path
for M since if there were an augmenting path with
larger weight, we could construct a matching of
size M1 with larger weight than M ?. ? - Theorem 9.2 provides a basis for a bipartite
weighted matching algorithm, which we will extend
later for general weighted matching.
25Max Wt. Matchings Vertex Labeling
- Theorem. Let G (V,E) be a graph with edge
weights w(u,v), let M be a matching in G and let
each vertex u have a non-negative label l(u). If - w(u,v) ? l(u) l(v) for u,v ? E (1)
- w(u,v) l(u) l(v) for u,v ? M (2)
- l(u) 0 if u is free (3)
- then M is a maximum weight matching.
- Proof. Let l be defined with respect to some
matching M and let N be any other matching. - Su,v?N w(u,v) ? Su l(u) Su,v?M w(u,v)
- Condition (1) implies the inequality, and (2)
and (3) imply the equality. ? - This theorem provides a basis for an alternative
max weight bipartite matching algorithm, which
can be extended to general graphs.
26Bipartitie Matching Using Vertex Labeling
- Start with M and let l(u)(1/2) maxx,y?E
w(x,y) note that this assignment satisfies
conditions (1) and (2). - At each step, we search for augmenting paths
using only edges u,v with l(u) l(v) w(u,v)
(called equality edges). - If we can't find an appropriate augmenting path,
we interrupt the search to modify the labeling.
The change maintains the truth of (1) and (2) and
creates at least one new equality edge or makes
(3) true. If (3) becomes true, we are done, if
not we resume the search. - M
- for u ? V ? l(u) maxx,y?E w(x,y)/2 make u
even p(u) null rof - Q u,v l(u) l(v) w(u,v)
- do there is a free vertex w with l(w) gt 0 ?
- let u,v ? Q with u even Q Q - u,v
- if v is unreached and matched ?
- make v odd and mate(v) even p(v) u
p(mate(v)) v - add to Q all equality edges incident to mate(v)
that are not already in Q
27- v is even ?
- augmenting path found augment M
- make all matched vertices unreached
- remove from Q all edges incident only to
unreached vertices. - fi
- if Q and there is a free vertex u with l(u)
gt 0 ? - d1 minl(u) u is even or zero if no even
vertices - d2 minl(u) l(v) - w(u,v) u,v, u
even, v unreached - d3 min(l(u) l(v) - w(u,v))/2 u,v,
u,v even - d mind1, d2, d3
- for u ? V ?
- if u is even ? l(u) l(u) - d u is odd ?
l(u) l(u) d fi - rof
- if d d2 or d d3 ? add new equality edges
to Q fi - fi
- od
28Preliminary Analysis
- Note that if d d1 following a change of labels
then all free vertices u have l(u)0 and the
algorithm terminates. - Notice that each time we begin the outer loop, Q
is non-empty and that (1) and (2) are true
initially. - Also, notice that Q contains only equality edges,
so the unmatched edges in the augmenting path are
equality edges. - Hence (2) is maintained by each augmentation.
- Notice also that each change of labels maintains
(1) and (2). - If d d2 or d d3 following a change of labels
then at least one new equality edge is formed and
between successive augmentations, any given edge
can become an equality edge at most once. - Hence the labels change ? m times between
successive augmentations.
29- Since each augmentation eliminates two violations
of (3), there are? ?n/2? augmentations and since
each edge is examined at most once between
successive augmentations, the number of
iterations of the outer do-loop is ? m(1 ?n/2?
), as is the number of label changes. - Hence, the algorithm terminates with a max
weighted matching M. - To implement the algorithm efficiently, we need
several data structures. First, we need heaps
h1.e and h1.o which store the even and odd
vertices respectively, with l(u) used as the key
for vertex u. - Changes to the labels are handled by performing
an addtokeys on the appropriate heap. Also, h1.e
is used to compute d1. - To compute d2, we maintain a heap h2 containing
non-equality edges u,v having one even and one
unreached endpoint, with key l(u) l(v)-w(u,v). - To compute d3, we maintain a heap h3 containing
non-equality edges u,v having two even
endpoints, with key l(u) l(v)-w(u,v).
30Implementing Bipartite Matching with Labeling
- With these data structures, the algorithm can be
written as follows. - M
- for u ? V ?
- l(u) maxx,y ? E w(x,y)/2 make u even p(u)
null insert(u, l(u),h1.e) - rof
- for u,v ? E ? add u,v to Q or h3 as
appropriate rof - d1 maxx,y?E w(x,y)/2
- do d1 gt 0 ?
- let u,v ? Q with u even Q Q - u,v
- if v is unreached and matched ?
- x mate(v) make v odd and x even p(v) u
p(mate(v)) v - insert(v, l(v), h1.o) insert(x, l(x), h1.e)
- for x,y ? E ? add x,y to Q, h2 or h3 as
appropriate rof - v is even ?
- augmenting path found augment M
- remove matched vertices from h1.o and h1.e and
update l - make matched vertices unreached
- remove from Q all edges incident only to
unreached vertices. - fi
31- if Q and d1 gt 0 ?
- if h1.e is empty ? d1 0
- h1.e is not empty ? d1
key(findmin(h1.e), h1.e) - fi
- d2 key(findmin(h2)) d3 key(findmin(h3))/2
- d mind1, d2, d3
- addtokeys(-d, h1.e) addtokeys(d, h1.o)
- addtokeys(-d, h3) addtokeys(-2d, h3)
- d1 d1 - d
- transfer equality edges from h2 and h3 to Q
- fi
- od
- Ignore the heap operations for the moment. The
number of augmentations is at most ?n/2? and
the number of label changes between successive
augmentations is at most m.
32Analysis
- Since an edge can be added to Q at most once
between augmentations, edges can be transferred
from h2 or h3 to Q at most m(1 ?n/2? ) times. - Note also that the time spent doing the
augmentations including updating the data
structures is O(mn). - The inner for-loop is executed at most 2m between
augmentations, so the number of iterations is at
most 2m(1 ?n/2? ). - So, neglecting the heap operations, the running
time is O(mn). Using d-heaps with d2 and keys
represented by key differences, the required heap
operations require O(log n) time each, giving
O(mn log n) time to find a maximum weighted
matching.
33Weighted Matchings in General Graphs
- If previous algorithm is extended to general
graphs, we run into a problem, since a matching
which is maximum with respect to a shrunken graph
may not be when extended to the entire graph. - However, there is another way to extend the
vertex labeling technique. For each odd subset
b?V, define a non-negative label z(b). Also, let
Z(u,v) Sbu,v?b z(b). - Theorem. Let G (V,E) be a graph with edge
weights w(u,v), let M be a matching in G, let
each vertex u have a non-negative label l(u), and
let each b ? V with an odd number of vertices
have an associated non-negative label z(b). If - (1) w(u,v) ? l(u) l(v) Z(u,v) for all u,v ?
E - (2) w(u,v) l(u) l(v) Z(u,v) for all u,v
?M - (3) l(u) 0 if u is free
- (4) z(b) 0 if the number of matching edges
with both endpoints in b is less than
(b-1)/2 - then M is a maximum weight matching.
34- Proof. Assume conditions (1) to (4) hold with
respect to some matching M, let N be any other
matching.
Hence, M is a maximum weight matching. ?
35General Matching Using Labeling
- The theorem provides the basis for an alternative
algorithm for maximum weighted matching in
graphs. In the algorithm we maintain variables
z(b) only for blossoms b all others are
implicitly zero. - We start with M , l(u) (1/2)
maxx,yw(x,y) and no blossoms, hence no
non-zero z(b)s note that this assignment
satisfies conditions (1), (2) and (4). - At each step, we search for augmenting paths
using only edges u,v with l(u) l(v) Z(u,v)
w(u,v) (called equality edges). - If we can't find an appropriate augmenting path,
we interrupt the search to modify the labeling.
The change maintains the truth of (1), (2) and
(4) and creates at least one new equality edge,
makes z(b) 0 for some odd blossom b or makes
(3) true. If (3) becomes true, we are done if no
new equality edge is created we modify the
labeling again and otherwise we resume the
search.
36- In the algorithm, we view each vertex as
belonging to some (possibly trivial) blossom in
the current shrunken graph. - We maintain variables z(b) for all non-trivial
blossoms b, including those contained within
other blossoms. However, when we change the zs,
we change only those belonging to top level
blossoms. - A blossom in the current graph may be odd, even
or unreached. A vertex is odd, even or unreached
if top level blossom containing it is. - For each blossom b, we maintain an edge entry(b)
which is the edge by which b was first reached in
the current augmenting path search. These edges
define a collection of trees in the current
graph. - For each vertex u, we let bu denote the blossom
containing u in the current graph.
37General Matching Algorithm
- M
- for u? V ? l(u) maxx,y?E w(x,y)/2 make u
even entry(u) null rof - Q u,v l(u) l(v) w(u,v)
- do there is a free vertex w with l(w) gt 0 ?
- let u,v?Q with bu even Q Q-u,v
- if bv is unreached and matched ?
- let v',x be the matching edge incident to bv,
with v'?bv - make bv odd and bx even entry(bv) u,v
entry(bx) v',x - add to Q all equality edges incident to bx that
are not already in Q - bv is even and bu and bv are in different
trees ? - augmenting path found augment M without
expanding blossoms - make all matched blossoms unreached
- remove from Q all edges incident only to
unreached vertices. - bv ? bu, is even and bu and bv are in the
same tree ? - form a new blossom b, let z(b) 0 and make b
even - let entry(b) entry(b') where b' is the base
of b - add to Q all equality edges incident to b that
are not already in Q - remove from Q edges joining vertices in b
- fi
38- do Q and there is a free vertex u with
l(u) gt 0 ? - d1 minl(u) u is even or zero if no even
vertices - d2 minl(u) l(v) Z(u,v) - w(u,v)
u,v, u even, v unreached - d3 min(l(u) l(v) Z(u,v) - w(u,v))/2
u,v, u,v even - d4 minz(b)/2 b is a non-trivial odd
blossom - d mind1,d2,d3,d4
- for u ? V ?
- if u is even ? l(u) l(u) - d u is odd ?
l(u) l(u) d fi - rof
- for top level blossoms b ?
- if b is even ? z(b) z(b)2d b is odd ?
z(b) z(b) - 2d fi - rof
- if d d2 or d d3 ? add new equality edges to
Q fi - if d d4 ? expand odd blossoms b with z(b)
0 fi - od
- od
39Preliminary Analysis
- Notice that each time we begin the outer loop, Q
is non-empty and that (1), (2) and (4) are true
initially. - Also, notice that Q contains only equality edges,
so the unmatched edges in the augmenting path are
equality edges. - Hence (2) is maintained by each augmentation.
- Notice also that each change of labels maintains
(1), (2) and (4). - Furthermore, if d d1 following a change of
labels then all free vertices u have l(u) 0 and
the algorithm terminates. - If d d2 or d d3 following a change of labels
then at least one new equality edge is formed and
between successive augmentations, any given edge
can become an equality edge at most once. - If d d4 following a change of labels then at
least one odd blossom is expanded. Since all odd
blossoms were formed before the most recent
augmentation, the number of times such blossoms
can be expanded between successive augmentations
is at most n/2.
40- Hence the labels change at most m n/2 times
between successive augmentations. - Since each augmentation eliminates two violations
of (3), there are at most ?n/2? augmentations. - Notice that between successive augmentations, any
given vertex can go from being odd or unreached
to even at most once. Hence the first guard of
the first if-statement is executed at most n
times between successive augmentations. - The second guard of the if is executed once
between successive augmentations. - Since a new blossom is formed each time the third
guard is executed, the third guard can be
executed at most n/2 times between successive
augmentations.
41- The case where bvbu is not covered by any of the
guards. The algorithm takes no action in this
case. This case can happen at most once per edge
between successive augmentations. - Hence the outer loop is executed at most m
(3/2)n 1 times between successive
augmentations. - Hence, the algorithm terminates with a maximum
weighted matching M. To implement the algorithm
efficiently, we need several data structures. - First, we maintain a partition on the vertex set,
where two vertices are the same set of the
partition if and only if they are in the same
blossom of the current graph. Note that the data
structure must support efficient expansion of
blossoms. - We use heaps h1.e and h1.o as before to store the
even and odd vertices respectively, with l(u)
used as the key for vertex u.
42- Changes to the keys are handled by performing an
addtokeys on the appropriate heap. Also, h1.e is
used to compute d1. - To compute d3, we maintain a heap h3 containing
non-equality edges u,v having two even
endpoints, with key l(u) l(v) Z(u,v) -w(u,v). - To compute d4 and maintain the blossom labels z,
we have heaps h1.o and h4.e containing odd and
even blossoms b in the current graph with key
z(b). - To compute d2 it doesn't suffice to have a single
heap of the inequality edges with a single even
endpoint. Instead, for each odd or unreached
blossom b in the current graph, we maintain a
heap h2(b) containing inequality edges incident
to b whose other endpoints are even. - We also maintain a heap h2, which for each
unreached blossom b, contains the minimum key
element of h2(b).
43Detailed Implementation
- With these data structures, we can write a more
detailed version of the algorithm. - for u?V ?
- l(u) maxx,y?E w(x,y)/2
- make u even p(u) null
- insert(u, l(u),h1.e)
- rof
- for u,v?E ?
- add u,v to Q or h3 as appropriate, with key
l(u) l(v) - w(u,v) - rof
- d1 maxx,y?E w(x,y)/2
- do d1 gt 0 ?
- let u,v?Q with u even Q Q - u,v
- if bv is unreached and matched ?
- let v',x be the matching edge incident to bv
with v' ?Bv - make bv odd and bx even
- entry(bv) u,v entry(bx) v',x
-
44- delete(findmin(h2(bv)),h2) delete(findmin(h2(bx
)),h2) - remove heap h2(bx)
- add vertices in bv to h1.o
- add vertices in bx to h1.e
- for all edges x',y incident to bx with x?bx ?
- add x',y to Q, h2(by) or h3 as appropriate
- rof
- bv is even and bu and bv are in different
trees ? - augmenting path found augment M
- make matched blossoms unreached
- remove unreached vertices from h1.o and h1.e
and update l - remove edges with no even endpoint from h2,
h2(b) - remove edges that lack two even endpoints from
h3 - remove unreached blossoms from h4.e and h4.o
- remove from Q all edges incident only to
unreached vertices
45- bv is even and bu and bv are in the same
tree ? - form a new blossom b, let z(b) 0 and make b
even - let entry(b) entry(b') where b' is the base
of b - remove from h4.e and h4.o all sub-blossoms of
b - add b to h4.e
- for all x in originally odd sub-blossoms ?
- for x,y ?
- if y?b ? add x,y to Q, h2(by) or h3 fi
- rof
- rof
- fi
- do Q and d1 gt 0 ?
- if h1.e is empty ? d1 0
- h1.e is not empty ? d1
key(findmin(h1.e), h1.e) - fi
- d2 w(findmin(h2)) d3 w(findmin(h3))/2
- d4 z(findmin(h4))/2
-
46- d mind1, d2, d3, d4 d1 d1-d
- addtokeys(-d, h1.e) addtokeys(d, h1.o)
- addtokeys(-d,h2) addtokeys(-2d,h3)
- addtokeys(2d, h4.e) addtokeys(-2d, h4.o)
- transfer new equality edges from h2() and h3
to Q - remove odd blossoms with z(b) 0 from h4 and
expand them, - updating other data structures appropriately
- od
- od
- In addition to the heaps specified above, we
maintain a list of sub-blossoms for each blossom
(and sub-blossom) in the graph. - We also define an ordering of the vertices within
each blossom in particular if u and v are both
in some blossom b and b' is the smallest blossom
containing both u and v, then we say u precedes v
if the sub-blossom of b' containing u appears
before the sub-blossom containing v in the list
of sub-blossoms for b'.
47Analysis
- Using this ordering, it's easy to devise a data
structure to maintain the partition on the
vertices. In particular, a collection of
self-adjusting binary search trees can be used,
where each search tree corresponds to a blossom
and the search tree order is just the order of
vertices within a blossom. - Notice that if we combine search trees in the
sub-blossom list order when a blossom is formed,
we can easily split the search tree again if we
should have to expand the blossom later. A
similar data structure is needed for h1.e and
h1.o. We add an additional set h1.u containing
the unreached vertices. Whenever a blossom goes
from being unreached to being even or odd, its
vertices are split from h1.u and added to h1.e or
h1.o. Similarly, when a blossom goes from odd to
unreached, its vertices can be split from h1.o
and transferred to h1.u.
48- Since h1.e and h1.o must also support the findmin
and addtokeys operations, they represent keys
using the Dkey, Dmin scheme used in the path set
data structure. - The h2() heaps are done in a similar way. We
again represent the edge costs differentially and
use binary search trees to allow efficient
splitting and linking. This requires that the
ordering scheme given earlier be extended to
edges. - h3, h4.e and h4.o can also be implemented using
binary search trees with differential keys.
Implicit deletion is used with h3. - With this implementation, the number of
operations required on the various data
structures is O(mn) and since the time per
operation is O(log n), the overall running time
is O(mn log n).