Subset Equality is NP Complete


Subset Correspondence, otherwise called the "Subset Total" issue, is an exemplary NP-complete computational issue. Given a bunch of numbers and an objective worth, the undertaking is to decide if there exists a subset of the numbers whose total is equivalent to the objective worth. The issue's NP-culmination emerges from its capacity to address an extensive variety of other NP-complete issues through polynomial-time decreases. Regardless of its straightforward definition, no realized effective calculation can tackle "Subset Correspondence" for all occurrences, making it of critical interest in hypothetical software engineering and streamlining, with functional applications in different fields, like cryptography, asset distribution, and dynamic issues.

Methods Used

  • Reduction from Subset Sum

  • Reduction from 3SAT

Reduction from Subset Sum

One way to deal with the exhibit that "Subset Fairness" is NP-finished is by showing a decrease from the notable NP-complete issue, the "Subset Total" issue.

Algorithm

  • Given a case of the "Subset Aggregate" issue, which is a bunch of whole numbers S and an objective worth T.

  • Make another case of the "Subset Equity" issue with a similar set S and target esteem 2T.

  • On the off chance that there exists a subset of S that summarizes to T in the "Subset Aggregate" issue, then, at that point, there will be a subset in the "Subset Uniformity" issue that summarizes to 2T by adding a similar subset with itself.

  • Assuming that there is no subset of S that summarizes to T in the "Subset Total" issue, then there will be no subset in the "Subset Fairness" issue that summarizes to 2T, as any subset with a total under 2T joined with itself can't surpass 2T.

  • This decrease shows that tackling "Subset Fairness" is pretty much as hard as addressing the "Subset Aggregate" issue, making it NP-complete.

Example

#include <iostream>
#include <vector>
using namespace std;

bool isSubsetSum(vector<int>& set, int n, int sum) {
   if (sum == 0) return true;
   if (n == 0) return false;

   if (set[n - 1] > sum) return isSubsetSum(set, n - 1, sum);

   return isSubsetSum(set, n - 1, sum) || isSubsetSum(set, n - 1, sum - set[n - 1]);
}

bool isSubsetAggregateReduction(vector<int>& set, int n, int sum) {
   return !isSubsetSum(set, n, sum) && !isSubsetSum(set, n, 2 * sum);
}

int main() {
   vector<int> set = {3, 34, 4, 12, 5, 2};
   int sum = 18; 
   if (isSubsetAggregateReduction(set, set.size(), sum)) {
      cout << "No subset exists in Subset Aggregate issue that sums to " << sum << " and no subset exists that sums to " << 2 * sum << " by adding the same subset with itself." << endl;
   } else {
      cout << "There exists a subset in Subset Aggregate issue that sums to " << sum << " or a subset in Subset Equity issue that sums to " << 2 * sum << " by adding the same subset with itself." << endl;
   }

   return 0;
}

Output

There exists a subset in Subset Aggregate issue that sums to 18 or a subset in Subset Equity issue that sums to 36 by adding the same subset with itself.

Reduction from 3SAT

Another methodology is to show that "Subset Correspondence" is NP-finished by diminishing it from one more known NP-complete issue, for example, the 3SAT issue straightforwardly.

Algorithm

  • Given an example of the 3SAT issue, which comprises a Boolean recipe in conjunctive ordinary structure with three literals for each condition.

  • Make another occasion of the "Subset Uniformity" issue with a bunch of whole numbers and an objective worth as follows:

  • a. For every variable in the 3SAT equation, make a number in the set with a worth of 1.

    b. For every proviso in the 3SAT equation, make a number in the set with a worth of 2.

    c. Set the objective worth to be the all out number of factors in addition to the all out number of provisos in the 3SAT recipe.

  • On the off chance that the 3SAT recipe is satisfiable, there exists a subset in the "Subset Uniformity" issue that summarizes the objective value by choosing one variable for each fulfilled provision.

  • On the off chance that the 3SAT recipe isn't satisfiable, then there is no subset in the "Subset Correspondence" issue that summarizes to the objective worth, as any legitimate subset would have to incorporate no less than one whole number with a worth of 2, relating to a fulfilled provision.

  • Since the 3SAT issue is known to be NP-finished, this decrease lays out the NP-culmination of "Subset Equity."

Example

#include <iostream>
#include <vector>
using namespace std;

bool ThreeSAT_Satisfiable(const vector<vector<int>>& clauses) {
   return false;
}

class SubsetUniformity {
private:
   vector<int> numbers;
   int targetValue;

public:
   SubsetUniformity(const vector<int>& vars, const vector<int>& clauses) {
      for (int v : vars) {
         numbers.push_back(1);
      }
      for (int c : clauses) {
         numbers.push_back(2);
      }
      targetValue = vars.size() + clauses.size();
   }

   bool isSubsetSumPossible(int idx, int sum) {
      if (sum == targetValue) {
         return true;
      }
      if (idx >= numbers.size() || sum > targetValue) {
         return false;
      }
      return isSubsetSumPossible(idx + 1, sum) || isSubsetSumPossible(idx + 1, sum + numbers[idx]);
   }

   bool hasSolution() {
      return isSubsetSumPossible(0, 0);
   }
};

int main() {
   vector<vector<int>> clauses = {
      {1, 2, -3},
      {-1, -2, 3},
      {-1, 2, 3}
   };

   bool isSatisfiable = ThreeSAT_Satisfiable(clauses);
   SubsetUniformity su(clauses[0], clauses[1]);

   cout << "3SAT Formula is " << (isSatisfiable ? "satisfiable." : "not satisfiable.") << endl;
   cout << "Subset Uniformity has " << (su.hasSolution() ? "a" : "no") << " solution." << endl;

   return 0;
}

Output

3SAT Formula is not satisfiable.
Subset Uniformity has a solution.

Conclusion

Both of these methodologies show that "Subset Equity," or the "Subset Aggregate" issue, is NP-finished, and accordingly, tracking down a productive calculation to settle it for all examples is improbable. Scientists frequently utilize dynamic programming or other estimation procedures to effectively take care of viable occasions of this issue.

Updated on: 04-Aug-2023

88 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements