1- import constraint
1+ import constraint as ct
22
33def node_list_colouring_problem (G ):
44 """
@@ -7,11 +7,11 @@ def node_list_colouring_problem(G):
77 :param G: A graph with lists of permissible colours assigned to nodes.
88 :return A node list-colouring constraint problem.
99 """
10- P = constraint .Problem ()
10+ P = ct .Problem ()
1111 for node in G .nodes ():
1212 P .addVariable (node , G .nodes [node ]['permissible' ])
1313 for edge in G .edges ():
14- P .addConstraint (constraint .AllDifferentConstraint (), edge )
14+ P .addConstraint (ct .AllDifferentConstraint (), edge )
1515 return (P )
1616
1717def node_list_colouring_solution (G ):
@@ -34,11 +34,11 @@ def edge_list_colouring_problem(G):
3434 :param G: A graph with lists of permissible colours assigned to edges.
3535 :return An edge list-colouring constraint problem.
3636 """
37- P = constraint .Problem ()
37+ P = ct .Problem ()
3838 for edge in G .edges ():
3939 P .addVariable (edge , G .edges [edge ]['permissible' ])
4040 for node in G .nodes ():
41- P .addConstraint (constraint .AllDifferentConstraint (), [tuple (sorted (x )) for x in G .edges (node )])
41+ P .addConstraint (ct .AllDifferentConstraint (), [tuple (sorted (x )) for x in G .edges (node )])
4242 return (P )
4343
4444def edge_list_colouring_solution (G ):
@@ -53,3 +53,37 @@ def edge_list_colouring_solution(G):
5353 for edge in G .edges ():
5454 G .edges ()[edge ]['colour' ] = S [edge ]
5555 return (G )
56+
57+ def total_list_colouring_problem (G ):
58+ """
59+ Return a constraint problem representing a total list-colouring instance.
60+
61+ :param G: A graph with lists of permissible colours assigned to nodes and edges.
62+ :return A total list-colouring constraint problem.
63+ """
64+ P = ct .Problem ()
65+ for node in G .nodes ():
66+ P .addVariable (node , G .nodes [node ]['permissible' ])
67+ for edge in G .edges ():
68+ P .addVariable (edge , G .edges [edge ]['permissible' ])
69+ for edge in G .edges ():
70+ P .addConstraint (ct .AllDifferentConstraint (), [edge [0 ], edge [1 ], edge ])
71+ for node in G .nodes ():
72+ P .addConstraint (ct .AllDifferentConstraint (), [node ] + [tuple (sorted (x )) for x in G .edges (node )])
73+ return (P )
74+
75+ def total_list_colouring_solution (G ):
76+ """
77+ Return a total list-coloured graph.
78+
79+ :param G: A graph with lists of permissible colours assigned to nodes and edges.
80+ :return A properly total list-coloured graph.
81+ """
82+ P = total_list_colouring_problem (G )
83+ S = P .getSolution ()
84+ for node in G .nodes :
85+ G .nodes [node ]['colour' ] = S [node ]
86+ for edge in G .edges ():
87+ G .edges ()[edge ]['colour' ] = S [edge ]
88+ return (G )
89+
0 commit comments