Hskladjas

Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

CMPSCI 611: Advanced Algorithms

Lecture 7: Bipartite Matchings and Union Find

Andrew McGregor

Last Compiled: January 31, 2024

1/18
Outline

Intersection of Matroids and Bipartite Matchings

Union-Find Data Structure

2/18
Bipartite Matchings

Problem
I Input: Bipartite graph B = (U, V , E ) where U, V are disjoint sets of
vertices and E is a set of edges between U and V .
I Output: The matching (i.e., subset of E where no two edges share a
vertex) of maximum size.

Example Application: There’s a set of tasks V to be performed and a set


of individuals U, each capable of doing a subset of the tasks.
I Each person may be assigned to at most one task.
I At most one person may be assigned a task.
I Not every person can do every task. Can encode this in E .

3/18
Intersection of Matroids

I The bipartite matching subset system is not a matroid but it is the


intersection of two matroids.
I Define:

I = subsets of E where each u ∈ U has degree at most 1

I 0 = subsets of E where each v ∈ V has degree at most 1


Then, bipartite matching subset system is (E , I ∩ I 0 )

Theorem
For matroids (E , I) and (E , I 0 ), the largest set in I ∩ I 0 can be found in
time O(|E |3 · C (I, I 0 )) where C (I, I 0 ) is time to check i ∈ I or i ∈ I 0 .
We won’t prove this general theorem but will focus on the special case of
bipartite matching. Note that there is no analogous theorem for the
intersection of three matroids.

4/18
Augmenting Paths Definitions
Let M be a matching in a bipartite graph B = (U, V , E ). A free vertex is
a node not incident to any edge in M. E.g., the blue vertices in
U

Definition
An augmenting path is an odd sequence of edges that begins and ends at
(different) free vertices and alternates between matching edges e ∈ M
and non-matching edges e ∈ E − M.
Definition
If P is an augmenting path for matching M, the symmetric difference of
M and P is M ⊕ P := (M ∪ P) − (M ∩ P).
U

5/18
Augmenting Paths Properties

Lemma
For matching M and augmenting path P, M ⊕ P is a matching and

|M ⊕ P| = |M| + 1 .

Lemma
If M is non-maximum matching, there exists an augmenting path.

Algorithm: Find augmenting paths until we can’t find anymore!

6/18
Finding an augmenting path allows us to “grow” matching

Lemma
For matching M and augmenting path P, M ⊕ P is a matching and

|M ⊕ P| = |M| + 1 .

Proof.
I A matching is a graph where no node has degree > 1
I Size of matching is (number of degree 1 nodes)/2
I Remove edges in P ∩ M and add edges in P \ M:
I Adds one to degree of two nodes that initially were free.
I Degree of interior points of P still have degree 1.

7/18
Augmenting path exists for non-maximum matching
Lemma
If M is non-maximum matching, there exists an augmenting path.

Proof.
I Let M 0 be a matching such that |M 0 | > |M|
I Consider E 0 = M ⊕ M 0 . . . consists of simple paths and cycles whose
edges alternate between M and M 0

I Cycles have the same number of edges from M 0 and M


I There must exist a path P with more edges from M 0 than M, i.e.,
one that starts and end with an edge in M 0
I This is an augmenting path: edges alternate between M 0 and M and
it starts and ends with free vertices

8/18
Bipartite Matching Algorithm

Algorithm
I M←∅
I While there exists an augmenting path P: M ← M ⊕ P
I Return M

We can find an augmenting path in O(|U||E |) time:


I Direct matched edges V → U and non-matched edges U → V
I For each free vertex u ∈ U, grow a BFS: If a free vertex v ∈ V is
reachable from u, we have an augmenting path

Total running time is O(min(|U|, |V |)|U||E |) because the maximum


matching size is at most min(|U|, |V |). Can be improved by finding the
augmenting paths in a more clever way.

9/18
Outline

Intersection of Matroids and Bipartite Matchings

Union-Find Data Structure

10/18
Recall Kruskal’s Algorithm. . .

Problem: Given an undirected, connected graph G = (V , E ) with


positive edge weights, find the minimum-weight subset E 0 ⊂ E such that
the graph G = (V , E 0 ) is a minimum spanning tree.

Algorithm (Kruskal)
1. Sort edges by non-decreasing weight
2. F = ∅
3. Until F is a spanning tree of G
3.1 Get the next edge e
3.2 If F + e is acyclic then F = F + e

We saw how to implement this with O(|E | log |E | + |V |2 ) running time.


This class: improving to O(|E | log |E |) via the union-find data structure.

11/18
Union-Find Data Structure
Encodes a set of disjoint sets where each set contains an element
designated as the “label” of the set. E.g.,

{a, b, c} labeled “a” {d, e, f } labeled “e”

Supports three operations:


1. Make-Set(v ): Adds a set {v } with label “v ”

{a, b, c} labeled “a” {d, e, f } labeled “e” {v } labeled “v ”

2. Union-Set(u, v ): Replaces sets including u and v with a new set


that is union of both sets and labels this set by some element it
contains. For example, the result of Union-Set(f , v ) is

{a, b, c} labeled “a” {d, e, f , v } labeled “e”

3. Find(v ): Returns the label of the set including v

12/18
Kruskal’s Algorithm with Union-Find

Algorithm (Kruskal)
1. Sort edges by non-decreasing weight
2. For each vertex v ∈ V : Make-Set(v )
3. F = ∅
4. For each edge e = (u, v ) in E
4.1 If Find(u)6=Find(v ) then Union(u, v ) and F = F + e

Well, how should we implement union-find. . .

13/18
Simple Implementation of Union-Find
1. Each disjoint set is stored as a linked list of nodes
2. Each node consists of three data items:
2.1 name of element
2.2 “label” pointer to label of the set
2.3 “next” pointer to next node in list
3. Also maintain auxiliary pointer for each label to last node of
corresponding list and the size of this list.

a b c d e f

1. Make-Set(v ): Takes O(1) time to add a single node.


2. Find(v ): Takes O(1) time to follow pointer to label.
3. Union-Set(u, v ): O(size of smaller set).
I Update “next” pointer at end of longer list to point to start of
shorter list
I Update “label” pointers of shorter list to point to label of other list
I Update auxiliary pointers and size information

14/18
Union-Find Analysis

Theorem
Consider a sequence of m operations including n Make-Set operations.
Total running time is O(m + n log n).

Proof.
I Total time from Find and Make-Set: O(m)
I Total time from Union: O(n log n)
I Updating next pointers: O(n)
I Updating label pointers: O(n log n) because the label pointer for a
node can be updated at most log2 n times.

Hence, Kruskal’s algorithm can be implemented in time

O(|E | log |E |) + O(|E | + |V | log |V |) = O(|E | log |E |)

15/18
Faster Implementation of Union Find

Theorem
There exists an implementation that, given a sequence of n Make-Set
operations and m total operations, takes O(mα(n)) time where α is the
inverse Ackermann’s function.

Definition (Ackermann’s Function)


Define a sequence of functions: A0 (x) = 1 + x and

Ak (x) = Ak−1 (Ak−1 (. . . Ak−1 (x) . . .))

where Ak−1 is applied x times. E.g., A1 (x) = 2x, A2 (x) = 2x x.


Ackermann function is A(k) = Ak (2). α(n) is defined as smallest k such
that A(k) ≥ n.

Example
2(...2048)
α(n) ≤ 4 for all n ≤ 22 where tower is of height 2048.

16/18
Idea Behind Faster Implementation
I Store each set as a rooted tree.
I Each node encodes an element and pointer to the parent.
I The element at the root is the label of the set.
1. Make-Set(v ): Takes O(1) time to add a single node.
2. Find(v ): Takes O(dv ) time where dv is the depth of v
3. Union-Set(u, v ): O(dv + du ) time
I Perform Find(u), and Find(v )
I Add pointer from root of smaller tree to root of larger tree

Extra Trick! Do path compression. When we do a Find operation, update


the pointers from all nodes encountered to point to the root. Increases
time by a constant factor but saves time for future Find operations.

More Details: See Section 21.4 of CLRS (3rd edition) or Section 5.1 of
DPV.

17/18
Blank Slide

18/18

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy