Advertisement
Mr_kindle

subsetsum.c

Nov 14th, 2022 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.10 KB | None | 0 0
  1. /*This code print all the subset of a given set whose sum of element = k*/
  2. /*'k' is provided by user*/
  3. /* first line of input is n integers which are elements of set
  4.     secone line is sum
  5. Input
  6.     1,2,3,4
  7.     5
  8. Output
  9.     (1,4),(2,3)
  10.     */
  11.  
  12.  
  13. #include<stdio.h>
  14. #include<math.h>
  15. #include<malloc.h>
  16. //#define ARRAYSIZE(x) sizeof(x)/sizeof(x[0])
  17.  
  18. int * subsetsum(int * , int);
  19. void subset(int* set, int arraysize, int n);
  20.  
  21. int main()
  22. {  
  23.     int *set,*set1,*set2,*sum1,*sum2;
  24.     int arraysize,i,j,k,m=0,found=0;
  25.     int set1size,set2size;
  26.     printf("Enter number of elements in array: ");
  27.     scanf("%d",&arraysize);
  28.     /*Bifurcatiing array */
  29.     set1size = arraysize/2;
  30.     set2size = arraysize - arraysize/2;
  31.     /*dynamically allocating memories*/
  32.     set = calloc(arraysize, sizeof(int));
  33.     set1 = calloc(set1size, sizeof(int));
  34.     set2 = calloc(set2size, sizeof(int));
  35.    
  36.     /*filling the array elements by users*/
  37.     printf("\nNow enter all the elements which must be unique: \n");
  38.     for(i=0;i<arraysize;i++)
  39.         {
  40.             printf("Enter element %d: ",i+1);
  41.             scanf("%d",&set[i]);
  42.             if(i<arraysize/2)
  43.             /*Bifuracating the set into two parts almost equal length*/
  44.                 set1[i] = set[i];
  45.             else
  46.             /*since m=0 hence set2 start filling from oth element*/
  47.                 set2[m++] = set[i];
  48.         }
  49.     printf("\n\nEnter the sum: ");
  50.     scanf("%d",&k);
  51.     /*Subsetsum is a fun which return an array which contain sum of all the subset elements*/
  52.          sum1 = subsetsum(set1, set1size);
  53.          sum2 = subsetsum(set2, set2size);
  54.          printf("\nThe set is following: \n");
  55.          printf(" (");
  56.          for(i=0;i<arraysize;i++)
  57.             printf("%d,",set[i]);
  58.             printf("\b ");
  59.             printf(")");
  60.            
  61.         printf("\n\n\nAll the subsets whose sum = %d is following: \n\n",k);
  62.         for(i=0;i<pow(2,set1size);i++)//traversing in set1 subset
  63.             for(j=0;j<pow(2,set2size);j++)//traversing in set2 subset
  64.                if(sum1[i] + sum2[j] == k)
  65.     /*now we know that ith subset of set1 & jth subset of set2 makes the sum*/
  66.     /*Now we will find the ith & jth subset and will print it*/
  67.                     {   found++;//indicate that the sum has been found
  68.                         printf(" ( ");
  69.                         /*subset is a fun which print the ith subset of a given array*/
  70.                         subset(set1,set1size,i);
  71.                         printf(",");
  72.                         subset(set2,set2size,j);
  73.                         printf(" ),");
  74.                      }
  75.                      printf("\b ");
  76.         if(found)
  77.             printf("\nThe required subset printed above: ");
  78.         else
  79.             printf("\nNo such subset found: ");
  80.          
  81. free(sum1);
  82. free(sum2);    
  83. free(set);
  84. free(set1);
  85. free(set2);
  86. return 0;
  87. }//main
  88.  
  89. int * subsetsum(int * set, int arraysize)
  90.     {  
  91.         int i,j,sum;
  92.         int *ptr;
  93.         ptr = calloc(pow(2,arraysize), sizeof(int));
  94.         for(i=0;i<pow(2,arraysize);i++)
  95.         {   sum=0;
  96.            
  97.             for(j=0;j<arraysize;j++)
  98.     /*if array has 'n' elements then check all 'n' bits of i (from 0 to n-1) either it is set or not*/
  99.     /*if jth bit of i is set then array[j] will be printed as subset*/
  100.                 if((1<<j) & i)
  101.                     {  
  102.                        sum = sum + set[j];
  103.                     }
  104.                 ptr[i] = sum;
  105.            
  106.                  
  107.         }
  108.         return ptr;
  109.     }//end of subsetsum
  110.    
  111. /*The following function print the nth subset of a given set*/
  112.    
  113. void subset(int* set, int arraysize, int n)
  114.     {
  115.         int j,count=0;
  116.         for(j=0;j<arraysize;j++)
  117.             if((1<<j) & n )
  118.                 {
  119.                     if(count>0)
  120.     /*if this 'j' loop run more than 1 time it will put ',' between two value printed*/
  121.                         printf(",");
  122.                     count++;
  123.                     printf("%d",set[j]);
  124.                 }
  125.                  
  126.     }//end of subset
  127.    
  128.    
  129.  
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement