Search code examples
algorithmwolfram-mathematicabijection

Algorithm to find bijection between arrays


I have two arrays, say A={1, 2, 3} and B={2, 4, 8} (array item count and numbers may vary). How do I find a bijection between the arrays.

In this case, it would be f:A->B; f(x)=2^(x)


Solution

  • As others have remarked, this problem is ill-defined.

    Other possible functions that give the same results are (among probably infinite others): (8 x)/3 - x^2 + x^3/3, x + (37 x^2)/18 - (4 x^3)/3 + (5 x^4)/18, and (259 x^3)/54 - (31 x^4)/9 + (35 x^5)/54.

    I found these solutions using:

    n = 5; (* try various other values *)
    A = {1, 2, 3} ; B = {2, 4, 8}
    eqs = Table[
      Sum[a[i] x[[1]]^i, {i, n}] == x[[2]], {x, {A, B}\[Transpose]}]
    sol = Solve[eqs, Table[a[i], {i, n}], Reals]
    Sum[a[i] x^i, {i, n}] /. sol
    

    Sometimes not all of the a[i]'s are fully determined and you may come up with values of your own.

    [tip: better not use variables starting with a capital letter in Mathematica so as not to get into conflict with reserved words]