update copyright message
[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  * @cvs-id $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <stdio.h>
31 #include <assert.h>
32
33 #include "bitset.h"
34 #include "bipartite.h"
35 #include "xmalloc.h"
36
37 struct _bipartite_t {
38         int n_left, n_right;
39         bitset_t *adj[1];
40 };
41
42 bipartite_t *bipartite_new(int n_left, int n_right)
43 {
44         int i, size;
45         bipartite_t *gr;
46
47         size = n_left > 0 ? n_left - 1 : 0;
48         gr = xmalloc(sizeof(*gr) + size * sizeof(void *));
49         memset(gr, 0, sizeof(*gr));
50
51         gr->n_left = n_left;
52         gr->n_right = n_right;
53
54         for(i = 0; i < n_left; ++i)
55                 gr->adj[i] = bitset_malloc(n_right);
56
57         return gr;
58 }
59
60 void bipartite_free(bipartite_t *gr)
61 {
62         int i;
63         for(i = 0; i < gr->n_left; ++i)
64                 bitset_free(gr->adj[i]);
65         free(gr);
66 }
67
68 void bipartite_add(bipartite_t *gr, int i, int j)
69 {
70         assert(i < gr->n_left && j < gr->n_right);
71         bitset_set(gr->adj[i], j);
72 }
73
74 void bipartite_remv(bipartite_t *gr, int i, int j)
75 {
76         assert(i < gr->n_left && j < gr->n_right);
77         bitset_clear(gr->adj[i], j);
78 }
79
80 int bipartite_adj(const bipartite_t *gr, int i, int j)
81 {
82         assert(i < gr->n_left && j < gr->n_right);
83         return bitset_is_set(gr->adj[i], j);
84 }
85
86 static int apply_alternating_path(const bipartite_t *gr, int *matching,
87                 bitset_t *matched_left, bitset_t *matched_right)
88 {
89         int left, right;
90         int done_something = 0;
91         bitset_t *tmp = bitset_alloca(gr->n_right);
92
93         for(left = 0; left < gr->n_left; ++left) {
94                 bitset_t *left_adj = gr->adj[left];
95                 int i;
96
97                 bitset_copy(tmp, left_adj);
98
99                 if(matching[left] >= 0) {
100                         int old_right = matching[left];
101
102                         /* Check of all neighbors of the left node are already matched.
103                          * We cannot improve this edge then. */
104                         if(bitset_contains(left_adj, matched_right))
105                                 continue;
106
107                         bitset_andnot(tmp, matched_right);
108                         right = bitset_next_set(tmp, 0);
109
110                         assert(right != -1);
111
112                         /*
113                                 We have to find another left node which has the old right one as a neighbor.
114                                 This node must not be part of a matching
115                         */
116                         for(i = 0; i < gr->n_left; ++i)
117                                 if(i != left && bitset_is_set(gr->adj[i], old_right) && !bitset_is_set(matched_left, i))
118                                         break;
119
120                         /* If no such node can be found, exit. */
121                         if(i >= gr->n_left)
122                                 continue;
123
124                         /* Else, we can improve this edge. */
125                         matching[left] = right;
126                         matching[i] = old_right;
127                         bitset_set(matched_left, i);
128                         bitset_set(matched_right, right);
129                         done_something = 1;
130                 }
131
132
133                 /* We have to create a new single edge */
134                 else {
135                         assert(!bitset_is_set(matched_left, left));
136
137                         bitset_andnot(tmp, matched_right);
138                         if(bitset_popcnt(tmp) == 0)
139                                 continue;
140
141                         right = bitset_next_set(tmp, 0);
142                         assert(!bitset_is_set(matched_right, right));
143                         matching[left] = right;
144                         bitset_set(matched_left, left);
145                         bitset_set(matched_right, right);
146                         done_something = 1;
147                 }
148         }
149
150         return done_something;
151 }
152
153 void bipartite_matching(const bipartite_t *gr, int *matching)
154 {
155         bitset_t *matched_left = bitset_alloca(gr->n_left);
156         bitset_t *matched_right = bitset_alloca(gr->n_right);
157
158         memset(matching, -1, gr->n_left * sizeof(int));
159         while(apply_alternating_path(gr, matching, matched_left, matched_right));
160 }
161
162 void bipartite_dump_f(FILE *f, const bipartite_t *gr)
163 {
164         int i;
165
166         for(i = 0; i < gr->n_left; ++i) {
167                 fprintf(f, "%d: ", i);
168                 bitset_fprint(f, gr->adj[i]);
169                 fprintf(f, "\n");
170         }
171 }
172
173 void bipartite_dump(const char *name, const bipartite_t *gr) {
174         FILE *f = fopen(name, "w");
175
176         if (f) {
177                 bipartite_dump_f(f, gr);
178                 fclose(f);
179         }
180 }