Using CUSP and recursion reimplement the 2nd program in home
Solution
void subset_sum(int s0[], int t0[],
int s0_size, int t0_size,
int sum0, int ite0,
int const target_sum0)
{
tot_nodes++;
if( target_sum0 == sum0 )
{
///// found subset
printSubset(t0, t0_size);
///// Exclude previously added item
subset_sum0(s, t0, s0_size, t0_size-1, sum0- s0[ite], ite0 + 1, target_sum0);
return;
}
else
{
///// generate nodes
for( int i = ite0; i < s0_size; i++ )
{
t0[t0_size] = s0[i];
//// next level node (along depth)
subset_sum0(s0, t0, s0_size, t0_size + 1, sum0 + s0[i], i + 1, target_sum0);
}
}
}
void generateSubsets(int s0[], int size, int target_sum0)
{
int *tuplet_vector = (int *)malloc(size * sizeof(int));
subset_sum0(s, tuplet_vector, size, 0, 0, 0, target_sum0);
free(tuplet_vector);
}
int main()
{
int weight[] = {10, 7, 5, 18, 12, 20, 15};
int size = ARRAYSIZE(weight);
generateSubsets(weight, size, 35);
printf(\"Nodes generated %d\ \", total_nodes);
return 0;
| void subset_sum(int s0[], int t0[], int s0_size, int t0_size, int sum0, int ite0, int const target_sum0) { tot_nodes++; if( target_sum0 == sum0 ) { ///// found subset printSubset(t0, t0_size); ///// Exclude previously added item subset_sum0(s, t0, s0_size, t0_size-1, sum0- s0[ite], ite0 + 1, target_sum0); return; } else { ///// generate nodes for( int i = ite0; i < s0_size; i++ ) { t0[t0_size] = s0[i]; //// next level node (along depth) subset_sum0(s0, t0, s0_size, t0_size + 1, sum0 + s0[i], i + 1, target_sum0); } } } void generateSubsets(int s0[], int size, int target_sum0) { int *tuplet_vector = (int *)malloc(size * sizeof(int)); subset_sum0(s, tuplet_vector, size, 0, 0, 0, target_sum0); free(tuplet_vector); } int main() { int weight[] = {10, 7, 5, 18, 12, 20, 15}; int size = ARRAYSIZE(weight); generateSubsets(weight, size, 35); printf(\"Nodes generated %d\ \", total_nodes); return 0; |
![Using CUSP and recursion, re-implement the 2^nd program in homework 1: create a list or an array of random integers (e.g., between [0, 50]) and a target number Using CUSP and recursion, re-implement the 2^nd program in homework 1: create a list or an array of random integers (e.g., between [0, 50]) and a target number](/WebImages/4/using-cusp-and-recursion-reimplement-the-2nd-program-in-home-977168-1761501323-0.webp)
![Using CUSP and recursion, re-implement the 2^nd program in homework 1: create a list or an array of random integers (e.g., between [0, 50]) and a target number Using CUSP and recursion, re-implement the 2^nd program in homework 1: create a list or an array of random integers (e.g., between [0, 50]) and a target number](/WebImages/4/using-cusp-and-recursion-reimplement-the-2nd-program-in-home-977168-1761501323-1.webp)
![Using CUSP and recursion, re-implement the 2^nd program in homework 1: create a list or an array of random integers (e.g., between [0, 50]) and a target number Using CUSP and recursion, re-implement the 2^nd program in homework 1: create a list or an array of random integers (e.g., between [0, 50]) and a target number](/WebImages/4/using-cusp-and-recursion-reimplement-the-2nd-program-in-home-977168-1761501323-2.webp)