Heap based permutation extremely slow for larger numbers

61 Views Asked by At

We are using Heap algorithm for generating permutations of an array a. . The generated permutations are passed to the printArr function, which calculates the correlation coefficient between two arrays X and Y based on the current permutation. This works as expected, however for larger numbers, e.g. more than 20. Heap's permutation is exponentially slow. Any idea how can we significantly improve the Heap based permutation.

void printArr(int a[], int n, double X[], double Y[], int *diff, double corrcoeffO)
{
    for (int i = 0; i < n; i++)
    {
        // cout << a[i] << " ";
    }

    // printf("\n");

    double corrcoeffHP = correlationCoefficient(X, Y, a, n);

    if (abs(corrcoeffHP) >= abs(corrcoeffO))
    {
        ++(*diffCount);
    }
}

void heapPermutation(int a[], int size, int n, double X[], double Y[], int *diffCount, double corrcoeff)
{
        if (size == 1)
    { 
        printArr(a, n, X, Y, diffCount, corrcoeff);
        return;
    }

    for (int i = 0; i < size; i++)
    {
        heapPermutation(a, size - 1, n, X, Y, diffCount, corrcoeff);

        if (size % 2 == 1)
        {
            swap(a[0], a[size - 1]);
        }
        else
        {
            swap(a[i], a[size - 1]);
        }
    }
}
0

There are 0 best solutions below