Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import networkx as nx
|
| 4 |
+
|
| 5 |
+
# Create lollipop graph
|
| 6 |
+
G = nx.lollipop_graph(4, 6)
|
| 7 |
+
|
| 8 |
+
# Initialize a list for path lengths
|
| 9 |
+
pathlengths = []
|
| 10 |
+
|
| 11 |
+
# Display the source-target shortest path lengths
|
| 12 |
+
st.write("### Source vertex {target:length, }")
|
| 13 |
+
for v in G.nodes():
|
| 14 |
+
spl = dict(nx.single_source_shortest_path_length(G, v))
|
| 15 |
+
st.write(f"Vertex {v}: {spl}")
|
| 16 |
+
for p in spl:
|
| 17 |
+
pathlengths.append(spl[p])
|
| 18 |
+
|
| 19 |
+
# Calculate and display the average shortest path length
|
| 20 |
+
avg_path_length = sum(pathlengths) / len(pathlengths)
|
| 21 |
+
st.write(f"### Average shortest path length: {avg_path_length}")
|
| 22 |
+
|
| 23 |
+
# Calculate and display the distribution of path lengths
|
| 24 |
+
dist = {}
|
| 25 |
+
for p in pathlengths:
|
| 26 |
+
if p in dist:
|
| 27 |
+
dist[p] += 1
|
| 28 |
+
else:
|
| 29 |
+
dist[p] = 1
|
| 30 |
+
|
| 31 |
+
st.write("### Length #paths")
|
| 32 |
+
for d in sorted(dist.keys()):
|
| 33 |
+
st.write(f"Length {d}: {dist[d]} paths")
|
| 34 |
+
|
| 35 |
+
# Display the graph metrics
|
| 36 |
+
st.write(f"### Graph Metrics")
|
| 37 |
+
st.write(f"Radius: {nx.radius(G)}")
|
| 38 |
+
st.write(f"Diameter: {nx.diameter(G)}")
|
| 39 |
+
st.write(f"Eccentricity: {nx.eccentricity(G)}")
|
| 40 |
+
st.write(f"Center: {nx.center(G)}")
|
| 41 |
+
st.write(f"Periphery: {nx.periphery(G)}")
|
| 42 |
+
st.write(f"Density: {nx.density(G)}")
|
| 43 |
+
|
| 44 |
+
# Visualize the graph
|
| 45 |
+
st.write("### Graph Visualization")
|
| 46 |
+
pos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility
|
| 47 |
+
plt.figure(figsize=(8, 6))
|
| 48 |
+
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
| 49 |
+
st.pyplot(plt)
|