I am new here (and to Mathematica) and would like to rebuild a simple binary decision diagram that I found here https://en.wikipedia.org/wiki/Binary_decision_diagram
A code has been provided as part of an answer to a related question (Binary decision diagrams (BDD)), but it seems to generate a syntax error when running it in Mathematica. Please see the code and error message below. Could anyone tell me what I have to do to fix it? Apologies in case this is a very simple question...
bf = BooleanConvert @ BooleanFunction[12432, 4][a, b, c, d]
(a && b && ! c) || (! a && b && c && d) || (b && ! c && ! d)
We can convert this to if-and-constants form:
BooleanConvert[bf, "IF"]
If[a, If[b, If[c, False, True], False], If[b, If[c, If[d, True, False], If[d, False, True]], False]]
Here's a function that converts a boolean function to its if-and-constants form, and visualizes it as a graph:
ClearAll[bddGraph];
bddGraph[bf_] :=
With[{eq = Replace[BooleanConvert[bf, "IF"], If -> List, ∞, Heads -> True]},
Graph[
(Labeled[#, Extract[eq, # ~ Append ~ 1]] & /@ Position[eq, _List])
~ Join ~
(Labeled[#, #] & /@ {True, False}),
Flatten[
{Labeled[# \[DirectedEdge] # ~ Append ~ 2, True],
Labeled[# \[DirectedEdge] # ~ Append ~ 3, False]} & /@
Position[eq, _List], 2]
/.
a_ \[DirectedEdge] b_ :>
a \[DirectedEdge] Extract[eq, b] /; BooleanQ[Extract[eq, b]]]]
Now we can visualize the decision tree:
bddGraph[bf]
However, instead of seeing the graph, I got the following error message:
Syntax::sntxf: "!(*StyleBox[\"\\"\\\\"\\"\", \"MT\"])!(*StyleBox[!(#), \"MT\"])!(*StyleBox[\"\\"\\\\" cannot be followed by \\\\"\\"\", \"MT\"])!(*StyleBox[!(((\[ DirectedEdge])) ((# ~ Append ~ 2))), \"MT\"])!(*StyleBox[\"\\"\\\\".\\"\", \"MT\"])!(*StyleBox[!(\"\"), \"MT\"])"
Syntax::tsntxi: "!(*StyleBox[\"\\"\\\\"\\"\", \"MT\"])!(*StyleBox[!(\[ DirectedEdge]), \"MT\"])!(*StyleBox[\"\\"\\\\" is incomplete; more input is needed.\\"\", \"MT\"])!(*StyleBox[!(\"\"), \"MT\"])"
Syntax::sntxi: Incomplete expression; more input is needed.
{{0, 0, 0, 1}, {0, 0, 1, 0}, {0, 1, 0, 0}, {0, 1, 1, 1}, {1, 0, 0, 0}, {1, 0, 1, 0}, {1, 1, 0, 1}, {1, 1, 1, 1}}
and it produced no error. $\endgroup$bddGraph[BooleanConvert@BooleanFunction[12432, 4][a, b, c, d]]
works perfectly on my system: V10.2 running on OS X 10.10.2. $\endgroup$