0% found this document useful (0 votes)
75 views6 pages

CSE5001 - Algorithms: Design and Implementation Registration Number:-18MCS0089 Exercise Name:-Set Cover Problem

The document contains C code for solving the set cover problem. It defines functions like check(), intersection(), difference(), and join() to operate on sets. The main() function takes input sets, finds the minimum number of sets needed to cover all elements by iteratively selecting the set with maximum intersection, and outputs the selected sets.

Uploaded by

Air
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views6 pages

CSE5001 - Algorithms: Design and Implementation Registration Number:-18MCS0089 Exercise Name:-Set Cover Problem

The document contains C code for solving the set cover problem. It defines functions like check(), intersection(), difference(), and join() to operate on sets. The main() function takes input sets, finds the minimum number of sets needed to cover all elements by iteratively selecting the set with maximum intersection, and outputs the selected sets.

Uploaded by

Air
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CSE5001 – Algorithms: Design and Implementation

Registration Number:-18MCS0089
Exercise Name:- Set cover Problem

#include<stdio.h>
int check(int*,int);
int intersection(int *,int *,int,int);
void difference(int *,int *,int,int);
void join(int *,int);

void join(int *c,int a)


{
static int k=1;
c[k]=a;
k++;
}

void difference(int *u,int *s,int a,int b)


{

int i,j;
for(i=1;i<=b;i++)
for(j=1;j<=a;j++)
if(*(u+j)==*(s+i))
{
*(u+j)=0;
}
}

int intersection(int *s,int *u,int a,int b)


{
int i,j,sum=0;
for(i=1;i<=b;i++)
for(j=1;j<=a;j++)
if(*(s+j)==*(u+i))
{
sum=sum+1;
}
return sum;
}

int check(int *u,int n)


{
int i,sum=0;
for(i=1;i<=n;i++)
if(*(u+i)!=0)
sum=sum+1;
return sum;
}
void main()
{
struct cover
{
int a[10];
int length;
}f[10];

int i,j,p,q,n,t,in,max,r,X[10],U[10],c[10];
printf("\nEnter number of elements to be inserted in set\n");
scanf("%d",&n);
printf("\nEnter set elements\n");
for(i=1;i<=n;i++)
scanf("%d",&X[i]);

printf("\nEnter number of sets present in family\n");


scanf("%d",&p);
for(i=1;i<=p;i++)
{
printf("\nEnter the number of elements to be inserted in set%d\n",i);
scanf("%d",&q);
printf("\nEnter the elements of set%d\n",i);
for(j=1;j<=q;j++)
{
scanf("%d",&f[i].a[j]);
}
f[i].length=q;
}
printf("\n");

for(i=1;i<=n;i++)
U[i]=X[i];

for(i=1;i<=n;i++)
c[i]=0;

t=check(U,n);

while(t!=0)
{
max=0;
for(i=1;i<=p;i++)
{
in=intersection(f[i].a,U,f[i].length,n);
if(in>max)
{
max=in;
r=i;
}
}
difference(U,f[r].a,n,f[r].length);
join(c,r);
t=check(U,n);
}
printf("\nThe sets to be selected are:\n");

for(i=1;i<=n;i++)
if(c[i]!=0)
printf("Set%d\t",c[i]);
}
TEST CASE

You might also like