Home > Backend Development > C++ > body text

Subset equality is NP-complete

王林
Release: 2023-08-28 23:41:06
forward
1388 people have browsed it

Subset equality is NP-complete

Subset correspondence, also known as the "subset total" problem, is an exemplary NP-complete computational problem. Given a bunch of numbers and an objective value, the task is to determine whether there exists a subset of numbers whose total number equals the objective value. The NP-capacity of this problem stems from its ability to solve a variety of other NP-complete problems by polynomial-time reduction. Regardless of its simple definition, there is no efficient computation that can solve the "subset correspondence" of all events, which makes it of great significance in hypothetical software engineering and simplification, and in different fields (e.g. cryptography, asset allocation and dynamic problems) with functional applications.

usage instructions

  • Reduction of subset sum

  • Reduced from 3SAT

Reduce from subset sum

One way to deal with "subset fairness" being an NP-complete problem is to show a significant reduction in NP-complete problems (the "total subsets" problem).

algorithm

  • Given a case of "subset aggregation" problem, it is a bunch of integers S and a target of value T.

  • Use a similar set S and target self-esteem 2T to make another case of the "subset equity" problem.

  • If there is a subset of S that is summarized as T in the "subset aggregation" problem, then at this time, there will be a subset 2T that is summarized as T in the "subset uniformity" problem by adding it to itself Similar subsets.

  • Assuming that there is no subset of S that summarizes to T in the "subset total" problem, then there is no subset that summarizes to 2T in the "subset fairness" problem, because any subset with a total The sum of subsets below 2T cannot exceed 2T.

  • This decline shows that solving the "subset fairness" problem is almost as difficult as solving the "subset aggregation" problem, making it an NP-complete problem.

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;
}
Copy after login

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.
Copy after login

Reduced from 3SAT

Another approach is to prove that "subset correspondence" is NP-complete by subtracting it directly from a known NP-complete problem (such as the 3SAT problem).

algorithm

  • gives an example of a 3SAT question containing a Boolean recipe in a joint ordinary structure with three literals per condition.

  • Let’s discuss the “subset uniformity” problem again with a bunch of integers and target values, as follows:

  • a.For each variable in the 3SAT equation, create a number with the value 1 in the set.

    b. For each additional condition in the 3SAT equation, generate a number with a value of 2 in the set.

    c. Set the target value to the full quantity of all additional conditions and all factors in the 3SAT recipe.

  • If the 3SAT scheme can be satisfied, there is a subset in the "subset uniformity" problem that summarizes the target value by choosing one variable for each satisfied condition.

  • If the 3SAT formula cannot be satisfied, then no subset in the "subset correspondence" problem can be generalized to the target value, because any legal subset must contain no less than an integer with a value of 2, which is consistent with the Relevant to the terms of performance.

  • Since the 3SAT problem is known to be completed in NP, this drop indicates the NP peak 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;
}
Copy after login

Output

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

in conclusion

Both approaches suggest that the "subset equity" or "subset aggregation" problem is NP-complete, and therefore it is impossible to track efficient computations to solve the problem for all examples. Scientists often utilize dynamic programming or other estimation procedures to efficiently solve feasible scenarios for this problem.

The above is the detailed content of Subset equality is NP-complete. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!