2-SAT study notes

Source: Internet
Author: User

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

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.