Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
union_find.c File Reference

Union find algorithm. More...

#include <stdio.h>
#include <stdlib.h>
Include dependency graph for union_find.c:

Macros

#define MAX_SIZE   1000
 maximum number of elements in the set
 

Functions

int find (int *p, int x)
 Find index of or value in an array. More...
 
void join (int *p, int x, int y)
 Function to join. More...
 
int main ()
 Main function.
 

Detailed Description

Union find algorithm.

Function Documentation

◆ find()

int find ( int *  p,
int  x 
)

Find index of or value in an array.

Parameters
[in,out]parray to search and update
xvalue to search
Returns
value at the index x
18 {
19  if (x >= MAX_SIZE)
20  {
21  fprintf(stderr, "Out-of bounds value\n");
22  exit(EXIT_FAILURE);
23  }
24 
25  if (p[x] == x)
26  {
27  return x;
28  }
29  else
30  {
31  p[x] = find(p, p[x]);
32  return p[x];
33  }
34 }

◆ join()

void join ( int *  p,
int  x,
int  y 
)

Function to join.

Parameters
[in,out]parray to join in
xvalue or index to join to
yvalue or index to join from
42 { p[find(p, x)] = find(p, y); }
Here is the call graph for this function:
MAX_SIZE
#define MAX_SIZE
maximum number of elements in the set
Definition: union_find.c:8
find
int find(int *p, int x)
Find index of or value in an array.
Definition: union_find.c:17