|
| 1 | +/*! @file disjoint_set.c |
| 2 | + @brief |
| 3 | + Contains definitions of routines supported by disjoint_set |
| 4 | +*/ |
| 5 | +#include "disjoint_set.h" |
| 6 | + |
| 7 | +void destroy_disjoint_set(t_gen d); |
| 8 | +void disjoint_set_make (t_gen d); |
| 9 | +int disjoint_set_find(t_gen d, int x); |
| 10 | +int disjoint_set_merge (t_gen d, int x, int y); |
| 11 | +void disjoint_set_print (t_gen d); |
| 12 | + |
| 13 | + |
| 14 | +/*! @brief |
| 15 | + * Create an instance of disjoint set data struct |
| 16 | + * @param name - Name of disjoint set instance |
| 17 | + * @param size - size of the disjoin set |
| 18 | + * @return - Pointer to instance of disjoint set |
| 19 | + * */ |
| 20 | +t_gen create_disjoint_set(char *name, int size) |
| 21 | +{ |
| 22 | + t_disjset *set = get_mem(1, sizeof(t_disjset)); |
| 23 | + |
| 24 | + set->name = name; |
| 25 | + set->size = size; |
| 26 | + set->subset = get_mem(size, sizeof(t_dsetnode)); |
| 27 | + |
| 28 | + set->make = disjoint_set_make; |
| 29 | + set->find = disjoint_set_find; |
| 30 | + set->merge = disjoint_set_merge; |
| 31 | + |
| 32 | + set->print = disjoint_set_print; |
| 33 | + set->destroy = destroy_disjoint_set; |
| 34 | + |
| 35 | + return set; |
| 36 | +} |
| 37 | + |
| 38 | +/*! @brief |
| 39 | + * Destroy the instance of disjoint set data struct |
| 40 | + * @param d - Pointer to instance of disjoint set |
| 41 | + * @return - NA |
| 42 | + * */ |
| 43 | +void destroy_disjoint_set(t_gen d) |
| 44 | +{ |
| 45 | + t_disjset *set = (t_disjset*)d; |
| 46 | + |
| 47 | + free_mem(set->subset); |
| 48 | + free_mem(set); |
| 49 | +} |
| 50 | + |
| 51 | +/*! @brief |
| 52 | + * The MakeSet operation adds a new element |
| 53 | + * @param d - Pointer to instance of disjoint set |
| 54 | + * @return - NA |
| 55 | + * */ |
| 56 | +void disjoint_set_make (t_gen d) |
| 57 | +{ |
| 58 | + t_disjset *set = (t_disjset*)d; |
| 59 | + |
| 60 | + for(int i = 0; i < set->size; i++) { |
| 61 | + set->subset[i].size = 1; |
| 62 | + set->subset[i].parent = i; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +/*! @brief |
| 67 | + * Find the set the node belongs to |
| 68 | + * the method defined follows path halving |
| 69 | + * @see https://en.wikipedia.org/wiki/Disjoint-set_data_structure |
| 70 | + * @param d - Pointer to instance of disjoint set |
| 71 | + * @param x - idx of node |
| 72 | + * @return - return the root element of the disjoint set |
| 73 | + * */ |
| 74 | +int disjoint_set_find(t_gen d, int x) |
| 75 | +{ |
| 76 | + t_disjset *set = (t_disjset*)d; |
| 77 | + int parent = set->subset[x].parent; |
| 78 | + while (parent != x) { |
| 79 | + parent = set->subset[x].parent; |
| 80 | + set->subset[x].parent = set->subset[parent].parent; |
| 81 | + x = set->subset[x].parent; |
| 82 | + } |
| 83 | + |
| 84 | + return x; |
| 85 | +} |
| 86 | + |
| 87 | +/*! @brief |
| 88 | + * Merge the given two subsets |
| 89 | + * @see https://en.wikipedia.org/wiki/Disjoint-set_data_structure |
| 90 | + * @param d - Pointer to instance of disjoint set |
| 91 | + * @param x - idx of first node |
| 92 | + * @param y - idx of second node |
| 93 | + * @return - if nodes already merged return -1 else 0 on merge |
| 94 | + * */ |
| 95 | +int disjoint_set_merge (t_gen d, int x, int y) |
| 96 | +{ |
| 97 | + t_disjset *set = (t_disjset*)d; |
| 98 | + |
| 99 | + x = set->find(set, x); |
| 100 | + y = set->find(set, y); |
| 101 | + |
| 102 | + if (x == y) { |
| 103 | + return -1; |
| 104 | + } |
| 105 | + |
| 106 | + if (set->subset[x].size < set->subset[y].size) { |
| 107 | + set->subset[x].parent = y; |
| 108 | + set->subset[x].size += set->subset[y].size; |
| 109 | + } |
| 110 | + else { |
| 111 | + set->subset[y].parent = x; |
| 112 | + set->subset[y].size += set->subset[x].size; |
| 113 | + } |
| 114 | + |
| 115 | + return 0; |
| 116 | +} |
| 117 | + |
| 118 | +/*! @brief |
| 119 | + * Print the elem of the disjoint set |
| 120 | + * @param d - Pointer to instance of link list |
| 121 | + * @return - NA |
| 122 | + * */ |
| 123 | +void disjoint_set_print (t_gen d) |
| 124 | +{ |
| 125 | + t_disjset *set = (t_disjset*)d; |
| 126 | + |
| 127 | + |
| 128 | + for(int i = 0; i < set->size; i++) { |
| 129 | + printf("[%d: %d %d] ",i, set->subset[i].parent, |
| 130 | + set->subset[i].size); |
| 131 | + } |
| 132 | + printf("\n"); |
| 133 | +} |
0 commit comments