Simple algorithm & hopcroft karp for bipartite graph

Preview:

Citation preview

Combinatorial Algorithms(Algorithms in Bipartite Graphs)

2

IntroductionAlgorithms in unweighted bipartite graph

Maximum matching A simple algorithm Hopcroft-Karp algorithm

Outline

3

DefinitionA graph G = (V, E) is bipartite if there exists

partition V = X ∪ Y with X ∩ Y = ∅ and E ⊆ X × Y.

Bipartite Graph typesUnweightedWeighted

For every edge e ∈ E , there is aweight w(e) .

Introduction

4

Example: There are a set of boys and a set of girls. Each boy only likes girls and each girl only

likes boys. A common friend wants to match each boy with

a girl such that the boy and girl are both happy – but they both will only be happy if the boy likes the girl and the girl likes the boy.

Is it possible for every situation?

Introduction

We can use a bipartite graph to model this

problem

5

ProblemTesting bipartitenessMatching

Maximum matching problem

Introduction

Maximum matching

7

DefinitionMatching

A Matching is a subset M ⊆ E such that ∀v ∈ V at most one edge in M is incident upon v

– Maximum matching• A Maximum Matching is matching M such that

everyother matching M′ satisfies |M′| ≤ |M|.• Unweighted graph: |M|= the number of edges• Weighted graph: |M|=

– Perfect Matching• A matching which matches all vertices of the graph

Maximum matching

8

A Matching

A Maximum Matching

Not a Matching

DefinitionWe say that a vertex is matched if it is incident to some

edge in M. Otherwise, the vertex is free

matched

free

(not perfect)

Maximum matching

9

DefinitionAlternating Paths

A path is alternating if its edges alternate between M and E − M.

Augmenting Paths An alternating path is

augmenting if both endpoints are free

Alternating Tree A tree rooted at some free

vertex v in which every path is an alternating path.

• Alternating paths ( Y1, X2, Y2, X4 )• Augmenting Path (Y1, X2, Y2, X4, Y4, X5)

Maximum matching

10

Property of Augmenting Paths Replacing the M edges by the E − M ones

increments size of the matching

(Path: Y1, X2, Y2, X4, Y4, X5)

Berge's Theorem: A matching M is maximum iff it has no augmenting path (Proof: Lec01 Page 3)

Maximum matching

11

A simple algorithm

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

Maximum matching

12

A simple algorithm

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

Maximum matching

13

A simple algorithm

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

Maximum matching

14

A simple algorithm

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

Maximum matching

15

A simple algorithm

• Commonly search algorithm (BFS, DFS) O(E)

• At most V times

• Complexity: O(VE)

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

Maximum matching

Hopcroft-Karp AlgorithmAn algorithm to find the maximum matching

given a bipartite graph

Gordon

Introduction

• The Hopcroft-Karp algorithm was published in 1973

• It is a matching algorithm that finds a maximum matching in bipartite graphs

• The main idea is to augment along a set of vertex-disjoint shortest augment paths simulatenously

• The complexity is O(√|V||E|)• In this section, some Theorems and

Lemmas from graph theory will be stated without showing the proof.

Definition

• We let the set A ⊕ B denote the symmetric difference of the set

• A ⊕ B = (A ∪ B) – (A ∩ B)• A maximal set of vertex-disjoint minimum

length augmenting path is defined as follows : It is a set of augmenting path No two path share a same vertex If the minimum length augmenting

path is of length k, then all paths in S are of length k

If p is an augmenting path not in S, then p shares a vertex with some path p’ in S

Algorithm The algorithm of Hopcroft and Kraft is as follows :

Given a graph G = (X ∪ Y),E)

1) Let M = {} ,2) Find S = {P1 , P2 , … Pk}3) While S ≠ {} M = M ⊕ S Find S4) Output M

Demonstration of algorithm at some stage

Let the dark edges represent the edges in a matching M

Demonstration of algorithm at some stage

Pink edges represent an augmenting path

Deleting them

Demonstration of algorithm at some stage

Another augmenting path

No more paths

Demonstration of algorithm at some stage

Pink edges represent the paths in maximal set S

M ⊕ SNote the before and after

Recommended