Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Hungarian Algorithm Implementation in C #1666

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

Bhupendrakumar20
Copy link
Contributor

issue solved #1569

Pull Request: Add Hungarian Algorithm Implementation in C

Description

This PR introduces an implementation of the Hungarian Algorithm (also known as the Kuhn-Munkres algorithm) in C to solve the assignment problem. The assignment problem is widely used in optimization, scheduling, and resource allocation, aiming to minimize the total cost of assigning tasks to workers, where each worker is assigned exactly one task, and each task is completed by only one worker.

Implementation Details

  • Matrix Representation: The cost matrix is a 4x4 matrix where each element represents the cost of assigning a particular task to a specific worker.
  • Algorithm Components:
    • u and v: Dual variables used to manage potentials for optimal assignment.
    • p and way: Arrays used for tracking the optimal path and backtracking in the matching process.
    • minv and used: Arrays to manage minimum values and visited status during the assignment process.

Code Structure

  1. hungarian_algorithm: Main function that implements the Hungarian Algorithm using matrix reduction and optimization techniques.
  2. min: Helper function to find the minimum of two values.
  3. main: Initializes the cost matrix and invokes the hungarian_algorithm function, which calculates and prints the minimum cost and the optimal assignment.

Example Output

Using the predefined 4x4 cost matrix:

9  2  7  8
6  4  3  7
5  8  1  8
7  6  9  4

Sample program output:

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 result provides an assignment of workers to tasks that minimizes the overall cost.

Testing

  • The code was compiled and tested using a C compiler (GCC).
  • The current implementation uses a 4x4 cost matrix, but this can be adjusted for different problem sizes by modifying the N constant and cost matrix.

Future Improvements

  • Extend the code to support dynamically sized matrices.
  • Add error handling for cases where no valid assignment exists.

@Bhupendrakumar20
Copy link
Contributor Author

@pankaj-bind kindly review the issue and kindly merged it possibly with labels

@pankaj-bind pankaj-bind merged commit 4a0d27e into AlgoGenesis:main Nov 5, 2024
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants