2-sat problem and its algorithm

Source: Internet
Author: User

Original address: Http://www.cppblog.com/MatoNo1/archive/2011/07/13/150766.aspx

"2-sat Problem"
An existing sequence of N Boolean values, given a number of constraints, such as a[x] and a[y]=0, a[x] or a[y] or a[z]=1, to determine the value of a[0..n-1], so that it satisfies all the constraints of the relationship. This is called the SAT problem, and, in particular, if the limit is limited to only two elements in each restriction relationship, it is called a 2-sat problem.

There are 11 possible limiting relationships due to the limitation of up to two elements in the 2-sat problem:
A[X]
Not a[x]
A[X] and A[y]
A[X] and not a[y]
A[X] OR A[y]
A[X] OR not a[y]
Not (A[x] and a[y])
Not (A[x] OR a[y])
A[X] XOR A[y]
Not (A[x] XOR a[y])
A[X] XOR not a[y]
Further, A[x] and A[y] equivalent (a[x]) and (A[y]) (i.e. can be split into a[x] and A[y] two limiting relationships), not (A[x] OR a[y]) equivalent to not a[x] and not a[y] (that is, can be split into not a[x] With not a[y] two limit relationships). As a result, there are up to 9 possible limiting relationships.

In the actual problem, the 2-sat problem is manifested in the following form most of the time: there are n pairs of items, each pair of items must be selected one, and there are some restrictions between them (such as a two items can not be selected, a certain two items can not be selected, a two items must and only one choice, An item must be selected), etc., at this time, each pair of items can be treated as a Boolean value (select the first item is equivalent to 0, choose the second equivalent of 1), if all the limiting relationship to limit the maximum of two items, they can be converted into 9 basic constraints, and thus into the 2-SAT model.

"Modeling"
In fact, the modeling of 2-sat problem is very similar to the actual problem.
Set up a 2N-order, with the points divided into n pairs, each pair of points represents the Boolean sequence a of an element of 0, 1 value (the following will represent A[i] 0 value Point is called I, the point representing A[i] 1 value is called I '). It is clear that each pair of points must be selected and only one. Then, the edges in the diagram have a specific meaning. If there is an edge <i in the figure, J&gt, then I must choose J. It can be found that in the above 9 restriction relationships, the latter 7 kinds of two-yuan restriction relationship can be implemented with a connecting edge, such as not (A[x] and a[y]) need to connect two edges <x, y ' > and <y, X ' >,a[x] or a[y] need to even two edges <x ', y > and <y ', x>. And the first two kinds of one-dimensional relationship, for A[x] (that is, X is required), can be achieved through the <x ', x>, and not a[x] (that is, X is not selectable), can be achieved through the edge <x, X ' >.

"O (NM) algorithm: Finding the solution with the smallest dictionary order"
According to the definition of the 2-sat built in the diagram, it can be found that if I to J has a path, if I select, then J also to choose, or if J is not selected, I also can not choose;
Therefore, a very intuitive algorithm is obtained:
(1) Set a state for each point v,v=0 is not determined, v=1 indicates that the selection is determined, v=2 indicates that the selection is not selected. Called a dot ishave determinedIf and only if its V value is not 0. Set up two queues Q1 and Q2, respectively, to store the number of points to be selected and the number of points to try to select.
(2) If all the points in the diagram have been determined, then find a set of solutions, end, otherwise, will Q1, Q2 empty, and select an indeterminate point I, will I join the queue Q1, I ' join the queue Q2;
(3) Find all successors of I. For subsequent J, if J is not determined, J is added to the queue Q1; if J ' (where J ' refers to another point with J at the same pair) is not determined, then J ' is added to the queue Q2;
(4) Traverse each point in the Q2, find all the previous trend of the point (need to first build a complement map), if the previous trend is not determined, it is added to the queue Q2;
(5) in the (3) (4) step, one of the following occurs, the attempt fails, or the attempt succeeds:
<1> a point that has been added to the queue Q1 is added to the queue Q2;
<2> a point that has been added to the queue Q2 is added to the queue Q1;
<3> the status of a J is 2;
<4> A status of a I ' or J ' is 1 or a pre-tending state of one I ' or J ' is 1;
(6) If this attempt succeeds, then the state of all points in the Q1 will be changed to 1, the Q2 a bit of the state to 2, turn (2), or try to point I ', if still fail the problem has no solution.
The algorithm has a time complexity of O (NM) (worst case attempt at all points, each attempt to traverse all edges), but in most cases it falls far short of this upper bound.
When implemented, you can use an array of VST to represent queues Q1 and Q2. Set up two flag variables I1 and i2 (requirements for different i,i1 and I2 are different, this can avoid each attempt to initialize once, save time), if vst[i]=i1 means I has been added Q1, if Vst[i]=i2 said I has been added Q2. However, Q1 and Q2 are still to be established, because the traversal (BFS) requires a queue, in order to prevent repeated traversal, the addition of the Q1 (or Q2) points in the VST value must not be equal to I1 (or i2). If there is a contradiction in the middle, abort the attempt immediately and declare failure.

Although in most cases the complexity of the algorithm does not reach O (NM), the overall performance is still less than the O (M) algorithm below. However, the algorithm has a very important use: to find the smallest dictionary order solution!
If the same pairs of points in the original are numbered consecutively (01, 23, 45 ...). ) You can try the No. 0 pair, the 1th pair ... in turn. Point, each pair of points in the first attempt to number small, if the failure and then try to number large. This makes it possible to find the smallest solution of the dictionary order (if there is a solution), because once a point is determined, it cannot be changed.
If the same pair of points in the original image is not contiguous (for example, 03, 25, 14 ...). ) then according to the number of points in the number of dots in ascending order to sort each pair of points, and then scan each point after sorting, first try its number of small points, if the success of the point selected, otherwise try to number a large point, if the success is selected, otherwise (all failed) no solution.

"O (M) algorithm"
According to the symmetry of the graph, all the strongly connected branches of the graph can be shrunk to a single point (because the points in the strongly connected branch are either selected or not selected), and then follow the topology in reverse order (each time a point of 0 is found, when the branch adjacency diagram is constructed, all edges are reversed) traverse the branch adjacency graph. This point (the connected branch of the expression) is selected and all its opposing points (note that there may be multiple interconnected branches of the connected branch, if for two connected branches S1 and S2, point I in S1, point I ' in the S2, then S1 and S2 opposites) and these opposing points are all marked as not selected, Until all points are marked. There will inevitably be no contradiction in this process (detailed proof of the process omitted, in the paper).
No solution: If all strong branches are found, there are points I and I ' are in the same branch, then there is no solution, otherwise there must be solutions.
Time complexity: O (m), time complexity of topological sequencing O (m), Total time complexity O (m) for strong branch time complexity.

The time complexity of the algorithm is low, but it can only find out any set of solutions, and can not guarantee the minimum dictionary order of the solution. Of course, if the original title does not need to ask for a specific solution, only need to determine whether there is a solution (some of the problem is two points + 2-sat), of course, should use this algorithm, as long as the strong Connectivity branch (Kosaraju, Tarjan can, recommend the latter) can be.

2-sat problem and its algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.