Find expected swaps to sort given Array where probability of swapping any inversion pair is equal
Given an array arr[] consisting of permutation of the first N natural numbers, the task is to find the expected number of swaps to sort the given array where the probability of swapping any inversion pair is equal and the probability of swapping any non-inversion pair is 0.
Examples:
Input: arr[] = {3, 2, 1}
Output: 2.3333
Explanation: The given array can be sorted in non-decreasing order using sequences of swaps as shown in the image below.
The expected numbers of swaps of each node can be calculated by 1 + (average of expected number of swaps of all the child nodes).
Since the nodes 3, 9, 10, 11, and 12 are already sorted, the number of expected swaps to sort them are 0.
The expected number of swaps of nodes 5, 6, 7, and 8 will be 1.
Similarly, the expected number of swaps of nodes 2 and 4 will be 2.
Hence, the expected number of swaps of node 1 is 1 + (2 + 2 + 0)/3 = 1 + 1.3333 = 2.3333
Input: arr[] = {1, 3, 2}
Output: 1.0
Explanation: Since there is only 1 inversion pair (arr[1], arr[2]), the expected number of swaps is 1.
Approach: The given problem can be solved with the help of Recursion and Backtracking using Memoization which is based on the following observations:
- An inversion in the array is defined as two indices (i, j) such that i < j and arr[i] > arr[j]. The expected number of swaps can be calculated recursively after swapping the integers of each valid inversion one at a time and recursively calling for the permutation after the swap until the whole permutation is sorted.
- Suppose there are K inversions in the current permutation and after swapping the ith inversion, the expected number of swaps to sort the permutation is denoted by Pi. Therefore, the expected number of swaps after swapping all possible inversions will be (P1 + P2 + … + PK)/K.
Using the above observations, the given problem can be solved by the following steps:
- Create a map, say memo that stores the expected number of swaps for a given permutation.
- Create a Recursive Function expectedSwaps(), which takes a permutation as an argument and returns the expected number of swaps.
- In the expectedSwaps() function, if the expected swaps of the current permutation are already calculated, return the answer. Otherwise, iterate over each valid inversion and swap the index of the current inversion and recursively call for the permutation after swapping.
- Find the sum of the expected swaps after each valid swap in a variable, say res, and the count of inversions in a variable, say K.
- After completing the above steps, print the value of res / K as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
map<vector< int >, double > memo;
double expectedSwaps(vector< int > a)
{
if (memo.count(a)) {
return memo[a];
}
double res = 0;
int K = 0;
for ( int i = 0; i < a.size(); i++) {
for ( int j = i + 1; j < a.size(); j++) {
if (a[i] > a[j]) {
swap(a[i], a[j]);
res += 1 + expectedSwaps(a);
swap(a[i], a[j]);
K++;
}
}
}
if (K == 0)
res = 0;
else
res /= K;
return memo[a] = res;
}
int main()
{
int N = 3;
vector< int > arr = { 3, 2, 1 };
cout << expectedSwaps(arr);
return 0;
}
|
Java
import java.util.*;
public class Main
{
static HashMap <ArrayList <Integer>, Double> memo;
static double expectedSwaps(ArrayList <Integer> a)
{
if (memo.containsKey(a)) {
return memo.get(a);
}
double res = 0 ;
int K = 0 ;
for ( int i = 0 ; i < a.size(); i++) {
for ( int j = i + 1 ; j < a.size(); j++) {
if (a.get(i) > a.get(j)) {
Collections.swap(a, i, j);
res += 1 + expectedSwaps(a);
Collections.swap(a, i, j);
K++;
}
}
}
if (K == 0 )
res = 0 ;
else
res /= K;
memo.put(a,res);
return res;
}
public static void main(String args[]) {
int N = 3 ;
ArrayList <Integer> arr
= new ArrayList <Integer> (Arrays.asList( 3 , 2 , 1 ));
memo = new HashMap <ArrayList <Integer>, Double> ();
System.out.println( expectedSwaps(arr) );
}
}
|
Python3
memo = {}
def expectedSwaps(a):
if ( tuple (a) in memo):
return memo[ tuple (a)]
res = 0
K = 0
for i in range ( len (a)):
for j in range (i + 1 , len (a)):
if (a[i] > a[j]):
temp = a[i]
a[i] = a[j]
a[j] = temp
res + = 1 + expectedSwaps(a)
temp = a[i]
a[i] = a[j]
a[j] = temp
K + = 1
if (K = = 0 ):
res = 0
else :
res / = K;
memo[ tuple (a)] = res
return res
N = 3
arr = [ 3 , 2 , 1 ]
print (expectedSwaps(arr))
|
Javascript
<script>
var dict = {};
function expectedSwaps(a)
{
if (dict.hasOwnProperty(a)) {
return dict[a];
}
var res = 0;
var K = 0;
for (let i = 0; i < a.length; i++)
{
for (let j = i + 1; j < a.length; j++)
{
if (a[i] > a[j]) {
var temp = a[i];
a[i] = a[j];
a[j] = temp;
res += 1 + expectedSwaps(a);
var temp2 = a[i];
a[i] = a[j];
a[j] = temp2;
K++;
}
}
}
if (K == 0) {
res = 0;
}
else {
res /= K;
}
dict[a] = res;
return res;
}
var N = 3;
var arr = [3, 2, 1];
document.write(expectedSwaps(arr).toFixed(5));
</script>
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public static Dictionary<List< int >, double > memo = new Dictionary<List< int >, double >();
public static bool checkEquality(List< int > a,List< int > b){
if (a.Count != b.Count) return false ;
for ( int i = 0 ; i < a.Count ; i++){
if (a[i]!=b[i]) return false ;
}
return true ;
}
public static double containsKey(List< int > a){
foreach (KeyValuePair<List< int >, double > x in memo){
if (checkEquality(a, x.Key)){
return x.Value;
}
}
return -1;
}
public static double expectedSwaps(List< int > a)
{
double res = containsKey(a);
if (res != -1) {
return res;
}
res = 0;
int K = 0;
for ( int i = 0 ; i < a.Count ; i++) {
for ( int j = i + 1 ; j < a.Count ; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
res += 1 + expectedSwaps(a);
temp = a[i];
a[i] = a[j];
a[j] = temp;
K++;
}
}
}
if (K == 0)
res = 0;
else
res = res/( double )K;
memo[ new List< int >(a)] = res;
return res;
}
public static void Main( string [] args){
List< int > arr = new List< int >{ 3, 2, 1 };
Console.Write(expectedSwaps(arr));
}
}
|
Time Complexity: O(N2N!)
Auxiliary Space: O(N!)
Last Updated :
02 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...