Fixed Win32 DLL support.
[libfirm] / include / libfirm / adt / hungarian.h
1 /********************************************************************
2  ********************************************************************
3  **
4  ** libhungarian by Cyrill Stachniss, 2004
5  **
6  ** Added to libFirm by Christian Wuerdig, 2006
7  ** Added several options for not-perfect matchings.
8  **
9  ** Solving the Minimum Assignment Problem using the
10  ** Hungarian Method.
11  **
12  ** ** This file may be freely copied and distributed! **
13  **
14  ** Parts of the used code was originally provided by the
15  ** "Stanford GraphGase", but I made changes to this code.
16  ** As asked by  the copyright node of the "Stanford GraphGase",
17  ** I hereby proclaim that this file are *NOT* part of the
18  ** "Stanford GraphGase" distribution!
19  **
20  ** This file is distributed in the hope that it will be useful,
21  ** but WITHOUT ANY WARRANTY; without even the implied
22  ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
23  ** PURPOSE.
24  **
25  ********************************************************************
26  ********************************************************************/
27
28 /**
29  * @file
30  * @brief   Solving the Minimum Assignment Problem using the Hungarian Method.
31  * @version $Id$
32  */
33 #ifndef FIRM_ADT_HUNGARIAN_H
34 #define FIRM_ADT_HUNGARIAN_H
35
36 #include "../begin.h"
37
38 #define HUNGARIAN_MODE_MINIMIZE_COST 0
39 #define HUNGARIAN_MODE_MAXIMIZE_UTIL 1
40
41 #define HUNGARIAN_MATCH_NORMAL  0
42 #define HUNGARIAN_MATCH_PERFECT 1
43
44 typedef struct _hungarian_problem_t hungarian_problem_t;
45
46 /**
47  * This method initialize the hungarian_problem structure and init
48  * the cost matrix (missing lines or columns are filled with 0).
49  *
50  * @param rows       Number of rows in the given matrix
51  * @param cols       Number of cols in the given matrix
52  * @param match_type The type of matching:
53  *                   HUNGARIAN_MATCH_PERFECT - every nodes matches another node
54  *                   HUNGARIAN_MATCH_NORMAL  - matchings of nodes having no edge getting removed
55  * @return The problem object.
56  */
57 FIRM_API hungarian_problem_t *hungarian_new(int rows, int cols, int match_type);
58
59 /**
60  * Adds an edge from left to right with some costs.
61  */
62 FIRM_API void hungarian_add(hungarian_problem_t *p, int left, int right, int cost);
63
64 /**
65  * Removes the edge from left to right.
66  */
67 FIRM_API void hungarian_remv(hungarian_problem_t *p, int left, int right);
68
69 /**
70  * Prepares the cost matrix dependent on the given mode.
71  *
72  * @param p     The hungarian object
73  * @param mode  HUNGARIAN_MODE_MAXIMIZE_UTIL or HUNGARIAN_MODE_MINIMIZE_COST (default)
74  */
75 FIRM_API void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode);
76
77 /**
78  * Destroys the hungarian object.
79  */
80 FIRM_API void hungarian_free(hungarian_problem_t *p);
81
82 /**
83  * This method computes the optimal assignment.
84  * @param p              The hungarian object
85  * @param assignment     The final assignment
86  * @param final_cost     The final costs
87  * @param cost_threshold Matchings with costs >= this limit will be removed (if limit > 0)
88  * @return 0 on success, negative number otherwise
89  */
90 FIRM_API int hungarian_solve(hungarian_problem_t *p, int *assignment, int *final_cost, int cost_threshold);
91
92 /**
93  * Print the cost matrix.
94  * @param p          The hungarian object
95  * @param cost_width The minimum field width of the costs
96  */
97 FIRM_API void hungarian_print_cost_matrix(hungarian_problem_t *p, int cost_width);
98
99 #include "../end.h"
100
101 #endif