File size: 2,728 Bytes
38f979d
 
 
 
e95128d
 
98c04d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e95128d
 
67e7fee
 
 
e95128d
 
98c04d6
e95128d
 
 
98c04d6
e95128d
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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"])

# Function to display properties and graph
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)

# Only display Basic: Properties when it is selected from the sidebar
if sidebar_option == "Basic: Properties":
    # Set the title for the page
    st.title("Basic: Properties")

    # Add a radio button for selecting the graph type
    option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))

    # Default example
    if option == "Default Example":
        G = nx.lollipop_graph(4, 6)
        display_graph_properties(G)

    # Create your own graph
    elif option == "Create your own":
        # Let the user input number of nodes and edges
        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 = nx.lollipop_graph(num_nodes, num_edges)
            display_graph_properties(G)