Skip to content

Commit

Permalink
Implement greedy node list colouring.
Browse files Browse the repository at this point in the history
  • Loading branch information
MHenderson committed Nov 14, 2024
1 parent b0ee9b7 commit dd9a3e0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
7 changes: 7 additions & 0 deletions listcolouring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ 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)."""
for u in G.nodes:
G.nodes[u]["colour"] = first_permissible_or_none_node(G, u)
return(G)

def print_list_edge_colouring(G):
"""Print assigned colours and lists of permissible colours for all edges in G."""
for n, nbrs in G.adj.items():
Expand Down
14 changes: 14 additions & 0 deletions tests/test_greedy-list-node-colouring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import networkx as nx

import listcolouring
from listcolouring import greedy_list_node_colouring

def test_greedy_list_node_colouring():
G = nx.complete_graph(3)
permissible_dict = {0: [0, 1], 1: [1, 2], 2: [2, 3]}
nx.set_node_attributes(G, permissible_dict, "permissible")
nx.set_node_attributes(G, None, "colour")
G = greedy_list_node_colouring(G)
assert G.nodes[0]["colour"] == 0
assert G.nodes[1]["colour"] == 1
assert G.nodes[2]["colour"] == 2

0 comments on commit dd9a3e0

Please sign in to comment.