I have a two-dimensional array of ints, for example:
int [][] board=
{
{23,17,3,29,12,10},
{17,4,11,12,10,19},
{32,33,25,25,28,35},
{27,29,24,25,23,37},
{29,40,34,26,24,39},
{23,37,29,36,31,3}
}
I don’t want to change the columns of this array at all; however, I would like to swap the rows so that the most similar rows are grouped together. Similar in this case means most number of equal elements.
Edit: Similar rows means, if one row has 1,2,3,4,5,6 and another has 1,2,3,4,9,10
They have 4 similarities.
What’s the best way to do this?
Note: the most number of rows I will have in my array is around 100 and the most number of elements in each row will be 10 so the complexity does matter as pointed out!
This question reduces to the traveling salesman problem. If you think of each row as being a city and then define some distance function which computes the distance between two rows. The question is how to order the rows so that the distance is minimized. This problem is NP-Complete and cannot be solved in a reasonable amount of time for 100 rows. The brute-force solution for this would require O(N!) computations. There are heuristic algorithms (algorithms that get close to the best answer) that will solve this in a reasonable time.
Traveling Salesman Problem (Wikipedia)
One example is to use a greedy algorithm. Choose one row at random, this is row 1. Then choose the closest row to row 1 as row 2. Then choose the closest row to row 2 as row 3. Run until all the rows are chosen. This is not a very optimal solution.