提问人:Liuzili 提问时间:4/13/2023 最后编辑:LW001Liuzili 更新时间:8/9/2023 访问量:69
如何在离散数学中找到所有最佳 DNF 形式?
How to find all optimal DNF forms in discrete maths?
问:
我试图用 Python 编写代码,但我不太明白如何打印所有可能的 DNF 表单及其真值表。我为此写了一个代码,但它没有打印任何东西,我在哪里犯了错误?
如果有人能分享一些关于如何将从真值表派生的图显示为布尔代数的想法,那也很好。在此代码中,我试图找到长度为 2 和大小为 3 的所有 DNF。也许有人对如何处理这些事情有一些想法,我将不胜感激。
import itertools
def generate_dnf(variables, length=None, size=None):
num_vars = len(variables)
dnf_formulas = []
if length is None:
lengths = range(num_vars + 1)
else:
lengths = [length]
if size is None:
sizes = range(num_vars + 1)
else:
sizes = [size]
for n in lengths:
for combination in itertools.product([True, False], repeat=num_vars):
if sum(combination) == n:
dnf = ''
for i in range(num_vars):
if not combination[i]:
dnf += '~'
dnf += variables[i]
dnf += ' or '
dnf_formulas.append(dnf[:-4])
filtered_dnf_formulas = []
for formula in dnf_formulas:
num_operators = formula.count(' or ') + formula.count(' and ')
if num_operators in sizes:
filtered_dnf_formulas.append(formula)
return filtered_dnf_formulas
variables = ['A', 'B', 'C']
dnfs = generate_dnf(variables, length=2, size=3)
for dnf in dnfs:
print(dnf)
答: 暂无答案
下一个:布尔否定的大步语义推理规则
评论
num_operators = formula.count(' or ') + formula.count(' and ')
num_operators
~