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