-
Notifications
You must be signed in to change notification settings - Fork 168
/
Disjoint_set_Dsa.c
87 lines (85 loc) · 2.05 KB
/
Disjoint_set_Dsa.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
#include <stdio.h>
#include <math.h>
int parent[100];
int rank[100];
void makeset(int v)
{
for (int i = 1; i <= v; i++)
{
parent[i] = i;
rank[i] = 0;
}
}
int findset(int v)
{
while (parent[v] != v)
{
v=parent[v];
}
return v;
}
int munion(int u, int v)
{
int r = findset(u);
int s = findset(v);
if (r == s)
return r;
else
{
if (rank[r] > rank[s])
{
parent[s] = r;
rank[r] = rank[r] + rank[s] ;
return r;
}
else
{
parent[r] = s;
return s;
}
}
}
int main()
{
int n, i, j, t;
printf(" How many disjoint set you want to create?\n");
scanf("%d", &n);
makeset(n);
printf("%d number of makeset operations are executed\n",n);
printf("To stop Union operation, press -1\n");
while (t != -1)
{
printf("Enter the value of t");
scanf("%d", &t);
if (t != -1)
{
printf("Enter i and j to perform Union(i,j) operation");
printf("I = ");
scanf("%d", &i);
printf("J = ");
scanf("%d", &j);
munion(i, j);
printf("UNION(%d,%d) is finished", i, j);
}
}
int count=0,f;
printf("Do you want to find the connected components");
printf("Press ' 1 ' for YES or, ' 0 ' for NO\n");
scanf("%d",&f);
if (f == 1)
{
for (int i = 1; i <= n; i++)
{
printf("%d ",findset(i));
if (findset(i) == i){
count = count + 1;
}
}
printf("The number of connected component is %d", count);
}
else
{
printf("We do not want to find the connected components");
}
return 0;
}