Download code

Jump to: navigation, search

Back to Disjoint_set_data_structure_(C)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

union_find_example.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Disjoint_set_data_structure_(C)?oldid=18651
14 */
15 
16 #include <assert.h>
17 #include "union_find.h"
18 
19 int main() {
20     int i1=1, i2=2, i3=3;
21     forest_node *s1=MakeSet(&i1), *s2=MakeSet(&i2), *s3=MakeSet(&i3);
22     assert(Find(s1) == s1);
23     Union(s1, s2);
24     assert(Find(s1) == Find(s2));
25     assert(Find(s1) != Find(s3));
26     Union(s2, s3);
27     assert(Find(s1) == Find(s2) &&
28            Find(s1) == Find(s3));
29     return 0;
30 }


hijacker
hijacker
hijacker
hijacker

union_find.h

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Disjoint_set_data_structure_(C)?oldid=18651
14 */
15 
16 #ifndef _UNION_FIND_H_
17 #define _UNION_FIND_H_
18 
19 typedef struct forest_node_t {
20     void* value;
21     struct forest_node_t* parent;
22     int rank;
23 } forest_node;
24 forest_node* MakeSet(void* value);
25 void Union(forest_node* node1, forest_node* node2);
26 forest_node* Find(forest_node* node);
27 
28 #endif /* #ifndef _UNION_FIND_H_ */
29 


hijacker
hijacker
hijacker
hijacker

union_find.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Disjoint_set_data_structure_(C)?oldid=18651
14 */
15 
16 #include <stdlib.h>
17 #include "union_find.h"
18 
19 forest_node* MakeSet(void* value) {
20     forest_node* node = malloc(sizeof(forest_node));
21     node->value = value;
22     node->parent = NULL;
23     node->rank = 0;
24     return node;
25 }
26 void Union(forest_node* node1, forest_node* node2) {
27     if (node1->rank > node2->rank) {
28         node2->parent = node1;
29     } else if (node2->rank > node1->rank) {
30         node1->parent = node2;
31     } else { /* they are equal */
32         node2->parent = node1;
33         node1->rank++;
34     }
35 }
36 forest_node* Find(forest_node* node) {
37     forest_node* temp;
38     /* Find the root */
39     forest_node* root = node;
40     while (root->parent != NULL) {
41         root = root->parent;
42     }
43     /* Update the parent pointers */
44     while (node->parent != NULL) {
45         temp = node->parent;
46         node->parent = root;
47         node = temp;
48     }
49     return root;
50 }


hijacker
hijacker
hijacker
hijacker

union_find_simple.c

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Disjoint_set_data_structure_(C)?oldid=18651
14 */
15 
16 #include <stdlib.h>
17 #include <assert.h>
18 
19 typedef struct forest_node_simple_t {
20     void* value;
21     struct forest_node_simple_t* parent;
22 } forest_node_simple;
23 
24 forest_node_simple* MakeSetSimple(void* value) {
25     forest_node_simple* node = malloc(sizeof(forest_node_simple));
26     node->value = value;
27     node->parent = NULL;
28     return node;
29 }
30 forest_node_simple* FindSimple(forest_node_simple* node) {
31     while (node->parent != NULL) {
32         node = node->parent;
33     }
34     return node;
35 }
36 /* Given the root elements of two trees, merge the trees into one tree */
37 void UnionSimple(forest_node_simple* node1, forest_node_simple* node2) {
38     node2->parent = node1; /* or node1->parent = node2; */
39 }
40 
41 int main() {
42     int i1=1, i2=2, i3=3;
43     forest_node_simple* s1=MakeSetSimple(&i1);
44     forest_node_simple* s2=MakeSetSimple(&i2);
45     forest_node_simple* s3=MakeSetSimple(&i3);
46 
47     assert(FindSimple(s1) == s1);
48     UnionSimple(s1, s2);
49     assert(FindSimple(s1) == FindSimple(s2));
50     assert(FindSimple(s1) != FindSimple(s3));
51     UnionSimple(s2, s3);
52     assert(FindSimple(s1) == FindSimple(s2) &&
53            FindSimple(s1) == FindSimple(s3));
54 
55     return 0;
56 }


hijacker
hijacker
hijacker
hijacker