Skip to content

Commit 4a0d27e

Browse files
authored
Merge pull request #1666 from Bhupendrakumar20/new-algo
Add Hungarian Algorithm Implementation in C
2 parents 7d88635 + 0aac005 commit 4a0d27e

File tree

2 files changed

+165
-0
lines changed

2 files changed

+165
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <stdio.h>
2+
#include <limits.h>
3+
#include <stdbool.h>
4+
5+
#define N 4 // Define the number of tasks and workers
6+
7+
int cost[N][N] = {
8+
{9, 2, 7, 8},
9+
{6, 4, 3, 7},
10+
{5, 8, 1, 8},
11+
{7, 6, 9, 4}
12+
};
13+
14+
int min(int a, int b) {
15+
return (a < b) ? a : b;
16+
}
17+
18+
// Function to implement the Hungarian Algorithm
19+
void hungarian_algorithm() {
20+
int u[N], v[N], p[N], way[N], minv[N];
21+
bool used[N];
22+
int i, j, k, l, j1, delta;
23+
24+
for (i = 0; i < N; i++) {
25+
u[i] = v[i] = 0;
26+
p[i] = way[i] = -1;
27+
}
28+
29+
for (i = 0; i < N; i++) {
30+
p[0] = i;
31+
j1 = 0;
32+
for (j = 0; j < N; j++) {
33+
minv[j] = INT_MAX;
34+
used[j] = false;
35+
}
36+
37+
do {
38+
used[j1] = true;
39+
int i0 = p[j1], delta = INT_MAX;
40+
j1 = -1;
41+
42+
for (j = 1; j < N; j++) {
43+
if (!used[j]) {
44+
int cur = cost[i0][j] - u[i0] - v[j];
45+
if (cur < minv[j]) {
46+
minv[j] = cur;
47+
way[j] = j1;
48+
}
49+
if (minv[j] < delta) {
50+
delta = minv[j];
51+
j1 = j;
52+
}
53+
}
54+
}
55+
56+
for (j = 0; j < N; j++) {
57+
if (used[j]) {
58+
u[p[j]] += delta;
59+
v[j] -= delta;
60+
} else {
61+
minv[j] -= delta;
62+
}
63+
}
64+
65+
} while (p[j1] != -1);
66+
67+
do {
68+
int j2 = way[j1];
69+
p[j1] = p[j2];
70+
j1 = j2;
71+
} while (j1);
72+
}
73+
74+
int result = -v[0];
75+
printf("Minimum cost: %d\n", result);
76+
printf("Optimal assignment:\n");
77+
for (j = 1; j < N; j++) {
78+
printf("Worker %d assigned to task %d\n", p[j] + 1, j);
79+
}
80+
}
81+
82+
int main() {
83+
hungarian_algorithm();
84+
return 0;
85+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
2+
# Hungarian Algorithm in C
3+
4+
This project contains an implementation of the Hungarian Algorithm (or Kuhn-Munkres algorithm) in C for solving the assignment problem. This algorithm is used to find the minimum-cost assignment of tasks to workers such that each worker is assigned exactly one task, and each task is completed by only one worker.
5+
6+
## Table of Contents
7+
- [Description](#description)
8+
- [Problem Statement](#problem-statement)
9+
- [Solution Approach](#solution-approach)
10+
- [Usage](#usage)
11+
- [Example](#example)
12+
- [Limitations](#limitations)
13+
14+
15+
## Description
16+
17+
The Hungarian Algorithm is widely used in optimization, specifically in solving assignment problems like matching workers to jobs at the minimum cost. This algorithm leverages matrix reduction and optimal matching techniques to achieve an efficient solution.
18+
19+
### Problem Statement
20+
21+
Given an \( N \times N \) cost matrix, where each element represents the cost for assigning a task to a worker, the Hungarian Algorithm finds an assignment that minimizes the total cost.
22+
23+
### Solution Approach
24+
25+
The algorithm works as follows:
26+
1. **Matrix Reduction**: Row and column reductions are performed to make it easier to find zero-cost assignments.
27+
2. **Covering Zeros**: It then finds the minimum number of lines to cover all zero entries in the matrix.
28+
3. **Adjustments and Iteration**: Adjustments are made to the matrix until an optimal assignment is found, corresponding to the minimum cost.
29+
30+
## Usage
31+
32+
### Requirements
33+
- C compiler (e.g., GCC).
34+
35+
### Running the Program
36+
37+
1. **Compile** the code with the following command:
38+
```bash
39+
gcc hungarian.c -o hungarian
40+
```
41+
2. **Execute** the program:
42+
```bash
43+
./hungarian
44+
```
45+
46+
The program will output the minimum cost for the assignment and the optimal assignment of workers to tasks.
47+
48+
### Code Structure
49+
50+
- **`isSafe`**: Checks if it’s safe to assign a color to a vertex based on adjacent vertex colors.
51+
- **`hungarian_algorithm`**: Implements the core logic of the Hungarian Algorithm.
52+
- **`main`**: Initializes the cost matrix and invokes the `hungarian_algorithm()` function to compute the optimal assignment.
53+
54+
## Example
55+
56+
Given the following 4x4 cost matrix:
57+
```plaintext
58+
9 2 7 8
59+
6 4 3 7
60+
5 8 1 8
61+
7 6 9 4
62+
```
63+
64+
With this cost matrix, the program outputs:
65+
```plaintext
66+
Minimum cost: 13
67+
Optimal assignment:
68+
Worker 1 assigned to task 2
69+
Worker 2 assigned to task 3
70+
Worker 3 assigned to task 1
71+
Worker 4 assigned to task 4
72+
```
73+
74+
This assignment ensures that the total cost is minimized.
75+
76+
## Limitations
77+
78+
- **Matrix Size**: The algorithm is implemented for a square \( N \times N \) matrix.
79+
- **Performance**: This implementation may not be optimized for very large matrices. Alternative, faster algorithms may be preferable for very large datasets.
80+

0 commit comments

Comments
 (0)