muruga778's picture
Create app.py
6f3ebc9 verified
raw
history blame
2.79 kB
import streamlit as st
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
# Define available colors
colors = [
"#FF9999", "#99FF99", "#9999FF", "#FFCC99", "#FFFF99",
"#CC99FF", "#66CCCC", "#FF6666", "#66FF66", "#6666FF"
]
# Define time slots and days
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"]
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
st.title("Timetable Scheduling using Graph Coloring")
# User input for timetable slots
user_slots = {}
for day in days:
user_slots[day] = []
st.subheader(f"Select slots for {day}")
for slot in time_slots:
subject = st.text_input(f"Enter subject for {slot} on {day}", key=f"{day}-{slot}")
if subject:
user_slots[day].append((slot, subject))
# Create graph representation
G = nx.Graph()
for day in user_slots:
for slot, subject in user_slots[day]:
G.add_node((day, slot, subject))
# Add edges for overlapping subjects (same time slots across days)
for day1 in user_slots:
for day2 in user_slots:
if day1 != day2:
for (slot1, subject1) in user_slots[day1]:
for (slot2, subject2) in user_slots[day2]:
if slot1 == slot2:
G.add_edge((day1, slot1, subject1), (day2, slot2, subject2))
# Apply Welsh-Powell Graph Coloring Algorithm
sorted_nodes = sorted(G.nodes, key=lambda x: G.degree(x), reverse=True)
color_map = {}
for node in sorted_nodes:
used = {color_map[neighbor] for neighbor in G.neighbors(node) if neighbor in color_map}
for color in colors:
if color not in used:
color_map[node] = color
break
# Convert to timetable format for visualization
timetable = {day: {slot: None for slot in time_slots} for day in days}
for (day, slot, subject), color in color_map.items():
timetable[day][slot] = (subject, color)
# Plot the timetable
fig, ax = plt.subplots(figsize=(12, 6))
ax.set_xticks(np.arange(len(days)) + 0.5)
ax.set_yticks(np.arange(len(time_slots)) + 0.5)
ax.set_xticklabels(days, fontsize=12)
ax.set_yticklabels(time_slots, fontsize=12)
ax.set_xlim(0, len(days))
ax.set_ylim(0, len(time_slots))
ax.invert_yaxis()
# Draw timetable slots
for i, day in enumerate(days):
for j, slot in enumerate(time_slots):
subject_info = timetable[day].get(slot)
if subject_info:
subject, color = subject_info
ax.add_patch(plt.Rectangle((i, j), 1, 1, color=color, edgecolor="black"))
ax.text(i + 0.5, j + 0.5, subject, ha='center', va='center', fontsize=10, fontweight='bold')
plt.title("Colored Timetable using Graph Coloring", fontsize=14)
plt.grid(True, linestyle="--", alpha=0.6)
st.pyplot(fig)