Look at the data structure write code (33) Tree and backtracking method (a) subset tree

Source: Internet
Author: User

Backtracking is a kind of branch which does not satisfy the condition in the exhaustive lifting, and has reached the method of improving efficiency. Its basic prototype is the first sequence traversal of a tree, and the path from the root to the leaf is a solution to the problem.

The basic framework of backtracking = determining the solution space + depth-first traversal + clipping function + Determining the result function

Where the solution space is divided into subsets tree and sort tree.

Detailed concept: Reference click to open the link and click the Open link

The generic template for recursive algorithms is as follows:

The backtracking method is a depth-first search for the solution space, so the backtracking method is implemented by recursive method in the general case.

Recursive backtracking method for n-fork Tree

void backtrack (int t)

{

if (T > N) {

Reach the leaf node and output the result.

Output (x);

}

else {

Traverse all sub-nodes of a node T

for (int i = f (n,t); I <= g (n,t); i + +) {

X[T] = H[i];

If the pruning condition is not met, continue traversing

if (constraint (t) && bound (t))

Backtrack (t + 1);

}

}

}

The following three examples are written according to this template.

Backtrack.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <cstring> #define SET_MAX_SIZE 1000static bool set[set_max_size];//get power set//times representation  The number of layers of the current recursive layer, starting at 0 levels//n represents the number of sets void Getpowset (int times,int n) {if (Times >= N) {for (int i = 0; i < n; i++) {if (set[i] = = true) {printf ("%d\t", I+1);}} printf ("\ n");} else{for (int i = 0; i < 2; i++) {Set[times] = I;getpowset (times+1,n);}}} Prints all substrings of a string//times represents the current recursive layer void Getallsubchar (int Times,char * string) {int len = strlen (string); if (Times >= len) { for (int i = 0; i < len; i++) {if (set[i] = = True) {printf ("%c\t", String[i]);}} printf ("\ n");} else{for (int i = 0; i < 2; i++) {Set[times] = I;getallsubchar (times+1,string);}}} Prints all combinations of equals N in the collection. void getallequal (int times,int arraylen,int * array,int equal) {int len = arraylen;if (Times >= len) {i NT sum = 0;for (int i = 0; i < len; i++) {if (set[i] = = True) {sum + = Array[i];}} if (sum = = equal) {for (int i = 0; i < len; i++) {if (set[i] = true) {printf ("%d\t", Array[i]);}} printf ("\ n");}} else{for (int i = 0; i < 2; i++) {Set[times] = i;int sum = 0;//cropping. Cut out the non-conforming conditions. for (int i = 0; I <= times; i++) {if (set[i]== true) {sum + = Array[i];}} if (sum <= equal) {getallequal (times+1,arraylen,array,equal);}}}} int _tmain (int argc, _tchar* argv[]) {printf ("The power set of 1~ N, enter N value:"), int n;scanf ("%d", &n);p rintf ("The power set of 1~%d is: \ n", "N."); Getpowset (0,n); char string[set_max_size];p rintf ("Enter string:"), scanf ("%s", string);p rintf ("All substrings of '%s are as follows: \ n", string); Getallsubchar (0,string);p rintf (the combination of "lookup set (3,2,5,6,7,8,10,20,4,15) equals 10 \ n"); int array[] = {3,2,5,6,7,8,10,20,4,15}; Getallequal (0,sizeof (array)/sizeof (int), array,10); return 0;}



Look at the data structure write code (33) Tree and backtracking method (a) subset tree

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.