2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Specialized implementation for perfect bipartite matching.
23 * @author Sebastian Hack
34 #include "bipartite.h"
42 bipartite_t *bipartite_new(int n_left, int n_right)
44 bipartite_t *gr = XMALLOCFZ(bipartite_t, adj, n_left);
48 gr->n_right = n_right;
50 for(i = 0; i < n_left; ++i)
51 gr->adj[i] = bitset_malloc(n_right);
56 void bipartite_free(bipartite_t *gr)
59 for(i = 0; i < gr->n_left; ++i)
60 bitset_free(gr->adj[i]);
64 void bipartite_add(bipartite_t *gr, int i, int j)
66 assert(i < gr->n_left && j < gr->n_right);
67 bitset_set(gr->adj[i], j);
70 void bipartite_remv(bipartite_t *gr, int i, int j)
72 assert(i < gr->n_left && j < gr->n_right);
73 bitset_clear(gr->adj[i], j);
76 int bipartite_adj(const bipartite_t *gr, int i, int j)
78 assert(i < gr->n_left && j < gr->n_right);
79 return bitset_is_set(gr->adj[i], j);
82 static int apply_alternating_path(const bipartite_t *gr, int *matching,
83 bitset_t *matched_left, bitset_t *matched_right)
86 int done_something = 0;
87 bitset_t *tmp = bitset_alloca(gr->n_right);
89 for(left = 0; left < gr->n_left; ++left) {
90 bitset_t *left_adj = gr->adj[left];
93 bitset_copy(tmp, left_adj);
95 if(matching[left] >= 0) {
96 int old_right = matching[left];
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))
103 bitset_andnot(tmp, matched_right);
104 right = bitset_next_set(tmp, 0);
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
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))
116 /* If no such node can be found, exit. */
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);
129 /* We have to create a new single edge */
131 assert(!bitset_is_set(matched_left, left));
133 bitset_andnot(tmp, matched_right);
134 if(bitset_popcnt(tmp) == 0)
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);
146 return done_something;
149 void bipartite_matching(const bipartite_t *gr, int *matching)
151 bitset_t *matched_left = bitset_alloca(gr->n_left);
152 bitset_t *matched_right = bitset_alloca(gr->n_right);
154 memset(matching, -1, gr->n_left * sizeof(int));
155 while(apply_alternating_path(gr, matching, matched_left, matched_right));
158 void bipartite_dump_f(FILE *f, const bipartite_t *gr)
162 for(i = 0; i < gr->n_left; ++i) {
163 fprintf(f, "%d: ", i);
164 bitset_fprint(f, gr->adj[i]);
169 void bipartite_dump(const char *name, const bipartite_t *gr) {
170 FILE *f = fopen(name, "w");
173 bipartite_dump_f(f, gr);