You are given 2 vectors:
x={a,b,c,d,e,f,r,e,t};
y={f,a,c,f,b,a,b,f,g};
and need to sort vector y in such a way that the HammingDistance[x,y]
is minimized. This literal implementation:
HumMin[x_, y_] := First[MinimalBy[Permutations[y], HammingDistance[#, x] &, 1]]
x
HumMin[x, y]
(*{a,b,c,d,e,f,r,e,t}*)
(*{a,b,c,f,f,f,b,a,g}*)
is terribly inefficient due to generally large space of Permutations[y]
. The order of non-matching elements between x and y is not important, so there could be several equivalent solutions and any would do. So in principle an algorithm should stop as soon as first solution is found. THis is when all elements of y identical to some elements of x aligned at the same position if possible. For example it is possible for one a in y and impossible for the other. What would be a more efficient implementation?