Permutation and combination algorithms

Source: Internet
Author: User
{Code ...} now there is an array that extracts 2 from the arr array (this may be 3 or more) Sub-arrays, put the length of the two arrays into the new array and add all the values in the new array. my current practice is to use loops. is there any formula that can be used directly...
$ Arr = ['A' => [,], 'B' => [,], 'c' =>, 9], 'D' => [6, 8, 1, 3, 5]; // a new array composed of the preceding values: $ new = [count ($ arr [a]) * count ($ arr [B]), count ($ arr [a]) * count ($ arr [c]), count ($ arr [a]) * count ($ arr [d]), count ($ arr [B) * count ($ arr [c]), count ($ arr [B) * count ($ arr [d]), count ($ arr [c) * count ($ arr [d])]; // Add all the values in the new array

Now there is an array that extracts 2 from the arr array (this may be 3 or more) Sub-arrays, put the length of the two arrays into the new array and add all the values in the new array. my current practice is to use loops. is there any formula that can be directly implemented?

Reply content:
$ Arr = ['A' => [,], 'B' => [,], 'c' =>, 9], 'D' => [6, 8, 1, 3, 5]; // a new array composed of the preceding values: $ new = [count ($ arr [a]) * count ($ arr [B]), count ($ arr [a]) * count ($ arr [c]), count ($ arr [a]) * count ($ arr [d]), count ($ arr [B) * count ($ arr [c]), count ($ arr [B) * count ($ arr [d]), count ($ arr [c) * count ($ arr [d])]; // Add all the values in the new array

Now there is an array that extracts 2 from the arr array (this may be 3 or more) Sub-arrays, put the length of the two arrays into the new array and add all the values in the new array. my current practice is to use loops. is there any formula that can be directly implemented?

There is no formula for implementation. The formula is actually an algorithm, and the algorithm may have loops.


  [,], 'B' => [,], 'c' => [, 9], 'D' => [6, 8, 1, 3, 5]; $ arr_len = array (); foreach ($ arr as $ item) {$ arr_len [] = count ($ item) ;}// the length of the word group can be calculated for the above loop $ res = 0; // the result to be output $ c_arr_len = $ arr_len; foreach ($ arr_len as $ k1 => $ v1) {unset ($ c_arr_len [$ k1]); // Delete if (count ($ c_arr_len)> 0) {foreach ($ c_arr_len as $ k2 => $ v2) {$ res + = ($ v1 * $ v2) ;}} var_dump ($ res ); // get the result. You do not need to generate your $ new array.

Yes
2*(a1*a2 + a1*a3 + ... + a2*a3 ...) === ( a1 + a2 + a3 + ... )**2 - (a1**2 + a2**2 + ...)
Then, you can use map and reduce to avoid round robin.
However, the degree of parallelism remains unchanged, or n ^ 2.

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.