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