43
Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Embed Size (px)

Citation preview

Page 1: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm

Vida MovahediElderlab, York University

June 2007

Page 2: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Outline

The Assignment Problem Bipartite Graphs and Matching Network Flow Hungarian Algorithm Example

Note: I am using some slides from reference files without any changes, I have marked them with a * in title

Page 3: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

History

Two Hungarian mathematicians: Dénes König (1936) and Jenő Egerváry (1931)

Harold W. Kuhn, "The Hungarian Method for the assignment problem", Naval Research Logistic Quarterly, 2:83-97, 1955.

J. Munkres, "Algorithms for the Assignment and Transportation Problems", Journal of the Society of Industrial and Applied Mathematics, 5(1):32-38, 1957.

Page 4: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

The Assignment Problem

Page 5: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

The Simple Assignment Problem Four individuals (i=1, 2, 3, 4) Four jobs (j=1, 2, 3, 4)

Qualification Matrix

Page 6: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

The Simple Assignment Problem (Cont.)

What is the largest number of jobs that can be assigned to qualified individuals (with not more than one job assigned to each individual)?

What is the largest number of 1’s that can be chosen from Q with no two chosen from the same row or column?

Page 7: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

The Simple Assignment Problem (Cont.)

Start from an assignment

Impossible to improve

“Complete”

“Incomplete”

Transfer 1

Transfer 2

New Assignmen

t

Page 8: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Bipartite Graphs

& The Matching Problem

Page 9: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Bipartite Graph

Individuals

Jobs

Alternating Path

Page 10: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

*Characterizing Bipartite Graphs

Theorem. Let G be a graph with at least 2 vertices. The following statements about G are equivalent: 1. G is bipartite. 2. G can be properly 2-colored. 3. G has no odd cycles.

Page 11: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

*Applications of Bipartite Graphs

Personnel Assignment Problem A company has workers X1, …, Xm and jobs Y1, …, Yn. Each

worker is qualified to do some jobs, but not others. Can every worker be assigned a job?

Optimal Assignment Problem Same basic setup as above, but now each pair (Xi, Yj) is

given a weighting wij indicated the ‘effectiveness’ (e.g. profit to company) of assigning worker Xi to job Yj.

How should jobs be assigned to maximize the total effectiveness of the assignments?

Marriage Problem There are k men and m women, and each male-female pair

has expressed whether or not they are willing to marry. How can we pair them up so that all the men are paired

with acceptable mates (or the gender-reversed question)?

Page 12: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

*Matchings

All three problems involve forming a matching in a bipartite graph:

Definition: Let G be a graph with {V1, V2}. A matching in G is a set of edges, no two of which share an endpoint.

Note: G does not need to bipartite, but in applications it often is.

A B C

D E F G H

000 100

010

001 101

111

110

011

Page 13: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Maximum and Perfect Matchings

A matching M is maximum if it has the largest size among all possible matchings.

A matching M is perfect if every vertex in G is incident with an edge in the matching. Does maximum imply perfect? Does perfect imply maximum?

Page 14: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

*M-alternating path

Given a matching M, a M-alternating path is a path that alternates between edges in M and edges not in M

M!M

!MM

Page 15: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

*M-augmenting path

M-augmenting paths can be used to enlarge matchings.

M!M!M

!MM

An M-alternating path whose endpoints are unsaturated by M.

Page 16: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Berge’s Theorem

Berge’s Theorem: A matching M is

maximum if and only if it has no

M-augmenting paths.

Page 17: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Formulating - Simple Assignment

Decision variable

Let A be the set of allowed assignments

0000

1000

0100

0001

X

otherwise0

j toassigned is i if1ijx

A(i,jx

jx

ix

xq

ij

Ajiiij

Ajijij

Ajiijij

in ) allfor 1,0

allfor ,1

allfor ,1s.t.

maximize

in ),(:

in ),(:

in ),(

1000

*1000

1*100

011*1

Q

Page 18: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Network Flow

Page 19: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Matching as Network Flow

ts

1000

*1000

1*100

011*1

Q

Bipartite Graph Network Flow Augmentation

Graph

Page 20: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

The General Assignment Problem n individuals (i=1, 2, …, n) n jobs (j=1, 2, …, n)

cost cij, cost of individual i to do job j How can we assign the jobs to

individuals to minimize the total cost?

Rating rij indicating the quality of work How can we assign the jobs to

individuals to maximize the total rating?

Page 21: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Formulating- General Assignment

Decision variable

Let A be the set of allowed assignments and cij be the cost of assigning i to j.

otherwise0

j toassigned is i if1ijx

A(i,jx

jx

ix

xc

ij

Ajiiij

Ajijij

Ajiijij

in ) allfor 1,0

allfor ,1

allfor ,1s.t.

.minimize

in ),(:

in ),(:

in ),(

Page 22: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

*Optimization Problem

St.X11+X12+X13+X14=1X21+X22+X24+X24=1X31+X32+X33+X34=1X41+X42+X43+X44=1X11+X21+X31+X41=1X12+X22+X32+X42=1X13+X23+X33+X43=1X14+X24+X34+X44=1

Min.

4X11+6X12+5X13+5X14

+7X21+4X22+5X23+6X24

+4X31+7X32+6X33+4X34

+5X41+3X42+4X43+7X44

Page 23: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Network Flow

Red 1 Red 2 Red 3 Red 4

Blue 1 2 5 3 4

Blue 2 13 2 4 5

Blue 3 4 3 8 3

Blue 4 13 6 4 3

Knowing the following capacities, what is the maximum flow from source to sink?

Page 24: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Augmentation Graphs -General case

u v0/75

0/10

Flow Graph

u v75

10

Augmentation Graph75-21=54

21+10=31

u v21/75

0/10

Flow Graph

u v

Augmentation Graph

Page 25: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

The Hungarian Algorithm

Page 26: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Why Hungarian?

Bipartite graph G with V nodes and E edges

The Hungarian algorithm: O(V3) The Network Flow algorithm: O(V.E2)

Page 27: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example

We must determine how jobs should be assigned to machines to minimize setup times, which are given below:

Job 1 Job 2 Job 3 Job 4

Machine 1 14 5 8 7

Machine 2 2 12 6 5

Machine 3 7 8 3 9

Machine 4 2 4 6 10

Page 28: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm Two Observations

Adding a constant to any row or column does not change the solution Changing C

If C is nonnegative and cijxij = 0 then X is a solution.

Let 2 zeroes in C be called independent if they appear in different rows and columns.

Page 29: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Theorem

A set of elements of a matrix are said to be ‘independent’ if no two of them lie in the same row or column.

König Theorem:If C is a matrix and m is the number of independent zero elements of C, then there are m lines which contain all the zero elements of C.

Page 30: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm

1. From each line (row or column) subtract its minimum element.

2. Find a maximum set of N’ mutually independent zeroes.

3. if N’ = N such zeroes have been found: output their indices and stopotherwise: cover all zeroes in W with N’ lines and find the minimum uncovered value; subtract it from all uncovered elements, and add it to all doubly covered elements; go to 2.

Page 31: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example

We must determine how jobs should be assigned to machines to minimize setup times, which are given below:

Job 1 Job 2 Job 3 Job 4

Machine 1 14 5 8 7

Machine 2 2 12 6 5

Machine 3 7 8 3 9

Machine 4 2 4 6 10

Page 32: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm

Step 1: (a) Find the minimum element in each row of the cost matrix. Form a new matrix by subtracting this cost from each row. (b) Find the minimum cost in each column of the new matrix, and subtract this from each column. This is the reduced cost matrix.

Page 33: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example: Step 1(a)Job 1 Job 2 Job 3 Job 4

Machine 1 14 5 8 7

Machine 2 2 12 6 5

Machine 3 7 8 3 9

Machine 4 2 4 6 10

Job 1 Job 2 Job 3 Job 4

Machine 1 9 0 3 2

Machine 2 0 10 4 3

Machine 3 4 5 0 6

Machine 4 0 2 4 8

Row Reduction

Page 34: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example: Step 1(b)

Job 1 Job 2 Job 3 Job 4

Machine 1 9 0 3 0

Machine 2 0 10 4 1

Machine 3 4 5 0 4

Machine 4 0 2 4 6

Job 1 Job 2 Job 3 Job 4

Machine 1 9 0 3 2

Machine 2 0 10 4 3

Machine 3 4 5 0 6

Machine 4 0 2 4 8

Column Reduction

Page 35: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm

Step 2: Draw the minimum number of lines that are needed to cover all the zeros in the reduced cost matrix. If m lines are required, then an optimal solution is available among the covered zeros in the matrix. Otherwise, continue to Step 3.

How do we find the minimum

number of lines?!

Page 36: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example: Step 2

Job 1 Job 2 Job 3 Job 4

Machine 1 9 0 3 0

Machine 2 0 10 4 1

Machine 3 4 5 0 4

Machine 4 0 2 4 6

We need 3<4 lines, so continue to Step 3

Page 37: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Hungarian Algorithm

Step 3: Find the smallest nonzero element (say, k) in the reduced cost matrix that is uncovered by the lines. Subtract k from each uncovered element, and add k to each element that is covered by two lines. Return to Step 2.

Page 38: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example: Step 3Job 1 Job 2 Job 3 Job 4

Machine 1 9 0 3 0

Machine 2 0 10 4 1

Machine 3 4 5 0 4

Machine 4 0 2 4 6

Job 1 Job 2 Job 3 Job 4

Machine 1 10 0 3 0

Machine 2 0 9 3 0

Machine 3 5 5 0 4

Machine 4 0 1 3 5

Page 39: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example: Step 2 (again)

Job 1 Job 2 Job 3 Job 4

Machine 1 10 0 3 0

Machine 2 0 9 3 0

Machine 3 5 5 0 4

Machine 4 0 1 3 5

Need 4 lines, so we have the optimal assignment and we stop

Zero Assignment

Page 40: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Example: Final Solution

Job 1 Job 2 Job 3 Job 4

Machine 1 10 0 3 0

Machine 2 0 9 3 0

Machine 3 5 5 0 4

Machine 4 0 1 3 5

Optimal assignment

1,1,1,1 24413312 xxxx

How did we know which

0’s to choose?!

Page 41: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Munkres Contribution

Providing a constructive procedure for finding1) A minimal set of lines which contain all

zeros,2) A maximal set of independent zeros

“Starred zeros” and “Primed zeros” Alternating sequence between 0* and 0’

Page 42: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

Resources (Thanks to Patrick Denis)

Mathworks central exchange (download code):

http://www.mathworks.com/matlabcentral/fileexchange/loadFile.do?objectIde43&objectType=file

Helpful websites http://en.wikipedia.org/wiki/Hungarian_algorithm http://www.public.iastate.edu/~ddoty/HungarianAlg

orithm.html http://

www.ifors.ms.unimelb.edu.au/tutorial/hungarian/index.html

Page 43: Hungarian Algorithm Vida Movahedi Elderlab, York University June 2007

References

http://www.skidmore.edu/~adean/MC3020409/Slides/MC302041019.ppt

http://www.math.ntu.edu.tw/~gjchang/courses/2002-09-graph-theory/Ch3 Matching and Factors.ppt

https://www.cse.yorku.ca/~jeff/courses/6111/syllabus/03.5-NetworkFlow.ppt