-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdisjoint_sets.c
115 lines (110 loc) · 2.2 KB
/
disjoint_sets.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include<stdio.h>
#include<stdlib.h>
void makeset();
void displayset();
int find(int x);
void Union(int x,int y);
void menu();
struct disjset
{
int parent[10];
int rank[10];
int n;
}dis;
void main()
{
int n,x,y;
printf("How many elements:");
scanf("%d",&dis.n);
makeset();
menu();
}
void makeset()
{
for(int i=0;i<dis.n;++i)
{
dis.parent[i]=i;
dis.rank[i]=0;
}
}
void displayset()
{
printf("\nParent Array:\n");
for(int i=0;i<dis.n;++i)
{
printf("%d ",dis.parent[i]);
}
printf("\nRank Array:\n");
for(int i=0;i<dis.n;++i)
printf("%d ",dis.rank[i]);
printf("\n");
}
int find(int x)
{
if(dis.parent[x]!=x)
dis.parent[x]=find(dis.parent[x]);
return dis.parent[x];
}
void Union(int x,int y)
{
int xset=find(x);
int yset=find(y);
if(xset==yset)
return;
if(dis.rank[xset]<dis.rank[yset])
{
dis.parent[xset]=yset;
dis.rank[xset]=-1;
}
else if(dis.rank[xset]>dis.rank[yset])
{
dis.parent[yset]=xset;
dis.rank[yset]=-1;
}
else
{
dis.parent[yset]=xset;
dis.rank[xset]=dis.rank[xset]+1;
dis.rank[yset]=-1;
}
}
void menu()
{
int ch,x,y;
printf("\n MENU \n");
printf("1.Union\n2.Find\n3.Display\n4.exit\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the first set:");
scanf("%d",&x);
printf("Enter the second set:");
scanf("%d",&y);
Union(x,y);
menu();
break;
case 2:
printf("Enter the first set:");
scanf("%d",&x);
printf("Enter the second set:");
scanf("%d",&y);
if(find(x)==find(y))
printf("connected components\n");
else
printf("Not connected components\n");
menu();
break;
case 3:
displayset();
menu();
break;
case 4:
exit(0);
default:
printf("Wrong Choice!");
menu();
break;
}
}