My solution to this problem is strangely simple, and it did pass all the test cases on EPI judge. In fact it runs twice as fast as the solution given in the github repository.
I don’t understand why the textbook contains such cumbersome solution.
My solution is as follows.
def apply_permutation(P, A):
for i in range(len(A)):
while P[i] != i:
A[i], A[P[i]] = A[P[i]], A[i]
a = P[i]
b = P[P[i]]
P[i] = b
P[a] = a
In each iteration of the for loop, I first exchange A[i] with A[P[i]] which moves A[i] in its correct location. I also exchange P[i] and P[P[i]] so that P[P[i]] becomes equal to P[i]. Now, we might have an incorrect element at A[i]. I repeat the above steps until I get the correct element in position A[i]. Since each swap places one element in correct position, the time complexity is O(n).
I don’t understand why this much simpler solution is not discussed in the book. Am I missing something here?
Here is the runtime of my program -
PS C:\Users\amogh\stuff\algo_ds.py\epi_judge> python
.\epi_judge_python\apply_permutation.py
Test PASSED (101/101) [ 50 ms]
Average running time: 551 us
Median running time: 36 us
*** You've passed ALL tests. Congratulations! ***
Here is the runtime of the solution provided in the github repository -
PS C:\Users\amogh\stuff\algo_ds.py\epi_judge> python
.\epi_judge_python\apply_permutation.py
Test PASSED (101/101) [ 80 ms]
Average running time: 900 us
Median running time: 76 us
*** You've passed ALL tests. Congratulations! ***