Spaces:
Sleeping
Sleeping
| 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", "13:15-14:45", "14:50-16:20", "16:25-17:55","18:00-19:30"] | |
| 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) # Sort nodes by degree | |
| subject_color = {} | |
| used_colors = set() | |
| color_map = {} | |
| for node in sorted_nodes: | |
| day, slot, subject = node | |
| # If subject already has a color, use it | |
| if subject in subject_color: | |
| color_map[node] = subject_color[subject] | |
| else: | |
| # Find a color that hasn't been used yet | |
| for color in colors: | |
| if color not in used_colors: | |
| subject_color[subject] = color | |
| color_map[node] = color | |
| used_colors.add(color) | |
| break | |
| else: | |
| st.error("Not enough colors for the number of unique subjects. Please add more colors.") | |
| # 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) |