Update app.py
Browse files
app.py
CHANGED
|
@@ -22,7 +22,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
| 22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
| 23 |
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
| 24 |
"Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
|
| 25 |
-
"Graph: Triads"])
|
| 26 |
|
| 27 |
# Helper function to draw and display graph
|
| 28 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
@@ -30,6 +30,104 @@ 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 triads_graph():
|
| 34 |
st.title("Graph: Triads")
|
| 35 |
|
|
|
|
| 22 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
| 23 |
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
| 24 |
"Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
|
| 25 |
+
"Graph: Triads", "Algorithms: Circuits"])
|
| 26 |
|
| 27 |
# Helper function to draw and display graph
|
| 28 |
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 |
+
|
| 36 |
+
# Option to choose between creating your own or using the default example
|
| 37 |
+
circuit_mode = st.radio(
|
| 38 |
+
"Choose a Mode:",
|
| 39 |
+
("Default Example", "Create Your Own"),
|
| 40 |
+
help="The default example shows a predefined Boolean circuit, or you can create your own."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
if circuit_mode == "Default Example":
|
| 44 |
+
# Define the default circuit
|
| 45 |
+
circuit = nx.DiGraph()
|
| 46 |
+
# Layer 0
|
| 47 |
+
circuit.add_node(0, label="∧", layer=0)
|
| 48 |
+
# Layer 1
|
| 49 |
+
circuit.add_node(1, label="∨", layer=1)
|
| 50 |
+
circuit.add_node(2, label="∨", layer=1)
|
| 51 |
+
circuit.add_edge(0, 1)
|
| 52 |
+
circuit.add_edge(0, 2)
|
| 53 |
+
# Layer 2
|
| 54 |
+
circuit.add_node(3, label="x", layer=2)
|
| 55 |
+
circuit.add_node(4, label="y", layer=2)
|
| 56 |
+
circuit.add_node(5, label="¬", layer=2)
|
| 57 |
+
circuit.add_edge(1, 3)
|
| 58 |
+
circuit.add_edge(1, 4)
|
| 59 |
+
circuit.add_edge(2, 4)
|
| 60 |
+
circuit.add_edge(2, 5)
|
| 61 |
+
# Layer 3
|
| 62 |
+
circuit.add_node(6, label="z", layer=3)
|
| 63 |
+
circuit.add_edge(5, 6)
|
| 64 |
+
# Convert the circuit to an equivalent formula.
|
| 65 |
+
formula = circuit_to_formula(circuit)
|
| 66 |
+
st.write("Formula: ", formula_to_string(formula))
|
| 67 |
+
|
| 68 |
+
labels = nx.get_node_attributes(circuit, "label")
|
| 69 |
+
options = {
|
| 70 |
+
"node_size": 600,
|
| 71 |
+
"alpha": 0.5,
|
| 72 |
+
"node_color": "blue",
|
| 73 |
+
"labels": labels,
|
| 74 |
+
"font_size": 22,
|
| 75 |
+
}
|
| 76 |
+
plt.figure(figsize=(8, 8))
|
| 77 |
+
pos = nx.multipartite_layout(circuit, subset_key="layer")
|
| 78 |
+
nx.draw_networkx(circuit, pos, **options)
|
| 79 |
+
plt.title(formula_to_string(formula))
|
| 80 |
+
plt.axis("equal")
|
| 81 |
+
st.pyplot()
|
| 82 |
+
|
| 83 |
+
elif circuit_mode == "Create Your Own":
|
| 84 |
+
st.write("### Create Your Own Circuit")
|
| 85 |
+
|
| 86 |
+
# Let user input number of layers, nodes and edges for the circuit
|
| 87 |
+
num_layers = st.number_input("Number of layers:", min_value=1, value=3)
|
| 88 |
+
num_nodes = st.number_input("Number of nodes:", min_value=1, value=5)
|
| 89 |
+
num_edges = st.number_input("Number of edges:", min_value=1, value=3)
|
| 90 |
+
|
| 91 |
+
circuit = nx.DiGraph()
|
| 92 |
+
|
| 93 |
+
# Allow user to input labels and edges
|
| 94 |
+
for layer in range(num_layers):
|
| 95 |
+
for node in range(num_nodes):
|
| 96 |
+
label = st.text_input(f"Enter label for node ({layer}, {node}):", key=f"label_{layer}_{node}")
|
| 97 |
+
circuit.add_node(f"{layer}_{node}", label=label, layer=layer)
|
| 98 |
+
|
| 99 |
+
# Allow user to input edges between nodes
|
| 100 |
+
edges = []
|
| 101 |
+
for i in range(num_edges):
|
| 102 |
+
source = st.text_input(f"Enter source node for edge {i+1} (e.g., '0_1')", key=f"source_edge_{i}")
|
| 103 |
+
dest = st.text_input(f"Enter destination node for edge {i+1} (e.g., '1_2')", key=f"dest_edge_{i}")
|
| 104 |
+
edges.append((source, dest))
|
| 105 |
+
circuit.add_edges_from(edges)
|
| 106 |
+
|
| 107 |
+
# Create the formula based on the circuit
|
| 108 |
+
formula = circuit_to_formula(circuit)
|
| 109 |
+
st.write("Formula: ", formula_to_string(formula))
|
| 110 |
+
|
| 111 |
+
# Draw the circuit
|
| 112 |
+
labels = nx.get_node_attributes(circuit, "label")
|
| 113 |
+
options = {
|
| 114 |
+
"node_size": 600,
|
| 115 |
+
"alpha": 0.5,
|
| 116 |
+
"node_color": "blue",
|
| 117 |
+
"labels": labels,
|
| 118 |
+
"font_size": 22,
|
| 119 |
+
}
|
| 120 |
+
plt.figure(figsize=(8, 8))
|
| 121 |
+
pos = nx.multipartite_layout(circuit, subset_key="layer")
|
| 122 |
+
nx.draw_networkx(circuit, pos, **options)
|
| 123 |
+
plt.title(formula_to_string(formula))
|
| 124 |
+
plt.axis("equal")
|
| 125 |
+
st.pyplot()
|
| 126 |
+
|
| 127 |
+
# Display the corresponding page based on sidebar option
|
| 128 |
+
if sidebar_option == "Algorithms: Circuits":
|
| 129 |
+
algorithms_circuits()
|
| 130 |
+
|
| 131 |
def triads_graph():
|
| 132 |
st.title("Graph: Triads")
|
| 133 |
|