7.1 KiB
7.1 KiB
In [ ]:
import networkx as nx
import matplotlib.pyplot as plt
G = nx.Graph()
G.add_edges_from([
("Maths", "Info"),
("Info", "Anglais"),
("Anglais", "Sport"),
("Sport", "Socio"),
("Socio", "Maths"),
])
plt.figure()
nx.draw(G, with_labels=True, node_color="lightblue", node_size=1500)
plt.show()In [ ]:
# NetworkX propose directement un algorithme glouton de coloration
coloration = nx.greedy_color(G, strategy="largest_first")
print("Créneau (couleur) attribué à chaque matière :")
for matiere, creneau in coloration.items():
print(f" {matiere} : créneau {creneau}")
nb_creneaux = len(set(coloration.values()))
print(f"\nNombre de créneaux utilisés : {nb_creneaux}")
# Visualisation avec les couleurs attribuées
palette = ["lightblue", "lightgreen", "lightcoral", "khaki", "plum"]
couleurs_sommets = [palette[coloration[noeud]] for noeud in G.nodes()]
plt.figure()
nx.draw(G, with_labels=True, node_color=couleurs_sommets, node_size=1500)
plt.show()