How to compute the absolute minimum amount of changes to convert one sortorder into another?

后端 未结 8 679
走了就别回头了
走了就别回头了 2021-02-04 06:04

Goal

How to encode the data that describes how to re-order a static list from a one order to another order using the minimum amount of data possible?

8条回答
  •  不知归路
    2021-02-04 06:46

    Algo part:

    A reordering of a list is called permutation. Each permutation can be split into a set of loops, with each loop of N elements requiring (N - 1) swaps. For example

    1, 2, 3, 4, 5, 6 --> 3, 2, 4, 1, 6, 5

    This can be split into 1 - 4 - 3 (requires 2 swaps) 2 - 2 (0 swaps) 5 - 6 (1 swap)

    To find a solution you can just pick any element at a wrong position and put it on its place.

    Details part:

    Of course, you can use smaller data types, RLE or some other encoding algorithms and so on.

    Very theoretical but non-practical part.

    All permutations of a sequence of N numbers can be lexicographically ordered, and one number from 0 to (N! - 1) is enough to represent the sequence. So, theoretically best answer is: compute the index of the permutation, transfer it, recreate the permutation by that index.

提交回复
热议问题