Home > Article > Backend Development > 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.
Reduction of subset sum
Reduced from 3SAT
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).
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.
#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; }
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.
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).
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".
#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; }
3SAT Formula is not satisfiable. Subset Uniformity has a solution.
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!