Skip to content

Commit

Permalink
Add implementation of greedy list total colouring.
Browse files Browse the repository at this point in the history
  • Loading branch information
MHenderson committed Nov 19, 2024
1 parent b760f1b commit fb19c92
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
15 changes: 11 additions & 4 deletions listcolouring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,18 @@ def greedy_list_edge_colouring(G):
G[u][v]["colour"] = first_permissible_or_none(G, u, v) # random.choice(colours)
return(G)

def greedy_list_node_colouring(G):
"""Assign the first permissible colour to every node (or None if all permissible
colours already used on incident edges)."""
def greedy_list_node_colouring(G, edges = False):
"""Assign the first permissible colour to every node or None if all permissible
colours already used on neighbouring nodes (and incident edges if edges = True)."""
for u in G.nodes:
G.nodes[u]["colour"] = first_permissible_or_none_node(G, u)
G.nodes[u]["colour"] = first_permissible_or_none_node(G, u, edges)
return(G)

def greedy_list_total_colouring(G):
"""Use a greedy strategy to colour the edges of G and then use a greedy node colouring
strategy to colour the nodes of G."""
G = greedy_list_edge_colouring(G)
G = greedy_list_node_colouring(G, edges = True)
return(G)

def print_list_edge_colouring(G):
Expand Down
16 changes: 16 additions & 0 deletions tests/test_greedy-list-total-colouring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import networkx as nx

import listcolouring
from listcolouring import greedy_list_total_colouring

def test_greedy_list_total_colouring():
G = nx.complete_graph(3)
permissible_dict_node = {0: [0, 1], 1: [1, 2], 2: [2, 4]}
nx.set_node_attributes(G, permissible_dict_node, "permissible")
nx.set_node_attributes(G, None, "colour")
nx.set_edge_attributes(G, [1, 2, 3], "permissible")
nx.set_edge_attributes(G, 2, "colour")
G = greedy_list_total_colouring(G)
assert G.nodes[0]["colour"] == 0
assert G.nodes[1]["colour"] == 2
assert G.nodes[2]["colour"] == 4

0 comments on commit fb19c92

Please sign in to comment.