Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import matplotlib.pyplot as plt
|
| 3 |
+
import networkx as nx
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Define available colors
|
| 7 |
+
colors = [
|
| 8 |
+
"#FF9999", "#99FF99", "#9999FF", "#FFCC99", "#FFFF99",
|
| 9 |
+
"#CC99FF", "#66CCCC", "#FF6666", "#66FF66", "#6666FF"
|
| 10 |
+
]
|
| 11 |
+
|
| 12 |
+
# Define time slots and days
|
| 13 |
+
time_slots = ["08:30-10:00", "10:05-11:35", "11:40-13:10", "14:00-15:30", "15:45-17:15", "17:30-19:00"]
|
| 14 |
+
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
|
| 15 |
+
|
| 16 |
+
st.title("Timetable Scheduling using Graph Coloring")
|
| 17 |
+
|
| 18 |
+
# User input for timetable slots
|
| 19 |
+
user_slots = {}
|
| 20 |
+
for day in days:
|
| 21 |
+
user_slots[day] = []
|
| 22 |
+
st.subheader(f"Select slots for {day}")
|
| 23 |
+
for slot in time_slots:
|
| 24 |
+
subject = st.text_input(f"Enter subject for {slot} on {day}", key=f"{day}-{slot}")
|
| 25 |
+
if subject:
|
| 26 |
+
user_slots[day].append((slot, subject))
|
| 27 |
+
|
| 28 |
+
# Create graph representation
|
| 29 |
+
G = nx.Graph()
|
| 30 |
+
for day in user_slots:
|
| 31 |
+
for slot, subject in user_slots[day]:
|
| 32 |
+
G.add_node((day, slot, subject))
|
| 33 |
+
|
| 34 |
+
# Add edges for overlapping subjects (same time slots across days)
|
| 35 |
+
for day1 in user_slots:
|
| 36 |
+
for day2 in user_slots:
|
| 37 |
+
if day1 != day2:
|
| 38 |
+
for (slot1, subject1) in user_slots[day1]:
|
| 39 |
+
for (slot2, subject2) in user_slots[day2]:
|
| 40 |
+
if slot1 == slot2:
|
| 41 |
+
G.add_edge((day1, slot1, subject1), (day2, slot2, subject2))
|
| 42 |
+
|
| 43 |
+
# Apply Welsh-Powell Graph Coloring Algorithm
|
| 44 |
+
sorted_nodes = sorted(G.nodes, key=lambda x: G.degree(x), reverse=True)
|
| 45 |
+
color_map = {}
|
| 46 |
+
for node in sorted_nodes:
|
| 47 |
+
used = {color_map[neighbor] for neighbor in G.neighbors(node) if neighbor in color_map}
|
| 48 |
+
for color in colors:
|
| 49 |
+
if color not in used:
|
| 50 |
+
color_map[node] = color
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# Convert to timetable format for visualization
|
| 54 |
+
timetable = {day: {slot: None for slot in time_slots} for day in days}
|
| 55 |
+
for (day, slot, subject), color in color_map.items():
|
| 56 |
+
timetable[day][slot] = (subject, color)
|
| 57 |
+
|
| 58 |
+
# Plot the timetable
|
| 59 |
+
fig, ax = plt.subplots(figsize=(12, 6))
|
| 60 |
+
ax.set_xticks(np.arange(len(days)) + 0.5)
|
| 61 |
+
ax.set_yticks(np.arange(len(time_slots)) + 0.5)
|
| 62 |
+
ax.set_xticklabels(days, fontsize=12)
|
| 63 |
+
ax.set_yticklabels(time_slots, fontsize=12)
|
| 64 |
+
ax.set_xlim(0, len(days))
|
| 65 |
+
ax.set_ylim(0, len(time_slots))
|
| 66 |
+
ax.invert_yaxis()
|
| 67 |
+
|
| 68 |
+
# Draw timetable slots
|
| 69 |
+
for i, day in enumerate(days):
|
| 70 |
+
for j, slot in enumerate(time_slots):
|
| 71 |
+
subject_info = timetable[day].get(slot)
|
| 72 |
+
if subject_info:
|
| 73 |
+
subject, color = subject_info
|
| 74 |
+
ax.add_patch(plt.Rectangle((i, j), 1, 1, color=color, edgecolor="black"))
|
| 75 |
+
ax.text(i + 0.5, j + 0.5, subject, ha='center', va='center', fontsize=10, fontweight='bold')
|
| 76 |
+
|
| 77 |
+
plt.title("Colored Timetable using Graph Coloring", fontsize=14)
|
| 78 |
+
plt.grid(True, linestyle="--", alpha=0.6)
|
| 79 |
+
st.pyplot(fig)
|