Update app.py
Browse files
app.py
CHANGED
|
@@ -21,7 +21,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
| 21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
| 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"])
|
| 25 |
|
| 26 |
# Helper function to draw and display graph
|
| 27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
@@ -29,6 +29,89 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
| 29 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
| 30 |
st.pyplot(plt)
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
def karate_club_graph():
|
| 33 |
st.title("Graph: Karate Club")
|
| 34 |
|
|
|
|
| 21 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
| 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 |
|
| 26 |
# Helper function to draw and display graph
|
| 27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
|
| 29 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
| 30 |
st.pyplot(plt)
|
| 31 |
|
| 32 |
+
def minimum_spanning_tree_graph():
|
| 33 |
+
st.title("Graph: Minimum Spanning Tree")
|
| 34 |
+
|
| 35 |
+
# Sidebar selection for Default Example or Custom Graph
|
| 36 |
+
graph_mode = st.radio(
|
| 37 |
+
"Choose a Mode:",
|
| 38 |
+
("Default Example", "Create Your Own"),
|
| 39 |
+
help="Default example shows a graph and its minimum spanning tree, or you can create your own graph."
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
if graph_mode == "Default Example":
|
| 43 |
+
# Create a default graph
|
| 44 |
+
G = nx.Graph()
|
| 45 |
+
G.add_edges_from(
|
| 46 |
+
[
|
| 47 |
+
(0, 1, {"weight": 4}),
|
| 48 |
+
(0, 7, {"weight": 8}),
|
| 49 |
+
(1, 7, {"weight": 11}),
|
| 50 |
+
(1, 2, {"weight": 8}),
|
| 51 |
+
(2, 8, {"weight": 2}),
|
| 52 |
+
(2, 5, {"weight": 4}),
|
| 53 |
+
(2, 3, {"weight": 7}),
|
| 54 |
+
(3, 4, {"weight": 9}),
|
| 55 |
+
(3, 5, {"weight": 14}),
|
| 56 |
+
(4, 5, {"weight": 10}),
|
| 57 |
+
(5, 6, {"weight": 2}),
|
| 58 |
+
(6, 8, {"weight": 6}),
|
| 59 |
+
(7, 8, {"weight": 7}),
|
| 60 |
+
]
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Find the minimum spanning tree
|
| 64 |
+
T = nx.minimum_spanning_tree(G)
|
| 65 |
+
|
| 66 |
+
# Visualize the graph and the minimum spanning tree
|
| 67 |
+
pos = nx.spring_layout(G)
|
| 68 |
+
fig, ax = plt.subplots(figsize=(8, 8))
|
| 69 |
+
nx.draw_networkx_nodes(G, pos, node_color="lightblue", node_size=500, ax=ax)
|
| 70 |
+
nx.draw_networkx_edges(G, pos, edge_color="grey", ax=ax)
|
| 71 |
+
nx.draw_networkx_labels(G, pos, font_size=12, font_family="sans-serif", ax=ax)
|
| 72 |
+
nx.draw_networkx_edge_labels(
|
| 73 |
+
G, pos, edge_labels={(u, v): d["weight"] for u, v, d in G.edges(data=True)}, ax=ax
|
| 74 |
+
)
|
| 75 |
+
nx.draw_networkx_edges(T, pos, edge_color="green", width=2, ax=ax)
|
| 76 |
+
ax.set_title("Graph and Minimum Spanning Tree")
|
| 77 |
+
plt.axis("off")
|
| 78 |
+
st.pyplot(fig)
|
| 79 |
+
|
| 80 |
+
elif graph_mode == "Create Your Own":
|
| 81 |
+
st.write("### Create Your Own Graph")
|
| 82 |
+
|
| 83 |
+
# Allow user to input the number of nodes and edges for custom 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":
|
| 113 |
+
minimum_spanning_tree_graph()
|
| 114 |
+
|
| 115 |
def karate_club_graph():
|
| 116 |
st.title("Graph: Karate Club")
|
| 117 |
|