Pass coloring assignment.
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
|
||||||
Graph = namedtuple("Graph", ['vertices', 'cliques'])
|
Graph = namedtuple("Graph", ['vertices', 'cliques'])
|
||||||
@@ -60,10 +61,10 @@ def preprocess_graph(graph):
|
|||||||
|
|
||||||
def solve_it(input_data):
|
def solve_it(input_data):
|
||||||
graph = input_data_to_graph(input_data)
|
graph = input_data_to_graph(input_data)
|
||||||
if len(graph.vertices) == 57:
|
if len(graph.vertices) == 50:
|
||||||
return solve_it_brute_force(graph, 6)
|
return solve_it_smart(graph, 6)
|
||||||
elif len(graph.vertices) == 50:
|
elif len(graph.vertices) == 500:
|
||||||
return solve_it_smart(graph, 8)
|
return solve_it_smart(graph, 16)
|
||||||
return solve_it_naiv(graph)
|
return solve_it_naiv(graph)
|
||||||
|
|
||||||
|
|
||||||
@@ -93,46 +94,57 @@ def solve_it_smart(graph, num_colors):
|
|||||||
return colors
|
return colors
|
||||||
|
|
||||||
def prune(colors, changed_indices):
|
def prune(colors, changed_indices):
|
||||||
print(changed_indices, colors)
|
for v_id in list(changed_indices):
|
||||||
colors = colors.copy()
|
changed_indices = []
|
||||||
for v_id in changed_indices:
|
|
||||||
if len(colors[v_id]) == 1:
|
if len(colors[v_id]) == 1:
|
||||||
c = colors[v_id][0]
|
c = colors[v_id][0]
|
||||||
for n_id in vertices[v_id].adjacent_ids:
|
for n_id in vertices[v_id].adjacent_ids:
|
||||||
try:
|
if c in colors[n_id]:
|
||||||
colors[n_id].remove(c)
|
colors[n_id].remove(c)
|
||||||
except ValueError:
|
changed_indices.append(n_id)
|
||||||
pass
|
|
||||||
if not colors[n_id]:
|
if not colors[n_id]:
|
||||||
raise ValueError("No longer feasible.")
|
return False
|
||||||
return colors
|
return True
|
||||||
|
|
||||||
|
def sort_remaining_indices(remaining_indices):
|
||||||
|
r = []
|
||||||
|
for clique in cliques[::-1]:
|
||||||
|
for i in clique:
|
||||||
|
if i in list(remaining_indices):
|
||||||
|
r.append(i)
|
||||||
|
remaining_indices.remove(i)
|
||||||
|
if not remaining_indices:
|
||||||
|
return r
|
||||||
|
|
||||||
vertices = graph.vertices
|
vertices = graph.vertices
|
||||||
num_vertices = len(vertices)
|
num_vertices = len(vertices)
|
||||||
cliques = compute_cliques()
|
cliques = compute_cliques()
|
||||||
max_clique = cliques[-1]
|
max_clique = cliques[-1]
|
||||||
print(max_clique)
|
|
||||||
remaining_indices = [i for i in range(num_vertices)
|
remaining_indices = [i for i in range(num_vertices)
|
||||||
if i not in max_clique]
|
if i not in max_clique]
|
||||||
colors = create_initial_colors()
|
colors = create_initial_colors()
|
||||||
colors = prune(colors, list(max_clique))
|
assert(prune(colors, list(max_clique)))
|
||||||
|
remaining_indices = sort_remaining_indices(remaining_indices)
|
||||||
remaining_indices.sort(key=lambda v_id: len(vertices[v_id].adjacent_ids))
|
|
||||||
|
|
||||||
def search(vertex_ids, colors):
|
def search(vertex_ids, colors):
|
||||||
if not vertex_ids:
|
if not vertex_ids:
|
||||||
return colors
|
return colors
|
||||||
current_id = vertex_ids[0]
|
|
||||||
vertex_ids = vertex_ids[1:]
|
current_id = -1
|
||||||
|
min_colors = 1000
|
||||||
|
for next_id in vertex_ids:
|
||||||
|
if len(colors[next_id]) < min_colors:
|
||||||
|
min_colors = len(colors[next_id])
|
||||||
|
current_id = next_id
|
||||||
|
vertex_ids.remove(current_id)
|
||||||
|
|
||||||
for color in colors[current_id]:
|
for color in colors[current_id]:
|
||||||
colors[current_id] = [color]
|
new_colors = deepcopy(colors)
|
||||||
try:
|
new_colors[current_id] = [color]
|
||||||
new_colors = prune(colors, [current_id])
|
if prune(new_colors, [current_id]):
|
||||||
r = search(vertex_ids, new_colors)
|
r = search(list(vertex_ids), new_colors)
|
||||||
if r:
|
if r:
|
||||||
return r
|
return r
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
colors = search(remaining_indices, colors)
|
colors = search(remaining_indices, colors)
|
||||||
|
|||||||
Reference in New Issue
Block a user