Update app.py
Browse files
app.py
CHANGED
|
@@ -84,29 +84,41 @@ def minimum_spanning_tree_graph():
|
|
| 84 |
num_nodes = st.number_input("Number of nodes", min_value=2, value=5)
|
| 85 |
num_edges = st.number_input("Number of edges", min_value=1, value=6)
|
| 86 |
|
| 87 |
-
# Create
|
| 88 |
-
G = nx.
|
| 89 |
|
| 90 |
-
#
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
|
| 97 |
-
#
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Display the corresponding page based on sidebar option
|
| 112 |
if sidebar_option == "Graph: Minimum Spanning Tree":
|
|
|
|
| 84 |
num_nodes = st.number_input("Number of nodes", min_value=2, value=5)
|
| 85 |
num_edges = st.number_input("Number of edges", min_value=1, value=6)
|
| 86 |
|
| 87 |
+
# Create empty graph
|
| 88 |
+
G = nx.Graph()
|
| 89 |
|
| 90 |
+
# Allow user to input the edges and their weights manually
|
| 91 |
+
edges = []
|
| 92 |
+
for i in range(num_edges):
|
| 93 |
+
source = st.number_input(f"Source node for edge {i+1}", min_value=0, max_value=num_nodes-1, key=f"source_{i}")
|
| 94 |
+
dest = st.number_input(f"Destination node for edge {i+1}", min_value=0, max_value=num_nodes-1, key=f"dest_{i}")
|
| 95 |
+
weight = st.number_input(f"Weight for edge ({source}, {dest})", min_value=1, value=1, key=f"weight_{i}")
|
| 96 |
+
edges.append((source, dest, {"weight": weight}))
|
| 97 |
|
| 98 |
+
# Add edges to the graph
|
| 99 |
+
G.add_edges_from(edges)
|
| 100 |
|
| 101 |
+
# Add nodes to the graph (to ensure all nodes are included, even if not explicitly added by the user)
|
| 102 |
+
G.add_nodes_from(range(num_nodes))
|
| 103 |
+
|
| 104 |
+
# Button to generate the graph and calculate MST
|
| 105 |
+
if st.button("Generate Graph"):
|
| 106 |
+
# Find the minimum spanning tree
|
| 107 |
+
T = nx.minimum_spanning_tree(G)
|
| 108 |
+
|
| 109 |
+
# Visualize the graph and the minimum spanning tree
|
| 110 |
+
pos = nx.spring_layout(G)
|
| 111 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 112 |
+
nx.draw_networkx_nodes(G, pos, node_color="lightblue", node_size=500, ax=ax)
|
| 113 |
+
nx.draw_networkx_edges(G, pos, edge_color="grey", ax=ax)
|
| 114 |
+
nx.draw_networkx_labels(G, pos, font_size=12, font_family="sans-serif", ax=ax)
|
| 115 |
+
nx.draw_networkx_edge_labels(
|
| 116 |
+
G, pos, edge_labels={(u, v): d["weight"] for u, v, d in G.edges(data=True)}, ax=ax
|
| 117 |
+
)
|
| 118 |
+
nx.draw_networkx_edges(T, pos, edge_color="green", width=2, ax=ax)
|
| 119 |
+
ax.set_title("Custom Graph and Minimum Spanning Tree")
|
| 120 |
+
plt.axis("off")
|
| 121 |
+
st.pyplot(fig)
|
| 122 |
|
| 123 |
# Display the corresponding page based on sidebar option
|
| 124 |
if sidebar_option == "Graph: Minimum Spanning Tree":
|