Partition Function

In combinatorics, a partition function P(n) represents the number of ways to partition an integer n into a sum of positive integers. Partition functions arise in many counting problems.

Some examples:

  • P(3) = 3, the partitions are: 1+1+1, 1+2, 3
  • P(4) = 5, the partitions are: 1+1+1+1, 1+1+2, 1+3, 2+2, 4

A simple recursive approach to calculate P(n):

Java example:

1
2
3
4
5
6
7
8
9
int partition(int n) {
  if (n == 0) return 1;

  int sum = 0;
  for (int k = 1; k <= n; k++) {
    sum += partition(n - k); 
  }
  return sum;
}

C++ example:

1
2
3
4
5
6
7
8
9
int partition(int n) {
  if (n == 0) return 1;
  
  int sum = 0;
  for (int k = 1; k <= n; k++) {
    sum += partition(n - k);
  }
  return sum;
} 

Python example:

1
2
3
4
5
6
7
8
9
def partition(n):
  if n == 0:
    return 1

  sum = 0
  for k in range(1, n+1):
    sum += partition(n - k)

  return sum

Partition functions have applications in counting problems and combinatorics. Generating functions and recurrence relations allow deriving closed forms.

The partition function is a concept in computer science often used in algorithms like quicksort or for solving array-based problems. It rearranges elements in an array so that all elements less than a chosen ‘pivot’ element go to its left, and all elements greater go to its right. The pivot ends up in its sorted position. The partition function is a key step in various sorting and searching algorithms.

Java Code for Partition Function

Here’s a simple Java code snippet for a partition function:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
public class Partition {
    public static int partition(int[] arr, int low, int high) {
        int pivot = arr[high];
        int i = low - 1;
        for (int j = low; j <= high - 1; j++) {
            if (arr[j] < pivot) {
                i++;
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        int temp = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = temp;
        return i + 1;
    }
}

C++ Code for Partition Function

Here’s the C++ version:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
int partition(int arr[], int low, int high) {
    int pivot = arr[high];
    int i = low - 1;
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            std::swap(arr[i], arr[j]);
        }
    }
    std::swap(arr[i + 1], arr[high]);
    return i + 1;
}

Python Code for Partition Function

And here’s how you’d write it in Python:

1
2
3
4
5
6
7
8
9
def partition(arr, low, high):
    pivot = arr[high]
    i = low - 1
    for j in range(low, high):
        if arr[j] < pivot:
            i += 1
            arr[i], arr[j] = arr[j], arr[i]
    arr[i + 1], arr[high] = arr[high], arr[i + 1]
    return i + 1

In each code snippet, the function partition takes an array arr and two indices, low and high, to work on a sub-array. The function uses the last element as a pivot, arranges the array around the pivot, and returns the sorted pivot index. This is used to divide the array into two halves for further sorting or other operations.