Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Now, consider the following practical problem. Suppose that you’re in charge of

ID: 3873547 • Letter: N

Question

Now, consider the following practical problem. Suppose that you’re in charge of picking a set of experiments to be launched into space. Each experiment has an ID number, an associated equipment mass in kilograms, and a “value rating” that approximates how important to science the collected data will be. The following table lists the set E of all experiments:

ID # Experiment Title Mass (kg) Value Rating

1 How rats behave in the absence of oxygen 36 5

2 Solar radiation study 264 9

3 X-ray observatory 188 6

4 Solar flare observation device 203 8

5 Low - gravity bear wrestling gear 104 8

6 CLASSIFIED TOP SECRET 7 6

7 Solid propellant behavior in microgravity 92 2

8 Cloaking field generator 65 8

9 Prototype handheld stun phaser 25 3

10 Laminar fluid flow study 170 6

11 Low Earth orbit race of African vs. European swallows 80 7

12 Hyper- efficient RCS thruster 22 4

Unfortunately, not all of the experiments can be flown due to mass constraints there’s only 700 kg of payload available for the experiments. Launching stuff into orbit is expensive! Thus, the challenge is to pick the subset of E whose elements have the highest possible combined value rating but at the same time have a combined mass of 700 kg or less. One way to solve this problem is to use av“brute force” approach. Consider ALL subsets ofcE. For each subset, determine its overall mass and value rating. Keep track of the highest-value subset with a combined mass of 700 or less. Once all the subsets are considered, this highest-value subset will be the one that you want to fly! Write a program that uses your function from #1 and the brute force technique to find and display the optimal solution. Which experiments should be chosen? What is the total mass and value rating of the optimal solution? Even though it works fine for this problem, why would this brute force approach be terribly inefficient if you had a large number of experiments? (Answer in Python code please)

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

#define ARRAYSIZE(a) (sizeof(a))/(sizeof(a[0]))

struct Experiment

{

int id;

char experimentTitle[50];

int mass;

int rating;

};

static int total_nodes;

// prints subset found

void printSubset(int A[], int size)

{

for (int i = 0; i < size; i++)

{

printf("%*d", 5, A[i]);

}

printf("n");

}

// 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(struct Experiment s[], struct Experiment t[],

int s_size, int t_size,

int sum, int ite,

int const target_sum)

{

total_nodes++;

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].mass, ite + 1, target_sum);

return;

}

else

{

// generate nodes along the breadth

for (int 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].mass, i + 1, target_sum);

}

}

}

// Wrapper to print subsets that sum to target_sum

// input is weights vector and target_sum

void generateSubsets(struct Experiment s[], int size, int target_sum)

{

struct Experiment *tuplet_vector = (struct Experiment *)malloc(size * sizeof(struct Experiment));

subset_sum(s, tuplet_vector, size, 0, 0, 0, target_sum);

free(tuplet_vector);

}

int main()

{

/*

*1 How rats behave in the absence of oxygen 36 5

2 Solar radiation study 264 9

3 X-ray observatory 188 6

4 Solar flare observation device 203 8

5 Low - gravity bear wrestling gear 104 8

6 CLASSIFIED TOP SECRET 7 6

7 Solid propellant behavior in microgravity 92 2

8 Cloaking field generator 65 8

9 Prototype handheld stun phaser 25 3

10 Laminar fluid flow study 170 6

11 Low Earth orbit race of African vs. European swallows 80 7

12 Hyper- efficient RCS thruster 22 4

*/

//int weights[] = { 10, 7, 5, 18, 12, 20, 15 };

struct Experiment e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12;

e1.id = 1;

strcpy(e1.experimentTitle,"How rats behave in the absence of oxygen ");

e1.mass = 36;

e1.rating = 5;

e1.id = 2;

strcpy(e1.experimentTitle, "Solar radiation study");

e1.mass = 264;

e1.rating = 9;

e1.id = 3;

strcpy(e1.experimentTitle, "X-ray observatory");

e1.mass = 188;

e1.rating = 6;

struct Experiment s[] = {e1,e2,e3};

int size = ARRAYSIZE(s);

generateSubsets(s, size, 700);

printf("Nodes generated %dn", total_nodes);

return 0;

}