The Wayback Machine - https://web.archive.org/web/20240707055237/https://www.geeksforgeeks.org/set-partition-is-np-complete/
Open In App

Set partition is NP complete

Last Updated : 02 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Set partition problem: Set partition problem partitions an array of numbers into two subsets such that the sum of each of these two subsets is the same. Let S be a set of numbers and A is a subset of numbers with sum S1, then there exists another subset containing the remainder of the elements (S – A) with sum S2, and S1 is equaled to S2.  

Problem Statement: Given a set S of N numbers, the task is to determine if the set contains two partitions of S, with both of them having exactly the same sum.  

Explanation:
An instance of the problem is an input specified to the problem. An instance of the Set Partition problem is a set S, and the task is to check whether there exist any two non-overlapping partitions of S having a sum of elements as sum. Since an NP-Complete problem is a problem which is both in NP and NP-hard, the proof for the statement that a problem is NP-Complete consists of two parts:

  1. The problem itself is in NP class.
  2. All other problems in NP class can be polynomial-time reducible to that. (B is polynomial-time reducible to C is denoted as B ≤ PC)

If the 2nd condition is only satisfied then the problem is called NP-Hard.

But it is not possible to reduce every NP problem into another NP problem to show its NP-Completeness all the time. Therefore, to show a problem is NP-Complete then prove that the problem is in NP and any NP-Complete problem is reducible to that i.e., if B is NP-Complete and B ≤ PC For C in NP, then C is NP-Complete. Thus, it can be concluded that the Set partition problem is NP-Complete using the following two propositions:

Set Partition Problem is in NP:
If any problem is in NP, then, given a ‘certificate’, which is a solution to the problem and an instance of the problem (a set S and two partitions A and A’, in this case), it can be proved that the certificate in polynomial time. This can be done in the following way:  

  1. For every element x in A and x’ in A’, verify that all the elements belonging to S are covered.
  2. Let S1 is 0, S2 is 0
  3. For every element x in A add that value to S1.
  4. For every element x’ in A’ add that value to S2.
  5. Verify that S1 is the same as S2.

The algorithm takes linear time in the size of the set of numbers.

Set Partition Problem is NP-Hard:
In order to prove that the Independent Set problem is NP-Hard, perform a reduction from a known NP-Hard problem to this problem. Carry out a reduction from which the Subset Sum Problem can be reduced to the Set Partition problem. The Subset Problem provides the input as a set of numbers S and a target sum t, the problem aims at finding the subset T belonging to S with a sum same as the t. Let s be the sum of members of S. Now, feed S’ = S ∪ {s − 2t} into the Set Partition problem.

Now prove that the problem of computing the set partition indeed boils down to the computation of the subset-sum. The reduction can be proved by the following two propositions:

Now, let us consider a set of the numbers T with summation equivalent to t(Subset Sum), then the remainder of the elements in S(assuming O) will have the sum o = s – t. Let us assume the original set is equal to T’ = T ∪ (s – 2t) which has a sum equal to t’. 
Now the following observations hold:

o     = s – t 
o – t = s – 2t, Difference in sum between O and T.
t’     = t + (s – 2t)
       = s – t
       = o, the sum of T’ and O are equal.

Hence, the original set can be partitioned into two subsets of sum (s – t) each. Therefore, the set partition problem is satisfied. 
Now suppose an equal-sum partitioning (A, A’) of S’ = S ∪ {s − 2t} exists. The sum of each partition is given by:

a = \frac{s + (s - 2*t)}{2} = (s - t)

Consider the partition containing the element {s – 2t} to be A’. Let A = A’- {s – 2t}. The sum of elements in A is given by:

A = s – t – {s – 2t}
   = t

Also, S’ – S = {s – 2t}. So A is a subset of S with a sum equal to t. 

Therefore, the subset sum problem is satisfied.


Previous Article
Next Article

Similar Reads

Print equal sum sets of array (Partition problem) | Set 1
Given an array arr[]. Determine whether it is possible to split the array into two sets such that the sum of elements in both the sets is equal. If it is possible, then print both the sets. If it is not possible then output -1. Examples : Input : arr = {5, 5, 1, 11} Output : Set 1 = {5, 5, 1}, Set 2 = {11} Sum of both the sets is 11 and equal. Inpu
14 min read
Partition of a set into K subsets with equal sum using BitMask and DP
Given an integer array arr[] of consisting of N integers, the task is check if it is possible to divide the given array into K non-empty subsets of equal sum such that every array element is part of a single subset.Examples: Input: arr[] = {2, 1, 4, 5, 6}, K = 3 Output: Yes Explanation: Possible subsets of given array are {2, 4}, (1, 5} and {6}Inpu
10 min read
Partition a set into two non-empty subsets such that the difference of subset sums is maximum
Given a set of integers S, the task is to divide the given set into two non-empty sets S1 and S2 such that the absolute difference between their sums is maximum, i.e., abs(sum(S1) – sum(S2)) is maximum. Example: Input: S[] = { 1, 2, 1 } Output: 2Explanation:The subsets are {1} and {2, 1}. Their absolute difference is abs(1 - (2+1)) = 2, which is ma
7 min read
Generate all partition of a set
Given a set A = {1, 2, 3, . . ., n }. It is called a partition of the set A if the following conditions follow: The union of all the sets is the set AThe intersection of any two sets is an empty setExamples: Input: n = 3Output: [{1, 2, 3}], [{1, 2}, {3}], [{1, 3}, {2}], [{1}, {2, 3}], [{1}, {2}, {3}]Explanation: For the set {1, 2, 3} these are 5 po
8 min read
Bell Numbers (Number of ways to Partition a Set)
Given a set of n elements, find number of ways of partitioning it. Examples: Input: n = 2Output: Number of ways = 2Explanation: Let the set be {1, 2} { {1}, {2} } { {1, 2} }Input: n = 3Output: Number of ways = 5Explanation: Let the set be {1, 2, 3} { {1}, {2}, {3} } { {1}, {2, 3} } { {2}, {1, 3} } { {3}, {1, 2} } { {1, 2, 3} }. Recommended practice
14 min read
Count number of ways to partition a set into k subsets
Given two numbers n and k where n represents a number of elements in a set, find a number of ways to partition the set into k subsets.Example: Input: n = 3, k = 2Output: 3Explanation: Let the set be {1, 2, 3}, we can partition it into 2 subsets in following ways {{1,2}, {3}}, {{1}, {2,3}}, {{1,3}, {2}} Input: n = 3, k = 1Output: 1Explanation: There
15+ min read
Partition a set into two subsets such that the difference of subset sums is minimum
Given a set of integers, the task is to divide it into two sets S1 and S2 such that the absolute difference between their sums is minimum. If there is a set S with n elements, then if we assume Subset1 has m elements, Subset2 must have n-m elements and the value of abs(sum(Subset1) - sum(Subset2)) should be minimum. Example: Input: arr[] = {1, 6, 1
15+ min read
Partition of a set into K subsets with equal sum
Given an integer array of N elements, the task is to divide this array into K non-empty subsets such that the sum of elements in every subset is same. All elements of this array should be part of exactly one partition. Examples: Input : arr = [2, 1, 4, 5, 6], K = 3 Output : Yes we can divide above array into 3 parts with equal sum as [[2, 4], [1, 5
15+ min read
Partition into two subsets of lengths K and (N - k) such that the difference of sums is maximum
Given an array of non-negative integers of length N and an integer K. Partition the given array into two subsets of length K and N - K so that the difference between the sum of both subsets is maximum. Examples : Input : arr[] = {8, 4, 5, 2, 10} k = 2 Output : 17 Explanation : Here, we can make first subset of length k = {4, 2} and second subset of
7 min read
Optimal partition of an array into four parts
Given an array of n non-negative integers. Choose three indices i.e. (0 <= index_1 <= index_ 2<= index_3 <= n) from the array to make four subsets such that the term sum(0, index_1) - sum(index_1, index_2) + sum(index_2, index_3) - sum(index_3, n) is maximum possible. Here, two indices say l and r means, the sum(l, r) will be the sum of
9 min read
Practice Tags :
three90RightbarBannerImg