Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -23,7 +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"])
|
27 |
|
28 |
# Helper function to draw and display graph
|
29 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
@@ -31,6 +31,66 @@ def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
31 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
32 |
st.pyplot(plt)
|
33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
def triads_graph():
|
35 |
st.title("Graph: Triads")
|
36 |
|
|
|
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"])
|
27 |
|
28 |
# Helper function to draw and display graph
|
29 |
def draw_graph(G, pos=None, title="Graph Visualization"):
|
|
|
31 |
nx.draw(G, pos=pos, with_labels=True, node_color='lightblue', node_size=500, font_size=10, font_weight='bold')
|
32 |
st.pyplot(plt)
|
33 |
|
34 |
+
def plot_cycle_detection(graph):
|
35 |
+
# Draw the graph
|
36 |
+
pos = nx.spring_layout(graph, seed=8020)
|
37 |
+
nx.draw(graph, pos, with_labels=True, node_size=2000, node_color="lightblue")
|
38 |
+
|
39 |
+
# Find and highlight the cycle
|
40 |
+
try:
|
41 |
+
cycle = nx.find_cycle(graph, orientation="original")
|
42 |
+
st.write("Cycle Detected:", cycle)
|
43 |
+
|
44 |
+
# Highlight the cycle in red
|
45 |
+
nx.draw_networkx_edges(graph, pos, edgelist=cycle, edge_color="r", width=2)
|
46 |
+
except nx.NetworkXNoCycle:
|
47 |
+
st.write("No cycle detected")
|
48 |
+
|
49 |
+
# Display the graph
|
50 |
+
plt.title("Cycle Detection in Directed Graph")
|
51 |
+
plt.show()
|
52 |
+
|
53 |
+
def algorithms_cycle_detection():
|
54 |
+
st.title("Algorithms: Cycle Detection")
|
55 |
+
|
56 |
+
# Option to choose between creating your own or using the default example
|
57 |
+
graph_mode = st.radio(
|
58 |
+
"Choose a Mode:",
|
59 |
+
("Default Example", "Create Your Own"),
|
60 |
+
help="The default example shows a predefined graph, or you can create your own."
|
61 |
+
)
|
62 |
+
|
63 |
+
if graph_mode == "Default Example":
|
64 |
+
# Create a predefined graph with a cycle
|
65 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 4), (4, 2), (3, 5), (3, 2), (1, 5)])
|
66 |
+
st.write("Default Graph: A simple directed graph with a cycle.")
|
67 |
+
plot_cycle_detection(G)
|
68 |
+
|
69 |
+
elif graph_mode == "Create Your Own":
|
70 |
+
st.write("### Create Your Own Graph")
|
71 |
+
|
72 |
+
# Input for creating custom graph
|
73 |
+
edges_input = st.text_area("Enter directed edges (e.g., (1, 2), (2, 3), (3, 4), (4, 2)):")
|
74 |
+
|
75 |
+
if st.button("Generate Graph"):
|
76 |
+
if edges_input:
|
77 |
+
try:
|
78 |
+
# Parse the input edges
|
79 |
+
edges = [tuple(map(int, edge.strip()[1:-1].split(","))) for edge in edges_input.split(",")]
|
80 |
+
G = nx.DiGraph(edges)
|
81 |
+
|
82 |
+
st.write("Custom Graph:", G.edges())
|
83 |
+
plot_cycle_detection(G)
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
st.error(f"Error creating the graph: {e}")
|
87 |
+
else:
|
88 |
+
st.error("Please enter valid directed edges.")
|
89 |
+
|
90 |
+
# Display the corresponding page based on sidebar option
|
91 |
+
if sidebar_option == "Algorithms: Cycle Detection":
|
92 |
+
algorithms_cycle_detection()
|
93 |
+
|
94 |
def triads_graph():
|
95 |
st.title("Graph: Triads")
|
96 |
|