Union find algorithm.
More...
#include <stdio.h>
#include <stdlib.h>
|
#define | MAX_SIZE 1000 |
| maximum number of elements in the set
|
|
◆ find()
int find |
( |
int * |
p, |
|
|
int |
x |
|
) |
| |
Find index of or value in an array.
- Parameters
-
[in,out] | p | array to search and update |
| x | value to search |
- Returns
- value at the index
x
21 fprintf(stderr,
"Out-of bounds value\n");
#define MAX_SIZE
maximum number of elements in the set
Definition: union_find.c:8
int find(int *p, int x)
Find index of or value in an array.
Definition: union_find.c:17
◆ join()
void join |
( |
int * |
p, |
|
|
int |
x, |
|
|
int |
y |
|
) |
| |
Function to join.
- Parameters
-
[in,out] | p | array to join in |
| x | value or index to join to |
| y | value or index to join from |
◆ main()
Main function.
50 for (
int i = 0; i < 10; i++)
56 join(union_set, 3, 5);
57 printf(
"The array is now: ");
58 for (
int i = 0; i < 10; i++)
60 printf(
"%d ", union_set[i]);
66 join(union_set, 3, 8);
67 printf(
"The array is now: ");
68 for (
int i = 0; i < 10; i++)
70 printf(
"%d ", union_set[i]);
76 join(union_set, 0, 5);
77 if (
find(union_set, 0) ==
find(union_set, 3))
79 printf(
"0 and 3 are groupped together\n");
81 printf(
"The array is now: ");
82 for (
int i = 0; i < 10; i++)
84 printf(
"%d ", union_set[i]);
void join(int *p, int x, int y)
Function to join.
Definition: union_find.c:42