0% found this document useful (0 votes)
14 views27 pages

5 Weighted Matching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views27 pages

5 Weighted Matching

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Weighted Bipartite

Matching
CS31005: Algorithms-II
Autumn 2020
IIT Kharagpur
Matching
 A matching in an undirected graph G = (V, E) is a
subset of edges M ⊆ E, such that for all vertices v ∈ V,
at most one edge of M is incident on v
 Size of the matching M = |M|
 Weight of the matching M (for weighted graphs) =
sum of the weights of the edges in M
 A maximum cardinality matching is a matching with
maximum number of edges among all possible
matchings
 A maximum weighted matching is a matching with
highest weight among all other matchings in the graph
 Our problem: Given a weighted bipartite graph G = (V,
E) with partitions X and Y, and positive weights on each
edge, find a maximum weighted matching in G
 Models assignment problems with cost in practice
 Simple flow based techniques that we used for
unweighted bipartite graphs no longer work for
weighted graphs…
9
4

5 5

12
1
3
5

A matching with weight 14 A maximum weighted


(maximum cardinality matching with weight 21
matching but not maximum (maximum weighted
weighted) matching but not maximum
cardinality)
Perfect Matching
 Given a matching M
 The vertices belonging to the edges of a matching are
saturated by the matching; the others are unsaturated (also
called free vertices)
 If a matching saturates every vertex of G, then it is a
perfect matching
 For a perfect matching to exist, number of vertices must be
even
 For bipartite graphs, the number of vertices in each partition
must be the same
 For any graph with n vertices, size of a perfect matching is
n/2
Augmenting Paths
 Given a matching M, a path between two distinct
vertices u and v is called an alternating path if the edges
in the path alternate between in M and not in M
 An alternating path P that begins and ends at unsaturated
vertices is an augmenting path
 Replacing M ∩ E(P) by (E(P) − M) produces a new
matching M′ with one more edge than M (i.e., augments
M)
x1 9 x1
y1 y1
5
x2 x2
5 5 y2 y2
x3 8 x3
1 y3 y3
x4 3 x4
5 y4 y4
x5 x5

P = <y2, x3, y3, x4> is an


{x1, x4, y4} are unsaturated
augmenting path
(x2, x3, x5, y1, y2, y3} are
M ∩ E(P) = {(x3,y3)}
saturated
E(P) – M = {(x3, y2), (x4, y3)}
P = <x1,y1,x2,y3,x5> is an
M’ = {(x1,y1), (x3,y2), (x4, y3)}
alternating path but not augmenting
is a higher cardinality matching
Key Result

[Berge’s Theorem] A matching M in a graph G is a maximum


matching in G if and only if G has no augmenting path

 This gives another way of finding maximum cardinality


matchings in bipartite graphs without depending on flows
 But does not help directly in finding a maximum weighted
matching (can you show a counterexample?)
 Instead, the algorithm we learn will use it in a related
graph
Hungarian Algorithm
 Also called Kuhn-Munkres algorithm
 Finds a maximum weighted perfect matching in a complete
bipartite graph
 |X| = |Y|
 An edge (x, y) exists between each pair x ∈ X and y ∈ Y
 So what if your input graph is not complete?
 Just add dummy vertices ( if needed) to make the no. of
vertices on both sides equal, and add edges that do not exist
with weight 0
 Find the maximum weighted matching in this new graph, then
throw away any dummy edge included in the matching
 Remaining edges will be the maximum weighted matching in
your original input graph
Equality Subgraph
 Assign a label l(u) to every vertex u
 Feasible labelling
l(x) + l(y) ≥ w(x,y) for any edge (x,y)
 Given a feasible labelling l, Equality Subgraph Gl = (V, El)
where
 El = {(x,y) | x ∈ X,Y ∈ Y, l(x) + l(y) = w(x,y)}
 Why is it important?

[Kuhn-Munkres Theorem]: Let l be a feasible labeling of G. If M is


a perfect matching in G l, then M is a maximum weighted matching
in G.
Hungarian Algorithm: Basic Idea
 Start with any feasible labeling l and M = ∅
 While M is not a perfect matching repeat
1. Find an augmenting path for M in El and augment M
2. If no augmenting path exists,
Improve l to l′ such that at least one new
edge is added to the equality subgraph
Go to Step 1
Initial Feasible Labeling
 Start with this feasible labelling
 l(x) = max{w(x,y)| y ∈ Y} for all x ∈ X
 l(y) = 0
 Guarantees that in the equality subgraph Gl
 El has at least one edge from every vertex x ∈ X
Some Definitions
 Let l be a feasible labeling
 Neighbor of u ∈ V
 Nl(u) = {v : (u, v) ∈ El }
 For any set S ⊆ V, neighborhood of S
 Nl(S) = ∪u∈S Nl(u)
 We will maintain two sets, S and T
 At any time, S and T will keep information about the
alternating/augmenting paths
 S will have a subset of vertices in X
 T will have a subset of vertices in Nl(S)
 S and T together will keep track of a tree of alternating
paths rooted at some free vertex in X (which will be in S)
How to find the matching
 Find a free vertex x ∈ X
 Must exist unless you have reached the perfect matching
 Create a tree rooted at X such that all paths in the tree
from x are alternating
 Vertices at even levels (0, 2, …) = vertices in S
 These will be in X
 Vertices at odd levels (1, 3, …)= vertices in T
 These will be in Y
 If we encounter a free vertex at odd level, we have found
an augmenting path
 Augment and continue
How to improve the labeling
 Let S ⊆ X and T = Nl(S) ≠ Y
 Let
αl = min{l(x)+ l(y) − w(x, y) | x ∈ S, y not in T}
 Note that αl > 0
 Then set
l′(v) = l(v) − αl if v ∈ S
= l(v) + αl if v ∈ T
= l(v) otherwise
 The updated labeling l′ is a feasible labeling with the
following properties:
 If (x, y) ∈ El for x ∈ S, y ∈ T then (x, y) ∈ El′
 Decrease in l(x) is same as increase in l(y)
 If (x, y) ∈ El for x not in S, y not in T then (x, y) ∈ El′
 Labels remain the same for them
 There is some edge (x, y) ∈ El′ for x ∈ S, y not in T
 At least for one edge ((the one with the minimum in αl
computation), l(x) is decreased by the excess, l(y) is
unchanged, so brings in the edge into the new equality graph

 This shows that the new labelling increases the


neighborhood of S
The Algorithm
Example
 We do not show the dummy 0-weight edges added, though they
are there, and you include them in all calculations of the steps of
the algorithm
Alternating Tree Generated
 Tree generated while discovering the
augmenting path in the last equality graph
 From free vertex x2, first go to y2, then to
x3, cannot grow this anymore
 Come back to x2, explore y1, then x1, then
y3, then x4, cannot grow this anymore
 Come back to x1, go to y4, found an
augmenting path, so stop
 Note that the tree depends on order of visit
 May have gone to y1 first from x2, then the
augmenting path would have been found
before y2 is explored (so y2 and x3 would
not have been in the tree)
 Similar possibility at x1 if we had visited y4
first
 The final maximum weighted matching found by the
Hungarian algorithm for the complete bipartite graph is
{(x1, y4), (x2, y1), (x3, y2), (x4, y3)} with weight 14 (= 0
+ 4 + 6 + 4)
 But (x1, y4) is a dummy edge (not in the original graph)
 So drop it
 Final maximum weighted matching M for the input graph
is {(x2, y1), (x3, y2), (x4, y3)} with weight 14
 x1 remains a free vertex as it cannot be matched
 Dropping dummy edge does not affect weight as its weight
is 0
Time Complexity
 Let |X| = |Y| = n
 The outer while loop in Step 2 is executed once when the size of the
matching increases by 1
 So max. no of iterations = size of perfect matching = n
 What is the time for one iteration of the outer while loop?
 Step 2(a) and 2(b) take O(1) time
 The while loop in step 2(c) can run O(n) times
 It can run when Nl(S) ≠ T until Nl(S) = T
 After coming out of the loop when Nl(S) = T, it can then run again from step
2(e) after the relabeling is done which makes Nl(S) ≠ T again
 Irrespective of where it runs from, every time the loop runs, it will either
augment M and break to go to while loop in Step 2, or add one new vertex
to S and T
 Since only O(n) vertices can be added before an augmenting path is found,
max. no. of iterations is O(n)
 Time per iteration of the while loop in 2(c) = O(n)
 If augmenting M, any path has maximum length O(n)
 If not, picking y and finding x takes O(n) time
 Total time for the while loop in Step 2(c) = O(n2)
 Step 2(d)
At  Computing α takes O(n2) time (in naïve approach)
l
 Updating the labels take O(n) time
 In the worst case, relabeling can be done O(n) times
 Each time adding exactly one new node to Nl(S)
 Total O(n3) time
 So total time for one iteration of the outer while loop =
O(1) + O(n2) + O(n3) = O(n3)
 So total time for the algorithm = no. of iterations of Step 2
× time for one iteration = O(n)× O(n3) = O(n4) =
O(|V|4)
 However, this uses a naïve approach that computes αl
from scratch every time, not efficient
 Time for step 2(d) can be reduced to O(n2) instead of O(n3) per iteration of
the outer while loop
 At any relabeling step, note that you have to consider (x,y) pairs such that x ∈ S,
y not in T
 ∀y not in T keep track of
slack(y) = minx∈S{ℓ(x)+ℓ(y) − w(x, y)}
 Initialize slack at beginning of outer while loop (Step 2) iteration in O(n) time as
only one node in S
 When a node goes from X-S to S (inside inner while loop in step 2(c)), update
slacks
 O(n) time as only one vertex moved in S each time, so does not change the time
for one iteration of the inner while loop we computed
 So total O(n2) time over all iterations of the while loop in step 2(c), same as before
 During relabeling, compute αl as miny∈T slack(y) in O(n) time
 So total O(n2) time as relabeling can be done at most O(n) times as we have seen
 After computing αl update slacks: ∀y not in T, slack(y) = slack(y) − αl
 O(n) time for each update, total O(n2) time over all relabeling

 Final Time complexity of Hungarian algorithm


O(n)× O(n2) = O(n3) = O(|V|3)
 Note:
 There is an equivalent matrix based description of
Hungarian algorithm that manipulates matrices instead of
bipartite graphs
 The algorithm is the same, just the representation is
different
 We will not do it in this class, but useful to know from a
practical implementation point of view

You might also like