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