I have the following problem: There is an amount of nodes each containing elements of different kinds. Each node has a max amount of elements it can hold. I want to sort the elements between the nodes so that they are grouped by element kind, rather than scattered between all nodes. To execute the sorting I can do swaps which can exchange an amount of elements for the same amount of elements from another node. I already programmed an algorithm which finds my exit goal (a list of nodes and what elements I want them to have at the end).
Now I can generate a list of swaps in multiple ways, but I'm interested if there is some known algorithm that I can use for finding an efficient list of swaps (which means as few swaps as possible to reach my end goal)?
Example:
before sorting:
(1) A, A, A, B, C
(2) A, B, B, C, D, E, F
(3) A, C, C, D, E
sorted:
(1) A, A, A, A, A
(2) B, B, B, C, C, C, C
(3) D, D, E, E, F
example list of swaps to reach the end goal
1. (1) C <--> (3) A
2. (2) D,E,F <--> (3) C,C,C
3. (1) B <--> (2) A
or better (1 less swap):
1. (2) D, E, F <--> (3) A, C, C
2. (1) B, C <--> (2) A, A
The final order is something that I can calculate already and can pass it to the algorithm. I'm looking for help mostly just with generating the list of swaps. Either way here's more info on the sorting part. If there are more elements of a kind than can fit in a single node, they should be spread between multiple nodes, as few as possible. The order of elements inside nodes or even which nodes they belong to is not important, I just want them to be grouped instead of scattered.
Each node can hold a set amount of elements, different from node to node. In reality the amount of space might be larger than the amount of things it holds but I thought of the problem in a way where every space is an element (whether it has something in it or not). So an empty space might be element A, and actual things B, C etc. That simplifies thinking about it in my opinion.