Home > other >  Identifying the Unique Sets of Pairs taken from a Set (Order not Important)
Identifying the Unique Sets of Pairs taken from a Set (Order not Important)

Time:08-15

Eventually I hope to build an algorithm in Python to do this (I'm newer to the language), but I'm hoping to find a direction of a study to help me understand the ideas behind it—I can tell at least it's related to combinatorics.

If we had six elements, {a, b, c, d, e, f}, we can identify 15 different pairs that could be made where order doesn't matter (n = 6, k = 2, combination).

They'd be: ab, ac, ad, ae, af, bc, bd, be, bf, cd, ce, cf, de, df, ef

However, what I'm interested in doing is identifying the different sets of pairs. Brute force, they seem to be:

  1. {ab, cd, ef}
  2. {ab, ce, df}
  3. {ab, cf, de}
  4. {ac, bd, ef}
  5. {ac, be, df}
  6. {ac, bf, de}
  7. {ad, bc, ef}
  8. {ad, be, cf}
  9. {ad, bf, ce}
  10. {ae, bc, df}
  11. {ae, bd, cf}
  12. {ae, bf, cd}
  13. {af, bc, de}
  14. {af, bd, ce}
  15. {af, be, cd}

Presuming no error on my part, there'd also be 15 lists, with 3 (or n/2) pairs/entries, where the order of pairings and the order within pairings doesn't matter. As noted, I'm hoping to eventually create some code that would construct these lists of pairs.

Starting points are appreciated!

CodePudding user response:

In your set of characters, each character would have 5 possible pairs if it isn't paired with itself: ab ac ad.... After you get all possible pairs for a, you can then move onto b, you would loop through the list once again but this time omitting the a as ab has already been found. You can repeat this and each time omitting the letters before itself until you are on the last letter. After this to get your 'sets of pairs', you can just loop through your pairs and adding them to a new list.

CodePudding user response:

  1. Say there are K elements remaining. If K is 0, quit.
  2. Pick the smallest.
  3. Pick any of the K-1 remaining elements to pair with it.
  4. Set K to K-2 and repeat.

That indeed appears to be what you did when building your listing by hand.

The total number of arrangements possible is the product of the K-1 values in step #2. For your example, K starts as 6, so K-1 is 5 the first time through the loop, and is 3 the second time, and 1 the third time: 5*3*1 = 15.

  • Related