Skip to content

Commit 03082bd

Browse files
authored
Merge pull request #23 from MHenderson/18-implement-total-list-colouring
Implement total list colouring
2 parents 47495f4 + 074049b commit 03082bd

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "vizing"
3-
version = "0.1.0.9001"
3+
version = "0.1.0.9002"
44
description = "Constraint-based list-colouring in Python."
55
authors = ["Matthew Henderson <[email protected]>"]
66
readme = "README.md"

src/vizing/__init__.py

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import constraint
1+
import constraint as ct
22

33
def 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

1717
def 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

4444
def 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+

tests/test_total-list-colouring.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import listcolouring as lc
2+
import networkx as nx
3+
import vizing as vz
4+
5+
def test_total_list_colouring_problem():
6+
G = nx.complete_graph(3)
7+
permissible_dict_edge = {(0, 1): [0, 1], (0, 2): [1, 2], (1, 2): [2, 4]}
8+
nx.set_edge_attributes(G, permissible_dict_edge, "permissible")
9+
nx.set_edge_attributes(G, None, "colour")
10+
permissible_dict_node = {0: [0, 1], 1: [1, 2], 2: [2, 4]}
11+
nx.set_node_attributes(G, permissible_dict_node, "permissible")
12+
nx.set_node_attributes(G, None, "colour")
13+
P = vz.total_list_colouring_problem(G)
14+
assert P._variables == {0: [0, 1], 1: [1, 2], 2: [2, 4], (0, 1): [0, 1], (0, 2): [1, 2], (1, 2): [2, 4]}
15+
16+
def test_total_list_colouring_solution():
17+
G = nx.petersen_graph()
18+
n_colours = 6
19+
G = lc.list_init_node(G, range(n_colours - 1), 3, 0)
20+
G = lc.list_init(G, range(n_colours - 1), 3, 0)
21+
G = vz.total_list_colouring_solution(G)
22+
assert G.nodes()[0]['colour'] == 0
23+

0 commit comments

Comments
 (0)