LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to loop trough all unique pair combinations of N items (pairing algorithm)

I have a list of items A,B,C,D,E,F.. and I'd like to loop through all possible unique pair combinations (note: AB and BA are the same pair). I know that the possible pair combinatios increase by (N-1)!! (!!=double factorial):
2 items : 1!! = 1=1
4 items : 3!! = 1*3=3
6 items : 5!! = 1*3*5=15
8 items : 7!! = 1*3*5*7=105
10 items : 9!! = 1*3*5*7*9=945

However I can't figure out an algorithm how to loop through all unique pair combinations once in Labview. I have N items, where N is even and reasonably low so that the number of pair combination is not too high. This is how it would look like for for 6 items: 15 different unique pair combinations. One row of index pairs is what I'd like to get for each loop index.
[0,1],[2,3],[4,5] (= AB,CD,EF)
[0,1],[2,4],[3,5] (= AB,CE,DF)
[0,1],[2,5],[3,4] (= AB,CF,DE)
[0,2],[1,3],[4,5] (= AC,BD,EF)
[0,2],[1,4],[3,5] (= AC,BE,DF)
[0,2],[1,5],[3,4] (= AC,BF,DE)
[0,3],[1,2],[4,5] (= AD,BC,EF)
[0,3],[1,4],[2,5] (= AD,BE,CF)
[0,3],[1,5],[2,4] (= AD,BF,CE)
[0,4],[1,2],[4,5] (= AE,BC,DF)
[0,4],[1,3],[2,5] (= AE,BD,CF)
[0,4],[1,5],[2,3] (= AE,BF,CD)
[0,5],[1,2],[3,4] (= AF,BC,DE)
[0,5],[1,3],[2,4] (= AF,BD,CE)
[0,5],[1,4],[2,3] (= AF,BE,CD)

I have score for each possible pair and I'm trying to get a group of pairs out of the N items with the highest total group score.

Thanks in advance

0 Kudos
Message 1 of 8
(5,408 Views)

Hi Andy,

 

use autoindexing:

check.png

 

An U8 array is used here instead of an Array of strings...

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 8
(5,396 Views)

Gerd I'm afraid that isn't correct. Your algorithm just gives an array increasing like this ...

 

1                        (1)

1+2+3                 (6)

1+2+3+4+5         (15)

1+2+3+4+5+6+7  (28)  etc

 

rather than in a double factorial manner. Smiley Sad

 

0 Kudos
Message 3 of 8
(5,382 Views)

Hi Neil,

 

the OP wanted to have all "unique combinations" of array elements (without permutations as "AB" is the same as "BA").

That's what my snippet provides…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 8
(5,374 Views)

 

Gerd, I think you'll find that your snippet gives "unique pairs" rather than "unique pair combinations".

 

Having said that, I think that the unique pairs can subsequently be used to generate the necessary combinations ... it's just a case of working out how ! Smiley Very Happy

 

 

0 Kudos
Message 5 of 8
(5,362 Views)

Hi Neil,

 

ok, I missed the word "combinations".

 

Atleast the OP now already has a "1 row list" of all possible combinations. Now we only need to pick some elements from it.

 

But well, this is a nice job of learning recursion and implementing it in LabVIEW. 😄

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 6 of 8
(5,354 Views)

Thanks for the replys so far. What I actually want are the unique combinations of the pairs. Greg, out of your list the unique pair combinations would be:
1st unique pair combination: [0,1] and [2,3]
2nd unique pair combination: [0,2] and [1,3]
3rd unique pair combination: [0,3] and [1,2]
The question remains how to get these.

0 Kudos
Message 7 of 8
(5,343 Views)

This was an interesting conversation a few years ago - http://forums.ni.com/t5/LabVIEW/How-can-I-create-list-of-permutations/td-p/1256988

Jim
You're entirely bonkers. But I'll tell you a secret. All the best people are. ~ Alice
For he does not know what will happen; So who can tell him when it will occur? Eccl. 8:7

Message 8 of 8
(5,331 Views)