-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayDisjointSet.c
More file actions
83 lines (76 loc) · 1.78 KB
/
arrayDisjointSet.c
File metadata and controls
83 lines (76 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <stdlib.h>
int* parent;
int* rank;
int FIND(int x){//retuens the representative of x
if(parent[x]!=x){
parent[x]= FIND(parent[x]);
}
return parent[x];
}
void UNION(int x,int y){//creates a union of the sets containing x and y
int xRep = FIND(x);
int yRep = FIND(y);
//printf("%d %d\n",xRep,yRep );
int Rep=yRep;
if(xRep!=yRep){//if the representatives are not equal
if(rank[xRep]<rank[yRep]){//if rank of y is greater than rank of x
parent[xRep]=yRep;
Rep=yRep;
}
else if (rank[xRep]>rank[yRep]){
parent[yRep]=xRep;
Rep=xRep;
}
else{
parent[xRep]=yRep;
Rep=yRep;
rank[yRep]++;//if the ranks are equal we increment the rank of any one
}
printf("representative for both is %d now\n",Rep );
}
else{
printf("They already have the same representative %d\n", Rep);
}
}
void main(){
int N,option=0,x,y;
printf("Enter N the number of disjoint elements. N elements will be created with the values 0,1,2...N-1 with initial ranks 0 \n");
scanf("%d",&N);
parent = (int*)malloc(sizeof(int)*N);
rank = (int*)malloc(sizeof(int)*N);
for(x=0;x<N;x++){
parent[x]=x;
rank[x]=0;
}
while(option!=3){
printf("1.Find an element's representative\n2.Make a union out of the sets containing two elements\n3.exit\n\n");
scanf("%d",&option);
if(option==1){
printf("Enter the element :");
scanf("%d",&x);
if(x<0 || x>=N){
printf("element not present\n");
}
else{
y=FIND(x);
printf("The representative is %d\n",y);
}
}
if(option==2){
printf("Enter the first element :");
scanf("%d",&x);
printf("Enter the second element :");
scanf("%d",&y);
if(x<0 || x>=N){
printf("%d not present\n",x);
break;
}
if(y<0 || y>=N){
printf("%d not present\n",y);
break;
}
UNION(x,y);
}
}
}