Skip to content

Commit

Permalink
Implement node list initialisation (#29)
Browse files Browse the repository at this point in the history
* Add list_init_node function.

* Add tests for the list_init_node function.
  • Loading branch information
MHenderson authored Nov 5, 2024
1 parent 048aad0 commit 99e4c07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
12 changes: 12 additions & 0 deletions listcolouring/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ def list_init(G, colours, k, seed):

return(G)

def list_init_node(G, colours, k, seed):
"""Assign a random subset of k colours to the list of permissible colours for every node of G."""
random.seed(seed)

permissible_colours = [random.sample(colours, k) for i in range(G.order())]
permissible_dict = dict(zip(G.nodes, permissible_colours))

nx.set_node_attributes(G, permissible_dict, "permissible")
nx.set_node_attributes(G, None, "colour")

return(G)

def colours_incident_with(G, u):
"""The list of colours on edges incident with vertex u in graph G."""
return(set([G[u][v]["colour"] for v in nx.neighbors(G, u)]))
Expand Down
10 changes: 10 additions & 0 deletions tests/test_list_init_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import networkx as nx

import listcolouring
from listcolouring import list_init_node

def test_list_init_node():
G = nx.petersen_graph()
G = list_init_node(G, range(0, 10), 3, 0)
assert G.nodes[0]['colour'] is None
assert len(G.nodes[0]['permissible']) == 3

0 comments on commit 99e4c07

Please sign in to comment.