Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 477139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:33:39+00:00 2026-05-13T00:33:39+00:00

I have been looking for a while, but I just can’t seem to find

  • 0

I have been looking for a while, but I just can’t seem to find any implementation of the 2-Sat algorithm.

I am working in c++ with the boost library (which has a strongly connected component module) and need some guidance to either create an efficient 2-Sat program or find an existing library for me to utilise through c++.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T00:33:39+00:00Added an answer on May 13, 2026 at 12:33 am

    I suppose you know how to model a 2-Sat problem to solve it with SCC.
    The way I handle vars and its negation isn’t very elegant, but allows a short implementation:
    Given n variables numbered from 0 to n-1, in the clauses -i means the negation of variable i, and in the graph i+n means the same (am I clear ?)

    #include <boost/config.hpp>
    #include <iostream>
    #include <vector>
    #include <boost/graph/strong_components.hpp>
    #include <boost/graph/adjacency_list.hpp>
    #include <boost/foreach.hpp>
    
    typedef std::pair<int, int> clause;
    
    //Properties of our graph. By default oriented graph
    typedef boost::adjacency_list<> Graph;
    
    
    const int nb_vars = 5;
    
    int var_to_node(int var)
    {
        if(var < 0)
            return (-var + nb_vars);
        else
            return var;
    }
    
    int main(int argc, char ** argv)
    {
        std::vector<clause> clauses;
        clauses.push_back(clause(1,2));
        clauses.push_back(clause(2,-4));
        clauses.push_back(clause(1,4));
        clauses.push_back(clause(1,3));
        clauses.push_back(clause(-2,4));
    
        //Creates a graph with twice as many nodes as variables
        Graph g(nb_vars * 2);
    
        //Let's add all the edges
        BOOST_FOREACH(clause c, clauses)
        {
            int v1 = c.first;
            int v2 = c.second;
            boost::add_edge(
                    var_to_node(-v1),
                    var_to_node(v2),
                    g);
    
            boost::add_edge(
                    var_to_node(-v2),
                    var_to_node(v1),
                    g);
        }
    
        // Every node will belong to a strongly connected component
        std::vector<int> component(num_vertices(g));
        std::cout << strong_components(g, &component[0]) << std::endl;
    
        // Let's check if there is variable having it's negation
        // in the same SCC
        bool satisfied = true;
        for(int i=0; i<nb_vars; i++)
        {
            if(component[i] == component[i+nb_vars])
                satisfied = false;
        }
        if(satisfied)
            std::cout << "Satisfied!" << std::endl;
        else
            std::cout << "Not satisfied!" << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been looking for a while now but I can not find an
I have been looking for a while now to find a solution to accomplish
I have been looking at using TDD and implementing proper testing (only just started
I've been looking around for a while how can I hotdeploy my projects when
I've been looking for this for a while, but haven't found an answer (blame
Well, I've been looking around and can't find a specific way to actually DISPLAY
I've been looking around for a while, and I have not been able to
I've been looking for a solution to this for a while now, have found
Ok, so my question is something I have been looking for for a while.
Have been looking at the MVC storefront and see that IQueryable is returned from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.