From 7dceadab1c59c55f3f6f025c9a163ee177dcbf28 Mon Sep 17 00:00:00 2001 From: Sesar Hersisson Date: Tue, 1 Oct 2019 15:46:18 +0000 Subject: [PATCH] Added test case and explanation to unionFind.c --- misc/unionFind.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/misc/unionFind.c b/misc/unionFind.c index 8af9cc04..8f1c4d05 100644 --- a/misc/unionFind.c +++ b/misc/unionFind.c @@ -1,3 +1,5 @@ +#include + int p[1000000]; int find(int x) { @@ -17,6 +19,31 @@ void join(int x, int y) p[find(x)] = find(y); } -int main() { +int main() +{ + // Have all array indexes that you need to use refrence themselves + for (int i = 0; i < 10; i++) + { + p[i] = i; + } + // p = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + join(3, 5); + // Now 3 and 5 are groupped together, that is find(3) = find(5) + // p = {0, 1, 2, 5, 4, 5, 6, 7, 8, 9} + join(3, 8); + // Now 3, 5 and are groupped together, find(3) = find(5) = find(8) + // p = {0, 1, 2, 5, 4, 8, 6, 7, 8, 9} + join(0, 5); + if(find(0) == find(3)) + { + printf("0 and 3 are groupped together\n"); + } + printf("The array is now: "); + for(int i = 0; i < 10; i++) + { + printf("%d ", p[i]); + } + printf("\n"); + return 0; } \ No newline at end of file