From 9961827878ce50fe6add58b38282befa1165a795 Mon Sep 17 00:00:00 2001 From: Matthew Henderson Date: Wed, 27 Nov 2024 09:34:26 +0000 Subject: [PATCH 1/2] Implement edge list-colouring. --- src/vizing/__init__.py | 26 ++++++++++++++++++++++++++ tests/test_edge-list-colouring.py | 20 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/test_edge-list-colouring.py diff --git a/src/vizing/__init__.py b/src/vizing/__init__.py index 2b88da3..a6c696c 100644 --- a/src/vizing/__init__.py +++ b/src/vizing/__init__.py @@ -27,3 +27,29 @@ def node_list_colouring_solution(G): G.nodes[node]['colour'] = S[node] return(G) +def edge_list_colouring_problem(G): + """ + Return a constraint problem representing an edge list-colouring instance. + + :param G: A graph with lists of permissible colours assigned to edges. + :return An edge list-colouring constraint problem. + """ + P = constraint.Problem() + for edge in G.edges(): + P.addVariable(edge, G.edges[edge]['permissible']) + for node in G.nodes(): + P.addConstraint(constraint.AllDifferentConstraint(), [tuple(sorted(x)) for x in G.edges(node)]) + return(P) + +def edge_list_colouring_solution(G): + """ + Return an edge-coloured graph. + + :param G: A graph with lists of permissible colours assigned to edges. + :return A properly edge list-coloured graph. + """ + P = edge_list_colouring_problem(G) + S = P.getSolution() + for edge in G.edges(): + G.edges()[edge]['colour'] = S[edge] + return(G) diff --git a/tests/test_edge-list-colouring.py b/tests/test_edge-list-colouring.py new file mode 100644 index 0000000..f129a3e --- /dev/null +++ b/tests/test_edge-list-colouring.py @@ -0,0 +1,20 @@ +import networkx as nx +import vizing as vz + +def test_edge_list_colouring_problem(): + G = nx.complete_graph(3) + permissible_dict_edge = {(0, 1): [0, 1], (0, 2): [1, 2], (1, 2): [2, 4]} + nx.set_edge_attributes(G, permissible_dict_edge, "permissible") + nx.set_edge_attributes(G, None, "colour") + P = vz.edge_list_colouring_problem(G) + assert P._variables == {(0, 1): [0, 1], (0, 2): [1, 2], (1, 2): [2, 4]} + +def test_edge_list_colouring_solution(): + G = nx.complete_graph(3) + permissible_dict_edge = {(0, 1): [0, 1], (0, 2): [1, 2], (1, 2): [2, 4]} + nx.set_edge_attributes(G, permissible_dict_edge, "permissible") + nx.set_edge_attributes(G, None, "colour") + G = vz.edge_list_colouring_solution(G) + assert G.edges()[(0, 1)]['colour'] == 1 + assert G.edges()[(0, 2)]['colour'] == 2 + assert G.edges()[(1, 2)]['colour'] == 4 From ba6f6adb0005fab5fea30a594f4e439091bdbeff Mon Sep 17 00:00:00 2001 From: Matthew Henderson Date: Wed, 27 Nov 2024 09:34:51 +0000 Subject: [PATCH 2/2] Bump development version number: 0.1.0.9000 -> 0.1.0.9001. --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 3c61db9..3c9a59a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "vizing" -version = "0.1.0.9000" +version = "0.1.0.9001" description = "Constraint-based list-colouring in Python." authors = ["Matthew Henderson "] readme = "README.md"