def start(self, rawgraph, treeDepth): self.td = treeDepth col = Coloring() trans = {} frat = {} if self.preprocess: if self.profile: preProfile = cProfile.Profile() preProfile.enable() self.echo("Preprocess coloring optimizations") pp_graph, postprocess = self.preprocess(rawgraph) if self.profile: preProfile.disable() printProfileStats( "preprocessing", preProfile) # Normalize graph so that its vertices are named 0, ..., n-1 pp_graph.remove_loops() orig, mapping = pp_graph.normalize() for i in xrange(0, len(orig)): assert i in orig, "Vertex {0} not contained in " \ "norm. graph of size {1}".format(i, len(orig)) g = self.ldo(orig) col = self.col(orig, g, trans, frat, col) # print col.color # User wants to output execution data if self.execdata: col_path = 'execdata/color/' if not os.path.exists(col_path): os.makedirs(col_path) os.makedirs(col_path + 'colorings') removed_vertices = rawgraph.nodes - pp_graph.nodes with open(col_path + 'colorings/0', 'w') as coloring_zero: # Write colors for vertices in the preprocessed graph to the # coloring file for vertex, clr in col.color.iteritems(): coloring_zero.write( str(mapping[vertex]) + ": " + str(clr) + '\n') correct, nodes = self.ctd(orig, g, col, treeDepth) i = 0 while (not correct): if self.profile: stepProfile = cProfile.Profile() stepProfile.enable() i += 1 self.echo("step", i) g, trans, frat = self.step(orig, g, trans, frat, col, nodes, i, treeDepth, self.ldo) col = self.col(orig, g, trans, frat, col) # print col.color # for key in col: # print str(mapping[key]) + ": " + str(col[key]), # User wants to output execution data if self.execdata: with open(col_path + 'colorings/' + str(i), 'w') as coloring_i: # Write colors for vertices in the preprocessed graph to # the coloring file for vertex, clr in col.color.iteritems(): coloring_i.write( str(mapping[vertex]) + ": " + str(clr) + '\n') correct, nodes = self.ctd(orig, g, col, treeDepth) if self.profile: stepProfile.disable() printProfileStats("step {0}".format(i), stepProfile) if correct: self.echo(" step", i, "is correct") break # end while self.echo("number of colors:", len(col)) if self.opt: if self.profile: optProfile = cProfile.Profile() optProfile.enable() self.echo("Optimizing...") col = self.opt(orig, g, trans, frat, col, i, treeDepth, self) self.echo("number of colors:", len(col)) if self.profile: optProfile.disable() printProfileStats( "optimizing", optProfile) # Map coloring back to original vertex labels colrenamed = Coloring() for v in col: colrenamed[mapping[v]] = col[v] if self.preprocess: if self.profile: postProfile = cProfile.Profile() postProfile.enable() self.echo("Postprocessing") col_restored = postprocess(colrenamed) self.echo("number of colors:", len(col_restored)) if self.profile: postProfile.disable() printProfileStats("optimizing", postProfile) if self.profile: mergeProfile = cProfile.Profile() mergeProfile.enable() self.echo("Merging color classes") col_merged = merge_colors(rawgraph, col_restored, treeDepth) self.echo("number of colors:", len(col_merged)) if self.execdata: for idx in range(i + 1): with open(col_path + 'colorings/' + str(idx), 'a') \ as coloring_i: next_color = len(col) for vertex in removed_vertices: # If degree is 0, assign color 0 if rawgraph.degree(vertex) == 0: coloring_i.write(str(vertex) + ": " + '0' + '\n') # Degree not zero, hence assign color equal to length # of coloring else: coloring_i.write( str(vertex) + ": " + str(next_color) + '\n') next_color += 1 with open(col_path + 'colorings/' + str(i + 1), 'w') as coloring_i: # Write colors for vertices in the preprocessed graph to the # coloring file for vertex, clr in col_merged.color.iteritems(): coloring_i.write(str(vertex) + ": " + str(clr) + '\n') if self.profile: mergeProfile.disable() printProfileStats( "merging", mergeProfile) return col_merged
def start(self, rawgraph, treeDepth): self.td = treeDepth col = Coloring() trans = {} frat = {} if self.preprocess: if self.profile: preProfile = cProfile.Profile() preProfile.enable() self.echo("Preprocess coloring optimizations") pp_graph, postprocess = self.preprocess(rawgraph) if self.profile: preProfile.disable() printProfileStats( "preprocessing", preProfile) # Normalize graph so that its vertices are named 0, ..., n-1 pp_graph.remove_loops() orig, mapping = pp_graph.normalize() for i in xrange(0, len(orig)): assert i in orig, "Vertex {0} not contained in " \ "norm. graph of size {1}".format(i, len(orig)) g = self.ldo(orig) col = self.col(orig, g, trans, frat, col) correct, nodes = self.ctd(orig, g, col, treeDepth) i = 0 while (not correct): if self.profile: stepProfile = cProfile.Profile() stepProfile.enable() i += 1 self.echo("step", i) g, trans, frat = self.step(orig, g, trans, frat, col, nodes, i, treeDepth, self.ldo) col = self.col(orig, g, trans, frat, col) correct, nodes = self.ctd(orig, g, col, treeDepth) if self.profile: stepProfile.disable() printProfileStats( "step {0}".format(i), stepProfile) if correct: self.echo(" step", i, "is correct") break # end while self.echo("number of colors:", len(col)) if self.opt: if self.profile: optProfile = cProfile.Profile() optProfile.enable() self.echo("Optimizing...") col = self.opt(orig, g, trans, frat, col, i, treeDepth, self) self.echo("number of colors:", len(col)) if self.profile: optProfile.disable() printProfileStats( "optimizing", optProfile) # Map coloring back to original vertex labels colrenamed = Coloring() for v in col: colrenamed[mapping[v]] = col[v] if self.preprocess: if self.profile: postProfile = cProfile.Profile() postProfile.enable() self.echo("Postprocessing") col_restored = postprocess(colrenamed) self.echo("number of colors:", len(col_restored)) if self.profile: postProfile.disable() printProfileStats( "optimizing", postProfile) if self.profile: mergeProfile = cProfile.Profile() mergeProfile.enable() self.echo("Merging color classes") col_merged = merge_colors(rawgraph, col_restored, treeDepth) self.echo("number of colors:", len(col_merged)) if self.profile: mergeProfile.disable() printProfileStats( "merging", mergeProfile) return col_merged
def start(self, rawgraph, treeDepth): self.td = treeDepth col = Coloring() trans = {} frat = {} if self.preprocess: if self.profile: preProfile = cProfile.Profile() preProfile.enable() self.echo("Preprocess coloring optimizations") pp_graph, postprocess = self.preprocess(rawgraph) if self.profile: preProfile.disable() printProfileStats("preprocessing", preProfile) # Normalize graph so that its vertices are named 0, ..., n-1 pp_graph.remove_loops() orig, mapping = pp_graph.normalize() for i in xrange(0, len(orig)): assert i in orig, "Vertex {0} not contained in " \ "norm. graph of size {1}".format(i, len(orig)) g = self.ldo(orig) col = self.col(orig, g, trans, frat, col) # print col.color # User wants to output execution data if self.execdata: col_path = 'execdata/color/' if not os.path.exists(col_path): os.makedirs(col_path) os.makedirs(col_path + 'colorings') removed_vertices = rawgraph.nodes - pp_graph.nodes with open(col_path + 'colorings/0', 'w') as coloring_zero: # Write colors for vertices in the preprocessed graph to the # coloring file for vertex, clr in col.color.iteritems(): coloring_zero.write( str(mapping[vertex]) + ": " + str(clr) + '\n') correct, nodes = self.ctd(orig, g, col, treeDepth) i = 0 while (not correct): if self.profile: stepProfile = cProfile.Profile() stepProfile.enable() i += 1 self.echo("step", i) g, trans, frat = self.step(orig, g, trans, frat, col, nodes, i, treeDepth, self.ldo) col = self.col(orig, g, trans, frat, col) # print col.color # for key in col: # print str(mapping[key]) + ": " + str(col[key]), # User wants to output execution data if self.execdata: with open(col_path + 'colorings/' + str(i), 'w') as coloring_i: # Write colors for vertices in the preprocessed graph to # the coloring file for vertex, clr in col.color.iteritems(): coloring_i.write( str(mapping[vertex]) + ": " + str(clr) + '\n') correct, nodes = self.ctd(orig, g, col, treeDepth) if self.profile: stepProfile.disable() printProfileStats("step {0}".format(i), stepProfile) if correct: self.echo(" step", i, "is correct") break # end while self.echo("number of colors:", len(col)) if self.opt: if self.profile: optProfile = cProfile.Profile() optProfile.enable() self.echo("Optimizing...") col = self.opt(orig, g, trans, frat, col, i, treeDepth, self) self.echo("number of colors:", len(col)) if self.profile: optProfile.disable() printProfileStats("optimizing", optProfile) # Map coloring back to original vertex labels colrenamed = Coloring() for v in col: colrenamed[mapping[v]] = col[v] if self.preprocess: if self.profile: postProfile = cProfile.Profile() postProfile.enable() self.echo("Postprocessing") col_restored = postprocess(colrenamed) self.echo("number of colors:", len(col_restored)) if self.profile: postProfile.disable() printProfileStats("optimizing", postProfile) if self.profile: mergeProfile = cProfile.Profile() mergeProfile.enable() self.echo("Merging color classes") col_merged = merge_colors(rawgraph, col_restored, treeDepth) self.echo("number of colors:", len(col_merged)) if self.execdata: for idx in range(i + 1): with open(col_path + 'colorings/' + str(idx), 'a') \ as coloring_i: next_color = len(col) for vertex in removed_vertices: # If degree is 0, assign color 0 if rawgraph.degree(vertex) == 0: coloring_i.write(str(vertex) + ": " + '0' + '\n') # Degree not zero, hence assign color equal to length # of coloring else: coloring_i.write( str(vertex) + ": " + str(next_color) + '\n') next_color += 1 with open(col_path + 'colorings/' + str(i + 1), 'w') as coloring_i: # Write colors for vertices in the preprocessed graph to the # coloring file for vertex, clr in col_merged.color.iteritems(): coloring_i.write(str(vertex) + ": " + str(clr) + '\n') if self.profile: mergeProfile.disable() printProfileStats("merging", mergeProfile) return col_merged