Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -15,7 +15,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
15 |
"Drawing: House With Colors", "Drawing: Labels And Colors",
|
16 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
17 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
18 |
-
"Drawing: Simple Path", "Drawing: Spectral Embedding"])
|
19 |
|
20 |
# Helper function to draw and display graph
|
21 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -23,6 +23,95 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
23 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
24 |
st.pyplot(plt)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
# Function to display Drawing: Spectral Embedding
|
27 |
def display_spectral_embedding():
|
28 |
st.title("Drawing: Spectral Embedding")
|
|
|
15 |
"Drawing: House With Colors", "Drawing: Labels And Colors",
|
16 |
"Drawing: Multipartite Layout", "Drawing: Node Colormap",
|
17 |
"Drawing: Rainbow Coloring", "Drawing: Random Geometric Graph","Drawing: Self-loops",
|
18 |
+
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem"])
|
19 |
|
20 |
# Helper function to draw and display graph
|
21 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
23 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
24 |
st.pyplot(plt)
|
25 |
|
26 |
+
# Function to display Traveling Salesman Problem
|
27 |
+
def display_tsp():
|
28 |
+
st.title("Drawing: Traveling Salesman Problem")
|
29 |
+
|
30 |
+
option = st.radio("Choose a graph type:", ("Default Example", "Create your own"))
|
31 |
+
|
32 |
+
if option == "Default Example":
|
33 |
+
# Default example of random geometric graph with TSP solution
|
34 |
+
G = nx.random_geometric_graph(20, radius=0.4, seed=3)
|
35 |
+
pos = nx.get_node_attributes(G, "pos")
|
36 |
+
|
37 |
+
# Depot should be at (0.5, 0.5)
|
38 |
+
pos[0] = (0.5, 0.5)
|
39 |
+
|
40 |
+
H = G.copy()
|
41 |
+
|
42 |
+
# Calculating the distances between the nodes as edge's weight.
|
43 |
+
for i in range(len(pos)):
|
44 |
+
for j in range(i + 1, len(pos)):
|
45 |
+
dist = math.hypot(pos[i][0] - pos[j][0], pos[i][1] - pos[j][1])
|
46 |
+
dist = dist
|
47 |
+
G.add_edge(i, j, weight=dist)
|
48 |
+
|
49 |
+
cycle = nx_app.christofides(G, weight="weight")
|
50 |
+
edge_list = list(nx.utils.pairwise(cycle))
|
51 |
+
|
52 |
+
# Draw closest edges on each node only
|
53 |
+
nx.draw_networkx_edges(H, pos, edge_color="blue", width=0.5)
|
54 |
+
|
55 |
+
# Draw the route
|
56 |
+
nx.draw_networkx(
|
57 |
+
G,
|
58 |
+
pos,
|
59 |
+
with_labels=True,
|
60 |
+
edgelist=edge_list,
|
61 |
+
edge_color="red",
|
62 |
+
node_size=200,
|
63 |
+
width=3,
|
64 |
+
)
|
65 |
+
|
66 |
+
st.pyplot(plt)
|
67 |
+
st.write("The route of the traveler is:", cycle)
|
68 |
+
|
69 |
+
elif option == "Create your own":
|
70 |
+
# User can create their own graph
|
71 |
+
num_nodes = st.slider("Number of nodes:", min_value=3, max_value=30, value=20)
|
72 |
+
radius = st.slider("Edge radius:", min_value=0.1, max_value=1.0, value=0.4)
|
73 |
+
|
74 |
+
# Create random geometric graph based on user input
|
75 |
+
G_custom = nx.random_geometric_graph(num_nodes, radius, seed=3)
|
76 |
+
pos = nx.get_node_attributes(G_custom, "pos")
|
77 |
+
|
78 |
+
# Depot should be at (0.5, 0.5)
|
79 |
+
pos[0] = (0.5, 0.5)
|
80 |
+
|
81 |
+
H = G_custom.copy()
|
82 |
+
|
83 |
+
# Calculating the distances between the nodes as edge's weight.
|
84 |
+
for i in range(len(pos)):
|
85 |
+
for j in range(i + 1, len(pos)):
|
86 |
+
dist = math.hypot(pos[i][0] - pos[j][0], pos[i][1] - pos[j][1])
|
87 |
+
dist = dist
|
88 |
+
G_custom.add_edge(i, j, weight=dist)
|
89 |
+
|
90 |
+
# Find TSP cycle using Christofides' approximation
|
91 |
+
cycle = nx_app.christofides(G_custom, weight="weight")
|
92 |
+
edge_list = list(nx.utils.pairwise(cycle))
|
93 |
+
|
94 |
+
# Draw closest edges on each node only
|
95 |
+
nx.draw_networkx_edges(H, pos, edge_color="blue", width=0.5)
|
96 |
+
|
97 |
+
# Draw the TSP route
|
98 |
+
nx.draw_networkx(
|
99 |
+
G_custom,
|
100 |
+
pos,
|
101 |
+
with_labels=True,
|
102 |
+
edgelist=edge_list,
|
103 |
+
edge_color="red",
|
104 |
+
node_size=200,
|
105 |
+
width=3,
|
106 |
+
)
|
107 |
+
|
108 |
+
st.pyplot(plt)
|
109 |
+
st.write("The route of the traveler is:", cycle)
|
110 |
+
|
111 |
+
# Display Drawing: Traveling Salesman Problem if selected
|
112 |
+
if sidebar_option == "Drawing: Traveling Salesman Problem":
|
113 |
+
display_tsp()
|
114 |
+
|
115 |
# Function to display Drawing: Spectral Embedding
|
116 |
def display_spectral_embedding():
|
117 |
st.title("Drawing: Spectral Embedding")
|