Creating a set of randomized ordered pairs from a list while excluding specified pairs

34 Views Asked by At

Say I have an array/set A that is composed of elements [a, b, c, d, e].

From this set, I would like to create a set of randomized ordered pairs while also excluding defined un-ordered pairs from occurring.

  • The randomized ordered pairs are such that a distinct x is mapped to a distinct y.
  • There can be no symmetry (a,a), (b,b), (c,c)
  • Reflection is allowed (a, b), (b, a)
  • If I exclude (a, b), it is also implied that (b, a) is an excluded pair.

Set A will always have 3 or more elements.

--

I can do this without exclusion by randomizing set A (Ar), creating a clone of the set (B), and shifting it's indices up 1.

A = [a, b, c, d, e]

Ar = [d, c, e, b, a]

B = [c, e, b, a, d]

And take my pairs as x from Ar and y from B. (d,c), (c, e), (e, b), (b, a), (a, d)

But now that I want to exclude certain pairs:

e.g exclude: [ (a,b), (d, e) ]

My results are now invalid as (b, a) exists.

The only way I understand doing this would be to just keep repeating my above shuffle-pair process until my exclusion pairs just happen to not be in the set of pairs, but this can prove to be extremely inefficient and take massive amounts of calculations.

--

Does there exist any algorithm for my case?

And what would the limitations be? e.g.

  • Set A must have more than 3 elements.
  • The set containing the exclusion pairs cannot have more than n pairs before math breaks.