Giter VIP home page Giter VIP logo

twosat's Introduction

TwoSAT

A 2SAT solver, written in Rust. The 2SAT language is the set of 2CNFs $\phi$ that are satisfiable. Specifically, $\phi\in 2SAT$ iff $\phi$ is a Boolean formula for which there exists at least one assignment of variables that makes $\phi$ true, and $\phi$ is of the form $\phi = C_1\wedge C_2\wedge ... \wedge C_n$ where each $C_i$ is a disjunction of at most 2 literals (i.e., an expression $a$ or $(a\vee b)$ where $a$ and $b$ are of the form $x_i$ or $\neg x_i$ for any input Boolean variable $x_i$).

The generalized problem $SAT$, of satisfiable Boolean formulas that are not restricted to be 2CNFs, is well known to be $NP$-Complete. However, 2SAT can be solved in linear time, and thus is definitely in $P$.

Usage

An input formula is specified using ~ for logical negation, & for logical AND, and | for logical OR. Variables may be any alphanumeric string, not starting with a number. Then, the input must be specified as a 2CNF, where clauses (ORs) with 2 variables are enclosed in parentheses, and all clauses are conjoined by &. & must not appear within parentheses, only | can. | cannot appear outside parentheses. Parentheses may not be nested. Some valid formula strings:

"(a|b)&~c&(~a|~d)&(a|a)&c"
"(x1|x2)&(x3|~x4)"

Some invalid formula strings (they are not 2CNFs):

"a|b"
"(a&b)|(c&d)"
"(a|b|c)"
"a&(b|c)&((c|a)&d|e)"

Note that my parser removes all whitespace, so an input like "hello there & (a | hello there)" (which is objectively terrible variable naming), is treated as "hellothere&(a|hellothere)" where the two variables are a and hellothere.

Then, if your correctly specified formula is <phi>, check if it's satisfiable with

$ cargo run <phi>

For example,

$ cargo run "(a|b)&(~a|d)&(b|c)&(~b|d)&(~b|e)&(~d|~c)&(~e|~d)"
Formula is not SAT

$ cargo run "(x1|x2) & (x1|~x3) & (~x1|~x2) & (x1|x4) & (~x1|~x5)"
Formula is SAT with assignment {"x4": true, "x3": false, "x1": false, "x2": true, "x5": false}

Repo Trivia

If you are curious enough to search through the git history of this repository you will find that I initially implemented this 2SAT solver using the logical rule of resolution. However, it ran in $O(n^4)$ time or something like that, incredibly inefficient (it was still polytime though!). Regardless, after learning about a linear time solution to 2SAT using strongly connected components of its implication graph (see Wikipedia), I decided to convert my solution to this more efficient one that other people had figured out, something more respectable to publish online.

twosat's People

Contributors

spectraldoy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.