Use foreach_pset().
[libfirm] / ir / adt / bipartite.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Specialized implementation for perfect bipartite matching.
23  * @author  Sebastian Hack
24  */
25 #include "config.h"
26
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include "bitset.h"
31 #include "bipartite.h"
32 #include "xmalloc.h"
33
34 struct bipartite_t {
35         int n_left, n_right;
36         bitset_t *adj[1];
37 };
38
39 bipartite_t *bipartite_new(int n_left, int n_right)
40 {
41         bipartite_t *gr = XMALLOCFZ(bipartite_t, adj, n_left);
42         int i;
43
44         gr->n_left = n_left;
45         gr->n_right = n_right;
46
47         for (i = 0; i < n_left; ++i)
48                 gr->adj[i] = bitset_malloc(n_right);
49
50         return gr;
51 }
52
53 void bipartite_free(bipartite_t *gr)
54 {
55         int i;
56         for (i = 0; i < gr->n_left; ++i)
57                 bitset_free(gr->adj[i]);
58         free(gr);
59 }
60
61 void bipartite_add(bipartite_t *gr, int i, int j)
62 {
63         assert(i < gr->n_left && j < gr->n_right);
64         bitset_set(gr->adj[i], j);
65 }
66
67 void bipartite_remv(bipartite_t *gr, int i, int j)
68 {
69         assert(i < gr->n_left && j < gr->n_right);
70         bitset_clear(gr->adj[i], j);
71 }
72
73 int bipartite_adj(const bipartite_t *gr, int i, int j)
74 {
75         assert(i < gr->n_left && j < gr->n_right);
76         return bitset_is_set(gr->adj[i], j);
77 }
78
79 static int apply_alternating_path(const bipartite_t *gr, int *matching,
80                 bitset_t *matched_left, bitset_t *matched_right)
81 {
82         int left, right;
83         int done_something = 0;
84         bitset_t *tmp = bitset_alloca(gr->n_right);
85
86         for (left = 0; left < gr->n_left; ++left) {
87                 bitset_t *left_adj = gr->adj[left];
88                 int i;
89
90                 bitset_copy(tmp, left_adj);
91
92                 if (matching[left] >= 0) {
93                         int old_right = matching[left];
94
95                         /* Check of all neighbors of the left node are already matched.
96                          * We cannot improve this edge then. */
97                         if (bitset_contains(left_adj, matched_right))
98                                 continue;
99
100                         bitset_andnot(tmp, matched_right);
101                         right = bitset_next_set(tmp, 0);
102
103                         assert(right != -1);
104
105                         /*
106                                 We have to find another left node which has the old right one as a neighbor.
107                                 This node must not be part of a matching
108                         */
109                         for (i = 0; i < gr->n_left; ++i)
110                                 if (i != left && bitset_is_set(gr->adj[i], old_right) && !bitset_is_set(matched_left, i))
111                                         break;
112
113                         /* If no such node can be found, exit. */
114                         if (i >= gr->n_left)
115                                 continue;
116
117                         /* Else, we can improve this edge. */
118                         matching[left] = right;
119                         matching[i] = old_right;
120                         bitset_set(matched_left, i);
121                         bitset_set(matched_right, right);
122                         done_something = 1;
123                 }
124
125
126                 /* We have to create a new single edge */
127                 else {
128                         assert(!bitset_is_set(matched_left, left));
129
130                         bitset_andnot(tmp, matched_right);
131                         if (bitset_is_empty(tmp))
132                                 continue;
133
134                         right = bitset_next_set(tmp, 0);
135                         assert(!bitset_is_set(matched_right, right));
136                         matching[left] = right;
137                         bitset_set(matched_left, left);
138                         bitset_set(matched_right, right);
139                         done_something = 1;
140                 }
141         }
142
143         return done_something;
144 }
145
146 void bipartite_matching(const bipartite_t *gr, int *matching)
147 {
148         bitset_t *matched_left = bitset_alloca(gr->n_left);
149         bitset_t *matched_right = bitset_alloca(gr->n_right);
150
151         memset(matching, -1, gr->n_left * sizeof(int));
152         while (apply_alternating_path(gr, matching, matched_left, matched_right)) {
153         }
154 }
155
156 void bipartite_dump_f(FILE *f, const bipartite_t *gr)
157 {
158         int i;
159
160         for (i = 0; i < gr->n_left; ++i) {
161                 fprintf(f, "%d: ", i);
162                 bitset_fprint(f, gr->adj[i]);
163                 fprintf(f, "\n");
164         }
165 }
166
167 void bipartite_dump(const char *name, const bipartite_t *gr)
168 {
169         FILE *f = fopen(name, "w");
170
171         if (f) {
172                 bipartite_dump_f(f, gr);
173                 fclose(f);
174         }
175 }