shukdevdatta123 commited on
Commit
bf2bbac
·
verified ·
1 Parent(s): d8aa040

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -175
app.py CHANGED
@@ -23,7 +23,7 @@ sidebar_option = st.sidebar.radio("Select an option",
23
  "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
24
  "Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
25
  "Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
26
- "Graph: Triads", "Algorithms: Circuits"])
27
 
28
  # Helper function to draw and display graph
29
  def draw_graph(G, pos=None, title="Graph Visualization"):
@@ -31,180 +31,6 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
31
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
32
  st.pyplot(plt)
33
 
34
- # Define custom operators for logical OR, AND, and NOT
35
- def custom_or(a, b):
36
- return Or(a, b)
37
-
38
- def custom_and(a, b):
39
- return And(a, b)
40
-
41
- def custom_not(a):
42
- return Not(a)
43
-
44
- # Function to convert the circuit to an equivalent formula.
45
- def circuit_to_formula(circuit):
46
- formula = nx.dag_to_branching(circuit)
47
- # Transfer the operator or variable labels for each node from the circuit to the formula.
48
- for v in formula:
49
- source = formula.nodes[v]["source"]
50
- formula.nodes[v]["label"] = circuit.nodes[source]["label"]
51
- return formula
52
-
53
- # Function to convert a sympy Boolean expression to a graph
54
- def formula_to_circuit(formula):
55
- circuit = nx.DiGraph()
56
- node_id = 0
57
-
58
- def add_formula_node(expr):
59
- nonlocal node_id
60
- # Create a unique node for each part of the formula
61
- current_node = node_id
62
- node_id += 1
63
- circuit.add_node(current_node, label=str(expr))
64
- return current_node
65
-
66
- def build_circuit(expr):
67
- if isinstance(expr, symbols):
68
- # It's a variable, just return it as a node
69
- return add_formula_node(expr)
70
- elif isinstance(expr, Not):
71
- # NOT operator
72
- child = build_circuit(expr.args[0])
73
- current_node = add_formula_node("¬")
74
- circuit.add_edge(current_node, child)
75
- return current_node
76
- elif isinstance(expr, Or) or isinstance(expr, And):
77
- # OR/AND operators
78
- left = build_circuit(expr.args[0])
79
- right = build_circuit(expr.args[1])
80
- current_node = add_formula_node(str(expr.func))
81
- circuit.add_edge(current_node, left)
82
- circuit.add_edge(current_node, right)
83
- return current_node
84
-
85
- build_circuit(formula)
86
- return circuit
87
-
88
- # Function to convert a formula graph to a string for display
89
- def formula_to_string(formula):
90
- def _to_string(formula, root):
91
- label = formula.nodes[root]["label"]
92
- if not formula[root]:
93
- return label
94
- children = formula[root]
95
- if len(children) == 1:
96
- child = nx.utils.arbitrary_element(children)
97
- return f"{label}({_to_string(formula, child)})"
98
- left, right = formula[root]
99
- left_subformula = _to_string(formula, left)
100
- right_subformula = _to_string(formula, right)
101
- return f"({left_subformula} {label} {right_subformula})"
102
-
103
- root = next(v for v, d in formula.in_degree() if d == 0)
104
- return _to_string(formula, root)
105
-
106
- # Main Streamlit application
107
- def algorithms_circuits():
108
- st.title("Algorithms: Circuits")
109
-
110
- # Option to choose between creating your own or using the default example
111
- circuit_mode = st.radio(
112
- "Choose a Mode:",
113
- ("Default Example", "Create Your Own"),
114
- help="The default example shows a predefined Boolean circuit, or you can create your own."
115
- )
116
-
117
- if circuit_mode == "Default Example":
118
- # Define the default circuit as before
119
- circuit = nx.DiGraph()
120
- # Layer 0
121
- circuit.add_node(0, label="∧", layer=0)
122
- # Layer 1
123
- circuit.add_node(1, label="∨", layer=1)
124
- circuit.add_node(2, label="∨", layer=1)
125
- circuit.add_edge(0, 1)
126
- circuit.add_edge(0, 2)
127
- # Layer 2
128
- circuit.add_node(3, label="x", layer=2)
129
- circuit.add_node(4, label="y", layer=2)
130
- circuit.add_node(5, label="¬", layer=2)
131
- circuit.add_edge(1, 3)
132
- circuit.add_edge(1, 4)
133
- circuit.add_edge(2, 4)
134
- circuit.add_edge(2, 5)
135
- # Layer 3
136
- circuit.add_node(6, label="z", layer=3)
137
- circuit.add_edge(5, 6)
138
- # Convert the circuit to an equivalent formula.
139
- formula = circuit_to_formula(circuit)
140
- st.write("Formula: ", formula_to_string(formula))
141
-
142
- labels = nx.get_node_attributes(circuit, "label")
143
- options = {
144
- "node_size": 600,
145
- "alpha": 0.5,
146
- "node_color": "blue",
147
- "labels": labels,
148
- "font_size": 22,
149
- }
150
- plt.figure(figsize=(8, 8))
151
- pos = nx.multipartite_layout(circuit, subset_key="layer")
152
- nx.draw_networkx(circuit, pos, **options)
153
- plt.title(formula_to_string(formula))
154
- plt.axis("equal")
155
- st.pyplot()
156
-
157
- elif circuit_mode == "Create Your Own":
158
- st.write("### Create Your Own Circuit")
159
-
160
- # Text input for the Boolean expression
161
- boolean_expression = st.text_input("Enter a Boolean expression (e.g., ((x or y) and (y or not(z)))):")
162
-
163
- # Generate button
164
- if st.button("Generate Circuit"):
165
- if boolean_expression:
166
- try:
167
- # Define symbols
168
- x, y, z = symbols('x y z')
169
-
170
- # Replace the custom characters with logical operators
171
- boolean_expression = boolean_expression.replace("∨", " or ").replace("∧", " and ").replace("¬", " not ")
172
-
173
- # Use eval to parse the input expression and replace symbols
174
- expr = eval(boolean_expression, {}, {
175
- "or": custom_or, "and": custom_and, "not": custom_not, "x": x, "y": y, "z": z})
176
-
177
- # Convert the formula to a circuit
178
- circuit = formula_to_circuit(expr)
179
-
180
- # Display the formula as a string
181
- st.write("Formula: ", formula_to_string(circuit))
182
-
183
- # Visualize the circuit
184
- labels = nx.get_node_attributes(circuit, "label")
185
- options = {
186
- "node_size": 600,
187
- "alpha": 0.5,
188
- "node_color": "blue",
189
- "labels": labels,
190
- "font_size": 22,
191
- }
192
- plt.figure(figsize=(8, 8))
193
- pos = nx.multipartite_layout(circuit, subset_key="layer")
194
- nx.draw_networkx(circuit, pos, **options)
195
- plt.title(formula_to_string(circuit))
196
- plt.axis("equal")
197
- st.pyplot()
198
-
199
- except Exception as e:
200
- st.error(f"Error parsing the expression: {e}")
201
- else:
202
- st.error("Please enter a valid Boolean expression.")
203
-
204
- # Display the corresponding page based on sidebar option
205
- if sidebar_option == "Algorithms: Circuits":
206
- algorithms_circuits()
207
-
208
  def triads_graph():
209
  st.title("Graph: Triads")
210
 
 
23
  "Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
24
  "Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
25
  "Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
26
+ "Graph: Triads"])
27
 
28
  # Helper function to draw and display graph
29
  def draw_graph(G, pos=None, title="Graph Visualization"):
 
31
  nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
32
  st.pyplot(plt)
33
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  def triads_graph():
35
  st.title("Graph: Triads")
36