Пример #1
0
def PerfectMatchingPrinciple(G):
    """Generates the clauses for the graph perfect matching principle.
    
    The principle claims that there is a way to select edges to such
    that all vertices have exactly one incident edge set to 1.

    Parameters
    ----------
    G : undirected graph

    """
    cnf=CNF()

    # Describe the formula
    name="Perfect Matching Principle"
    
    if hasattr(G,'name'):
        cnf.header=name+" of graph:\n"+G.name+"\n"+cnf.header
    else:
        cnf.header=name+".\n"+cnf.header

    def var_name(u,v):
        if u<=v:
            return 'x_{{{0},{1}}}'.format(u,v)
        else:
            return 'x_{{{0},{1}}}'.format(v,u)
            
    # Each vertex has exactly one edge set to one.
    for v in enumerate_vertices(G):

        edge_vars = [var_name(u,v) for u in neighbors(G,v)]
        cnf.add_equal_to(edge_vars,1)

    return cnf
Пример #2
0
def CountingPrinciple(M, p):
    """Generates the clauses for the counting matching principle.

    The principle claims that there is a way to partition M in sets of
    size p each.

    Arguments:
    - `M`  : size of the domain
    - `p`  : size of each class

    """
    cnf = CNF()

    # Describe the formula
    name = "Counting Principle: {0} divided in parts of size {1}.".format(M, p)
    cnf.header = name + "\n" + cnf.header

    def var_name(tpl):
        return "Y_{{" + ",".join("{0}".format(v) for v in tpl) + "}}"

    # Incidence lists
    incidence = [[] for _ in range(M)]
    for tpl in combinations(list(range(M)), p):
        for i in tpl:
            incidence[i].append(tpl)

    # Each element of the domain is in exactly one part.
    for el in range(M):
        edge_vars = [var_name(tpl) for tpl in incidence[el]]
        cnf.add_equal_to(edge_vars, 1)

    return cnf
Пример #3
0
def CountingPrinciple(M,p):
    """Generates the clauses for the counting matching principle.
    
    The principle claims that there is a way to partition M in sets of
    size p each.

    Arguments:
    - `M`  : size of the domain
    - `p`  : size of each class

    """
    cnf=CNF()

    # Describe the formula
    name="Counting Principle: {0} divided in parts of size {1}.".format(M,p)
    cnf.header=name+"\n"+cnf.header

    def var_name(tpl):
        return "Y_{{"+",".join("{0}".format(v) for v in tpl)+"}}"

    # Incidence lists
    incidence=[[] for _ in range(M)]
    for tpl in combinations(range(M),p):
        for i in tpl:
            incidence[i].append(tpl)
    
    # Each element of the domain is in exactly one part.
    for el in range(M):
        edge_vars = [var_name(tpl) for tpl in incidence[el]]
        cnf.add_equal_to(edge_vars,1)

    return cnf
Пример #4
0
def TseitinFormula(graph,charges=None):
    """Build a Tseitin formula based on the input graph.

    Odd charge is put on the first vertex by default, unless other
    vertices are is specified in input.

    Arguments:
    - `graph`: input graph
    - `charges': odd or even charge for each vertex
    """
    V=enumerate_vertices(graph)

    if charges==None:
        charges=[1]+[0]*(len(V)-1)             # odd charge on first vertex
    else:
        charges = [bool(c) for c in charges]   # map to boolean

    if len(charges)<len(V):
        charges=charges+[0]*(len(V)-len(charges))  # pad with even charges

    # init formula
    tse=CNF()
    for e in sorted(graph.edges(),key=sorted):
        tse.add_variable(edgename(e))

    # add constraints
    for v,c in zip(V,charges):
        
        # produce all clauses and save half of them
        names = [ edgename((u,v)) for u in neighbors(graph,v) ]
        for cls in parity_constraint(names,c):
            tse.add_clause(list(cls),strict=True)

    return tse
Пример #5
0
def PerfectMatchingPrinciple(graph):
    """Generates the clauses for the graph perfect matching principle.
    
    The principle claims that there is a way to select edges to such
    that all vertices have exactly one incident edge set to 1.

    Arguments:
    - `graph`  : undirected graph

    """
    cnf=CNF()

    # Describe the formula
    name="Perfect Matching Principle"
    
    if hasattr(graph,'name'):
        cnf.header=name+" of graph:\n"+graph.name+"\n"+cnf.header
    else:
        cnf.header=name+".\n"+cnf.header

    def var_name(u,v):
        if u<=v:
            return 'x_{{{0},{1}}}'.format(u,v)
        else:
            return 'x_{{{0},{1}}}'.format(v,u)
            
    # Each vertex has exactly one edge set to one.
    for v in graph.nodes():

        edge_vars = [var_name(u,v) for u in graph.adj[v]]

        for cls in equal_to_constraint(edge_vars,1):
            cnf.add_clause(cls)

    return cnf
Пример #6
0
 def test_one_eq(self) :
     opb="""\
     * #variable= 5 #constraint= 1
     *
     +1 x1 +1 x2 +1 x3 +1 x4 +1 x5 = 2;
     """
     F=CNF()
     F.add_equal_to(["a","b","c","d","e"],2)
     self.assertCnfEqualsOPB(F,opb)
Пример #7
0
 def test_weighted_gt(self) :
     opb="""\
     * #variable= 5 #constraint= 1
     *
     +1 x1 +2 x2 +3 x3 -1 x4 -2 x5 >= 3;
     """
     F=CNF()
     F.add_linear(1,"a",2,"b",3,"c",-1,"d",-2,"e",">",2)
     self.assertCnfEqualsOPB(F,opb)
Пример #8
0
 def test_one_eq(self):
     opb = """\
     * #variable= 5 #constraint= 1
     *
     +1 x1 +1 x2 +1 x3 +1 x4 +1 x5 = 2;
     """
     F = CNF()
     F.add_equal_to(["a", "b", "c", "d", "e"], 2)
     self.assertCnfEqualsOPB(F, opb)
Пример #9
0
 def test_one_leq(self):
     opb = """\
     * #variable= 5 #constraint= 1
     *
     -1 x1 -1 x2 -1 x3 -1 x4 -1 x5 >= -2;
     """
     F = CNF()
     F.add_less_or_equal(["a", "b", "c", "d", "e"], 2)
     self.assertCnfEqualsOPB(F, opb)
Пример #10
0
 def test_one_gt(self):
     opb = """\
     * #variable= 5 #constraint= 1
     *
     +1 x1 +1 x2 +1 x3 +1 x4 +1 x5 >= 3;
     """
     F = CNF()
     F.add_strictly_greater_than(["a", "b", "c", "d", "e"], 2)
     self.assertCnfEqualsOPB(F, opb)
Пример #11
0
 def test_one_clause(self):
     opb = """\
     * #variable= 4 #constraint= 1
     *
     +1 x1 +1 x2 -1 x3 -1 x4 >= -1;
     """
     F = CNF()
     F.add_clause([(True, "a"), (True, "b"), (False, "c"), (False, "d")])
     self.assertCnfEqualsOPB(F, opb)
Пример #12
0
 def test_weighted_gt(self):
     opb = """\
     * #variable= 5 #constraint= 1
     *
     +1 x1 +2 x2 +3 x3 -1 x4 -2 x5 >= 3;
     """
     F = CNF()
     F.add_linear(1, "a", 2, "b", 3, "c", -1, "d", -2, "e", ">", 2)
     self.assertCnfEqualsOPB(F, opb)
Пример #13
0
 def test_weighted_leq(self):
     opb = """\
     * #variable= 5 #constraint= 1
     *
     -1 x1 -2 x2 -3 x3 +1 x4 +2 x5 >= -2;
     """
     F = CNF()
     F.add_linear(1, "a", 2, "b", 3, "c", -1, "d", -2, "e", "<=", 2)
     self.assertCnfEqualsOPB(F, opb)
Пример #14
0
 def test_one_lt(self):
     opb = """\
     * #variable= 5 #constraint= 1
     *
     -1 x1 -1 x2 -1 x3 -1 x4 -1 x5 >= -1;
     """
     F = CNF()
     F.add_strictly_less_than(["a", "b", "c", "d", "e"], 2)
     self.assertCnfEqualsOPB(F, opb)
Пример #15
0
 def test_one_lt(self) :
     opb="""\
     * #variable= 5 #constraint= 1
     *
     -1 x1 -1 x2 -1 x3 -1 x4 -1 x5 >= -1;
     """
     F=CNF()
     F.add_strictly_less_than(["a","b","c","d","e"],2)
     self.assertCnfEqualsOPB(F,opb)
Пример #16
0
 def test_one_gt(self) :
     opb="""\
     * #variable= 5 #constraint= 1
     *
     +1 x1 +1 x2 +1 x3 +1 x4 +1 x5 >= 3;
     """
     F=CNF()
     F.add_strictly_greater_than(["a","b","c","d","e"],2)
     self.assertCnfEqualsOPB(F,opb)
Пример #17
0
 def test_one_leq(self) :
     opb="""\
     * #variable= 5 #constraint= 1
     *
     -1 x1 -1 x2 -1 x3 -1 x4 -1 x5 >= -2;
     """
     F=CNF()
     F.add_less_or_equal(["a","b","c","d","e"],2)
     self.assertCnfEqualsOPB(F,opb)
Пример #18
0
 def test_one_clause(self) :
     opb="""\
     * #variable= 4 #constraint= 1
     *
     +1 x1 +1 x2 -1 x3 -1 x4 >= -1;
     """
     F=CNF()
     F.add_clause([(True,"a"),(True,"b"),(False,"c"),(False,"d")])
     self.assertCnfEqualsOPB(F,opb)
Пример #19
0
 def test_weighted_leq(self) :
     opb="""\
     * #variable= 5 #constraint= 1
     *
     -1 x1 -2 x2 -3 x3 +1 x4 +2 x5 >= -2;
     """
     F=CNF()
     F.add_linear(1,"a",2,"b",3,"c",-1,"d",-2,"e","<=",2)
     self.assertCnfEqualsOPB(F,opb)
Пример #20
0
 def test_strict_clause_insertion(self):
     F=CNF()
     F.mode_strict()
     F.add_variable("S")
     F.add_variable("T")
     F.add_variable("U")
     self.assertTrue(len(list(F.variables()))==3)
     F.add_clause([(True,"S"),(False,"T")])
     F.add_clause([(True,"T"),(False,"U")])
     self.assertRaises(ValueError, F.add_clause,
                       [(True,"T"),(False,"V")])
Пример #21
0
 def test_parity(self) :
     opb="""\
     * #variable= 3 #constraint= 4
     *
     +1 x1 +1 x2 +1 x3 >= 1;
     +1 x1 -1 x2 -1 x3 >= -1;
     -1 x1 +1 x2 -1 x3 >= -1;
     -1 x1 -1 x2 +1 x3 >= -1;
     """
     F=CNF()
     F.add_parity(["a","b","c"],1)
     self.assertCnfEqualsOPB(F,opb)
Пример #22
0
def EvenColoringFormula(G):
    """Even coloring formula

    The formula is defined on a graph :math:`G` and claims that it is
    possible to split the edges of the graph in two parts, so that
    each vertex has an equal number of incident edges in each part.

    The formula is defined on graphs where all vertices have even
    degree. The formula is satisfiable only on those graphs with an
    even number of vertices in each connected component [1]_.

    Arguments
    ---------
    G : networkx.Graph 
       a simple undirected graph where all vertices have even degree

    Raises
    ------
    ValueError
       if the graph in input has a vertex with odd degree

    Returns
    -------
    CNF object

    References
    ----------
    .. [1] Locality and Hard SAT-instances, Klas Markstrom
       Journal on Satisfiability, Boolean Modeling and Computation 2 (2006) 221-228

    """
    F = CNF()
    F.mode_strict()
    F.header = "Even coloring formula on graph " + G.name + "\n" + F.header

    def var_name(u, v):
        if u <= v:
            return 'x_{{{0},{1}}}'.format(u, v)
        else:
            return 'x_{{{0},{1}}}'.format(v, u)

    for (u, v) in enumerate_edges(G):
        F.add_variable(var_name(u, v))

    # Defined on both side
    for v in enumerate_vertices(G):

        if G.degree(v) % 2 == 1:
            raise ValueError(
                "Markstrom formulas requires all vertices to have even degree."
            )

        edge_vars = [var_name(u, v) for u in neighbors(G, v)]

        # F.add_exactly_half_floor would work the same
        F.add_exactly_half_ceil(edge_vars)

    return F
Пример #23
0
 def test_parity(self):
     opb = """\
     * #variable= 3 #constraint= 4
     *
     +1 x1 +1 x2 +1 x3 >= 1;
     +1 x1 -1 x2 -1 x3 >= -1;
     -1 x1 +1 x2 -1 x3 >= -1;
     -1 x1 -1 x2 +1 x3 >= -1;
     """
     F = CNF()
     F.add_parity(["a", "b", "c"], 1)
     self.assertCnfEqualsOPB(F, opb)
Пример #24
0
    def build_cnf(args):
        """Build an disjunction

        Arguments:
        - `args`: command line options
        """
        clause = [ (True,"x_{}".format(i)) for i in range(args.P) ] + \
                 [ (False,"y_{}".format(i)) for i in range(args.N) ]
        orcnf =  CNF([clause])
        orcnf.header = "Clause with {} positive and {} negative literals\n\n".format(args.P,args.N) + \
                       orcnf.header
        return orcnf
Пример #25
0
    def build_cnf(args):
        """Build a conjunction

        Arguments:
        - `args`: command line options
        """
        clauses = [ [(True,"x_{}".format(i))] for i in range(args.P) ] + \
                  [ [(False,"y_{}".format(i))] for i in range(args.N) ]
        andcnf =  CNF(clauses)
        andcnf.header = "Singleton clauses: {} positive and {} negative\n\n""".format(args.P,args.N) +\
                        andcnf.header
        return andcnf
Пример #26
0
    def build_cnf(args):
        """Build a conjunction

        Arguments:
        - `args`: command line options
        """
        clauses = [ [(True,"x_{}".format(i))] for i in range(args.P) ] + \
                  [ [(False,"y_{}".format(i))] for i in range(args.N) ]
        andcnf = CNF(clauses)
        andcnf.header = "Singleton clauses: {} positive and {} negative\n\n""".format(args.P,args.N) +\
                        andcnf.header
        return andcnf
Пример #27
0
    def build_cnf(args):
        """Build an disjunction

        Arguments:
        - `args`: command line options
        """
        clause = [ (True,"x_{}".format(i)) for i in range(args.P) ] + \
                 [ (False,"y_{}".format(i)) for i in range(args.N) ]
        orcnf = CNF([clause])
        orcnf.header = "Clause with {} positive and {} negative literals\n\n".format(args.P,args.N) + \
                       orcnf.header
        return orcnf
Пример #28
0
    def getFormula(self):
        self.f = CNF()
        self.f._header += headerInfo
        self.addAllVariables()

        result = fe.And()
        for i in range(self.numRows):
            result.append(self.addRowConstraint(i))

        for j in range(self.numCols):
            result.append(self.addColConstraint(j))

        fe.toCNFgen(self.f, result)
        return self.f
Пример #29
0
def BinaryPigeonholePrinciple(pigeons, holes):
    """Binary Pigeonhole Principle CNF formula

    The pigeonhole principle claims that no M pigeons can sit in
    N pigeonholes without collision if M>N. This formula encodes the
    principle using binary strings to identify the holes.

    Parameters
    ----------
    pigeon : int 
       number of pigeons
    holes : int 
       number of holes
    """

    bphp = CNF()
    bphp.header="Binary Pigeonhole Principle for {0} pigeons and {1} holes\n".format(pigeons,holes)\
                 + bphp.header

    bphpgen = bphp.binary_mapping(xrange(1, pigeons + 1),
                                  xrange(1, holes + 1),
                                  injective=True)

    for v in bphpgen.variables():
        bphp.add_variable(v)

    for c in bphpgen.clauses():
        bphp.add_clause_unsafe(c)

    return bphp
Пример #30
0
def PythagoreanTriples(N):
    """There is a Pythagorean triples free coloring on N 
    
    The formula claims that it is possible to bicolor the numbers from
    1 to :math:`N` so that there  is no monochromatic triplet 
    :math:`(x,y,z)` so that :math:`x^2+y^2=z^2`.

    Parameters
    ----------
    N  : int
         size of the interval

    Return
    ------
    A CNF object

    Raises
    ------
    ValueError
       Parameters are not positive integers

    References
    ----------
    .. [1] M. J. Heule, O. Kullmann, and V. W. Marek. 
           Solving and verifying the boolean pythagorean triples problem via cube-and-conquer. 
           arXiv preprint arXiv:1605.00723, 2016.
    """

    ptn = CNF()

    ptn.header = dedent("""
It is possible to bicolor the numbers from
1 to {} so that there  is no monochromatic triplets
(x,y,z) such that x^2+y^2=z^2

""".format(N)) + ptn.header

    def V(i):
        return "x_{{{}}}".format(i)

    # Variables represent the coloring of the number
    for i in range(1, N + 1):
        ptn.add_variable(V(i))

    for x, y in combinations(list(range(1, N + 1)), 2):
        z = int(sqrt(x**2 + y**2))
        if z <= N and z**2 == x**2 + y**2:
            ptn.add_clause([(True, V(x)), (True, V(y)), (True, V(z))],
                           strict=True)
            ptn.add_clause([(False, V(x)), (False, V(y)), (False, V(z))],
                           strict=True)

    return ptn
Пример #31
0
 def test_empty(self):
     opb = """\
     * #variable= 0 #constraint= 0
     *
     """
     F = CNF()
     self.assertCnfEqualsOPB(F, opb)
Пример #32
0
def GraphAutomorphism(G):
    """Graph Automorphism formula

    The formula is the CNF encoding of the statement that a graph G
    has a nontrivial automorphism, i.e. an automorphism different from
    the idential one.

    Parameter
    ---------
    G : a simple graph

    Returns
    -------
    A CNF formula which is satiafiable if and only if graph G has a
    nontrivial automorphism.
    """
    tmp = CNF()
    header = "Graph automorphism formula for graph " + G.name + "\n" + tmp.header
    F = GraphIsomorphism(G, G)
    F.header = header

    var = _graph_isomorphism_var

    F.add_clause([(False, var(u, u)) for u in enumerate_vertices(G)],
                 strict=True)

    return F
Пример #33
0
def EvenColoringFormula(G):
    """Even coloring formula

    The formula is defined on a graph :math:`G` and claims that it is
    possible to split the edges of the graph in two parts, so that
    each vertex has an equal number of incident edges in each part.

    The formula is defined on graphs where all vertices have even
    degree. The formula is satisfiable only on those graphs with an
    even number of vertices in each connected component [1]_.

    Arguments
    ---------
    G : networkx.Graph 
       a simple undirected graph where all vertices have even degree

    Raises
    ------
    ValueError
       if the graph in input has a vertex with odd degree

    Returns
    -------
    CNF object

    References
    ----------
    .. [1] Locality and Hard SAT-instances, Klas Markstrom
       Journal on Satisfiability, Boolean Modeling and Computation 2 (2006) 221-228

    """
    F = CNF()
    F.mode_strict()
    F.header = "Even coloring formula on graph " + G.name + "\n" + F.header

    def var_name(u,v):
        if u<=v:
            return 'x_{{{0},{1}}}'.format(u,v)
        else:
            return 'x_{{{0},{1}}}'.format(v,u)
    
    for (u, v) in enumerate_edges(G):
        F.add_variable(var_name(u, v))

    # Defined on both side
    for v in enumerate_vertices(G):

        if G.degree(v) % 2 == 1:
            raise ValueError("Markstrom formulas requires all vertices to have even degree.")

        edge_vars = [ var_name(u, v) for u in neighbors(G, v) ]

        F.add_exactly_half_ceil(edge_vars)   # F.add_exactly_half_floor would work the same

    return F
Пример #34
0
def BinaryPigeonholePrinciple(pigeons,holes):
    """Binary Pigeonhole Principle CNF formula

    The pigeonhole principle claims that no M pigeons can sit in
    N pigeonholes without collision if M>N. This formula encodes the
    principle using binary strings to identify the holes.

    Parameters
    ----------
    pigeon : int 
       number of pigeons
    holes : int 
       number of holes
    """

    bphp=CNF()
    bphp.header="Binary Pigeonhole Principle for {0} pigeons and {1} holes\n".format(pigeons,holes)\
                 + bphp.header
    
    bphpgen=bphp.binary_mapping(xrange(1,pigeons+1), xrange(1,holes+1), injective = True)

    for v in bphpgen.variables():
        bphp.add_variable(v)
        
    for c in bphpgen.clauses():
        bphp.add_clause(c,strict=True)

    return bphp
Пример #35
0
def HiddenPigeonholePrinciple(pigeons, items, holes):
    """Hidden Pigeonhole Principle CNF formula

    Each pigeon has multiple items that should be fit into a hole, however each
    hole can only contain items of at most one pigeon.

    It is called hidden pigeon hole principle as it is only adding more
    constraints to the origianal pigeon hole principle, which makes it difficult
    to detect "the right" at-most-one constraints.

    Arguments:
    - `pigeons`: number of pigeons
    - `items`: number of items per pigeon
    - `holes`:   number of holes
    """
    def var(p, i, h):
        return 'p_{{{0},{1},{2}}}'.format(p, h, i)

    php = CNF()
    php.header = "hidden pigeon hole principle formula for {0} pigeons, each having {2} items ,and {1} holes\n".format(pigeons, holes, items)\
        + php.header

    for p in range(pigeons):
        for i in range(items):
            php.add_clause([(True, var(p, i, h)) for h in range(holes)])

    for h in range(holes):
        for p1 in range(pigeons):
            for p2 in range(p1):
                for i1 in range(items):
                    for i2 in range(items):
                        php.add_clause([(False, var(p1, i1, h)),
                                        (False, var(p2, i2, h))])

    return php
Пример #36
0
def BinaryPigeonholePrinciple(pigeons, holes):
    """Binary Pigeonhole Principle CNF formula

    The pigeonhole principle claims that no M pigeons can sit in
    N pigeonholes without collision if M>N. This formula encodes the
    principle using binary strings to identify the holes.

    Parameters
    ----------
    pigeon : int
       number of pigeons
    holes : int
       number of holes
    """

    bphp = CNF()
    bphp.header = "Binary Pigeonhole Principle for {0} pigeons and {1} holes\n".format(pigeons, holes)\
        + bphp.header

    mapping = binary_mapping(range(1, pigeons + 1),
                             range(1, holes + 1),
                             injective=True)
    bphp.mode_unchecked()
    mapping.load_variables_to_formula(bphp)
    mapping.load_clauses_to_formula(bphp)
    bphp.mode_default()

    return bphp
Пример #37
0
    def build_cnf(args):
        """Build an empty CNF formula 

        Parameters
        ----------
        args : ignored 
             command line options
        """
        return CNF()
Пример #38
0
    def build_cnf(args):
        """Build a CNF formula with an empty clause 

        Parameters
        ----------
        args : ignored 
             command line options
        """
        return CNF([[]])
Пример #39
0
 def test_one(self):
     ass = [{'x_1': True, 'x_2': False}]
     F = RandomKCNF(2, 2, 3, planted_assignments=ass)
     G = CNF([
         [(True, 'x_1'), (False, 'x_2')],
         [(False, 'x_1'), (False, 'x_2')],
         [(True, 'x_1'), (True, 'x_2')],
     ])
     self.assertCnfEqual(F, G)
Пример #40
0
 def test_most(self):
     ass = [
         {'x_1': True, 'x_2': False},
         {'x_1': False, 'x_2': False},
         {'x_1': True, 'x_2': True},
     ]
     F = RandomKCNF(2, 2, 1, planted_assignments=ass)
     G = CNF([[(True, 'x_1'), (False, 'x_2')]])
     self.assertCnfEqual(F, G)
Пример #41
0
 def test_opposite_literals(self):
     F = CNF()
     F.auto_add_variables = True
     F.allow_opposite_literals = True
     F.add_clause([(True, "S"), (False, "T"), (False, "S")])
     F.allow_opposite_literals = False
     self.assertRaises(ValueError, F.add_clause, [(True, "T"), (True, "V"),
                                                  (False, "T")])
Пример #42
0
def BinaryCliqueFormula(G, k):
    """Test whether a graph has a k-clique.

    Given a graph :math:`G` and a non negative value :math:`k`, the
    CNF formula claims that :math:`G` contains a :math:`k`-clique.
    This formula uses the binary encoding, in the sense that the
    clique elements are indexed by strings of bits.

    Parameters
    ----------
    G : networkx.Graph
        a simple graph
    k : a non negative integer
        clique size

    Returns
    -------
    a CNF object

    """
    F = CNF()
    F.header = "Binary {0}-clique formula\n".format(k) + F.header

    clauses_gen = F.binary_mapping(xrange(1, k + 1),
                                   G.nodes(),
                                   injective=True,
                                   nondecreasing=True)

    for v in clauses_gen.variables():
        F.add_variable(v)

    for c in clauses_gen.clauses():
        F.add_clause(c, strict=True)

    for (i1, i2), (v1, v2) in product(combinations(xrange(1, k + 1), 2),
                                      combinations(G.nodes(), 2)):

        if not G.has_edge(v1, v2):
            F.add_clause(clauses_gen.forbid_image(i1, v1) +
                         clauses_gen.forbid_image(i2, v2),
                         strict=True)

    return F
Пример #43
0
def RamseyLowerBoundFormula(s,k,N):
    """Formula claiming that Ramsey number r(s,k) > N

    Arguments:
    - `s`: independent set size
    - `k`: clique size
    - `N`: vertices
    """

    ram=CNF()
    ram.mode_strict()

    ram.header=dedent("""\
        CNF encoding of the claim that there is a graph of %d vertices
        with no independent set of size %d and no clique of size %d
        """ % (N,s,k)) + ram.header

    #
    # One variable per edge (indices are ordered)
    #
    for edge in combinations(xrange(1,N+1),2):
        ram.add_variable('e_{{{0},{1}}}'.format(*edge))
    
    #
    # No independent set of size s
    #
    for vertex_set in combinations(xrange(1,N+1),s):
        clause=[]
        for edge in combinations(vertex_set,2):
            clause += [(True,'e_{{{0},{1}}}'.format(*edge))]
        ram.add_clause(clause)

    #
    # No clique of size k
    #
    for vertex_set in combinations(xrange(1,N+1),k):
        clause=[]
        for edge in combinations(vertex_set,2):
            clause+=[(False,'e_{{{0},{1}}}'.format(*edge))]
        ram.add_clause(clause)

    return ram
Пример #44
0
def RandomKCNF(k, n, m, seed=None, planted_assignments=[]):
    """Build a random k-CNF

    Sample :math:`m` clauses over :math:`n` variables, each of width
    :math:`k`, uniformly at random. The sampling is done without
    repetition, meaning that whenever a randomly picked clause is
    already in the CNF, it is sampled again.

    Parameters
    ----------
    k : int
       width of each clause

    n : int
       number of variables to choose from. The resulting CNF object
       will contain n variables even if some are not mentioned in the clauses.

    m : int
       number of clauses to generate

    seed : hashable object
       seed of the random generator

    planted_assignments : iterable(dict), optional 
       a set of total/partial assigments such that all clauses in the formula 
       will be satisfied by all of them.

    Returns
    -------
    a CNF object

    Raises
    ------
    ValueError
        when some paramenter is negative, or when k>n.

    """
    if seed:
        random.seed(seed)


    if n<0 or m<0 or k<0:
        raise ValueError("Parameters must be non-negatives.")

    if k>n:
        raise ValueError("Clauses cannot have more {} literals.".format(n))

    F = CNF()
    F.header = "Random {}-CNF over {} variables and {} clauses\n".format(k,n,m) + F.header

    indices = xrange(1,n+1)
    for i in indices:
        F.add_variable('x_{0}'.format(i))
    try:
        for clause in sample_clauses(k, indices, m, planted_assignments):
            F.add_clause(list(clause), strict=True)
    except ValueError:
        raise ValueError("There are fewer clauses available than the number requested")

    return F
Пример #45
0
def PebblingFormula(digraph):
    """Pebbling formula

    Build a pebbling formula from the directed graph. If the graph has
    an `ordered_vertices` attribute, then it is used to enumerate the
    vertices (and the corresponding variables).

    Arguments:
    - `digraph`: directed acyclic graph.
    """
    if not is_dag(digraph):
        raise ValueError(
            "Pebbling formula is defined only for directed acyclic graphs")

    peb = CNF()

    if hasattr(digraph, 'name'):
        peb.header = "Pebbling formula of: " + digraph.name + "\n\n" + peb.header
    else:
        peb.header = "Pebbling formula\n\n" + peb.header

    # add variables in the appropriate order
    vertices = enumerate_vertices(digraph)
    position = dict((v, i) for (i, v) in enumerate(vertices))

    for v in vertices:
        peb.add_variable(
            v, description="There is a pebble on vertex ${}$".format(v))

    # add the clauses
    for v in vertices:

        # If predecessors are pebbled the vertex must be pebbled
        pred = sorted(digraph.predecessors(v), key=lambda x: position[x])
        peb.add_clause_unsafe([(False, p) for p in pred] + [(True, v)])

        if digraph.out_degree(v) == 0:  #the sink
            peb.add_clause_unsafe([(False, v)])

    return peb
Пример #46
0
def PerfectMatchingPrinciple(G):
    """Generates the clauses for the graph perfect matching principle.

    The principle claims that there is a way to select edges to such
    that all vertices have exactly one incident edge set to 1.

    Parameters
    ----------
    G : undirected graph

    """
    cnf = CNF()

    # Describe the formula
    name = "Perfect Matching Principle"

    if hasattr(G, 'name'):
        cnf.header = name + " of graph:\n" + G.name + "\n" + cnf.header
    else:
        cnf.header = name + ".\n" + cnf.header

    def var_name(u, v):
        if u <= v:
            return 'x_{{{0},{1}}}'.format(u, v)
        else:
            return 'x_{{{0},{1}}}'.format(v, u)

    # Each vertex has exactly one edge set to one.
    for v in enumerate_vertices(G):

        edge_vars = [var_name(u, v) for u in neighbors(G, v)]
        cnf.add_equal_to(edge_vars, 1)

    return cnf
Пример #47
0
def BinaryPigeonholePrinciple(pigeons,holes):
    """Binary Pigeonhole Principle CNF formula

    The pigeonhole principle claims that no M pigeons can sit in
    N pigeonholes without collision if M>N. This formula encodes the
    principle using binary strings to identify the holes.

    Parameters
    ----------
    pigeon : int 
       number of pigeons
    holes : int 
       number of holes
    """

    bphp=CNF()
    bphp.header="Binary Pigeonhole Principle for {0} pigeons and {1} holes\n".format(pigeons,holes)\
                 + bphp.header
    
    mapping=binary_mapping(xrange(1,pigeons+1),
                           xrange(1,holes+1), injective = True)
    bphp.mode_unchecked()
    mapping.load_variables_to_formula(bphp)
    mapping.load_clauses_to_formula(bphp)
    bphp.mode_default()

    return bphp
Пример #48
0
    def __call__(self):
        mn = self.m * self.n
        A = [0, 1] * (mn // 2) + [0] * (mn % 2)
        random.shuffle(A)
        A = [[A[i * self.n + j] for j in range(self.n)] for i in range(self.m)]
        b = [0, 1] * (self.m // 2) + [0] * (self.m % 2)


        self.f = CNF()

        terms = [(-1, self.x(i)) for i in range(self.n)]
        if self.eq:
            self.f.add_linear(*toArgs(terms, "==", -self.k))
        else:
            self.f.add_linear(*toArgs(terms, ">=", -self.k))

        for i in range(self.m):
            terms = [(1, self.x(j)) for j in range(self.n) if A[i][j] == 1]
            if len(terms) > 0:
                self.addXor(terms, b[i])

        return self.f
Пример #49
0
 def test_opposite_literals(self):
     F=CNF()
     F.auto_add_variables = True
     F.allow_opposite_literals = True
     F.add_clause([(True,"S"),(False,"T"),(False,"S")])
     F.allow_opposite_literals = False
     self.assertRaises(ValueError, F.add_clause,
                       [(True,"T"),(True,"V"),(False,"T")])
Пример #50
0
def BinaryCliqueFormula(G,k):
    """Test whether a graph has a k-clique.

    Given a graph :math:`G` and a non negative value :math:`k`, the
    CNF formula claims that :math:`G` contains a :math:`k`-clique.
    This formula uses the binary encoding, in the sense that the
    clique elements are indexed by strings of bits.

    Parameters
    ----------
    G : networkx.Graph
        a simple graph
    k : a non negative integer
        clique size

    Returns
    -------
    a CNF object

    """
    F=CNF()
    F.header="Binary {0}-clique formula\n".format(k) + F.header
    
    mapping=CNF.binary_mapping(xrange(1,k+1), G.nodes(),
                               injective = True,
                               nondecreasing = True)

    mapping.load_variables_to_formula(F)
    mapping.load_clauses_to_formula(F)

    for (i1,i2),(v1,v2) in product(combinations(xrange(1,k+1),2),
                                   combinations(G.nodes(),2)):
    
        if not G.has_edge(v1,v2):
            F.add_clause( clauses_gen.forbid_image(i1,v1) +
                          clauses_gen.forbid_image(i2,v2))

    return F
Пример #51
0
def RamseyLowerBoundFormula(s, k, N):
    """Formula claiming that Ramsey number r(s,k) > N

    Arguments:
    - `s`: independent set size
    - `k`: clique size
    - `N`: vertices
    """

    ram = CNF()

    ram.header = dedent("""\
        CNF encoding of the claim that there is a graph of %d vertices
        with no independent set of size %d and no clique of size %d
        """ % (N, s, k)) + ram.header

    #
    # One variable per edge (indices are ordered)
    #
    for edge in combinations(range(1, N + 1), 2):
        ram.add_variable('e_{{{0},{1}}}'.format(*edge))

    #
    # No independent set of size s
    #
    for vertex_set in combinations(range(1, N + 1), s):
        clause = []
        for edge in combinations(vertex_set, 2):
            clause += [(True, 'e_{{{0},{1}}}'.format(*edge))]
        ram.add_clause(clause, strict=True)

    #
    # No clique of size k
    #
    for vertex_set in combinations(range(1, N + 1), k):
        clause = []
        for edge in combinations(vertex_set, 2):
            clause += [(False, 'e_{{{0},{1}}}'.format(*edge))]
        ram.add_clause(clause, strict=True)

    return ram
Пример #52
0
def PebblingFormula(digraph):
    """Pebbling formula

    Build a pebbling formula from the directed graph. If the graph has
    an `ordered_vertices` attribute, then it is used to enumerate the
    vertices (and the corresponding variables).

    Arguments:
    - `digraph`: directed acyclic graph.
    """
    if not is_dag(digraph):
        raise ValueError("Pebbling formula is defined only for directed acyclic graphs")

    peb=CNF()

    if hasattr(digraph,'name'):
        peb.header="Pebbling formula of: "+digraph.name+"\n\n"+peb.header
    else:
        peb.header="Pebbling formula\n\n"+peb.header

    # add variables in the appropriate order
    vertices=enumerate_vertices(digraph)
    position=dict((v,i) for (i,v) in enumerate(vertices))

    for v in vertices:
        peb.add_variable(v,description="There is a pebble on vertex ${}$".format(v))

    # add the clauses
    for v in vertices:

        # If predecessors are pebbled the vertex must be pebbled
        pred=sorted(digraph.predecessors(v),key=lambda x:position[x])
        peb.add_clause_unsafe([(False,p) for p in pred]+[(True,v)])

        if digraph.out_degree(v)==0: #the sink
            peb.add_clause_unsafe([(False,v)])

    return peb
Пример #53
0
def PythagoreanTriples(N):
    """There is a Pythagorean triples free coloring on N 
    
    The formula claims that it is possible to bicolor the numbers from
    1 to :math:`N` so that there  is no monochromatic triplet 
    :math:`(x,y,z)` so that :math:`x^2+y^2=z^2`.

    Parameters
    ----------
    N  : int
         size of the interval

    Return
    ------
    A CNF object

    Raises
    ------
    ValueError
       Parameters are not positive integers

    References
    ----------
    .. [1] M. J. Heule, O. Kullmann, and V. W. Marek. 
           Solving and verifying the boolean pythagorean triples problem via cube-and-conquer. 
           arXiv preprint arXiv:1605.00723, 2016.
    """

    ptn=CNF()

    ptn.header=dedent("""
It is possible to bicolor the numbers from
1 to {} so that there  is no monochromatic triplets
(x,y,z) such that x^2+y^2=z^2

""".format(N)) + ptn.header

    def V(i):
        return "x_{{{}}}".format(i)

    # Variables represent the coloring of the number
    for i in xrange(1,N+1):
        ptn.add_variable(V(i))

        
    for x,y in combinations(range(1,N+1),2):
        z = int(sqrt(x**2 +  y**2))
        if z <=N and z**2 == x**2 + y**2:
            ptn.add_clause([ (True, V(x)), (True, V(y)), (True, V(z))])
            ptn.add_clause([ (False,V(x)), (False,V(y)), (False,V(z))])

    return ptn
Пример #54
0
def VertexCover(G, d):
    F = CNF()

    def D(v):
        return "x_{{{0}}}".format(v)

    def N(v):
        return tuple(sorted([e for e in G.edges(v)]))

    # Fix the vertex order
    V = enumerate_vertices(G)

    # Create variables
    for v in V:
        F.add_variable(D(v))

    # Not too many true variables
    F.add_less_or_equal([D(v) for v in V], d)

    # Every edge must have a true D variable
    for e in G.edges():
        F.add_clause([(True, D(v)) for v in e])

    return F
Пример #55
0
def VertexCover(G,d):
    F=CNF()

    def D(v):
        return "x_{{{0}}}".format(v)

    def N(v):
        return tuple(sorted([ e for e in G.edges(v) ]))

    # Fix the vertex order
    V=enumerate_vertices(G)

    # Create variables
    for v in V:
        F.add_variable(D(v))

    # Not too many true variables
    F.add_less_or_equal([D(v) for v in V],d)

    # Every edge must have a true D variable
    for e in G.edges():
        F.add_clause([ (True,D(v)) for v in e])
        
    return F
Пример #56
0
def DominatingSet(G,d, alternative = False):
    r"""Generates the clauses for a dominating set for G of size <= d

    The formula encodes the fact that the graph :math:`G` has
    a dominating set of size :math:`d`. This means that it is possible
    to pick at most :math:`d` vertices in :math:`V(G)` so that all remaining
    vertices have distance at most one from the selected ones.

    Parameters
    ----------
    G : networkx.Graph
        a simple undirected graph
    d : a positive int
        the size limit for the dominating set
    alternative : bool
        use an alternative construction that 
        is provably hard from resolution proofs.

    Returns
    -------
    CNF
       the CNF encoding for dominating of size :math:`\leq d` for graph :math:`G`

    """
    F=CNF()

    if not isinstance(d,int) or d<1:
        ValueError("Parameter \"d\" is expected to be a positive integer")
    
    # Describe the formula
    name="{}-dominating set".format(d)
    
    if hasattr(G,'name'):
        F.header=name+" of graph:\n"+G.name+".\n\n"+F.header
    else:
        F.header=name+".\n\n"+F.header

    # Fix the vertex order
    V=enumerate_vertices(G)
    

    def D(v):
        return "x_{{{0}}}".format(v)

    def M(v,i):
        return "g_{{{0},{1}}}".format(v,i)

    def N(v):
        return tuple(sorted([ v ] + [ u for u in G.neighbors(v) ]))
    
    # Create variables
    for v in V:
        F.add_variable(D(v))
    for i,v in product(range(1,d+1),V):
        F.add_variable(M(v,i))
    
    # No two (active) vertices map to the same index
    if alternative:
        for u,v in combinations(V,2):
            for i in range(1,d+1):
                F.add_clause( [ (False,D(u)),(False,D(v)), (False,M(u,i)), (False,M(v,i))    ])
    else:
        for i in range(1,d+1):
            for c in CNF.less_or_equal_constraint([M(v,i) for v in V],1):
                F.add_clause(c)
                
    # (Active) Vertices in the sequence are not repeated
    if alternative:
        for v in V:
            for i,j in combinations(range(1,d+1),2):
                F.add_clause([(False,D(v)),(False,M(v,i)),(False,M(v,j))])
    else:
        for i,j in combinations_with_replacement(range(1,d+1),2):
            i,j = min(i,j),max(i,j)
            for u,v in combinations(V,2):
                u,v = max(u,v),min(u,v)
                F.add_clause([(False,M(u,i)),(False,M(v,j))])

    # D(v) = M(v,1) or M(v,2) or ... or M(v,d)        
    if not alternative:
        for i,v in product(range(1,d+1),V):
            F.add_clause([(False,M(v,i)),(True,D(v))])
    for v in V:
        F.add_clause([(False,D(v))] + [(True,M(v,i)) for i in range(1,d+1)])
    
        
    # Every neighborhood must have a true D variable
    neighborhoods = sorted( set(N(v) for v in V) )
    for N in neighborhoods:
        F.add_clause([ (True,D(v)) for v in N])
        
    return F
Пример #57
0
def GraphColoringFormula(G,colors,functional=True):
    """Generates the clauses for colorability formula

    The formula encodes the fact that the graph :math:`G` has a coloring
    with color set ``colors``. This means that it is possible to
    assign one among the elements in ``colors``to that each vertex of
    the graph such that no two adjacent vertices get the same color.

    Parameters
    ----------
    G : networkx.Graph
        a simple undirected graph
    colors : list or positive int
        a list of colors or a number of colors

    Returns
    -------
    CNF
       the CNF encoding of the coloring problem on graph ``G``

    """
    col=CNF()
    col.mode_strict()

    if isinstance(colors,int) and colors>=0:
        colors = range(1,colors+1)
    
    if not isinstance(list, collections.Iterable):
        ValueError("Parameter \"colors\" is expected to be a iterable")
    
    # Describe the formula
    name="graph colorability"
    
    if hasattr(G,'name'):
        col.header=name+" of graph:\n"+G.name+".\n\n"+col.header
    else:
        col.header=name+".\n\n"+col.header

    # Fix the vertex order
    V=enumerate_vertices(G)

    # Create the variables
    for vertex in V:
        for color in colors:
            col.add_variable('x_{{{0},{1}}}'.format(vertex,color))
        
    # Each vertex has a color
    for vertex in V:
        clause = []
        for color in colors:
            clause += [(True,'x_{{{0},{1}}}'.format(vertex,color))]
        col.add_clause(clause)
        
        # unique color per vertex
        if functional:
            for (c1,c2) in combinations(colors,2):
                col.add_clause([
                    (False,'x_{{{0},{1}}}'.format(vertex,c1)),
                    (False,'x_{{{0},{1}}}'.format(vertex,c2))])

    # This is a legal coloring
    for (v1,v2) in enumerate_edges(G):
        for c in colors:
            col.add_clause([
                (False,'x_{{{0},{1}}}'.format(v1,c)),
                (False,'x_{{{0},{1}}}'.format(v2,c))])
            
    return col
Пример #58
0
def StoneFormula(D,nstones):
    """Stone formulas

    The stone formulas have been introduced in [2]_ and generalized in
    [1]_. They are one of the classic examples that separate regular
    resolutions from general resolution [1]_.

    A \"Stones formula\" from a directed acyclic graph :math:`D`
    claims that each vertex of the graph is associated with one on
    :math:`s` stones (not necessarily in an injective way).
    In particular for each vertex :math:`v` in :math:`V(D)` and each
    stone :math:`j` we have a variable :math:`P_{v,j}` that claims
    that stone :math:`j` is associated to vertex :math:`v`.

    Each stone can be either red or blue, and not both.
    The propositional variable :math:`R_j` if true when the stone
    :math:`j` is red and false otherwise.

    The clauses of the formula encode the following constraints.
    If a stone is on a source vertex (i.e. a vertex with no incoming
    edges), then it must be red. If all stones on the predecessors of
    a vertex are red, then the stone of the vertex itself must be red.

    The formula furthermore enforces that the stones on the sinks
    (i.e. vertices with no outgoing edges) are blue.

    .. note:: The exact formula structure depends by the graph and on
              its topological order, which is determined by the
              ``enumerate_vertices(D)``.

    Parameters
    ----------
    D : a directed acyclic graph
        it should be a directed acyclic graph.
    nstones : int
       the number of stones.

    Raises
    ------
    ValueError
       if :math:`D` is not a directed acyclic graph
    
    ValueError
       if the number of stones is negative

    References
    ----------
    .. [1] M. Alekhnovich, J. Johannsen, T. Pitassi and A. Urquhart
    	   An Exponential Separation between Regular and General Resolution.
           Theory of Computing (2007)
    .. [2] R. Raz and P. McKenzie
           Separation of the monotone NC hierarchy.
           Combinatorica (1999)

    """
    if not is_dag(D):
        raise ValueError("Stone formulas are defined only for directed acyclic graphs.")
    
    if nstones<0:
        raise ValueError("There must be at least one stone.")

    cnf = CNF()

    if hasattr(D, 'name'):
        cnf.header = "Stone formula of: " + D.name + "\nwith " + str(nstones) + " stones\n" + cnf.header
    else:
        cnf.header = "Stone formula with " + str(nstones) + " stones\n" + cnf.header

    # Add variables in the appropriate order
    vertices=enumerate_vertices(D)
    position=dict((v,i) for (i,v) in enumerate(vertices))
    stones=range(1,nstones+1)

    # Caching variable names
    color_vn = {}
    stone_vn = {}
    
    # Stones->Vertices variables
    for v in vertices:
        for j in stones:
            stone_vn[(v,j)] = "P_{{{0},{1}}}".format(v,j) 
            cnf.add_variable(stone_vn[(v,j)],
                             description="Stone ${1}$ on vertex ${0}$".format(v,j))

    # Color variables
    for j in stones:
        color_vn[j] = "R_{{{0}}}".format(j)
        cnf.add_variable(color_vn[j],
                         description="Stone ${}$ is red".format(j))
    
    # Each vertex has some stone
    for v in vertices:
        cnf.add_clause_unsafe([(True,stone_vn[(v,j)]) for j in stones])
        
    # If predecessors have red stones, the sink must have a red stone
    for v in vertices:
        for j in stones:
            pred=sorted(D.predecessors(v),key=lambda x:position[x])
            for stones_tuple in product([s for s in stones if s!=j],repeat=len(pred)):
                cnf.add_clause_unsafe([(False, stone_vn[(p,s)]) for (p,s) in zip(pred,stones_tuple)] +
                                      [(False, stone_vn[(v,j)])] +
                                      [(False, color_vn[s]) for s in _uniqify_list(stones_tuple)] +
                                      [(True,  color_vn[j])])
        
        if D.out_degree(v)==0: #the sink
            for j in stones:
                cnf.add_clause_unsafe([ (False,stone_vn[(v,j)]), (False,color_vn[j])])

    return cnf
Пример #59
0
def GraphIsomorphism(G1, G2):
    """Graph Isomorphism formula

    The formula is the CNF encoding of the statement that two simple
    graphs G1 and G2 are isomorphic.

    Parameters
    ----------
    G1 : networkx.Graph
        an undirected graph object
    G2 : networkx.Graph
        an undirected graph object

    Returns
    -------
    A CNF formula which is satiafiable if and only if graphs G1 and G2
    are isomorphic.

    """
    F = CNF()
    F.header = "Graph Isomorphism problem between graphs " +\
               G1.name + " and " + G2.name + "\n" + F.header

    U=enumerate_vertices(G1)
    V=enumerate_vertices(G2)
    var = _graph_isomorphism_var

    for (u, v) in product(U,V):
        F.add_variable(var(u, v))

    # Defined on both side
    for u in U:
        F.add_clause([(True, var(u, v)) for v in V], strict=True)

    for v in V:
        F.add_clause([(True, var(u, v)) for u in U], strict=True)

    # Injective on both sides
    for u in U:
        for v1, v2 in combinations(V, 2):
            F.add_clause([(False, var(u, v1)),
                          (False, var(u, v2))], strict=True)
    for v in V:
        for u1, u2 in combinations(U, 2):
            F.add_clause([(False, var(u1, v)),
                          (False, var(u2, v))], strict=True)

    # Edge consistency
    for u1, u2 in combinations(U, 2):
        for v1, v2 in combinations(V, 2):
            if G1.has_edge(u1, u2) != G2.has_edge(v1, v2):
                F.add_clause([(False, var(u1, v1)),
                              (False, var(u2, v2))], strict=True)
                F.add_clause([(False, var(u1, v2)),
                              (False, var(u2, v1))], strict=True)

    return F
Пример #60
0
def SparseStoneFormula(D,B):
    """Sparse Stone formulas

    This is a variant of the :py:func:`StoneFormula`. See that for
    a description of the formula. This variant is such that each
    vertex has only a small selection of which stone can go to that
    vertex. In particular which stones are allowed on each vertex is
    specified by a bipartite graph :math:`B` on which the left
    vertices represent the vertices of DAG :math:`D` and the right
    vertices are the stones. 

    If a vertex of :math:`D` correspond to the left vertex :math:`v`
    in :math:`B`, then its neighbors describe which stones are allowed
    for it.

    The vertices in :math:`D` do not need to have the same name as the
    one on the left side of :math:`B`. It is only important that the
    number of vertices in :math:`D` is the same as the vertices in the
    left side of :math:`B`.

    In that case the element at position :math:`i` in the ordered
    sequence ``enumerate_vertices(D)`` corresponds to the element of
    rank :math:`i` in the sequence of left side vertices of
    :math:`B` according to the output of ``Left, Right =
    bipartite_sets(B)``.

    Standard :py:func:`StoneFormula` is essentially equivalent to
    a sparse stone formula where :math:`B` is the complete graph.

    Parameters
    ----------
    D : a directed acyclic graph
        it should be a directed acyclic graph.
    B : bipartite graph

    Raises
    ------
    ValueError
       if :math:`D` is not a directed acyclic graph
    
    ValueError
       if :math:`B` is not a bipartite graph

    ValueError
       when size differs between :math:`D` and the left side of
       :math:`B`

    See Also
    --------
    StoneFormula

    """
    if not is_dag(D):
        raise ValueError("Stone formulas are defined only for directed acyclic graphs.")

    if not has_bipartition(B):
        raise ValueError("Vertices to stones mapping must be specified with a bipartite graph")
    
    Left, Right = bipartite_sets(B)
    nstones = len(Right)

    if len(Left) != D.order():
        raise ValueError("Formula requires the bipartite left side to match #vertices of the DAG.")
     
    cnf = CNF()
    
    if hasattr(D, 'name'):
        cnf.header = "Sparse Stone formula of: " + D.name + "\nwith " + str(nstones) + " stones\n" + cnf.header
    else:
        cnf.header = "Sparse Stone formula with " + str(nstones) + " stones\n" + cnf.header

    # add variables in the appropriate order
    vertices=enumerate_vertices(D)
    stones=range(1,nstones+1)
    
    mapping = cnf.unary_mapping(vertices,stones,sparsity_pattern=B)

    stone_formula_helper(cnf,D,mapping)
    return cnf