Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,114 +23,7 @@ sidebar_option = st.sidebar.radio("Select an option",
|
|
23 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
24 |
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
25 |
"Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
|
26 |
-
"Graph: Triads", "Algorithms: Cycle Detection", "Algorithms: Greedy Coloring"
|
27 |
-
|
28 |
-
def plot_shortest_path(graph, start, end):
|
29 |
-
try:
|
30 |
-
# Find the shortest path from start to end node
|
31 |
-
path = nx.shortest_path(graph, source=start, target=end, weight='weight')
|
32 |
-
st.write(f"Shortest path from {start} to {end}:", path)
|
33 |
-
|
34 |
-
# Create a list of edges in the shortest path
|
35 |
-
path_edges = list(zip(path, path[1:]))
|
36 |
-
|
37 |
-
# Create a list of all edges, and assign colors based on whether they are in the shortest path or not
|
38 |
-
edge_colors = [
|
39 |
-
"red" if edge in path_edges or tuple(reversed(edge)) in path_edges else "black"
|
40 |
-
for edge in graph.edges()
|
41 |
-
]
|
42 |
-
|
43 |
-
# Visualize the graph
|
44 |
-
pos = nx.spring_layout(graph)
|
45 |
-
nx.draw_networkx_nodes(graph, pos)
|
46 |
-
nx.draw_networkx_edges(graph, pos, edge_color=edge_colors)
|
47 |
-
nx.draw_networkx_labels(graph, pos)
|
48 |
-
nx.draw_networkx_edge_labels(
|
49 |
-
graph, pos, edge_labels={(u, v): d["weight"] for u, v, d in graph.edges(data=True)}
|
50 |
-
)
|
51 |
-
plt.title(f"Shortest Path from {start} to {end}")
|
52 |
-
st.pyplot(plt)
|
53 |
-
|
54 |
-
except nx.NetworkXNoPath:
|
55 |
-
st.error(f"No path found between {start} and {end}.")
|
56 |
-
except nx.NodeNotFound as e:
|
57 |
-
st.error(f"Error: {str(e)}")
|
58 |
-
|
59 |
-
def algorithms_shortest_path():
|
60 |
-
st.title("Algorithms: Find Shortest Path")
|
61 |
-
|
62 |
-
# Option to choose between creating your own or using the default example
|
63 |
-
graph_mode = st.radio(
|
64 |
-
"Choose a Mode:",
|
65 |
-
("Default Example", "Create Your Own"),
|
66 |
-
help="The default example shows a predefined graph, or you can create your own."
|
67 |
-
)
|
68 |
-
|
69 |
-
if graph_mode == "Default Example":
|
70 |
-
# Create a predefined graph for the shortest path example
|
71 |
-
G = nx.Graph()
|
72 |
-
G.add_nodes_from(["A", "B", "C", "D", "E", "F", "G", "H"])
|
73 |
-
G.add_edge("A", "B", weight=4)
|
74 |
-
G.add_edge("A", "H", weight=8)
|
75 |
-
G.add_edge("B", "C", weight=8)
|
76 |
-
G.add_edge("B", "H", weight=11)
|
77 |
-
G.add_edge("C", "D", weight=7)
|
78 |
-
G.add_edge("C", "F", weight=4)
|
79 |
-
G.add_edge("C", "I", weight=2)
|
80 |
-
G.add_edge("D", "E", weight=9)
|
81 |
-
G.add_edge("D", "F", weight=14)
|
82 |
-
G.add_edge("E", "F", weight=10)
|
83 |
-
G.add_edge("F", "G", weight=2)
|
84 |
-
G.add_edge("G", "H", weight=1)
|
85 |
-
G.add_edge("G", "I", weight=6)
|
86 |
-
G.add_edge("H", "I", weight=7)
|
87 |
-
|
88 |
-
# Set start and end nodes
|
89 |
-
start = "A"
|
90 |
-
end = "E"
|
91 |
-
plot_shortest_path(G, start, end)
|
92 |
-
|
93 |
-
elif graph_mode == "Create Your Own":
|
94 |
-
st.write("### Create Your Own Graph")
|
95 |
-
|
96 |
-
# Input for creating a custom graph
|
97 |
-
nodes_input = st.text_area("Enter nodes (e.g., A, B, C, D):")
|
98 |
-
edges_input = st.text_area("Enter edges with weights (e.g., A,B,4, B,C,7):").strip()
|
99 |
-
start_node = st.text_input("Enter start node (e.g., A):")
|
100 |
-
end_node = st.text_input("Enter end node (e.g., E):")
|
101 |
-
|
102 |
-
if st.button("Generate Graph"):
|
103 |
-
if nodes_input and edges_input and start_node and end_node:
|
104 |
-
try:
|
105 |
-
# Parse the input for nodes and edges
|
106 |
-
nodes = nodes_input.split(",")
|
107 |
-
edges = [
|
108 |
-
tuple(edge.split(","))
|
109 |
-
for edge in edges_input.split("\n") if edge.strip()
|
110 |
-
]
|
111 |
-
edges = [(u, v, int(weight)) for u, v, weight in edges] # Parse weights as integers
|
112 |
-
|
113 |
-
# Create graph
|
114 |
-
G = nx.Graph()
|
115 |
-
G.add_nodes_from(nodes)
|
116 |
-
G.add_weighted_edges_from(edges)
|
117 |
-
|
118 |
-
# Show custom graph edges
|
119 |
-
st.write("Custom Graph:", G.edges())
|
120 |
-
|
121 |
-
# Check if start and end nodes are valid
|
122 |
-
if start_node not in G.nodes or end_node not in G.nodes:
|
123 |
-
st.error("Start or End node is not in the graph!")
|
124 |
-
else:
|
125 |
-
plot_shortest_path(G, start_node, end_node)
|
126 |
-
|
127 |
-
except Exception as e:
|
128 |
-
st.error(f"Error creating the graph: {e}")
|
129 |
-
else:
|
130 |
-
st.error("Please enter valid nodes, edges, and start/end nodes.")
|
131 |
-
|
132 |
-
if sidebar_option == "Algorithms: Find Shortest Path":
|
133 |
-
algorithms_shortest_path()
|
134 |
|
135 |
def plot_greedy_coloring(graph):
|
136 |
# Apply greedy coloring
|
|
|
23 |
"Drawing: Simple Path", "Drawing: Spectral Embedding", "Drawing: Traveling Salesman Problem",
|
24 |
"Drawing: Weighted Graph", "3D Drawing: Animations of 3D Rotation", "3D Drawing: Basic Matplotlib",
|
25 |
"Graph: DAG - Topological Layout", "Graph: Erdos Renyi", "Graph: Karate Club", "Graph: Minimum Spanning Tree",
|
26 |
+
"Graph: Triads", "Algorithms: Cycle Detection", "Algorithms: Greedy Coloring"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
def plot_greedy_coloring(graph):
|
29 |
# Apply greedy coloring
|