Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -36,15 +36,23 @@ def plot_cycle_detection(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
|
40 |
try:
|
41 |
-
|
42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
except nx.NetworkXNoCycle:
|
47 |
-
st.write("No cycle detected")
|
48 |
|
49 |
# Display the graph
|
50 |
plt.title("Cycle Detection in Directed Graph")
|
@@ -61,16 +69,16 @@ def algorithms_cycle_detection():
|
|
61 |
)
|
62 |
|
63 |
if graph_mode == "Default Example":
|
64 |
-
# Create a predefined graph with
|
65 |
-
G = nx.DiGraph([(1, 2), (2, 3), (3,
|
66 |
-
st.write("Default Graph: A simple directed graph with
|
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,
|
74 |
|
75 |
if st.button("Generate Graph"):
|
76 |
if edges_input:
|
|
|
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 all cycles in the directed graph
|
40 |
try:
|
41 |
+
cycles = list(nx.simple_cycles(graph))
|
42 |
+
if cycles:
|
43 |
+
st.write("Cycles Detected:")
|
44 |
+
for cycle in cycles:
|
45 |
+
st.write(cycle)
|
46 |
+
|
47 |
+
# Highlight each cycle in red
|
48 |
+
for cycle in cycles:
|
49 |
+
edges_in_cycle = [(cycle[i], cycle[i + 1] if i + 1 < len(cycle) else cycle[0]) for i in range(len(cycle))]
|
50 |
+
nx.draw_networkx_edges(graph, pos, edgelist=edges_in_cycle, edge_color="r", width=2)
|
51 |
+
else:
|
52 |
+
st.write("No cycles detected")
|
53 |
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"Error detecting cycles: {e}")
|
|
|
|
|
56 |
|
57 |
# Display the graph
|
58 |
plt.title("Cycle Detection in Directed Graph")
|
|
|
69 |
)
|
70 |
|
71 |
if graph_mode == "Default Example":
|
72 |
+
# Create a predefined graph with multiple cycles
|
73 |
+
G = nx.DiGraph([(1, 2), (2, 3), (3, 1), (3, 4), (4, 5), (5, 3)])
|
74 |
+
st.write("Default Graph: A simple directed graph with multiple cycles.")
|
75 |
plot_cycle_detection(G)
|
76 |
|
77 |
elif graph_mode == "Create Your Own":
|
78 |
st.write("### Create Your Own Graph")
|
79 |
|
80 |
# Input for creating custom graph
|
81 |
+
edges_input = st.text_area("Enter directed edges (e.g., (1, 2), (2, 3), (3, 1), (3, 4)):").strip()
|
82 |
|
83 |
if st.button("Generate Graph"):
|
84 |
if edges_input:
|