From 0aac0051083ce6d1ec553f9f80ea0d9ee86c1722 Mon Sep 17 00:00:00 2001 From: Bhupendrakumar20 Date: Tue, 5 Nov 2024 19:37:36 +0530 Subject: [PATCH] new Hungarian Algo --- .../Hungarian Algorithm/Program.c | 85 +++++++++++++++++++ .../Hungarian Algorithm/README.md | 80 +++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 Graph Algorithms/Hungarian Algorithm/Program.c create mode 100644 Graph Algorithms/Hungarian Algorithm/README.md diff --git a/Graph Algorithms/Hungarian Algorithm/Program.c b/Graph Algorithms/Hungarian Algorithm/Program.c new file mode 100644 index 00000000..44f11b3a --- /dev/null +++ b/Graph Algorithms/Hungarian Algorithm/Program.c @@ -0,0 +1,85 @@ +#include +#include +#include + +#define N 4 // Define the number of tasks and workers + +int cost[N][N] = { + {9, 2, 7, 8}, + {6, 4, 3, 7}, + {5, 8, 1, 8}, + {7, 6, 9, 4} +}; + +int min(int a, int b) { + return (a < b) ? a : b; +} + +// Function to implement the Hungarian Algorithm +void hungarian_algorithm() { + int u[N], v[N], p[N], way[N], minv[N]; + bool used[N]; + int i, j, k, l, j1, delta; + + for (i = 0; i < N; i++) { + u[i] = v[i] = 0; + p[i] = way[i] = -1; + } + + for (i = 0; i < N; i++) { + p[0] = i; + j1 = 0; + for (j = 0; j < N; j++) { + minv[j] = INT_MAX; + used[j] = false; + } + + do { + used[j1] = true; + int i0 = p[j1], delta = INT_MAX; + j1 = -1; + + for (j = 1; j < N; j++) { + if (!used[j]) { + int cur = cost[i0][j] - u[i0] - v[j]; + if (cur < minv[j]) { + minv[j] = cur; + way[j] = j1; + } + if (minv[j] < delta) { + delta = minv[j]; + j1 = j; + } + } + } + + for (j = 0; j < N; j++) { + if (used[j]) { + u[p[j]] += delta; + v[j] -= delta; + } else { + minv[j] -= delta; + } + } + + } while (p[j1] != -1); + + do { + int j2 = way[j1]; + p[j1] = p[j2]; + j1 = j2; + } while (j1); + } + + int result = -v[0]; + printf("Minimum cost: %d\n", result); + printf("Optimal assignment:\n"); + for (j = 1; j < N; j++) { + printf("Worker %d assigned to task %d\n", p[j] + 1, j); + } +} + +int main() { + hungarian_algorithm(); + return 0; +} diff --git a/Graph Algorithms/Hungarian Algorithm/README.md b/Graph Algorithms/Hungarian Algorithm/README.md new file mode 100644 index 00000000..b03ced4b --- /dev/null +++ b/Graph Algorithms/Hungarian Algorithm/README.md @@ -0,0 +1,80 @@ + +# Hungarian Algorithm in C + +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. + +## Table of Contents +- [Description](#description) +- [Problem Statement](#problem-statement) +- [Solution Approach](#solution-approach) +- [Usage](#usage) +- [Example](#example) +- [Limitations](#limitations) + + +## Description + +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. + +### Problem Statement + +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. + +### Solution Approach + +The algorithm works as follows: +1. **Matrix Reduction**: Row and column reductions are performed to make it easier to find zero-cost assignments. +2. **Covering Zeros**: It then finds the minimum number of lines to cover all zero entries in the matrix. +3. **Adjustments and Iteration**: Adjustments are made to the matrix until an optimal assignment is found, corresponding to the minimum cost. + +## Usage + +### Requirements +- C compiler (e.g., GCC). + +### Running the Program + +1. **Compile** the code with the following command: + ```bash + gcc hungarian.c -o hungarian + ``` +2. **Execute** the program: + ```bash + ./hungarian + ``` + +The program will output the minimum cost for the assignment and the optimal assignment of workers to tasks. + +### Code Structure + +- **`isSafe`**: Checks if it’s safe to assign a color to a vertex based on adjacent vertex colors. +- **`hungarian_algorithm`**: Implements the core logic of the Hungarian Algorithm. +- **`main`**: Initializes the cost matrix and invokes the `hungarian_algorithm()` function to compute the optimal assignment. + +## Example + +Given the following 4x4 cost matrix: +```plaintext +9 2 7 8 +6 4 3 7 +5 8 1 8 +7 6 9 4 +``` + +With this cost matrix, the program outputs: +```plaintext +Minimum cost: 13 +Optimal assignment: +Worker 1 assigned to task 2 +Worker 2 assigned to task 3 +Worker 3 assigned to task 1 +Worker 4 assigned to task 4 +``` + +This assignment ensures that the total cost is minimized. + +## Limitations + +- **Matrix Size**: The algorithm is implemented for a square \( N \times N \) matrix. +- **Performance**: This implementation may not be optimized for very large matrices. Alternative, faster algorithms may be preferable for very large datasets. +