unionfind.h
Go to the documentation of this file.
1 /* Copyright (C) 2013-2016, The Regents of The University of Michigan.
2 All rights reserved.
3 This software was developed in the APRIL Robotics Lab under the
4 direction of Edwin Olson, ebolson@umich.edu. This software may be
5 available under alternative licensing terms; contact the address above.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice, this
9  list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation
12  and/or other materials provided with the distribution.
13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 The views and conclusions contained in the software and documentation are those
24 of the authors and should not be interpreted as representing official policies,
25 either expressed or implied, of the Regents of The University of Michigan.
26 */
27 
28 #pragma once
29 
30 #include <string.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 
34 typedef struct unionfind unionfind_t;
35 
36 struct unionfind
37 {
38  uint32_t maxid;
39 
40  // Parent node for each. Initialized to 0xffffffff
41  uint32_t *parent;
42 
43  // The size of the tree excluding the root
44  uint32_t *size;
45 };
46 
47 static inline unionfind_t *unionfind_create(uint32_t maxid)
48 {
49  unionfind_t *uf = (unionfind_t*) calloc(1, sizeof(unionfind_t));
50  uf->maxid = maxid;
51  uf->parent = (uint32_t *) malloc((maxid+1) * sizeof(uint32_t) * 2);
52  memset(uf->parent, 0xff, (maxid+1) * sizeof(uint32_t));
53  uf->size = uf->parent + (maxid+1);
54  memset(uf->size, 0, (maxid+1) * sizeof(uint32_t));
55  return uf;
56 }
57 
58 static inline void unionfind_destroy(unionfind_t *uf)
59 {
60  free(uf->parent);
61  free(uf);
62 }
63 
64 /*
65 static inline uint32_t unionfind_get_representative(unionfind_t *uf, uint32_t id)
66 {
67  // base case: a node is its own parent
68  if (uf->parent[id] == id)
69  return id;
70 
71  // otherwise, recurse
72  uint32_t root = unionfind_get_representative(uf, uf->parent[id]);
73 
74  // short circuit the path. [XXX This write prevents tail recursion]
75  uf->parent[id] = root;
76 
77  return root;
78 }
79 */
80 
81 // this one seems to be every-so-slightly faster than the recursive
82 // version above.
83 static inline uint32_t unionfind_get_representative(unionfind_t *uf, uint32_t id)
84 {
85  uint32_t root = uf->parent[id];
86  // unititialized node, so set to self
87  if (root == 0xffffffff) {
88  uf->parent[id] = id;
89  return id;
90  }
91 
92  // chase down the root
93  while (uf->parent[root] != root) {
94  root = uf->parent[root];
95  }
96 
97  // go back and collapse the tree.
98  while (uf->parent[id] != root) {
99  uint32_t tmp = uf->parent[id];
100  uf->parent[id] = root;
101  id = tmp;
102  }
103 
104  return root;
105 }
106 
107 static inline uint32_t unionfind_get_set_size(unionfind_t *uf, uint32_t id)
108 {
109  uint32_t repid = unionfind_get_representative(uf, id);
110  return uf->size[repid] + 1;
111 }
112 
113 static inline uint32_t unionfind_connect(unionfind_t *uf, uint32_t aid, uint32_t bid)
114 {
115  uint32_t aroot = unionfind_get_representative(uf, aid);
116  uint32_t broot = unionfind_get_representative(uf, bid);
117 
118  if (aroot == broot)
119  return aroot;
120 
121  // we don't perform "union by rank", but we perform a similar
122  // operation (but probably without the same asymptotic guarantee):
123  // We join trees based on the number of *elements* (as opposed to
124  // rank) contained within each tree. I.e., we use size as a proxy
125  // for rank. In my testing, it's often *faster* to use size than
126  // rank, perhaps because the rank of the tree isn't that critical
127  // if there are very few nodes in it.
128  uint32_t asize = uf->size[aroot] + 1;
129  uint32_t bsize = uf->size[broot] + 1;
130 
131  // optimization idea: We could shortcut some or all of the tree
132  // that is grafted onto the other tree. Pro: those nodes were just
133  // read and so are probably in cache. Con: it might end up being
134  // wasted effort -- the tree might be grafted onto another tree in
135  // a moment!
136  if (asize > bsize) {
137  uf->parent[broot] = aroot;
138  uf->size[aroot] += bsize;
139  return aroot;
140  } else {
141  uf->parent[aroot] = broot;
142  uf->size[broot] += asize;
143  return broot;
144  }
145 }
unionfind::size
uint32_t * size
Definition: unionfind.h:44
unionfind_get_set_size
static uint32_t unionfind_get_set_size(unionfind_t *uf, uint32_t id)
Definition: unionfind.h:107
unionfind
Definition: unionfind.h:36
unionfind::parent
uint32_t * parent
Definition: unionfind.h:41
unionfind::maxid
uint32_t maxid
Definition: unionfind.h:38
unionfind_connect
static uint32_t unionfind_connect(unionfind_t *uf, uint32_t aid, uint32_t bid)
Definition: unionfind.h:113
unionfind_create
static unionfind_t * unionfind_create(uint32_t maxid)
Definition: unionfind.h:47
unionfind_get_representative
static uint32_t unionfind_get_representative(unionfind_t *uf, uint32_t id)
Definition: unionfind.h:83
unionfind_destroy
static void unionfind_destroy(unionfind_t *uf)
Definition: unionfind.h:58


apriltag
Author(s): Edwin Olson , Max Krogius
autogenerated on Sun Apr 20 2025 02:08:47