|
| 1 | +#include <stdio.h> |
| 2 | +#include <limits.h> |
| 3 | + |
| 4 | +#define N 4 // Number of workers and tasks |
| 5 | + |
| 6 | +void hungarianAlgorithm(int costMatrix[N][N]); |
| 7 | + |
| 8 | +int main() { |
| 9 | + int costMatrix[N][N] = { |
| 10 | + {9, 2, 7, 8}, |
| 11 | + {6, 4, 3, 7}, |
| 12 | + {5, 8, 1, 8}, |
| 13 | + {7, 6, 9, 4} |
| 14 | + }; |
| 15 | + |
| 16 | + hungarianAlgorithm(costMatrix); |
| 17 | + |
| 18 | + return 0; |
| 19 | +} |
| 20 | + |
| 21 | +void hungarianAlgorithm(int costMatrix[N][N]) { |
| 22 | + int i, j; |
| 23 | + int numWorkers = N, numTasks = N; |
| 24 | + |
| 25 | + int minCost, minCostIdx; |
| 26 | + int rowCover[N] = {0}; |
| 27 | + int colCover[N] = {0}; |
| 28 | + int assignment[N][2] = {0}; // Stores the assignment |
| 29 | + |
| 30 | + // Step 1: Subtract the smallest value in each row from all elements in that row |
| 31 | + for (i = 0; i < numWorkers; i++) { |
| 32 | + minCost = INT_MAX; |
| 33 | + for (j = 0; j < numTasks; j++) { |
| 34 | + if (costMatrix[i][j] < minCost) { |
| 35 | + minCost = costMatrix[i][j]; |
| 36 | + } |
| 37 | + } |
| 38 | + for (j = 0; j < numTasks; j++) { |
| 39 | + costMatrix[i][j] -= minCost; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Step 2: Find a zero in the cost matrix and mark the row and column |
| 44 | + for (i = 0; i < numWorkers; i++) { |
| 45 | + for (j = 0; j < numTasks; j++) { |
| 46 | + if (costMatrix[i][j] == 0 && !rowCover[i] && !colCover[j]) { |
| 47 | + assignment[i][0] = i; |
| 48 | + assignment[i][1] = j; |
| 49 | + rowCover[i] = 1; |
| 50 | + colCover[j] = 1; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // Step 3: Check if all rows are covered |
| 56 | + int rowCoveredCount = 0; |
| 57 | + for (i = 0; i < numWorkers; i++) { |
| 58 | + rowCoveredCount += rowCover[i]; |
| 59 | + } |
| 60 | + |
| 61 | + if (rowCoveredCount == numWorkers) { |
| 62 | + // All rows are covered, we have the optimal assignment |
| 63 | + printf("Optimal Assignment:\n"); |
| 64 | + for (i = 0; i < numWorkers; i++) { |
| 65 | + printf("Worker %d -> Task %d\n", assignment[i][0] + 1, assignment[i][1] + 1); |
| 66 | + } |
| 67 | + return; |
| 68 | + } else { |
| 69 | + // Proceed to step 4 |
| 70 | + } |
| 71 | + |
| 72 | + // Step 4: Find the minimum uncovered value (minCost) in the cost matrix |
| 73 | + minCost = INT_MAX; |
| 74 | + for (i = 0; i < numWorkers; i++) { |
| 75 | + for (j = 0; j < numTasks; j++) { |
| 76 | + if (!rowCover[i] && !colCover[j] && costMatrix[i][j] < minCost) { |
| 77 | + minCost = costMatrix[i][j]; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Step 5: Subtract minCost from all uncovered elements and add it to all elements at the intersection of covering lines |
| 83 | + for (i = 0; i < numWorkers; i++) { |
| 84 | + for (j = 0; j < numTasks; j++) { |
| 85 | + if (!rowCover[i] && !colCover[j]) { |
| 86 | + costMatrix[i][j] -= minCost; |
| 87 | + } else if (rowCover[i] && colCover[j]) { |
| 88 | + costMatrix[i][j] += minCost; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Continue to step 3 |
| 94 | + hungarianAlgorithm(costMatrix); |
| 95 | +} |
0 commit comments