Spaces:
Sleeping
Sleeping
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"])
|
25 |
|
26 |
# Helper function to draw and display graph
|
27 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -29,6 +29,75 @@ 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 dag_topological_layout():
|
33 |
st.title("Graph: DAG - Topological Layout")
|
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"])
|
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 erdos_renyi_graph():
|
33 |
+
st.title("Graph: Erdos Renyi")
|
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 random graph, or you can create your own Erdos-Renyi graph."
|
40 |
+
)
|
41 |
+
|
42 |
+
if graph_mode == "Default Example":
|
43 |
+
# Default random graph parameters
|
44 |
+
n = 10 # 10 nodes
|
45 |
+
m = 20 # 20 edges
|
46 |
+
seed = 20160 # seed random number generators for reproducibility
|
47 |
+
|
48 |
+
# Create random graph
|
49 |
+
G = nx.gnm_random_graph(n, m, seed=seed)
|
50 |
+
|
51 |
+
# Display node properties
|
52 |
+
st.write("### Node Degree and Clustering Coefficient")
|
53 |
+
for v in nx.nodes(G):
|
54 |
+
st.write(f"Node {v}: Degree = {nx.degree(G, v)}, Clustering Coefficient = {nx.clustering(G, v)}")
|
55 |
+
|
56 |
+
# Display adjacency list
|
57 |
+
st.write("### Adjacency List")
|
58 |
+
adj_list = "\n".join([line for line in nx.generate_adjlist(G)])
|
59 |
+
st.text(adj_list)
|
60 |
+
|
61 |
+
# Visualize the graph
|
62 |
+
pos = nx.spring_layout(G, seed=seed) # Seed for reproducible layout
|
63 |
+
fig, ax = plt.subplots()
|
64 |
+
nx.draw(G, pos=pos, ax=ax, with_labels=True, node_color="skyblue", edge_color="gray")
|
65 |
+
ax.set_title("Erdos-Renyi Random Graph")
|
66 |
+
st.pyplot(fig)
|
67 |
+
|
68 |
+
elif graph_mode == "Create Your Own":
|
69 |
+
st.write("### Create Your Own Random Erdos-Renyi Graph")
|
70 |
+
|
71 |
+
# Allow user to input the number of nodes and edges
|
72 |
+
n = st.number_input("Number of nodes (n)", min_value=2, value=10)
|
73 |
+
m = st.number_input("Number of edges (m)", min_value=1, value=20)
|
74 |
+
|
75 |
+
seed = st.number_input("Seed", value=20160)
|
76 |
+
|
77 |
+
# Create random graph
|
78 |
+
G = nx.gnm_random_graph(n, m, seed=seed)
|
79 |
+
|
80 |
+
# Display node properties
|
81 |
+
st.write("### Node Degree and Clustering Coefficient")
|
82 |
+
for v in nx.nodes(G):
|
83 |
+
st.write(f"Node {v}: Degree = {nx.degree(G, v)}, Clustering Coefficient = {nx.clustering(G, v)}")
|
84 |
+
|
85 |
+
# Display adjacency list
|
86 |
+
st.write("### Adjacency List")
|
87 |
+
adj_list = "\n".join([line for line in nx.generate_adjlist(G)])
|
88 |
+
st.text(adj_list)
|
89 |
+
|
90 |
+
# Visualize the graph
|
91 |
+
pos = nx.spring_layout(G, seed=seed) # Seed for reproducible layout
|
92 |
+
fig, ax = plt.subplots()
|
93 |
+
nx.draw(G, pos=pos, ax=ax, with_labels=True, node_color="skyblue", edge_color="gray")
|
94 |
+
ax.set_title("Erdos-Renyi Random Graph")
|
95 |
+
st.pyplot(fig)
|
96 |
+
|
97 |
+
# Display the corresponding page based on sidebar option
|
98 |
+
if sidebar_option == "Graph: Erdos Renyi":
|
99 |
+
erdos_renyi_graph()
|
100 |
+
|
101 |
def dag_topological_layout():
|
102 |
st.title("Graph: DAG - Topological Layout")
|
103 |
|