Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of greedy list total colouring. #39

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading