Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,48 +5,65 @@ import networkx as nx
|
|
5 |
# Add a headline
|
6 |
st.title("Basic: Properties")
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
#
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
st.write(
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.write(
|
42 |
-
st.write(f"
|
43 |
-
st.write(f"
|
44 |
-
st.write(f"
|
45 |
-
st.write(f"
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
nx.
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Add a headline
|
6 |
st.title("Basic: Properties")
|
7 |
|
8 |
+
# Add a radio button for selecting the graph type
|
9 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
10 |
+
|
11 |
+
# Function to display properties and graph
|
12 |
+
def display_graph_properties(G):
|
13 |
+
# Initialize a list for path lengths
|
14 |
+
pathlengths = []
|
15 |
+
|
16 |
+
# Display the source-target shortest path lengths
|
17 |
+
st.write("### Source vertex {target:length, }")
|
18 |
+
for v in G.nodes():
|
19 |
+
spl = dict(nx.single_source_shortest_path_length(G, v))
|
20 |
+
st.write(f"Vertex {v}: {spl}")
|
21 |
+
for p in spl:
|
22 |
+
pathlengths.append(spl[p])
|
23 |
+
|
24 |
+
# Calculate and display the average shortest path length
|
25 |
+
avg_path_length = sum(pathlengths) / len(pathlengths)
|
26 |
+
st.write(f"### Average shortest path length: {avg_path_length}")
|
27 |
+
|
28 |
+
# Calculate and display the distribution of path lengths
|
29 |
+
dist = {}
|
30 |
+
for p in pathlengths:
|
31 |
+
if p in dist:
|
32 |
+
dist[p] += 1
|
33 |
+
else:
|
34 |
+
dist[p] = 1
|
35 |
+
|
36 |
+
st.write("### Length #paths")
|
37 |
+
for d in sorted(dist.keys()):
|
38 |
+
st.write(f"Length {d}: {dist[d]} paths")
|
39 |
+
|
40 |
+
# Display the graph metrics with a "Properties" heading
|
41 |
+
st.write("### Properties")
|
42 |
+
st.write(f"Radius: {nx.radius(G)}")
|
43 |
+
st.write(f"Diameter: {nx.diameter(G)}")
|
44 |
+
st.write(f"Eccentricity: {nx.eccentricity(G)}")
|
45 |
+
st.write(f"Center: {nx.center(G)}")
|
46 |
+
st.write(f"Periphery: {nx.periphery(G)}")
|
47 |
+
st.write(f"Density: {nx.density(G)}")
|
48 |
+
|
49 |
+
# Visualize the graph
|
50 |
+
st.write("### Graph Visualization")
|
51 |
+
pos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility
|
52 |
+
plt.figure(figsize=(8, 6))
|
53 |
+
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
54 |
+
st.pyplot(plt)
|
55 |
+
|
56 |
+
# Default example
|
57 |
+
if option == "Default Example":
|
58 |
+
G = nx.lollipop_graph(4, 6)
|
59 |
+
display_graph_properties(G)
|
60 |
+
|
61 |
+
# Create your own graph
|
62 |
+
elif option == "Create your own":
|
63 |
+
# Let the user input number of nodes and edges
|
64 |
+
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
|
65 |
+
num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
|
66 |
+
|
67 |
+
if num_nodes >= 2 and num_edges >= 1:
|
68 |
+
G = nx.lollipop_graph(num_nodes, num_edges)
|
69 |
+
display_graph_properties(G)
|