I want to write a function to select combination of k element from list of n element.I have the given source file in pdf form that i converted in text.And i got the following list .I was trying to use dfs
to find the combinations but there are constraint that need to be managed first, its confusing me.Say we need to select 3 subjects from the following list. In the following lines /
means any one of these subjects,like for Ex.from a)History or Sociology or Economics
should be selected.The constraint's are that Mathematics Can't be in any combination with Bengali or Hindi.
Some valid combination are
History,Bengali,Sanskrit
Bengali,Philosophy,English
The snapshot of the list -
a) History/Sociology/Economics
b) Bengali/Hindi
c) Sanskrit/Mathematics
d) Philosophy
e) Political Science
f) English
Total 3 subject combination will be something like (6C3*3*2*2)-24
(from my calculation)
I am thinking a way to represent the list so that i can code it efficiently .But can't figure out a reasonable way to solve this .
Here is my implementation
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int counter=0;
void combination(char *p[10],char *d[10],int start,int end,int index,int r){
int i,j,k=0;
// char ch[10]="History";
if(index==r){
if(!strcmp(d[0],"History")){
if(!strcmp(d[1],"Sociology") || !strcmp(d[1],"Economics") || !strcmp(d[2],"Sociology") || !strcmp(d[2],"Economics"))
return;
}
if(!strcmp(d[0],"Sociology")){
if(!strcmp(d[1],"History") || !strcmp(d[1],"Economics") || !strcmp(d[2],"History") || !strcmp(d[2],"Economics"))
return;
}
if(!strcmp(d[0],"Economics")){
if(!strcmp(d[1],"History") || !strcmp(d[1],"Sociology") || !strcmp(d[2],"History") || !strcmp(d[2],"Sociology"))
return;
}
if(!strcmp(d[0],"Bengali")){
if(!strcmp(d[1],"Hindi") || !strcmp(d[2],"Hindi") || !strcmp(d[1],"Mathematics") || !strcmp(d[2],"Mathematics"))
return;
}
if(!strcmp(d[0],"Hindi")){
if(!strcmp(d[1],"Bengali") || !strcmp(d[2],"Bengali") || !strcmp(d[1],"Mathematics") || !strcmp(d[2],"Mathematics"))
return;
}
if(!strcmp(d[0],"Sanskrit")){
if(!strcmp(d[1],"Mathematics") || !strcmp(d[2],"Mathematics"))
return;
}
if(!strcmp(d[0],"Mathematics")){
if(!strcmp(d[1],"Sanskrit") || !strcmp(d[2],"Sanskrit") || !strcmp(d[1],"Bengali") || !strcmp(d[2],"Bengali") || !strcmp(d[1],"Hindi") || !strcmp(d[2],"Hindi"))
return;
}
if(!strcmp(d[1],"Mathematics")){
if(!strcmp(d[2],"Bengali") || !strcmp(d[2],"Hindi"))
return;
}
if(!strcmp(d[2],"Mathematics")){
if(!strcmp(d[1],"Bengali") || !strcmp(d[1],"Hindi"))
return;
}
for(j=0;j<r;j++)
printf("%s ",d[j]);
counter++;
printf("\n");
return;
}
for(i=start;i<=end && end-i+1>=r-index; i++){
d[index]=p[i];
combination(p,d,i+1,end,index+1,r);
}
}
int main(){
char *subject[10],n[50],*p,*data[10];
int len,i,m;
printf("\nEnter the no subject\n");
scanf("%d",&m);
printf("\nEnter name of subject\n");
for(i=0;i<m;i++){
scanf("%s",n);
len=strlen(n);
p=(char *)malloc(len+1);
strcpy(p,n);
subject[i]=p;
}
combination(subject,data,0,m-1,0,3);
printf("\nCount=%d\n",counter);
return 0;
}
What if the list is getting bigger then how to handle the output it will be much large then complexity will be increase and eventually it will fail Is there any elegant way to solve this with predefined language data structure in any language