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
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, J. Munkres, "Algorithms for the Assignment and Transportation Problems", Journal of the Society of Industrial and Applied Mathematics, 5(1):32-38, 1957.
The Assignment Problem
The Simple Assignment Problem Four individuals (i=1, 2, 3, 4) Four jobs (j=1, 2, 3, 4) Qualification Matrix
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?
The Simple Assignment Problem (Cont.) Start from an assignment Impossible to improve “Complete” “Incomplete” Transfer 1 Transfer 2 New Assignment
Bipartite Graphs & The Matching Problem
Bipartite Graph IndividualsJobs Alternatin g Path
*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.
*Applications of Bipartite Graphs Personnel Assignment Problem A company has workers X 1, …, X m and jobs Y 1, …, Y n. 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 (X i, Y j ) is given a weighting w ij indicated the ‘effectiveness’ (e.g. profit to company) of assigning worker X i to job Y j. 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)?
*Matchings All three problems involve forming a matching in a bipartite graph: Definition: Let G be a graph with {V 1, V 2 }. 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.
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?
*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 M
*M-augmenting path M-augmenting paths can be used to enlarge matchings. M !M M An M-alternating path whose endpoints are unsaturated by M.
Berge’s Theorem Berge’s Theorem: A matching M is maximum if and only if it has no M-augmenting paths.
Formulating - Simple Assignment Decision variable Let A be the set of allowed assignments
Network Flow
Matching as Network Flow t s Bipartite Graph Network Flow Augmentation Graph
The General Assignment Problem n individuals (i=1, 2, …, n) n jobs (j=1, 2, …, n) cost c ij, cost of individual i to do job j How can we assign the jobs to individuals to minimize the total cost? Rating r ij indicating the quality of work How can we assign the jobs to individuals to maximize the total rating?
Formulating- General Assignment Decision variable Let A be the set of allowed assignments and c ij be the cost of assigning i to j.
*Optimization Problem St. X 11 +X 12 +X 13 +X 14 =1 X 21 +X 22 +X 24 +X 24 =1 X 31 +X 32 +X 33 +X 34 =1 X 41 +X 42 +X 43 +X 44 =1 X 11 +X 21 +X 31 +X 41 =1 X 12 +X 22 +X 32 +X 42 =1 X 13 +X 23 +X 33 +X 43 =1 X 14 +X 24 +X 34 +X 44 =1 Min. 4X 11 +6X 12 +5X 13 +5X 14 +7X 21 +4X 22 +5X 23 +6X 24 +4X 31 +7X 32 +6X 33 +4X 34 +5X 41 +3X 42 +4X 43 +7X 44
Network Flow Red 1Red 2Red 3Red 4 Blue Blue Blue Blue Knowing the following capacities, what is the maximum flow from source to sink?
Augmentation Graphs -General case u v 0/75 0/10 Flow Graph u v Augmentation Graph 75-21= =31 u v 21/75 0/10 Flow Graph u v Augmentation Graph
The Hungarian Algorithm
Why Hungarian? Bipartite graph G with V nodes and E edges The Hungarian algorithm: O(V 3 ) The Network Flow algorithm: O(V.E 2 )
Example We must determine how jobs should be assigned to machines to minimize setup times, which are given below: Job 1Job 2Job 3Job 4 Machine Machine Machine Machine
Hungarian Algorithm Two Observations Adding a constant to any row or column does not change the solution Changing C If C is nonnegative and c ij x ij = 0 then X is a solution. Let 2 zeroes in C be called independent if they appear in different rows and columns.
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.
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 stop otherwise: 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.
Example We must determine how jobs should be assigned to machines to minimize setup times, which are given below: Job 1Job 2Job 3Job 4 Machine Machine Machine Machine
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.
Example: Step 1(a) Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Row Reduction
Example: Step 1(b) Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Column Reduction
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?!
Example: Step 2 Job 1Job 2Job 3Job 4 Machine Machine Machine Machine We need 3<4 lines, so continue to Step 3
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.
Example: Step 3 Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Job 1Job 2Job 3Job 4 Machine Machine Machine Machine 40135
Example: Step 2 (again) Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Need 4 lines, so we have the optimal assignment and we stop Zero Assignment
Example: Final Solution Job 1Job 2Job 3Job 4 Machine Machine Machine Machine Optimal assignment How did we know which 0’s to choose?!
Munkres Contribution Providing a constructive procedure for finding 1)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’
Resources (Thanks to Patrick Denis) Mathworks central exchange (download code): adFile.do?objectIde43&objectType=file adFile.do?objectIde43&objectType=file Helpful websites ex.html ex.html
References 9/Slides/MC ppt es/ graph-theory/Ch3 Matching and Factors.ppt /syllabus/03.5-NetworkFlow.ppt