Skip to content

feat(graphs): implement linear-time 2-SAT solver with Kosaraju - #15746

Open
matheusrodrigues2006 wants to merge 3 commits into
TheAlgorithms:masterfrom
matheusrodrigues2006:feat/two-sat-solver
Open

matheusrodrigues2006 wants to merge 3 commits into
TheAlgorithms:masterfrom
matheusrodrigues2006:feat/two-sat-solver

Conversation

@matheusrodrigues2006

Copy link
Copy Markdown

Describe your change

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • Documentation change?
    Summary
    Add a dedicated 2-SAT solver in graphs/two_sat.py. The algorithm constructs an implication graph from a 2-CNF formula and uses iterative Kosaraju strongly connected components to determine whether a satisfying assignment exists.

For a clause (a OR b), the graph contains NOT a -> b and NOT b -> a. If any variable and its negation belong to the same strongly connected component, the formula is unsatisfiable; otherwise, the function returns a Boolean assignment.

The implementation includes:

  • Explicit validation of variable counts and literals;
  • Iterative DFS to avoid recursion-depth errors;
  • O(V + C) time and space complexity for V variables and C clauses;
  • Doctests for valid, invalid, satisfiable, and unsatisfiable cases;
  • A deterministic test against exhaustive truth tables on small formulas;
  • A long implication-chain test with 2,000 variables.
    This is different from the repository's general-purpose DPLL SAT solver: it uses the specialized polynomial-time solution available for 2-CNF.
    Verification
    Executed locally with Python 3.13.5:
  • python -m doctest -v graphs/two_sat.py: 13 examples passed.
  • pytest -q graphs/two_sat.py --doctest-modules: 3 tests passed.
  • A separate seeded test checked 12,000 random formulas against exhaustive truth tables and validated all returned assignments: all passed.
  • An implication chain of 2,000 variables passed without recursion errors.
    Ruff and the full upstream CI on Python 3.15t were not executed in the local environment; those checks still need to run in GitHub Actions.
    Checklist
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
    References:
  • https://cp-algorithms.com/graph/2SAT.html
  • https://en.wikipedia.org/wiki/2-satisfiability

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Nov 13, 2026
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Nov 13, 2026
@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Nov 13, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant