Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,35 @@ from streamlit.components.v1 import html
|
|
11 |
import matplotlib.colors as mpl
|
12 |
from PIL import Image
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
# Sidebar for selecting an option
|
15 |
sidebar_option = st.sidebar.radio("Select an option",
|
16 |
["Introductory Tutorial", "Basic: Properties",
|
@@ -72,6 +101,29 @@ if sidebar_option == "Introductory Tutorial":
|
|
72 |
st.write(desc)
|
73 |
st.write("---")
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
def plot_greedy_coloring(graph):
|
76 |
# Apply greedy coloring
|
77 |
graph_coloring = nx.greedy_color(graph)
|
|
|
11 |
import matplotlib.colors as mpl
|
12 |
from PIL import Image
|
13 |
|
14 |
+
# Function for bubble sort with step-by-step visualization
|
15 |
+
def bubble_sort(arr, animate=False):
|
16 |
+
n = len(arr)
|
17 |
+
steps = []
|
18 |
+
if animate:
|
19 |
+
steps.append(arr.copy()) # Store initial state for animation
|
20 |
+
for i in range(n):
|
21 |
+
for j in range(0, n-i-1):
|
22 |
+
if arr[j] > arr[j+1]:
|
23 |
+
arr[j], arr[j+1] = arr[j+1], arr[j] # Swap the elements
|
24 |
+
if animate:
|
25 |
+
steps.append(arr.copy()) # Store each step for animation
|
26 |
+
return arr, steps
|
27 |
+
|
28 |
+
# Function to animate the sorting process
|
29 |
+
def animate_sorting(arr, steps):
|
30 |
+
fig, ax = plt.subplots()
|
31 |
+
ax.set_title("Bubble Sort Animation")
|
32 |
+
bar_width = 0.5
|
33 |
+
rects = ax.bar(range(len(arr)), arr, width=bar_width)
|
34 |
+
|
35 |
+
def update(step):
|
36 |
+
for rect, height in zip(rects, steps[step]):
|
37 |
+
rect.set_height(height)
|
38 |
+
return rects
|
39 |
+
|
40 |
+
ani = animation.FuncAnimation(fig, update, frames=len(steps), repeat=False, interval=300, blit=True)
|
41 |
+
st.pyplot(fig) # Display the animation in Streamlit
|
42 |
+
|
43 |
# Sidebar for selecting an option
|
44 |
sidebar_option = st.sidebar.radio("Select an option",
|
45 |
["Introductory Tutorial", "Basic: Properties",
|
|
|
101 |
st.write(desc)
|
102 |
st.write("---")
|
103 |
|
104 |
+
# Add the Bubble Sort Simulator button
|
105 |
+
if st.button("Bubble Sort Simulator"):
|
106 |
+
st.title("Bubble Sort with Animation")
|
107 |
+
|
108 |
+
# Get input array from user
|
109 |
+
user_input = st.text_input("Enter the array elements separated by spaces:")
|
110 |
+
if user_input:
|
111 |
+
arr = list(map(int, user_input.split()))
|
112 |
+
|
113 |
+
# Ask the user if they want an animation
|
114 |
+
animate_option = st.radio("Do you want to see the animation?", ("Yes", "No"))
|
115 |
+
animate = animate_option == 'Yes'
|
116 |
+
|
117 |
+
# Perform bubble sort and get the steps for animation
|
118 |
+
sorted_array, steps = bubble_sort(arr, animate)
|
119 |
+
|
120 |
+
# Display the sorted array
|
121 |
+
st.write("Sorted Array:", sorted_array)
|
122 |
+
|
123 |
+
# Show animation if required
|
124 |
+
if animate:
|
125 |
+
animate_sorting(arr, steps)
|
126 |
+
|
127 |
def plot_greedy_coloring(graph):
|
128 |
# Apply greedy coloring
|
129 |
graph_coloring = nx.greedy_color(graph)
|