shukdevdatta123 commited on
Commit
f3c42dc
·
verified ·
1 Parent(s): e3de282

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -20
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 random graph with user input
88
- G = nx.gnm_random_graph(num_nodes, num_edges)
89
 
90
- # Add random weights to the edges
91
- for u, v in G.edges():
92
- G[u][v]["weight"] = st.number_input(f"Weight for edge ({u}, {v})", value=1)
 
 
 
 
93
 
94
- # Find the minimum spanning tree
95
- T = nx.minimum_spanning_tree(G)
96
 
97
- # Visualize the graph and the minimum spanning tree
98
- pos = nx.spring_layout(G)
99
- fig, ax = plt.subplots(figsize=(8, 8))
100
- nx.draw_networkx_nodes(G, pos, node_color="lightblue", node_size=500, ax=ax)
101
- nx.draw_networkx_edges(G, pos, edge_color="grey", ax=ax)
102
- nx.draw_networkx_labels(G, pos, font_size=12, font_family="sans-serif", ax=ax)
103
- nx.draw_networkx_edge_labels(
104
- G, pos, edge_labels={(u, v): d["weight"] for u, v, d in G.edges(data=True)}, ax=ax
105
- )
106
- nx.draw_networkx_edges(T, pos, edge_color="green", width=2, ax=ax)
107
- ax.set_title("Custom Graph and Minimum Spanning Tree")
108
- plt.axis("off")
109
- st.pyplot(fig)
 
 
 
 
 
 
 
 
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":