Recursive Algorithms and variants for JavaScript combination

Source: Internet
Author: User

There are many types of combination algorithms for implementing an array, with the largest number of recursion, and there is no shortage of efficient examples on the Internet. Here only one implementation method is demonstrated.

 

Code

1 /**
2 * recursive combination
3 * select all combinations of num (0 <num <= N) numbers from arr [1N]
4 */
5 function combine (ARR, num ){
6 var r = [];
7 (function f (t, A, n ){
8 If (n = 0) return R. Push (t );
9 For (VAR I = 0, L = A. length; I <= L-N; I ++ ){
10 F (t. Concat (A [I]), A. Slice (I + 1), n-1 );
11}
12}) ([], arr, num );
13 return R;
14}
15
16/** test code **/
17 combine ([1, 2, 4, 5, 6, 7, 8, 9], 3 );

However, things are not so simple. Now I have a new problem. I have defined a new array.

VaR arr = [[1, 2, 3],

[1, 2, 3, 4, 5],

[2, 4, 6, 8],

[3,5, 7,9];

The requirement is to take any value from each element of the array arr to form a length of 4 (that is, arr. length. at this time, if you want to use the recursion above again, you need to modify it. the main change is to pass in the parameters of the anonymous function each time, to ensure that the array to be recycled each time is the next element of ARR rather than the current element itself.

 

Code

1 function combine_ex (ARR ){
2 var r = [];
3 (function f (t, A, n ){
4 If (n = 0) return R. Push (t );
5 For (VAR I = 0; I <A [n-1]. length; I ++ ){
6 F (t. Concat (A [n-1] [I]), A, n-1 );
7}
8}) ([], arr, arr. Length );
9 return R;
10}

Then there is a new problem. If you want to get a variable-length two-dimensional array above, you must obtain a value from each element of the array arr, what should I do if I compose a combination with any value in length? The solution is here. It is nothing more than using the first two functions, first finding the set of the longest combination, and then finding the combination of the specified length for each element.

In summary, the demand for composite algorithms is diverse. No matter how you change it, you can derive composite algorithms from the most basic algorithms.

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.