Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -30,6 +30,42 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
30 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
31 |
st.pyplot(plt)
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
def algorithms_circuits():
|
34 |
st.title("Algorithms: Circuits")
|
35 |
|
|
|
30 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
31 |
st.pyplot(plt)
|
32 |
|
33 |
+
def circuit_to_formula(circuit):
|
34 |
+
# Convert the circuit to an equivalent formula.
|
35 |
+
formula = nx.dag_to_branching(circuit)
|
36 |
+
# Transfer the operator or variable labels for each node from the
|
37 |
+
# circuit to the formula.
|
38 |
+
for v in formula:
|
39 |
+
source = formula.nodes[v]["source"]
|
40 |
+
formula.nodes[v]["label"] = circuit.nodes[source]["label"]
|
41 |
+
return formula
|
42 |
+
|
43 |
+
def formula_to_string(formula):
|
44 |
+
def _to_string(formula, root):
|
45 |
+
# If there are no children, this is a variable node.
|
46 |
+
label = formula.nodes[root]["label"]
|
47 |
+
if not formula[root]:
|
48 |
+
return label
|
49 |
+
# Otherwise, this is an operator.
|
50 |
+
children = formula[root]
|
51 |
+
# If one child, the label must be a NOT operator.
|
52 |
+
if len(children) == 1:
|
53 |
+
child = nx.utils.arbitrary_element(children)
|
54 |
+
return f"{label}({_to_string(formula, child)})"
|
55 |
+
# NB "left" and "right" here are a little misleading: there is
|
56 |
+
# no order on the children of a node. That's okay because the
|
57 |
+
# Boolean AND and OR operators are symmetric. It just means that the
|
58 |
+
# order of the operands cannot be predicted and hence the
|
59 |
+
# function does not necessarily behave the same way on every
|
60 |
+
# invocation.
|
61 |
+
left, right = formula[root]
|
62 |
+
left_subformula = _to_string(formula, left)
|
63 |
+
right_subformula = _to_string(formula, right)
|
64 |
+
return f"({left_subformula} {label} {right_subformula})"
|
65 |
+
|
66 |
+
root = next(v for v, d in formula.in_degree() if d == 0)
|
67 |
+
return _to_string(formula, root)
|
68 |
+
|
69 |
def algorithms_circuits():
|
70 |
st.title("Algorithms: Circuits")
|
71 |
|