当前位置: 首页>>代码示例>>Python>>正文


Python CNF.sparse_mapping方法代码示例

本文整理汇总了Python中cnfformula.cnf.CNF.sparse_mapping方法的典型用法代码示例。如果您正苦于以下问题:Python CNF.sparse_mapping方法的具体用法?Python CNF.sparse_mapping怎么用?Python CNF.sparse_mapping使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在cnfformula.cnf.CNF的用法示例。


在下文中一共展示了CNF.sparse_mapping方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: GraphPigeonholePrinciple

# 需要导入模块: from cnfformula.cnf import CNF [as 别名]
# 或者: from cnfformula.cnf.CNF import sparse_mapping [as 别名]
def GraphPigeonholePrinciple(graph,functional=False,onto=False):
    """Graph Pigeonhole Principle CNF formula

    The graph pigeonhole principle CNF formula, defined on a bipartite
    graph G=(L,R,E), claims that there is a subset E' of the edges such that 
    every vertex on the left size L has at least one incident edge in E' and 
    every edge on the right side R has at most one incident edge in E'.

    This is possible only if the graph has a matching of size |L|.

    There are different variants of this formula, depending on the
    values of `functional` and `onto` argument.

    - PHP(G):  each left vertex can be incident to multiple edges in E'
    - FPHP(G): each left vertex must be incident to exaclty one edge in E'
    - onto-PHP: all right vertices must be incident to some vertex
    - matching: E' must be a perfect matching between L and R

    Arguments:
    - `graph` : bipartite graph
    - `functional`: add clauses to enforce at most one edge per left vertex
    - `onto`: add clauses to enforce that any right vertex has one incident edge


    Remark: the graph vertices must have the 'bipartite' attribute
    set. Left vertices must have it set to 0 and the right ones to
    1. A KeyException is raised otherwise.

    """

    def var_name(p,h):
        return 'p_{{{0},{1}}}'.format(p,h)

    if functional:
        if onto:
            formula_name="Graph matching"
        else:
            formula_name="Graph functional pigeonhole principle"
    else:
        if onto:
            formula_name="Graph onto pigeonhole principle"
        else:
            formula_name="Graph pigeonhole principle"


    gphp=CNF()
    gphp.header="{0} formula for graph {1}\n".format(formula_name,graph.name)

    Left, _ = bipartite_sets(graph)

    for p in Left:
        for h in neighbors(graph,p):
            gphp.add_variable(var_name(p,h))

    clauses=gphp.sparse_mapping( graph,
                                 var_name=var_name,
                                 complete = True,
                                 injective = True,
                                 functional = functional,
                                 surjective = onto)

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

    return gphp
开发者ID:,项目名称:,代码行数:67,代码来源:


注:本文中的cnfformula.cnf.CNF.sparse_mapping方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。