【问题标题】:Creating and simplifying of bool formula in Python在 Python 中创建和简化 bool 公式
【发布时间】:2021-04-28 13:10:42
【问题描述】:

存在通过 Magu 方法(高等教育中的离散数学)找到最大内部稳定图集的问题。给定一个带有邻接矩阵的图(已经实现),那么您需要制作布尔公式,其中将有相邻顶点的所有否定的析取的合取。也就是说,例如,如果存在从 V1 到 V2 以及从 V1 到 V3 的路径,则公式将包括 (!V1 v !V2) & (!V1 v !V3)。然后这个表达式被转换为一个缩短的 DNF,然后你可以看到每个括号中缺少图的哪些顶点——这些顶点恰好形成了这些最大的内部稳定集。只是问题是如何制作这个初始布尔公式,然后你可以将这个公式简化为缩写的 DNF?我假设您需要使用 sympy 库,但我找不到关于我的问题的任何具体信息......目前的代码

import numpy as np
from sympy import *
from sympy.logic.boolalg import And
from sympy.logic.boolalg import Or

print("Number of nodes in graph:")
amount = int(input())
matrix = np.zeros((amount, amount))
i = 0
j = 0

while i < amount:
    while j < amount:
        print("insert matrix element", i, j, ":")
        matrix[i, j] = int(input())
        j += 1
    j = 0
    i += 1
print(matrix)

【问题讨论】:

    标签: python numpy sympy discrete-mathematics


    【解决方案1】:

    问题解决了

    i = 0
    j = 0
    counter = 0
    while i < amount:
        while j < amount:
            if matrix[i, j] == 1:
                x = symbols(str(i + 1))
                y = symbols(str(j + 1))
                if counter == 0:
                    formula = (~x | ~y)
                    counter += 1
                else:
                    formula = formula & (~x | ~y)
            j += 1
        j = 0
        i += 1
    formula_to_string = pycode(simplify_logic(formula, form='dnf', force=True))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2021-07-23
      • 1970-01-01
      相关资源
      最近更新 更多