added comment and cvs id
[libfirm] / ir / adt / hungarian.h
1 /********************************************************************
2  ********************************************************************
3  **
4  ** libhungarian by Cyrill Stachniss, 2004
5  **
6  ** Added and adapted to libFirm by Christian Wuerdig, 2006
7  **
8  ** Solving the Minimum Assignment Problem using the
9  ** Hungarian Method.
10  **
11  ** ** This file may be freely copied and distributed! **
12  **
13  ** Parts of the used code was originally provided by the
14  ** "Stanford GraphGase", but I made changes to this code.
15  ** As asked by  the copyright node of the "Stanford GraphGase",
16  ** I hereby proclaim that this file are *NOT* part of the
17  ** "Stanford GraphGase" distrubition!
18  **
19  ** This file is distributed in the hope that it will be useful,
20  ** but WITHOUT ANY WARRANTY; without even the implied
21  ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
22  ** PURPOSE.
23  **
24  ********************************************************************
25  ********************************************************************/
26
27 /* $Id$ */
28
29 #ifndef _HUNGARIAN_H_
30 #define _HUNGARIAN_H_
31
32 #define HUNGARIAN_MODE_MINIMIZE_COST 0
33 #define HUNGARIAN_MODE_MAXIMIZE_UTIL 1
34
35 typedef struct _hungarian_problem_t hungarian_problem_t;
36
37 /**
38  * This method initialize the hungarian_problem structure and init
39  * the cost matrix (missing lines or columns are filled with 0).
40  *
41  * @param rows  Number of rows in the given matrix
42  * @param cols  Number of cols in the given matrix
43  * @param width Element width for matrix dumping
44  * @return The problem object.
45  */
46 hungarian_problem_t *hungarian_new(int rows, int cols, int width);
47
48 /**
49  * Adds an edge from left to right with some costs.
50  */
51 void hungarian_add(hungarian_problem_t *p, int left, int right, int cost);
52
53 /**
54 * Removes the edge from left to right.
55 */
56 void hungarian_remv(hungarian_problem_t *p, int left, int right);
57
58 /**
59  * Prepares the cost matrix, dependend on the given mode.
60  *
61  * @param p     The hungarian object
62  * @param mode  HUNGARIAN_MODE_MAXIMIZE_UTIL or HUNGARIAN_MODE_MINIMIZE_COST (default)
63  */
64 void hungarian_prepare_cost_matrix(hungarian_problem_t *p, int mode);
65
66 /**
67  * Destroys the hungarian object.
68  */
69 void hungarian_free(hungarian_problem_t *p);
70
71 /**
72  * This method computes the optimal assignment.
73  * @param p          The hungarian object
74  * @param assignment The final assignment
75  * @return Negative value if solution is invalid, 0 otherwise
76  */
77 int hungarian_solve(hungarian_problem_t *p, int *assignment);
78
79 /**
80  * Print the cost matrix.
81  * @param p The hungarian object
82  */
83 void hungarian_print_costmatrix(hungarian_problem_t *p);
84
85 #endif /* _HUNGARIAN_H_ */