Skip to content

Commit

Permalink
Merge pull request #17 from MHenderson/16-docs-and-tests
Browse files Browse the repository at this point in the history
Add docs and tests for both functions
  • Loading branch information
MHenderson authored Nov 26, 2024
2 parents cf301c1 + 3aa3358 commit 2188249
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
18 changes: 15 additions & 3 deletions src/vizing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
import constraint

def list_colouring_problem(G):
def node_list_colouring_problem(G):
"""
Return a constraint problem representing a node list-colouring instance.
:param G: A graph with lists of permissible colours assigned to nodes.
:return A node list-colouring constraint problem.
"""
P = constraint.Problem()
for node in G.nodes():
P.addVariable(node, G.nodes[node]['permissible'])
for edge in G.edges():
P.addConstraint(constraint.AllDifferentConstraint(), edge)
return(P)

def list_colouring_solution(G):
P = list_colouring_problem(G)
def node_list_colouring_solution(G):
"""
Return a list-coloured graph.
:param G: A graph with lists of permissible colours assigned to nodes.
:return A properly list-coloured graph.
"""
P = node_list_colouring_problem(G)
S = P.getSolution()
for node in S:
G.nodes[node]['colour'] = S[node]
Expand Down
11 changes: 0 additions & 11 deletions tests/test_list-colouring-solution.py

This file was deleted.

17 changes: 17 additions & 0 deletions tests/test_node_list-colouring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import listcolouring
import networkx as nx
import vizing

def test_node_list_colouring_problem():
G = nx.petersen_graph()
G = listcolouring.list_init_node(G, range(1, 10), 3, 0)
G = vizing.node_list_colouring_problem(G)

def test_node_list_colouring_solution():
G = nx.complete_graph(3)
permissible_dict = {0: [0, 1], 1: [1, 2], 2: [2, 3]}
nx.set_node_attributes(G, permissible_dict, "permissible")
G = vizing.node_list_colouring_solution(G)
assert G.nodes[0]['colour'] == 1
assert G.nodes[1]['colour'] == 2
assert G.nodes[2]['colour'] == 3

0 comments on commit 2188249

Please sign in to comment.