[77] | 1 | /* |
---|
| 2 | Copyright (C) 2006 Pedro Felzenszwalb |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #ifndef DISJOINT_SET |
---|
| 20 | #define DISJOINT_SET |
---|
| 21 | |
---|
| 22 | // disjoint-set forests using union-by-rank and path compression (sort of). |
---|
| 23 | |
---|
| 24 | typedef struct { |
---|
| 25 | int rank; |
---|
| 26 | int p; |
---|
| 27 | int size; |
---|
| 28 | } uni_elt; |
---|
| 29 | /* |
---|
| 30 | class universe { |
---|
| 31 | public: |
---|
| 32 | universe(int elements); |
---|
| 33 | ~universe(); |
---|
| 34 | int find(int x); |
---|
| 35 | void join(int x, int y); |
---|
| 36 | int size(int x) const { return elts[x].size; } |
---|
| 37 | int num_sets() const { return num; } |
---|
| 38 | |
---|
| 39 | private: |
---|
| 40 | uni_elt *elts; |
---|
| 41 | int num; |
---|
| 42 | }; |
---|
| 43 | */ |
---|
| 44 | typedef struct{ |
---|
| 45 | |
---|
| 46 | uni_elt *elts; |
---|
| 47 | int num; |
---|
| 48 | }universe; |
---|
| 49 | |
---|
| 50 | int universe_size(universe *self,int x) { return self->elts[x].size; } |
---|
| 51 | |
---|
| 52 | int universe_num_sets(universe *self) { return self->num; } |
---|
| 53 | universe * universe_construct(int elements) { |
---|
| 54 | universe *self = (universe *)malloc(sizeof(universe)); |
---|
| 55 | self->elts = (uni_elt *)malloc(elements*sizeof(uni_elt));//new uni_elt[elements]; |
---|
| 56 | self->num = elements; |
---|
| 57 | for (int i = 0; i < elements; i++) { |
---|
| 58 | self->elts[i].rank = 0; |
---|
| 59 | self->elts[i].size = 1; |
---|
| 60 | self->elts[i].p = i; |
---|
| 61 | } |
---|
| 62 | return self; |
---|
| 63 | } |
---|
| 64 | void universe_destruct(universe *self){ |
---|
| 65 | free(self->elts); |
---|
| 66 | } |
---|
| 67 | /* |
---|
| 68 | universe::~universe() { |
---|
| 69 | delete [] elts; |
---|
| 70 | } |
---|
| 71 | */ |
---|
| 72 | int universe_find(universe *self, int x){ |
---|
| 73 | int y = x; |
---|
| 74 | while (y != self->elts[y].p) |
---|
| 75 | y = self->elts[y].p; |
---|
| 76 | self->elts[x].p = y; |
---|
| 77 | return y; |
---|
| 78 | |
---|
| 79 | } |
---|
| 80 | /* |
---|
| 81 | int universe::find(int x) { |
---|
| 82 | int y = x; |
---|
| 83 | while (y != elts[y].p) |
---|
| 84 | y = elts[y].p; |
---|
| 85 | elts[x].p = y; |
---|
| 86 | return y; |
---|
| 87 | } |
---|
| 88 | */ |
---|
| 89 | |
---|
| 90 | void universe_join(universe *self,int x, int y){ |
---|
| 91 | if (self->elts[x].rank > self->elts[y].rank) { |
---|
| 92 | self->elts[y].p = x; |
---|
| 93 | self->elts[x].size += self->elts[y].size; |
---|
| 94 | } else { |
---|
| 95 | self->elts[x].p = y; |
---|
| 96 | self->elts[y].size += self->elts[x].size; |
---|
| 97 | if (self->elts[x].rank == self->elts[y].rank) |
---|
| 98 | self->elts[y].rank++; |
---|
| 99 | } |
---|
| 100 | self->num--; |
---|
| 101 | } |
---|
| 102 | /* |
---|
| 103 | void universe::join(int x, int y) { |
---|
| 104 | if (elts[x].rank > elts[y].rank) { |
---|
| 105 | elts[y].p = x; |
---|
| 106 | elts[x].size += elts[y].size; |
---|
| 107 | } else { |
---|
| 108 | elts[x].p = y; |
---|
| 109 | elts[y].size += elts[x].size; |
---|
| 110 | if (elts[x].rank == elts[y].rank) |
---|
| 111 | elts[y].rank++; |
---|
| 112 | } |
---|
| 113 | num--; |
---|
| 114 | } |
---|
| 115 | */ |
---|
| 116 | #endif |
---|