Simple algorithm & hopcroft karp for bipartite graph

23
Combinatorial Algorithms (Algorithms in Bipartite Graphs)

Transcript of Simple algorithm & hopcroft karp for bipartite graph

Page 1: Simple algorithm & hopcroft karp for bipartite graph

Combinatorial Algorithms(Algorithms in Bipartite Graphs)

Page 2: Simple algorithm & hopcroft karp for bipartite graph

2

IntroductionAlgorithms in unweighted bipartite graph

Maximum matching A simple algorithm Hopcroft-Karp algorithm

Outline

Page 3: Simple algorithm & hopcroft karp for bipartite graph

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

Page 4: Simple algorithm & hopcroft karp for bipartite graph

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

Page 5: Simple algorithm & hopcroft karp for bipartite graph

5

ProblemTesting bipartitenessMatching

Maximum matching problem

Introduction

Page 6: Simple algorithm & hopcroft karp for bipartite graph

Maximum matching

Page 7: Simple algorithm & hopcroft karp for bipartite graph

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

Page 8: Simple algorithm & hopcroft karp for bipartite graph

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

Page 9: Simple algorithm & hopcroft karp for bipartite graph

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

Page 10: Simple algorithm & hopcroft karp for bipartite graph

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

Page 11: Simple algorithm & hopcroft karp for bipartite graph

11

A simple algorithm

X2  

X3  

X1  Y1 

Y2 

Y3 

Y4 

Maximum matching

Page 12: Simple algorithm & hopcroft karp for bipartite graph

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

Page 13: Simple algorithm & hopcroft karp for bipartite graph

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

Page 14: Simple algorithm & hopcroft karp for bipartite graph

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

Page 15: Simple algorithm & hopcroft karp for bipartite graph

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

Page 16: Simple algorithm & hopcroft karp for bipartite graph

Hopcroft-Karp AlgorithmAn algorithm to find the maximum matching

given a bipartite graph

Gordon

Page 17: Simple algorithm & hopcroft karp for bipartite graph

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.

Page 18: Simple algorithm & hopcroft karp for bipartite graph

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

Page 19: Simple algorithm & hopcroft karp for bipartite graph

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

Page 20: Simple algorithm & hopcroft karp for bipartite graph

Demonstration of algorithm at some stage

Let the dark edges represent the edges in a matching M

Page 21: Simple algorithm & hopcroft karp for bipartite graph

Demonstration of algorithm at some stage

Pink edges represent an augmenting path

Deleting them

Page 22: Simple algorithm & hopcroft karp for bipartite graph

Demonstration of algorithm at some stage

Another augmenting path

No more paths

Page 23: Simple algorithm & hopcroft karp for bipartite graph

Demonstration of algorithm at some stage

Pink edges represent the paths in maximal set S

M ⊕ SNote the before and after