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