I learned the 2-Sat algorithm today.
A Brief Introduction to an algorithm
First, it should solve the problem: (I personally understand) There are n items, take or not take, two States; there are m conditions, each condition limits how to take, for example, if item 1 is used, item 3 cannot be used, or item 1 must be used as item 3; Item 2-Sat algorithm can be used to obtain the item 3, and whether the item has a solution.
The following is the algorithm content of this province:
Graph creation:
In fact, the essence of an algorithm is to find the necessary conditions, for example, X1 | X2. That is to say, if this condition is true, when X1 is negative, X2 must be certain, when X2 is negative, the X1 ratio is Ken. Therefore, a directed graph is created to split each variable into two vertices. 2I indicates true and 2I + 1 indicates no. For the preceding example, then we can get two sides. The negative vertex of X1 points to the positive vertex of X2, and the negative vertex of X2 points to the negative vertex of X1. Therefore, each condition must have two sides.
Search:
For the I variable, if 2I is true, 2I and all the edges it can reach are marked as true. If two vertices of a variable are marked during this process, 2I is true or false (because a variable can only be true or false, and cannot be true or false at the same time). Change the value of 2I + 1 to true and set 2I + 1 to true, if there are still contradictions, there is no solution