C Everything
This is a C repository containing a curated set of generic data structures and algorithm.
disjoint_set.c
Go to the documentation of this file.
1 
5 #include "disjoint_set.h"
6 
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 
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;
31 
34 
35  return set;
36 }
37 
44 {
45  t_disjset *set = (t_disjset*)d;
46 
47  free_mem(set->subset);
48  free_mem(set);
49 }
50 
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 
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 
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 
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 }
void destroy_disjoint_set(t_gen d)
Destroy the instance of disjoint set data struct
Definition: disjoint_set.c:43
void disjoint_set_print(t_gen d)
Print the elem of the disjoint set
Definition: disjoint_set.c:123
t_gen create_disjoint_set(char *name, int size)
Create an instance of disjoint set data struct
Definition: disjoint_set.c:20
int disjoint_set_merge(t_gen d, int x, int y)
Merge the given two subsets
Definition: disjoint_set.c:95
void disjoint_set_make(t_gen d)
The MakeSet operation adds a new element
Definition: disjoint_set.c:56
int disjoint_set_find(t_gen d, int x)
Find the set the node belongs to the method defined follows path halving
Definition: disjoint_set.c:74
Contains declations of disjoint_set types, operations and structure.
#define free_mem(mem_addr)
Definition: memory_manager.h:9
#define get_mem(nmemb, size)
Definition: memory_manager.h:8
Disjoint set main struct definition.
Definition: disjoint_set.h:19
f_print print
routine to print elements in the disjoint set
Definition: disjoint_set.h:28
int size
Max size of elems stored in disjoint set.
Definition: disjoint_set.h:21
f_set2 merge
routine to merge to set
Definition: disjoint_set.h:26
char * name
Name of link list instance *‍/.
Definition: disjoint_set.h:20
f_destroy destroy
routine to destroy the instace of disjoint set
Definition: disjoint_set.h:29
t_dsetnode * subset
Pointer to N sets.
Definition: disjoint_set.h:22
f_set1 find
routine to find an elem in the set
Definition: disjoint_set.h:25
f_vgen make
routine to add an new elem to the set
Definition: disjoint_set.h:24
Disjoint set node definition.
Definition: disjoint_set.h:9
int size
size of the current set
Definition: disjoint_set.h:10
int parent
Parent idx of the set.
Definition: disjoint_set.h:11
void * t_gen
Base Data type used for all data structure and data elements.
Definition: typedefs.h:41