| import streamlit as st |
| import matplotlib.pyplot as plt |
| import networkx as nx |
|
|
| |
| G = nx.lollipop_graph(4, 6) |
|
|
| |
| pathlengths = [] |
|
|
| |
| 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]) |
|
|
| |
| avg_path_length = sum(pathlengths) / len(pathlengths) |
| st.write(f"### Average shortest path length: {avg_path_length}") |
|
|
| |
| 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") |
|
|
| |
| st.write(f"### Graph Metrics") |
| 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)}") |
|
|
| |
| st.write("### Graph Visualization") |
| pos = nx.spring_layout(G, seed=3068) |
| 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) |
|
|