Matching

Matching problems involve optimising the allocation of tasks to achieve the most efficient outcome. By evaluating factors such as time, cost, or resources, the optimal way to organise and assign tasks can be determined. For example, in a project setting, tasks can be assigned to team members based on their expertise, availability, and the time required to complete each task. This ensures that the project is completed efficiently while minimising delays and costs.


Use this page to revise the following concepts within matching:


Different Representations

A bipartite graph is a type of graph in which the vertices are divided into two distinct sets which are typically arranged in two columns. Edges connect vertices from one set to the other, but not within the same set. Depending on the context of the problem, these edges can be either directed or undirected, representing different types of relationships or assignments.

For example, consider a scenario where persons \(A, B\) and \(C\) are assigned to complete tasks \(X, Y\) and \(Z\). This could be represented as a bipartite graph:

This bipartite graph describes the following relationships:

  • Person \(A\) can complete tasks \(X\) or \(Y\)
  • Person \(B\) can complete tasks \(X\) or \(Z\)
  • Person \(C\) can complete task \(Y\)

graph with vertices A, B, C aligned in a column on the left, and vertices X, Y, Z aligned in a column on the right. Directed edges from A to X, A to Y, B to X, B to Z, and C to Y. To ensure that all tasks are completed by one person, the graph can be simplified by assigning specific tasks. \(C\) must complete task \(Y\). Since \(A\) must complete either \(Y\) or \(X\), this leaves \(A\) to complete task \(X\). Since \(B\) must complete \(X\) or \(Z\), this leaves \(B\) to complete task \(Z\).

graph with vertices A, B, C aligned in a column on the left, and vertices X, Y, Z aligned in a column on the right. Directed edges from A to X, B to Z, and C to Y

This example demonstrates how a bipartite graph can use constraints to optimise task allocation. While this example is simple and does not include weighted edges, weighted edges could represent additional factors such as the time required to complete each task. In scenarios where multiple options exist for assigning tasks, these weights would play a crucial role in determining the allocation.

However, as the number of vertices increases, the number of options that exist for assigning tasks increases, or the weights become more similar, manually finding the optimal solution becomes significantly more complex. In such cases, the Hungarian algorithm can be used to efficiently determine the optimal allocation in weighted bipartite graphs.

Hungarian Algorithm

The Hungarian Algorithm optimises task allocation by finding the solution with the minimum total weight by using a cost matrix. This explanation focuses on its application to complete graphs with equal numbers of vertices in each set.

The algorithm is applicable only to weighted bipartite graphs with non-negative values and ensures that each vertex in one set is assigned to exactly one vertex in the other set.

The steps involved in the Hungarian Algorithm are:

  1. Construct the square \((n\times n)\) cost matrix (if not provided) where:
    1. The rows represent those completing tasks
    2. The columns represent the tasks assigned
  2. Row reduction: subtract the lowest value from each row.
  3. Minimum number of lines check: cover all the zeros in the matrix using the minimum number of horizontal or vertical lines. If the number of lines equals to the number of row or columns \((n)\) then the optimal solution is found. If not, proceed to next step.
  4. Column reduction: subtract the lowest value from each column. If there is already a zero in the column, the column remains unchanged as zero is the lowest value.
  5. Minimum number of lines check
  6. Uncovered values: find the lowest value from all the uncovered values, and:
    1. Subtract it from any other uncovered values
    2. Add it to any intersecting covering lines
  7. Repeat step 5-6 until the number of minimum lines equals to the number or rows or columns \((n)\)
  8. Assign the allocations: zeros in the cost matrix represent the corresponding lowest allocation for those completing it and the task assigned. Make the allocation such that each row and column is only assigned once.
  9. Find the minimum cost: sum the weights that correspond to the assigned tasks.

Worked Example

A construction company hires four subcontractors \((A, B, C, D)\) to complete four different tasks \((W, X, Y, Z)\). Each of the subcontractors have provided the cost, in thousands of dollars, for completing each of the task as summarised in the cost matrix below:

 Task
Subcontractor \(W\) \(X\) \(Y\) \(Z\)
\(A\) 12 9 7 10
\(B\) 8 7 6 9
\(C\) 9 12 10 11
\(D\) 7 6 9 8

Use the Hungarian algorithm to determine the optimal assignment of subcontractors to tasks to minimise the total cost of the project. Provide the assignment and the total cost.

Begin by performing row reduction by subtracting the lowest values in each row – \(A: 7, B:6, C:9, D:6\)

 Task
Subcontractor \(W\) \(X\) \(Y\) \(Z\)
\(A\) 12\(-\)7 9\(-\)7 7\(-\)7 10\(-\)7
\(B\) 8\(-\)6 7\(-\)6 6\(-\)6 9\(-\)6
\(C\) 9\(-\)9 12\(-\)9 10\(-\)9 11\(-\)9
\(D\) 7\(-\)6 6\(-\)6 9\(-\)6 8\(-\)6

Which results in

 Task
Subcontractor \(W\) \(X\) \(Y\) \(Z\)
\(A\) 5 2 0 3
\(B\) 2 1 0 3
\(C\) 0 3 1 2
\(D\) 1 0 3 2

Check minimum lines

Three lines are required to cover all the zeros; therefore the optimal solution has not been determined.

Perform column reduction by subtracting the lowest value in each column (columns with zero remain unchanged): \(Z:2\)

 Task
Subcontractor \(W\) \(X\) \(Y\) \(Z\)
\(A\) 5 2 0 3\(-\)2
\(B\) 2 1 0 3\(-\)2
\(C\) 0 3 1 2\(-\)2
\(D\) 1 0 3 2\(-\)2

This results in

 Task
Subcontractor \(W\) \(X\) \(Y\) \(Z\)
\(A\) 5 2 0 1
\(B\) 2 1 0 1
\(C\) 0 3 1 0
\(D\) 1 0 3 0

Check minimum lines

Three lines are required to cover all the zeros; therefore the optimal solution has not been determined.

Proceed with performing the uncovered values step: subtract the lowest uncovered value of 1 from all the uncovered values, and add to intersecting line values.

This results in

 Task
Subcontractor \(W\) \(X\) \(Y\) \(Z\)
\(A\) 4 1 0 0
\(B\) 1 0 0 0
\(C\) 0 3 2 0
\(D\) 1 0 4 0

Check the minimum lines

A minimum of four lines is required to cover all the zeros. Therefore, the optimal solution has been determined.

The zeros describe the optimal allocations:

  • \(A\) must perform \(Y\) or \(Z\)
  • \(B\) must perform \(X\), \(Y\) or \(Z\)
  • \(C\) must perform \(W\) or \(Z\)
  • \(D\) must perform \(X\) or \(Z\)
  • From this, there are multiple possible allocations:

    Case 1 – \(C:W, A:Y, B:X, D:Z\)

    Case 2 – \(C:W, A:Y, D:Z, D:X\)

    Case 3 – \(C:W, A:Z. B:Y, D:X\)

    Each of these cases should result in the same cost:

    Case 1 – \(9+7+7+8=31\)

    Case 2 – \(9+7+9+6=31\)

    Case 3 – \(9+10+6+6=31\)