# Ticket #7734: trac_7734-edit.patch

File trac_7734-edit.patch, 5.9 KB (added by rlm, 11 years ago)
• ## sage/graphs/graph_coloring.py

```# HG changeset patch
# User Robert L. Miller <rlm@rlmiller.org>
# Date 1261171365 28800
# Node ID bec6170973022d3ba1752f6e8abb79dc0a546b49
# Parent  267524b6e358e6e1cc57657639ed579e989b022c
Added doctests, testing on both sides of 'optional'

diff -r 267524b6e358 -r bec617097302 sage/graphs/graph_coloring.py```
 a EXAMPLE:: sage: from sage.graphs.graph_coloring import vertex_coloring sage: g = graphs.PetersenGraph() sage: vertex_coloring(g, value_only=True) # optional - requires GLPK or CBC 3 sage: from sage.graphs.graph_coloring import vertex_coloring sage: g = graphs.PetersenGraph() sage: vertex_coloring(g, value_only=True) # optional - requires GLPK or CBC 3 sage: d = vertex_coloring(g, hex_colors=True)  # optional - requires GLPK or CBC sage: for k in sorted(d.keys()):               # optional - requires GLPK or CBC ...     print k, d[k]                          # optional - requires GLPK or CBC #0000ff [4, 7, 8] #00ff00 [1, 3, 5, 9] #ff0000 [0, 2, 6] sage: vertex_coloring(g, k=3)                  # optional - requires GLPK or CBC [[0, 2, 6], [1, 3, 5, 9], [4, 7, 8]] sage: vertex_coloring(g, k=2)                  # optional - requires GLPK or CBC False sage: vertex_coloring(g, k=4)                  # optional - requires GLPK or CBC [[9, 8, 2, 0], [7, 6, 4], [5, 3, 1], []] sage: vertex_coloring(g, k=2, value_only=True) # optional - requires GLPK or CBC False sage: vertex_coloring(g, k=4, value_only=True) # optional - requires GLPK or CBC True Several different families of graphs do not depend on MIP solving, such as graphs without edges:: sage: g = Graph(20) sage: vertex_coloring(g) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] sage: vertex_coloring(g, value_only=True) 1 sage: vertex_coloring(g, hex_colors=True) {'#ff0000': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]} sage: vertex_coloring(g, k=4, value_only=True) True Or bipartite graphs:: sage: g = graphs.CompleteBipartiteGraph(3,4) sage: vertex_coloring(g) ([0, 1, 2], [3, 4, 5, 6]) sage: vertex_coloring(g, value_only=True) 2 sage: d = vertex_coloring(g, hex_colors=True) sage: for k in sorted(d.keys()): ...     print k, d[k] #00ffff [3, 4, 5, 6] #ff0000 [0, 1, 2] sage: vertex_coloring(g, k=4, value_only=True) True """ from sage.numerical.mip import MixedIntegerLinearProgram from sage.plot.colors import rainbow if value_only: return 1 elif hex_colors: return dict(zip(rainbow(1), g.vertices())) return {rainbow(1)[0]: g.vertices()} else: return g.vertices() # - Bipartite set EXAMPLE:: sage: from sage.graphs.graph_coloring import edge_coloring sage: g = graphs.PetersenGraph() sage: edge_coloring(g, value_only=True) # optional - requires GLPK or CBC 4 sage: from sage.graphs.graph_coloring import edge_coloring sage: g = graphs.PetersenGraph() sage: edge_coloring(g, value_only=True) # optional - requires GLPK or CBC 4 sage: d = edge_coloring(g, value_only=False, hex_colors=True, log=1) # optional - requires GLPK or CBC sage: for k in sorted(d.keys()): # optional - requires GLPK or CBC ...     print k, d[k]            # optional - requires GLPK or CBC #00ffff [(0, 5, None), (1, 2, None), (3, 4, None), (6, 8, None), (7, 9, None)] #7f00ff [(0, 4, None), (2, 3, None), (5, 7, None), (6, 9, None)] #7fff00 [(1, 6, None), (5, 8, None)] #ff0000 [(0, 1, None), (2, 7, None), (3, 8, None), (4, 9, None)] Complete graphs are colored using the linear-time round-robin coloring:: Complete graphs are colored using the linear-time round-robin coloring, and do not depend on MIP solving:: sage: from sage.graphs.graph_coloring import edge_coloring sage: len(edge_coloring(graphs.CompleteGraph(20))) 19 sage: g = graphs.CompleteGraph(20) sage: len(edge_coloring(g)) 19 sage: edge_coloring(g, value_only=True) 20 sage: g = graphs.CompleteGraph(7) sage: d = edge_coloring(g, hex_colors=True) sage: for k in sorted(d.keys()): ...     print k, d[k] #0091ff [(0, 1), (2, 6), (3, 5)] #00ff91 [(0, 6), (1, 5), (2, 4)] #4800ff [(0, 3), (1, 2), (4, 6)] #48ff00 [(0, 4), (1, 3), (5, 6)] #ff0000 [(1, 6), (2, 5), (3, 4)] #ff00da [(0, 5), (1, 4), (2, 3)] #ffda00 [(0, 2), (3, 6), (4, 5)] TESTS: This used to cause an infinite loop when GLPK was not installed (here we test, whether GLPK is installed or not, by using a try block), trac #7734:: sage: from sage.graphs.graph_coloring import edge_coloring sage: g = graphs.CirculantGraph(24, [2,3]) sage: try: ...       v = edge_coloring(g, value_only=False) ... except ValueError: ...       pass """ from sage.numerical.mip import MixedIntegerLinearProgram from sage.plot.colors import rainbow for (u, v, c) in r.edge_iterator(): classes[c].append((vertices[u], vertices[v])) if hex_colors: return zip(rainbow(len(classes)), classes) return dict(zip(rainbow(len(classes)), classes)) else: return classes