shukdevdatta123's picture
Update app.py
8f75d16 verified
raw
history blame
4.42 kB
import streamlit as st
import matplotlib.pyplot as plt
import networkx as nx
# Add a sidebar with options
sidebar_option = st.sidebar.radio("Select an option",
["Select an option", "Basic: Properties", "Basic: Read and write graphs"])
# Function to display properties and graph for Basic: Properties
def display_graph_properties(G):
# Initialize a list for path lengths
pathlengths = []
# Display the source-target shortest path lengths
st.write("### Source vertex {target:length, }")
for v in G.nodes():
spl = dict(nx.single_source_shortest_path_length(G, v))
st.write(f"Vertex {v}: {spl}")
for p in spl:
pathlengths.append(spl[p])
# Calculate and display the average shortest path length
avg_path_length = sum(pathlengths) / len(pathlengths)
st.write(f"### Average shortest path length: {avg_path_length}")
# Calculate and display the distribution of path lengths
dist = {}
for p in pathlengths:
if p in dist:
dist[p] += 1
else:
dist[p] = 1
st.write("### Length #paths")
for d in sorted(dist.keys()):
st.write(f"Length {d}: {dist[d]} paths")
# Display the graph metrics with a "Properties" heading
st.write("### Properties")
st.write(f"Radius: {nx.radius(G)}")
st.write(f"Diameter: {nx.diameter(G)}")
st.write(f"Eccentricity: {nx.eccentricity(G)}")
st.write(f"Center: {nx.center(G)}")
st.write(f"Periphery: {nx.periphery(G)}")
st.write(f"Density: {nx.density(G)}")
# Visualize the graph
st.write("### Graph Visualization")
pos = nx.spring_layout(G, seed=3068) # Seed layout for reproducibility
plt.figure(figsize=(8, 6))
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
st.pyplot(plt)
# Function to display graph for Basic: Read and write graphs
def display_read_write_graph(G):
# Print the adjacency list of the graph
st.write("### Adjacency List:")
for line in nx.generate_adjlist(G):
st.write(line)
# Write the graph's edge list to a file
st.write("### Writing Edge List to 'grid.edgelist' file:")
nx.write_edgelist(G, path="grid.edgelist", delimiter=":")
st.write("Edge list written to 'grid.edgelist'")
# Read the graph from the edge list
st.write("### Reading Edge List from 'grid.edgelist' file:")
H = nx.read_edgelist(path="grid.edgelist", delimiter=":")
st.write("Edge list read into graph H")
# Visualize the graph
st.write("### Graph Visualization:")
pos = nx.spring_layout(H, seed=200) # Seed for reproducibility
plt.figure(figsize=(8, 6))
nx.draw(H, pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
st.pyplot(plt)
# Display Basic: Properties if selected
if sidebar_option == "Basic: Properties":
st.title("Basic: Properties")
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
# Default example: 5x5 grid graph
if option == "Default Example":
G = nx.lollipop_graph(4, 6)
display_graph_properties(G)
# Create your own graph
elif option == "Create your own":
num_nodes = st.number_input("Number of nodes:", min_value=2, max_value=50, value=5)
num_edges = st.number_input("Number of edges per group (for lollipop graph):", min_value=1, max_value=10, value=3)
if num_nodes >= 2 and num_edges >= 1:
G_custom = nx.lollipop_graph(num_nodes, num_edges)
display_graph_properties(G_custom)
# Display Basic: Read and write graphs if selected
elif sidebar_option == "Basic: Read and write graphs":
st.title("Basic: Read and write graphs")
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
# Default example: 5x5 grid graph
if option == "Default Example":
G = nx.grid_2d_graph(5, 5) # 5x5 grid
display_read_write_graph(G)
# Create your own graph
elif option == "Create your own":
rows = st.number_input("Number of rows:", min_value=2, max_value=20, value=5)
cols = st.number_input("Number of columns:", min_value=2, max_value=20, value=5)
if rows >= 2 and cols >= 2:
G_custom = nx.grid_2d_graph(rows, cols)
display_read_write_graph(G_custom)