add documentation make target, fix docu bugs
[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  */
32 #ifndef FIRM_ADT_HUNGARIAN_H
33 #define FIRM_ADT_HUNGARIAN_H
34
35 #include "../begin.h"
36
37 typedef enum match_type_t {
38         HUNGARIAN_MATCH_NORMAL,   /**< ever nodes matches another node */
39         HUNGARIAN_MATCH_PERFECT   /**< matchings of nodes having no edge getting
40                                        removed */
41 } match_type_t;
42
43 typedef enum hungarian_mode_t {
44         HUNGARIAN_MODE_MINIMIZE_COST,
45         HUNGARIAN_MODE_MAXIMIZE_UTIL
46 } hungarian_mode_t;
47
48 typedef struct hungarian_problem_t hungarian_problem_t;
49
50 /**
51  * This method initialize the hungarian_problem structure and init
52  * the cost matrix (missing lines or columns are filled with 0).
53  *
54  * @param num_rows   Number of rows in the given matrix
55  * @param num_cols   Number of cols in the given matrix
56  * @param match_type The type of matching
57  * @return The problem object.
58  */
59 FIRM_API hungarian_problem_t *hungarian_new(unsigned num_rows,
60                                             unsigned num_cols,
61                                             match_type_t match_type);
62
63 /**
64  * Adds an edge from left to right with some costs.
65  */
66 FIRM_API void hungarian_add(hungarian_problem_t *p, unsigned left,
67                             unsigned right, unsigned cost);
68
69 /**
70  * Removes the edge from left to right.
71  */
72 FIRM_API void hungarian_remove(hungarian_problem_t *p, unsigned left,
73                                unsigned right);
74
75 /**
76  * Prepares the cost matrix dependent on the given mode.
77  *
78  * @param p     The hungarian object
79  * @param mode  specify whether to minimize or maximize the costs
80  */
81 FIRM_API void hungarian_prepare_cost_matrix(hungarian_problem_t *p,
82                                             hungarian_mode_t mode);
83
84 /**
85  * Destroys the hungarian object.
86  */
87 FIRM_API void hungarian_free(hungarian_problem_t *p);
88
89 /**
90  * This method computes the optimal assignment.
91  * @param p              The hungarian object
92  * @param assignment     The final assignment
93  * @param final_cost     The final costs
94  * @param cost_threshold Matchings with costs >= this limit will be removed (if limit > 0)
95  * @return 0 on success, negative number otherwise
96  */
97 FIRM_API int hungarian_solve(hungarian_problem_t *p, unsigned *assignment,
98                              unsigned *final_cost, unsigned cost_threshold);
99
100 /**
101  * Print the cost matrix.
102  * @param p          The hungarian object
103  * @param cost_width The minimum field width of the costs
104  */
105 FIRM_API void hungarian_print_cost_matrix(hungarian_problem_t *p,
106                                           int cost_width);
107
108 #include "../end.h"
109
110 #endif