-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunion.c
More file actions
195 lines (157 loc) · 3.18 KB
/
union.c
File metadata and controls
195 lines (157 loc) · 3.18 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# include<stdio.h>
# include<malloc.h>
struct node
{ int element,count,parent;
char setname[10];
}*ptr[50];
int root[2],k=-1;
void readset()
{ int n,i;
struct node *temp;
int temparray[20];
temp=(struct node *)malloc(sizeof(struct node));
temp->parent=-1;
printf("\nEnter the set name:");
scanf("%s",temp->setname);
printf("\nEnter the no. of elements in the set:");
scanf("%d",&n);
temp->count=n;
printf("\nEnter the root element:");
scanf("%d",&temp->element);
root[++k]=temp->element;
ptr[temp->element]=temp;
printf("\nEnter the set elements:");
for(i=0;i<n-1;i++)
scanf("%d",&temparray[i]);
for(i=0;i<n-1;i++)
{ temp=(struct node *)malloc(sizeof(struct node));
temp->count=-1;
temp->element=temparray[i];
printf("\nEnter the parent of element %d:",temparray[i]);
scanf("%d",&temp->parent);
ptr[temp->element]=temp;
}
}
void Union()
{ struct node *t1,*t2;
t1=ptr[root[0]];
t2=ptr[root[1]];
if(t1->count < t2->count)
{ t1->parent=t2->element;
t2->count+=t1->count;
}
else
{ t2->parent=t1->element;
t1->count+=t2->count;
}
}
void display()
{ int i;
for(i=0;i<50;i++)
{ if(ptr[i]!=NULL)
printf("\nElement:%d\tParent:%d",ptr[i]->element,ptr[i]->parent);
}
}
void find(int x)
{ struct node *temp;
int root,par;
temp=ptr[x];
while(temp->parent!=-1)
temp=ptr[temp->parent];
root=temp->element;
temp=ptr[x];
while(temp->parent!=-1)
{ par=temp->parent;
if(temp->parent!=root)
temp->parent=root;
temp=ptr[par];
}
printf("\nThe element %d belongs to the set %s",x,temp->setname);
printf("\nAfter path compression:");
display();
}
void main()
{ int ch,x;
for(x=0;x<50;x++)
ptr[x]=NULL;
do
{ printf("\nMENU\n\t1.Create sets\n\t2.Find\n\t3.Union\n\t4.Exit\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{ case 1:printf("\nEnter the details of first set:\n");
readset();
printf("\nEnter the details of second set:\n");
readset();
break;
case 2:printf("\nEnter the element to be searched for:");
scanf("%d",&x);
find(x);
break;
case 3:Union();
printf("\nAfter Union\n");
display();
break;
case 4:break;
}
}while(ch!=4);
}
/*
OUTPUT
---------------------
MENU
1.Create sets
2.Find
3.Union
4.Exit
Enter your choice:1
Enter the details of first set:
Enter the set name:a
Enter the no. of elements in the set:3
Enter the root element:1
Enter the set elements:2
3
Enter the parent of element 2:1
Enter the parent of element 3:1
Enter the details of second set:
Enter the set name:b
Enter the no. of elements in the set:3
Enter the root element:10
Enter the set elements:20
30
Enter the parent of element 20:10
Enter the parent of element 30:20
MENU
1.Create sets
2.Find
3.Union
4.Exit
Enter your choice:2
Enter the element to be searched for:10
The element 10 belongs to the set b
After path compression:
Element:1 Parent:-1
Element:2 Parent:1
Element:3 Parent:1
Element:10 Parent:-1
Element:20 Parent:10
Element:30 Parent:20
MENU
1.Create sets
2.Find
3.Union
4.Exit
Enter your choice:3
After Union
Element:1 Parent:-1
Element:2 Parent:1
Element:3 Parent:1
Element:10 Parent:1
Element:20 Parent:10
Element:30 Parent:20
MENU
1.Create sets
2.Find
3.Union
4.Exit
Enter your choice:4
*/