Ok, I can't get around 2 ^ N, but I can reduce the sample set. To do this, we compute "Constrained Constraints." Compost. A restriction is a restriction if, if all parameters on the left side are selected, then none of the options on the right side can be selected, but no other restrictions based on the parameters of the left side can be applied.
We need to compute the set of all possible composite constraints from the set of constraints. Although not necessary, we will “fix” the existing restrictions by replacing the left and right hands if the group of the right hand is smaller than the group of the left. This can reduce some of the tight constraints, although more efficient heuristics for exchange are possible.
We also need to calculate the “minimum set” of options that can be arbitrarily selected for each group. This minimum set is calculated by removing from the list of available options any parameters that appear on the left side of the configured restriction.
The algorithm follows, but I do not prove that it calculates CC correctly. I will prove that if this happens, then they can be used to calculate the number of possible combinations.
Now you can calculate the number of combinations with the formula below. Let a CC call consist of a folded constraint. Then the number of combinations:
C(Mininum Set) + CCC1 + ... + CCCn
Where:
- C (minimum set) - the number of possible combinations with a minimum set.
- CCCx is the number of combinations, possibly taking the minimum set, replacing any groups for which there is an option in the left hand of CCx with this option, and then deleting any parameters on the right side of CCx.
Note that the expression is purely additive. This means that in order for this expression to produce the expected result, the following two conditions must be met:
- There are no two terms that may contain the same combination.
- All combinations must be subject to these conditions.
- Invalid combinations may be obtained by any term.
For the first proof, note that there are no two different CCs with the same left hand. If two CCs have the same left hand but different right hands, this means that there is a restriction on the addition that should apply to one of the CCs, or an unacceptable restriction applied to the other.
Since there are no two CCs with the same left hand, and the minimum set does not contain the left hand of any CC by definition, any two CCs can be selected with at least one parameter that is selected for one, but not for the other. Therefore, no two CCs can give the same combination.
In the second proof, we note that the set CCs contains, by definition, all valid combinations for the options on the left.
Suppose that there is one combination that does not appear in either the minimum set or the CC set. If this combination does not contain a left parameter, then it should be a combination of the minimum set by definition. Therefore, it should contain options from the left hand.
Since the set of CCs contains all valid combinations for the left hand, there is one CC with the same left options. Therefore, this combination should have an option that is not included in any combination for this CC. But the only options not included in this CC are those that appear in the left hand for other CCs, and those that should be excluded from it by restrictions. Since this is not so, then this combination cannot exist.
For the third proof, we first consider the minimal set. The minimum set does not contain any parameters in the left hand of any group. Since all restrictions are between one left and one right parameter, no restriction applies to the minimum set.
Now consider the SS. By definition, CC has a valid combination of left options. Any option that is incompatible with the left hand should be displayed in the right hand, and any option from this right hand should be removed from the minimum set. Since there are no options on the minimum set, where it is incompatible with each other for starters, there can be an unsatisfied restriction in any combination on CC.
And this completes the proof.
Let's see how this applies with the example from the comments:
G1: x1, x2, x3 G2: y1, y2 G3: z1, z2, z3 R1: x1 <-> y2 R2: x3 <-> y2 R3: y1 <-> z1 R4: y2 <-> z2 R5: y2 <-> z3 CC1: {x1} <-> {y2} CC2: {x3} <-> {y2} CC3: {y1} <-> {z1} CC4: {x1, y1} <-> {y2, z1} CC5: {x3, y1} <-> {y2, z1} CC6: {y2} <-> {z2, z3}
Briefly think about non-list compound groups:
R1&R2: {x1, x3} <-> {y2} -- not in the list because x1 and x3 belongs to the same group R1&R5: {x1, y2} <-> {y2} -- not in the list because the left hand of R2, y2 appears in the right hand of R1
Now let's see what options are possible in each set:
Minimum Set: (x2), (), (z1, z2, z3) CC1: (x1), (), (z1, z2, z3) -- replace G1 with x1, remove y2 from G2 CC2: (x3), (), (z1, z2, z3) -- replace G1 with x3, remove y2 from G2 CC3: (x2), (y1), (z2, z3) -- replace G2 with y1, remove z1 from G3 CC4: (x1), (y1), (z2, z3) -- replace G1 with x1, G2 with y1, remove y2 and z1 CC5: (x3), (y1), (z2, z3) -- replace G1 with x3, G2 with y1, remove y2 and z1 CC6: (x2), (y2), (z1) -- replace G2 with y2, remove z2 and z3 from G3
Now add things up:
C(Minimum Set) = 1 * 0 *3 = 0 CCC1 = 1 * 0 * 3 = 0 CCC2 = 1 * 0 * 3 = 0 CCC3 = 1 * 1 * 2 = 2 CCC4 = 1 * 1 * 2 = 2 CCC5 = 1 * 1 * 2 = 2 CCC6 = 1 * 1 * 1 = 1 C(Minimum Set) + CCC1 + CCC2 + CCC3 + CCC4 + CCC5 + CCC6 0 + 0 + 0 + 2 + 2 + 2 + 1 = 7
I will add one more thought. Despite the fact that there are only 6 CCCs for 5 rules, which are much less than 32 otherwise expected 32, these CCCs are calculated with the worst time of the least time, i.e., for each rule you must compare and combine it with all created still CCC. You can think of them as binary numbers, where the bit is set if the rule is combined and not set if not.
However, incompatible combinations are immediately discarded, so that combining each new rule does not waste time on combinations that are already considered invalid. In addition, by sorting the rules before the start, consecutive rules in the same group can be dropped without testing for incompatibility with the correct data structures.
As this specific example shows, the average time can be much better than 2 ^ N.
Alternative Algorithms and Considerations
They talk about 2-SAT and 3-SAT. It seems to me that this is a 2-SAT problem, in the sense that every restriction a b is actually a sentence "! A ||! B". Thus, all restrictions together can simply be written as "(! X1 ||! Y2) && (! X1 ||! Z4) && (! Y2 & &! Z3)", etc. This means you can "solve" it in the sense that you can find out if there is a logical purpose for each option that changes this value. There is a linear algorithm for this Aspall, Plass and Tarjan, with a slide presentation here .
But knowing whether restrictions can be allowed or not is not specified . The number of ways to set all the parameters was set while maintaining the problem with 2-SAT.
Effective algorithms now exist for counting the number of ways to satisfy the 2-SAT problem. For example, this article presents an algorithm that runs at 1.2561 n . But even this will not help us, since we need to know which solutions should be able to calculate the number of combinations that satisfy this solution.
According to Wikipedia, this article has an algorithm that effectively lists all the solutions that we want. But if the calculation is already exponential, it will be listed. Better than 2 n is possible, but still exponentially.
If we list all the solutions to the 2-SAT problem, the number of combinations for each group will be set to 1 times the number of free options that are not displayed in any restriction, for each group for which there is no choice was chosen by the solution.
For example, taking the previous set of groups and restrictions. The 2-SAT problem, including mutual exclusion, is this:
(! x1 ||! y2) && (! x3 ||! y2) && (! y1 ||! z1) && (! y2 ||! z2) && (! y2 ||! z3) & & (! x1 | |! x3) && (! y1 ||! y2) && (! z1 ||! z2) && (! z1 ||! z3) && (! z2 ||! z3)
The first line is the five rules. The second line is the mutual exclusion of all parameters in the same group that appear in the restriction rules.
The solutions to these problems with 2-SAT are:
x1 x3 y1 y2 z1 z2 z3 Combinations true false true false false true false 1 true false true false false false true 1 true false true false false false false 0 true false false false true false false 0 true false false false false true false 0 true false false false false false true 0 true false false false false false false 0 false true true false false true false 1 false true true false false false true 1 false true true false false false false 0 false true false false true false false 0 false true false false false true false 0 false true false false false false true 0 false true false false false false false 0 false false true false false true false 1 false false true false false false true 1 false false true false false false false 0 false false false true true false false 1 false false false true false false false 0 false false false false true false false 0 false false false false false true false 0 false false false false false false true 0 false false false false false false false 0
In the first two solutions there are no groups without the selected option, therefore the number of combinations is 1. The third solution does not have the option selected for the G3 group, so we multiply 1 by 0. In the lines that start with "false false", no group was selected for the G1 group no options: x2. Therefore, we multiply 1 by 1 for them and by 0 if there is no option for G2 or G3 (in this case, the number of free options is 0).
Now the question is how to include one option that will be selected in each group, and still claim that it is a 2-SAT. The problem, as indicated, has two implicit restrictions: for each group, one and only one must be selected. These two restrictions can be written as:
x 1 || x 2 || x 3 (for group x with options x 1 .. x 3 )
(! x 1 ||! x 2 ) && & && & & (! x 1 ||! x 3 ) && & & (! x 2 ||! x 3 )
The later limitation is 2-SAT, the first of which is 3-SAT for any non-trivial case. Be that as it may, I do not apply the first restriction, but then the counter becomes 0. The counting algorithm should look like this:
- For unlimited combinations, multiply the number of unlimited restrictions in each group by each other.
- For combinations with full limitation, add the result of the following calculations:
- For each solution, multiply the number of restrictions without restrictions in each group without the option judged to be “true” for each other.
So, for each group in which there is at least one parameter without restrictions, the choice is implicit and anonymous. For each group in which all parameters are part of some restriction, if no parameter has been selected, the count for this group becomes 0, and therefore the number of combinations for this solution also becomes 0.
This is similar to tricking the problem out of the> 2-SAT limit. In the end, if it was possible, then the 3-SAT problem could be solved simply by listing the solutions of the 2-SAT part of it, and then discarding all that do not satisfy its 3-SAT part. Alas, there is one fundamental difference that I can determine:
- All predicates that are not resolved by the 2-SAT part of the problem do not contain any additional restrictions.
Given this rather restrictive restriction on offers, we can solve this problem by listing solutions to the explicit 2-SAT restrictions.
If someone wants to continue this, continue. I am satisfied with the improvement of the proposed solution 2 n .