Search for all possible combinations of values ​​between two arrays

I have two arrays of strings, not necessarily the same length, I want to find all possible "sets" of combinations between two values ​​from arrays without repeating from any array.
For example, given arrays:
{"A1", "A2", "A3"}
{"B1", "B2"}
As a result, I want the following sets:
{("A1", "B1"), ("A2", "B2")}
{("A1", "B1"), ("A3", "B2")}
{("A1", "B2"), ("A2", "B1")}
{(" A1 "," B2 "), (" A3 "," B1 ")}
{(" A2 "," B1 "), (" A3 "," B2 ")}
{("A2 "," B2 "), (" A3 "," B1 ")}

My general direction is to create a recursive function that takes two arrays as a parameter and deletes each “selected” row at a time, calling itself until any array is empty, however I'm a bit concerned about performance issues ( I need to run this code on about 1000 pairs of string arrays).
Can someone direct me to an effective method for this?

+5
source share
7 answers

It might be useful to think of two arrays as sides of a table:

        A1      A2      A3
---+-------+-------+-------+
B1 | B1,A1 | B1,A2 | B1,A3 |
---+-------+-------+-------+
B2 | B2,A1 | B2,A2 | B2,A3 |
---+-------+-------+-------+

This means that the loop is nested in another, one loop for rows and another for columns. This will give you an initial set of pairs:

{B1,A1} {B1,A2} {B1,A3} {B2,A1} {B2,A2} {B2,A3}

. , :

      B1,A1 B1,A2 B1,A3 B2,A1 B2,A2 B2,A3
-----+-----+-----+-----+-----+-----+-----+
B1,A1|     |  X  |  X  |  X  |  X  |  X  |
-----+-----+-----+-----+-----+-----+-----+
B1,A2|     |     |  X  |  X  |  X  |  X  |
-----+-----+-----+-----+-----+-----+-----+
B1,A3|     |     |     |  X  |  X  |  X  |
-----+-----+-----+-----+-----+-----+-----+
B2,A1|     |     |     |     |  X  |  X  |
-----+-----+-----+-----+-----+-----+-----+
B2,A2|     |     |     |     |     |  X  |
-----+-----+-----+-----+-----+-----+-----+
B2,A3|     |     |     |     |     |     |
-----+-----+-----+-----+-----+-----+-----+

(: ).

+9

:

:
A n, B m, n <= m.
A = [0, 1, 2, ..., n - 1].
B = [0, 1, 2, ..., m - 1].
A B. One possible injective and non-surjective mapping

:
, , .. N.

B, n A.

: Permutation 1Permutation 2Permutation 3Permutation 4

:

class Helper {
public:
    /**
     * @brief generateArray
     * @param size
     * @return A vector [0, 1, ..., size - 1]
     */
    vector<int> generateArray(int size) {
        vector<int> arr;
        for (int i = 0; i < size; ++i) {
            arr.push_back(i);
        }
        return arr;
    }

    /**
     * @brief generateMatches
     * @param n, cardinality of the vector X, where X = [0,1, ..., n - 1].
     * @param m, cardinality of the vector Y, where Y = [0,1, ..., m - 1].
     * @return All possible injective and non-surjective mappings 
     * from the smaller vector to the larger vector.
     */
    vector<vector<pair<int, int> > > generateMatches(int n, int m) {
        // Deal with n > m. Swap back when generating pairs.
        bool swapped = false;
        if (n > m) {
            swapped = true;
            swap(n, m);
        }
        // Now n is smaller or equal to m
        vector<int> A = generateArray(n);
        vector<int> B = generateArray(m);
        vector<vector<pair<int, int> > > matches;
        // Generate all the permutations of m
        do {
            vector<pair<int, int> > match;
            for (int i = 0; i < n; ++i) {
                pair<int, int> p;
                if (swapped) {
                    // Swap back to the original order.
                    p = make_pair(A[i], B[i]);
                } else {
                    p = make_pair(B[i], A[i]);
                }
                match.push_back(p);
            }
            matches.push_back(match);
                // Generate next permutation.
        } while(next_permutaion(B.begin(), B.end())); 
        return matches;
    }
};
+2

string[] arr = new string[3];
        string[] arr1 = new string[4];
        string[] jointarr = new string[100];

        for (int i = 0; i < arr.Length; i++)
        {
            arr[i] = "A" + (i + 1);
        }

        for (int i = 0; i < arr1.Length; i++)
        {
            arr1[i] = "B" + (i + 1);
        }

        int k=0;
        for (int i = 0; i < arr.Length; i++)
        {
            for (int j = 0; j < arr1.Length; j++)
            {
                jointarr[k] = arr[i] + " " + arr1[j];
                k++;
            }
        }
+1

, , , , , :

0

( ) (. ). , .

IEnumerable<Tuple<string, string>> Combinations(
  IEnumerable<string> list1, 
  IEnumerable<string> list2) {}

( "" ), , ( = ):

1 2 ( - )

  • 1
  • 2
  • ( - )
0

, :

  • 2 {A_i, A_j} A,
  • 2 {B_k, B_l} B,
  • 2 { (A_i, B_k), (A_j, B_l) }, { (A_i, B_l), (A_j, B_k) }.

2- A B , .

|A| * (|A| - 1) * |B| * (|B| - 1) / 2.

4 :

for i = 1 ... |A|
  for j = i+1 ... |A|
    for k = 1 ... |B|
      for l = k+1 ... |B|
        make 2 combinations {(A_i, B_k),(A_j, B_l)}, {(A_i, B_l), (A_j, B_k)}
0

, , , , , . , , , .

x- A , B . , . :

A={1,2,...m} and B={1,2,....n}     n>=m

C m , j A (i) B (j). , .. C-, n (n-1)..... (n-m + 1) : n!/(M + 1)!

, , A B, A , , .. .

-:

for i= 1 to n
   C(1)=B(i)
   for j= 1 to n-1
      C(2)=B'(j)        '  B' is B with element i removed
         .........
          for x = 1 to n-m
             C(m)=B'''(x)   'B is now reduced with (m-1) elements
          next x

1 .

A m, AllPairs, :

   AllPairs (A,B,C)
    if Size(A)>1             ' check no of elements in A        
      for i=1 to Size(B)
       C(Size(C)-Size(A)+1)= B(i)
       A'=Remove element 1 from A
       B'=Remove element i from B
       Call AllPairs(A',B',C)     'recursive call
      Next i
    else                          ' only one element in A
      for j=1 to Size(B)
      C(Size(C)) = B(i)  'looping last element in C through all unused in B
      Collect.ADD(C)      'collect C-arrays here for later use
      Next j                 
  End AllPairs

, C , A ( A). C , A B , A , . . ( ) Jingie Zheng answer - ( ). , . VB .

0

All Articles