-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassroom_coloring.py
More file actions
69 lines (60 loc) · 1.88 KB
/
classroom_coloring.py
File metadata and controls
69 lines (60 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import networkx as nx
import matplotlib.pyplot as plt
# Define the graph (Classroom seating as adjacency list)
graph = {
'A': ['B', 'D'],
'B': ['A', 'C', 'E'],
'C': ['B', 'F'],
'D': ['A', 'E', 'G'],
'E': ['B', 'D', 'F', 'H'],
'F': ['C', 'E', 'I'],
'G': ['D', 'H'],
'H': ['E', 'G', 'I'],
'I': ['F', 'H']
}
# Available color palette (extendable if needed)
color_palette = ['red', 'green', 'blue', 'yellow', 'orange', 'purple', 'cyan', 'brown']
# Greedy coloring algorithm
def greedy_coloring(graph):
color_assignment = {}
for node in graph:
# Collect used colors from adjacent nodes
used_colors = {color_assignment.get(neigh) for neigh in graph[node] if neigh in color_assignment}
# Assign the lowest unused color
for color in color_palette:
if color not in used_colors:
color_assignment[node] = color
break
return color_assignment
# Apply the greedy coloring
coloring_result = greedy_coloring(graph)
# Create a graph using NetworkX
G = nx.Graph()
for node, neighbors in graph.items():
for neighbor in neighbors:
G.add_edge(node, neighbor)
# Prepare node colors from coloring result
node_colors = [coloring_result[node] for node in G.nodes()]
# Set positions manually to mimic a 3x3 classroom seating grid
positions = {
'A': (0, 2),
'B': (1, 2),
'C': (2, 2),
'D': (0, 1),
'E': (1, 1),
'F': (2, 1),
'G': (0, 0),
'H': (1, 0),
'I': (2, 0)
}
# Plotting the graph
plt.figure(figsize=(8, 6))
nx.draw(G, pos=positions, with_labels=True, node_color=node_colors,
node_size=1200, font_size=14, font_weight='bold')
plt.title("Classroom Seating Plan - Greedy Coloring", fontsize=16)
plt.axis('off')
plt.show()
# Print the color assignment
print("Seat : Assigned Color")
for seat, color in coloring_result.items():
print(f"{seat} : {color}")