Recursive and backtracking methods for subset sums

Source: Internet
Author: User

A subset and an instance of the problem are <S, C> 〉. S = {x1, x2 ,..., Xn} is a set of positive integers, and C is a positive integer. The subset and the problem determine whether a subset S1 of s exists, so that Σ x = C (X in S1 ). Try to design a method to solve the subset and problem. You can assume that the processing range does not exceed the int type.

Recursive Solution

// Subset and problem (recursion). cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> # include <vector> using namespace STD; void subsetsum (int A [], int level, int total, vector <bool> & frame, int N) {If (level <n) {If (Total = A [level]) {frame [level] = true; For (INT I = 0; I <N; I ++) {If (frame [I]) cout <A [I] <"" ;}cout <Endl; return ;} if (total> A [level]) {frame [level] = true; subsetsum (A, level + 1, total-A [level], frame, n );} if (total <A [level]) {frame [level] = false; subsetsum (A, level + 1, total, frame, n );}} else {While (frame [level-1] = false) level --; frame [level] = false; subsetsum (A, level + 1, total + A [level], frame, n) ;}int _ tmain (INT argc, _ tchar * argv []) {int arr [] = {2, 5, 15, 8, 20}; vector <bool> frame (); subsetsum (ARR, frame, 5); Return 0 ;}

Backtracking

// Subset and problem (rollback). cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <iostream> # include <vector> # include <algorithm> # include <string> using namespace STD; // find if the given Subset sum exists.int findsubsetsum (vector <int> & arr, int given, vector <bool> & bounded DED) {int cur = 0; // points to the current value. int sum = 0; // current Subset sum. while (cur> = 0) {// if current one is not supported ded if (false = supported ded [cur]) {// include current one supported ded [cur] = true; s Um + = arr [cur]; // find the given Subset sum if (sum = given) {return 1;} else if (sum> given) // exceed the given sum {encoded ded [cur] = false; sum-= arr [cur];} cur ++;} // backtrace if (cur> = arr. size ()) {/*** the following two cycles exclude the ** elements included in the result and not included in the result ** In the result in sequence until the next element included in the result is found. element ** example: use 1 and 0 to indicate including and not including ** if the result is 1110011, then the third 1 is the element found ** to change it to 0, but the traversal starts from 4th 0. **/While (true = encoded ded [cur-1]) {cur --; encoded ded [cur] = false; sum-= arr [cur]; // backtrace to the head if (cur <1) return 0;} while (false = encoded ded [cur-1]) {cur --; If (cur <1) return 0;} // change the status of current-1, not current! Encoded ded [cur-1] = false; sum-= arr [cur-1];} return 0;} int _ tmain (INT argc, _ tchar * argv []) {int arr [] = {2, 5, 15, 8, 20}; vector <int> V (ARR, arr + sizeof (ARR)/sizeof (INT )); vector <bool> included (sizeof (ARR)/sizeof (INT), false); int given = 33; // 5 + 8 + 20 if (findsubsetsum (v, given, encoded DED) {vector <bool>: iterator iterb = encoded ded. begin (); vector <int >:: iterator iteri = v. begin (); For (; I Terb! = Included ded. End (); iterb ++, iteri ++) {If (* iterb) cout <* iteri <Endl ;}} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.