CUDD产品总和布尔表达式

时间:2016-04-08 10:09:11

标签: binary-decision-diagram cudd

我想为以下布尔函数创建BDD:

F = (A'B'C'D') OR (A'B C) OR (C' D') OR (A)

我设法仅使用以下代码创建F = (A'B'C'D')但是如何将其他产品术语添加到现有BDD中?

 int main (int argc, char *argv[])
{
    char filename[30];
    DdManager *gbm; /* Global BDD manager. */
    gbm = Cudd_Init(0,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0); /* Initialize a new BDD manager. */
    DdNode *bdd, *var, *tmp_neg, *tmp;
    int i;
    bdd = Cudd_ReadOne(gbm); /*Returns the logic one constant of the manager*/
    Cudd_Ref(bdd); /*Increases the reference count of a node*/

    for (i = 3; i >= 0; i--) {
        var = Cudd_bddIthVar(gbm,i); /*Create a new BDD variable*/
        tmp_neg = Cudd_Not(var); /*Perform NOT boolean operation*/
        tmp = Cudd_bddAnd(gbm, tmp_neg, bdd); /*Perform AND boolean operation*/
        Cudd_Ref(tmp);
        Cudd_RecursiveDeref(gbm,bdd);
        bdd = tmp;
    }

    bdd = Cudd_BddToAdd(gbm, bdd); 
    print_dd (gbm, bdd, 2,4);   
    sprintf(filename, "./bdd/graph.dot"); 
    write_dd(gbm, bdd, filename);  
    Cudd_Quit(gbm);
    return 0;
}

2 个答案:

答案 0 :(得分:2)

独立构建每个连接,以便conj0conj3,确保只能否定正确的文字。我不是特别精通C,现在没有开发环境,所以你需要做一些修正。

我将使用以下映射

A <=> BDD(0)
B <=> BDD(1)
C <=> BDD(2)
D <=> BDD(3)

按照你的for循环中的方式构建conj0。之后确保conj0 = bdd

对于将conj1进行编码的(A' B C)使用

bdd = Cudd_IthVar(gbm, 0);
bdd = Cudd_Not(bdd);
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 1));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
tmp = Cudd_And(gbm, bdd, Cudd_IthVar(gbm, 2));
Cudd_Ref(tmp);
Cudd_Deref(gbm, bdd);
bdd = tmp;
conj1 = bdd;

conj2conj3执行相同操作。

在你完成所有连接之后,使用Cudd_bddOr()构建顶级分离。 还要确保你得到Cudd_Ref()Cudd_Deref(),否则你会泄漏内存。

答案 1 :(得分:1)

如果您只对该特定功能感兴趣,可以通过以下方式构建并检查它:

#include <stdio.h>
#include <stdlib.h>
#include "cudd.h"

int main(void) {
  /* Get set. */
  DdManager * mgr = Cudd_Init(4,0,CUDD_UNIQUE_SLOTS,CUDD_CACHE_SLOTS,0);
  DdNode *a = Cudd_bddIthVar(mgr, 0);
  DdNode *c = Cudd_bddIthVar(mgr, 1);
  DdNode *b = Cudd_bddIthVar(mgr, 2);
  DdNode *d = Cudd_bddIthVar(mgr, 3);
  char const * const inames[] = {"a", "c", "b", "d"};
  /* Build BDD. */
  DdNode * tmp = Cudd_bddIte(mgr, c, b, Cudd_Not(d));
  Cudd_Ref(tmp);
  DdNode * f = Cudd_bddOr(mgr, a, tmp);
  Cudd_Ref(f);
  Cudd_RecursiveDeref(mgr, tmp);
  /* Inspect it. */
  printf("f");
  Cudd_PrintSummary(mgr, f, 4, 0);
  Cudd_bddPrintCover(mgr, f, f);
  char * fform = Cudd_FactoredFormString(mgr, f, inames);
  printf("%s\n", fform);
  /* Break up camp and go home. */
  free(fform);
  Cudd_RecursiveDeref(mgr, f);
  int err = Cudd_CheckZeroRef(mgr);
  Cudd_Quit(mgr);
  return err;
}

注意(最佳)变量顺序的选择。你应该看到这个输出:

f: 5 nodes 1 leaves 12 minterms
1--- 1
-11- 1
-0-0 1

a | (c & b | !c & !d)