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