Skip to content

Instantly share code, notes, and snippets.

View domdomdom12's full-sized avatar

Dominic Wigmore-Shepherd domdomdom12

  • London
View GitHub Profile
@domdomdom12
domdomdom12 / graph_colouring_complex.py
Last active September 2, 2021 09:21
Complex Graph Colouring model
# souped up GCP
import numpy as np
from ortools.sat.python import cp_model
class GraphColouringCP:
def __init__(
self,
edge_array: np.array,
@domdomdom12
domdomdom12 / graph_colouring_greedy.py
Last active September 2, 2021 09:23
Greedy algo for the GCP
import numpy as np
def greedy_graph_colouring(edge_array: np.array):
def color_nodes(graph):
color_map = {}
# Consider nodes in descending degree
for node in sorted(graph, key=lambda x: len(graph[x]), reverse=True):
neighbor_colors = set(color_map.get(neigh) for neigh in graph[node])
color_map[node] = next(
color for color in range(len(graph)) if color not in neighbor_colors
@domdomdom12
domdomdom12 / create_GCP_test_instance.py
Last active September 2, 2021 09:24
Test instance creation for the GCP
import numpy as np
from typing import Union
def create_test_instance(
num_nodes: int,
edge_density_power_exponent: float,
edge_density_lb: float,
edge_density_ub: float,
seed: Union[int, None] = None,
verbose: bool=False
@domdomdom12
domdomdom12 / graph_colouring_simple.py
Last active September 2, 2021 09:22
Simple Graph Colouring model
# simple GCP
import numpy as np
from ortools.sat.python import cp_model
class GraphColouringCP:
def __init__(
self,
edge_array: np.array,