Skip to content

Commit

Permalink
Implement first permissible or none strategy for node colouring.
Browse files Browse the repository at this point in the history
  • Loading branch information
MHenderson committed Nov 7, 2024
1 parent 6436a1d commit c1ae6fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion listcolouring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ def first_permissible_or_none(G, u, v):
choice = None
return(choice)

def first_permissible_or_none_node(G, u):
""" Returns the first element of A = P - X if A is non-empty otherwise returns None.
X is the list of colours on neighbours of u.
P is the list of permissible colours for node u.
"""
X = colours_on_neighbours(G, u)
P = set(G.nodes[u]["permissible"])
choices = P - X
if(len(choices) > 0):
choice = list(choices)[0]
else:
choice = None
return(choice)

def greedy_list_edge_colouring(G):
"""Assign the first permissible colour to every edge (or None if all permissible
colours already used on incident edges)."""
Expand All @@ -65,4 +79,4 @@ def print_list_edge_colouring(G):
perm = eattr['permissible']
col = eattr['colour']
print(f"({n}, {nbr}, {perm}, {col})")

12 changes: 12 additions & 0 deletions tests/test_first-permissible-or-none-node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import networkx as nx

import listcolouring
from listcolouring import first_permissible_or_none_node

def test_first_permissible_or_none_node():
G = nx.complete_graph(3)
permissible_dict = {0: [0, 1], 1: [1, 2], 2: [2, 3]}
nx.set_node_attributes(G, permissible_dict, "permissible")
assert first_permissible_or_none_node(G, 0) == 0
assert first_permissible_or_none_node(G, 1) == 1
assert first_permissible_or_node_node(G, 2) == 2

0 comments on commit c1ae6fc

Please sign in to comment.