Minimum Number of swap for a permutation

Description

Given an array of permutation PP with size of NN, find the number of operations that one should execute so that the array is in sorted. In an operation, one could choose two indices ii and jj such that 1i,jN1 \leq i,j \leq N and swap the value of PiP_i and PjP_j.

For example, for N=5N=5 and P=[1,2,5,3,4]P=[1,2,5,3,4], the number of operations that should be done is 22. We could do the operation on pair of indices (3,4)(3,4) and (4,5)(4,5).

Solving the Problem

Intuition

One can observe that there may exists some kind of circuit like pattern in PP. I use the term circuit loosely here. A circuit CC is defined as a set of number (that represents indices in the PP) such that the graph that it could create—which will be later explained—resembles a cycle loop. It’s obvious that a graph GG would be defined by its (V,E)(V,E). It’s obvious that the VV is defined by set of indices in the circuit CC itself. The set EE could be created by iteratively augmenting edge of (u,Pu)(u,P_u) for uCu \in C.

The graph is a directed cyclic type because for each node nn, where nVn \in V, we know that there is only 11 other node pointing towards node nn—due to there exists only 1 other node mm that should replace nn such that the node mm would be in its correct position—and only 1 other node oo such that the correct position of node nn is occupied by node oo.

Solution

We could solve each circuit independently—because each node in circuit CC does not need to “interfere”/swap with the other circuit.

For each circuit CC, the number of swap needed is C|C|. For each CC, we can arbitarily choose a node nn and do swap it to its correct position. The iteration continues until there only exists 2 nodes in CC that is still not in the right place. Because there exists only 2 remaining node, it’s obvious that we could just swap—just do 11 operation—and both node are swapped in a right place.

Therefore, the number of minimum swap is cCc1\sum_{c \in C}{|c|-1} where CC is the set of circuit that exists in PP. However, this equation could be further simplified. We could do the distribution rules and change the answer into cCc+cC1\sum_{c \in C} |c| + \sum_{c \in C} -1. The first part of the summation is equals to NN as the sum of size of all circuits are equal to the number of element in the PP and the secnd part is equal to the number of circuit, i.e. C|C|.

Because of that, the simplified solution are NN - C|C|.