Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please use C++ Got started then got stuck. I am trying to teach myself and it has gotten difficult so any help would be greatly
Please use C++
Got started then got stuck. I am trying to teach myself and it has gotten difficult so any help would be greatly appreciated.
#include #include #include #include #include #include struct ssum_elem { unsigned int x; std::string name; }; class ssum_instance { unsigned int target=0; std::vector elems; std::vector> feasible; // feasible[i][x] = TRUE if there is a subset // of a[0..i] adding up to x; FALSE otherwise // (once instacne has been solved) // int done=false; // flag indicating if dp has been run or // not on this instance public: // Function: read_elems // Description: reads elements from standard-input; // Format: sequence of pairs where // is non-negative int and // is a string associated with element void read_elems( std::istream &stream) { ssum_elem e; elems.clear(); // while(std::cin >> e.x && std::cin >> e.name) { while(stream>> e.x && stream >> e.name) { elems.push_back(e); } done = false; } // Function: solve // Desc: populates dynamic programming table of // calling object for specified target sum. // Returns true/false depending on whether the // target sum is achievalble or not. // Table remains intact after call. // Object can be reused for alternative target sums. bool solve(unsigned int tgt) { unsigned int n = elems.size(); unsigned int i, x; if(target == tgt && done) return feasible[n-1][tgt]; target = tgt; feasible = std::vector>(n, std::vector(target+1, false)); // leftmost column (column zero) is all TRUE because // a target sum of zero is always acheivable (via the // empty set). for(i=0; i= elems[i].x && feasible[i-1][x-elems[i].x]) feasible[i][x] = true; } // otherwise, feasible[0][x] remains false } done = true; return feasible[n-1][target]; } }; // end class /** * usage: ssumWhat needs clarification?
For this assignment you will implement a dynamic-programming algorithm for the subset-sum problem with a couple of wrinkles. Recall the formulation of the subset sum problem in its simplest form: GIVEN: positive integers a[1], a[2], ..., a[N], and target sum T OUTPUT: true if there is a subset S C{1..N} such that Lies(a[i]) = T false otherwise. ASSIGNMENT SUMMARY: you will write a program for the subset-sum problem which will report not only if the target sum can be formed, but also: 1. the number of distinct subsets yielding the target sum 2. the size of the smallest subset yielding the target sum 3. the number of distinct subsets yielding the target sum and are of minimum size. 4. the lexicographically first subset from (3) THE BASIC ALGORITHM: The idea behind a dynamic-programming algorithm for this problem is based on a table of booleans S[i][y] with dimensions S[1..N] [O..T] The algorithm populates this table with the following interpretation: S[i][y] = true if and only if a target sum of y can be formed by selecting elements from a[1]..a[i]. (and false otherwise) The table can be correctly populated by observing the following base-cases and recursive rules: BASE CASES S[i][0] = true for all le{l..N} (empty set has sum zero). S[1] [a[1]] = true (singleton) S[1] [y] = false Vy + a[1] INDUCTIVE/RECURSIVE CASES: (for i>1) S[i][y] = true if s[i-1] [y] = true OR (y >= a[i]) AND (S[i-1] [y-a[i]] = true) (false otherwise) Pseudo-code populating the table according to these rules follows: for(i=1; i The cmd-line argument is the target sum itself. The program reads the elements a [1]...a[N] from std-input. 1. YOUR SOURCECODE: If your program has gotten long, you may just include the key components needed to explain your solution (i.e., your data structures and bookkeeping, how they are populated and how solutions are extracted). You will submit your complete code electronically. You might format your source code with line numbers for easy reference in your subsequent explanations (below). 2. EXPLANATION: Explain your logic for computing the number of distinct subsets totaling the target. Include references to your actual code from (1). 3. EXPLANATION: Explain your logic for determining the minimum sized subset totaling the target. Including references to your actual code from (1) EXPLANATION: Explain your logic for capturing the lexicographically first min-sized subset totaling the target. Include references to your actual code from (1). 5. DATA: Run your program on the electoral college data in electoral.txt with a target of T=269 (exactly half of the grand total of 538 electoral votes). Cut and paste the results of your run (including all 4 key outputs) 6. DATA: Run your program on the partial electoral college data in purple. txt using a target of T=220. Recall that the safe Republican" states totaled 49 electoral votes and that 49+220=269. Cut and paste the results of your run (including all 4 key outputs) 7. DATA: Run your program on the partial electoral college data in purple.txt using a target of T=121. Recall that the safe democratic states totaled 148 electoral votes and that 148+121=269. Cut and paste the results of your run (including all 4 key outputs) For this assignment you will implement a dynamic-programming algorithm for the subset-sum problem with a couple of wrinkles. Recall the formulation of the subset sum problem in its simplest form: GIVEN: positive integers a[1], a[2], ..., a[N], and target sum T OUTPUT: true if there is a subset S C{1..N} such that Lies(a[i]) = T false otherwise. ASSIGNMENT SUMMARY: you will write a program for the subset-sum problem which will report not only if the target sum can be formed, but also: 1. the number of distinct subsets yielding the target sum 2. the size of the smallest subset yielding the target sum 3. the number of distinct subsets yielding the target sum and are of minimum size. 4. the lexicographically first subset from (3) THE BASIC ALGORITHM: The idea behind a dynamic-programming algorithm for this problem is based on a table of booleans S[i][y] with dimensions S[1..N] [O..T] The algorithm populates this table with the following interpretation: S[i][y] = true if and only if a target sum of y can be formed by selecting elements from a[1]..a[i]. (and false otherwise) The table can be correctly populated by observing the following base-cases and recursive rules: BASE CASES S[i][0] = true for all le{l..N} (empty set has sum zero). S[1] [a[1]] = true (singleton) S[1] [y] = false Vy + a[1] INDUCTIVE/RECURSIVE CASES: (for i>1) S[i][y] = true if s[i-1] [y] = true OR (y >= a[i]) AND (S[i-1] [y-a[i]] = true) (false otherwise) Pseudo-code populating the table according to these rules follows: for(i=1; i The cmd-line argument is the target sum itself. The program reads the elements a [1]...a[N] from std-input. 1. YOUR SOURCECODE: If your program has gotten long, you may just include the key components needed to explain your solution (i.e., your data structures and bookkeeping, how they are populated and how solutions are extracted). You will submit your complete code electronically. You might format your source code with line numbers for easy reference in your subsequent explanations (below). 2. EXPLANATION: Explain your logic for computing the number of distinct subsets totaling the target. Include references to your actual code from (1). 3. EXPLANATION: Explain your logic for determining the minimum sized subset totaling the target. Including references to your actual code from (1) EXPLANATION: Explain your logic for capturing the lexicographically first min-sized subset totaling the target. Include references to your actual code from (1). 5. DATA: Run your program on the electoral college data in electoral.txt with a target of T=269 (exactly half of the grand total of 538 electoral votes). Cut and paste the results of your run (including all 4 key outputs) 6. DATA: Run your program on the partial electoral college data in purple. txt using a target of T=220. Recall that the safe Republican" states totaled 49 electoral votes and that 49+220=269. Cut and paste the results of your run (including all 4 key outputs) 7. DATA: Run your program on the partial electoral college data in purple.txt using a target of T=121. Recall that the safe democratic states totaled 148 electoral votes and that 148+121=269. Cut and paste the results of your run (including all 4 key outputs)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started