取消
显示结果 
搜索替代 
您的意思是: 

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 项奖励
1 条消息(共 8 条)
7,899 次查看

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
2 条消息(共 8 条)
7,887 次查看

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. 难过表情

 

0 项奖励
3 条消息(共 8 条)
7,873 次查看

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 项奖励
4 条消息(共 8 条)
7,865 次查看

 

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 ! 极度高兴的表情

 

 

0 项奖励
5 条消息(共 8 条)
7,853 次查看

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 项奖励
6 条消息(共 8 条)
7,845 次查看

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 项奖励
7 条消息(共 8 条)
7,834 次查看

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

8 条消息(共 8 条)
7,822 次查看