Binary Decision Diagrams¶
libmc.BDD
offers a class for generating binary decision diagrams.
Constants¶
The two constant nodes representing True
and False
can be accessed
through the static class methods true()
and
false()
respectively.
true = BDD.true()
false = BDD.false()
Variables¶
Variable nodes are defined by having an index greater or equal to zero.
a = BDD(0)
b = BDD(1)
Since they have to be unique, a repeated call to the constructor using the same index will return a reference to the specific node instead of allocating a new one.
assert a is BDD(0)
assert b is BDD(1)
Operators¶
Operations are carried out using the logical connectives ~
, |
, &
and ^
~a
a & b
a | b
a ^ b
Visualization¶
The toDot()
method can be used generate the DOT language
string for a given BDD
:
dot = (~(a ^ b)).toDot()
Write it to a file:
with open("xnor.dot", 'w') as f:
f.write(dot)
Generate a PDF:
with open("xnor.pdf", 'w') as f:
import subprocess
subprocess.run(["dot", "-Tpdf", "xnor.dot"], stdout=f)
Enjoy :)
![graph BDD {
"-1560438204313394375" [label="@1"]
"-4499075771716060229" [label="@0",color=red]
"-1560438204313394375" -- "-4499075771716060229" [style=dashed,color=red]
"0" [shape=box]
"-4499075771716060229" -- "0" [style=dashed,color=red]
"1" [shape=box]
"-4499075771716060229" -- "1"
"-1786681312931940090" [label="@0"]
"-1560438204313394375" -- "-1786681312931940090"
"-1786681312931940090" -- "0" [style=dashed,color=red]
"-1786681312931940090" -- "1"
}](_images/graphviz-c1352081720788e37ffe25e416901f2960db5bce.png)