예제 #1
0
// inputs
// s            - set vector
// t            - tuplet vector
// s_size       - set size
// t_size       - tuplet size so far
// sum          - sum so far
// ite          - nodes count
// target_sum   - sum to be found
void subset_sum(int s[], int t[],
                int s_size, int t_size,
                int sum, int ite,
                int const target_sum)
{
    total_nodes++;
    printf("%d\n", callno);
    callno++;
    if( target_sum == sum )
    {
        // We found subset
        printSubset(t, t_size);
        // Exclude previously added item and consider next candidate
        subset_sum(s, t, s_size, t_size-1, sum - s[ite], ite + 1, target_sum);
        return;
    }
    else
    {
        // generate nodes along the breadth
        for(  i = ite; i < s_size; i++ )
        {
            t[t_size] = s[i];
            // consider next level node (along depth)
            subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum);
        }
    }
}
예제 #2
0
// inputs
// s            - set vector
// t            - tuplet vector
// s_size       - set size
// t_size       - tuplet size so far
// sum          - sum so far
// ite          - nodes count
// target_sum   - sum to be found
void subset_sum(int s[], int t[],
                int s_size, int t_size,
                unsigned long long sum, int ite,
                int const target_sum)
{
    total_nodes++;
    if((sum>0)&&(sum%target_sum==0 ))
    {
        // We found subset
        printSubset(t, t_size);
        // Exclude previously added item and consider next candidate
     //   subset_sum(s, t, s_size, t_size-1, sum - s[ite], ite + 1, target_sum);
        return;
    }
    else
    {
        int i;
        // generate nodes along the breadth
        for( i = ite; i < s_size; i++ )
        {
            t[t_size] = s[i];
            // consider next level node (along depth)
            subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum);
        }
    }
}
예제 #3
0
// print all subSets
void CreateSet(FILE*in, int n, int num){
    if ( n<1 || n>MaxItems ) return;

    int k, j, Set[MaxItems+1], Numbers[n+1];

    for (j=1;j<= n;j++)
        fscanf(in,"%d",&Numbers[j]);


    for( j=0; j<=n; j++ ) 
         Set[j]= -1; //initialise
    
    k=1;
    while( k>=1 ){
           while( Set[k]<1 ){ 
                  Set[k]= Set[k]+1;
                  if( k==n ) 
                      printSubset(in, Set,Numbers, k, num); 
                  ++k; 
                  if( k>n ) 
                      break; 
                  Set[k]= -1; 
           }
           --k; 
    }
}