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 b  
    289289
    290290    EXAMPLE::
    291291   
    292        sage: from sage.graphs.graph_coloring import vertex_coloring
    293        sage: g = graphs.PetersenGraph()
    294        sage: vertex_coloring(g, value_only=True) # optional - requires GLPK or CBC
    295        3
     292        sage: from sage.graphs.graph_coloring import vertex_coloring
     293        sage: g = graphs.PetersenGraph()
     294        sage: vertex_coloring(g, value_only=True) # optional - requires GLPK or CBC
     295        3
     296        sage: d = vertex_coloring(g, hex_colors=True)  # optional - requires GLPK or CBC
     297        sage: for k in sorted(d.keys()):               # optional - requires GLPK or CBC
     298        ...     print k, d[k]                          # optional - requires GLPK or CBC
     299        #0000ff [4, 7, 8]
     300        #00ff00 [1, 3, 5, 9]
     301        #ff0000 [0, 2, 6]
     302        sage: vertex_coloring(g, k=3)                  # optional - requires GLPK or CBC
     303        [[0, 2, 6], [1, 3, 5, 9], [4, 7, 8]]
     304        sage: vertex_coloring(g, k=2)                  # optional - requires GLPK or CBC
     305        False
     306        sage: vertex_coloring(g, k=4)                  # optional - requires GLPK or CBC
     307        [[9, 8, 2, 0], [7, 6, 4], [5, 3, 1], []]
     308        sage: vertex_coloring(g, k=2, value_only=True) # optional - requires GLPK or CBC
     309        False
     310        sage: vertex_coloring(g, k=4, value_only=True) # optional - requires GLPK or CBC
     311        True
     312
     313    Several different families of graphs do not depend on MIP solving, such as
     314    graphs without edges::
     315
     316        sage: g = Graph(20)
     317        sage: vertex_coloring(g)
     318        [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
     319        sage: vertex_coloring(g, value_only=True)
     320        1
     321        sage: vertex_coloring(g, hex_colors=True)
     322        {'#ff0000': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]}
     323        sage: vertex_coloring(g, k=4, value_only=True)
     324        True
     325
     326    Or bipartite graphs::
     327   
     328        sage: g = graphs.CompleteBipartiteGraph(3,4)
     329        sage: vertex_coloring(g)
     330        ([0, 1, 2], [3, 4, 5, 6])
     331        sage: vertex_coloring(g, value_only=True)
     332        2
     333        sage: d = vertex_coloring(g, hex_colors=True)
     334        sage: for k in sorted(d.keys()):
     335        ...     print k, d[k]
     336        #00ffff [3, 4, 5, 6]
     337        #ff0000 [0, 1, 2]
     338        sage: vertex_coloring(g, k=4, value_only=True)
     339        True
     340
    296341    """
    297342    from sage.numerical.mip import MixedIntegerLinearProgram
    298343    from sage.plot.colors import rainbow
     
    306351            if value_only:
    307352                return 1
    308353            elif hex_colors:
    309                 return dict(zip(rainbow(1), g.vertices()))
     354                return {rainbow(1)[0]: g.vertices()}
    310355            else:
    311356                return g.vertices()
    312357        # - Bipartite set
     
    500545
    501546    EXAMPLE::
    502547   
    503        sage: from sage.graphs.graph_coloring import edge_coloring
    504        sage: g = graphs.PetersenGraph()
    505        sage: edge_coloring(g, value_only=True) # optional - requires GLPK or CBC
    506        4
     548        sage: from sage.graphs.graph_coloring import edge_coloring
     549        sage: g = graphs.PetersenGraph()
     550        sage: edge_coloring(g, value_only=True) # optional - requires GLPK or CBC
     551        4
     552        sage: d = edge_coloring(g, value_only=False, hex_colors=True, log=1) # optional - requires GLPK or CBC
     553        sage: for k in sorted(d.keys()): # optional - requires GLPK or CBC
     554        ...     print k, d[k]            # optional - requires GLPK or CBC
     555        #00ffff [(0, 5, None), (1, 2, None), (3, 4, None), (6, 8, None), (7, 9, None)]
     556        #7f00ff [(0, 4, None), (2, 3, None), (5, 7, None), (6, 9, None)]
     557        #7fff00 [(1, 6, None), (5, 8, None)]
     558        #ff0000 [(0, 1, None), (2, 7, None), (3, 8, None), (4, 9, None)]
    507559
    508     Complete graphs are colored using the linear-time round-robin coloring::
     560    Complete graphs are colored using the linear-time round-robin coloring, and
     561    do not depend on MIP solving::
    509562
    510        sage: from sage.graphs.graph_coloring import edge_coloring
    511        sage: len(edge_coloring(graphs.CompleteGraph(20)))
    512        19
     563        sage: g = graphs.CompleteGraph(20)
     564        sage: len(edge_coloring(g))
     565        19
     566        sage: edge_coloring(g, value_only=True)
     567        20
     568        sage: g = graphs.CompleteGraph(7)
     569        sage: d = edge_coloring(g, hex_colors=True)
     570        sage: for k in sorted(d.keys()):
     571        ...     print k, d[k]
     572        #0091ff [(0, 1), (2, 6), (3, 5)]
     573        #00ff91 [(0, 6), (1, 5), (2, 4)]
     574        #4800ff [(0, 3), (1, 2), (4, 6)]
     575        #48ff00 [(0, 4), (1, 3), (5, 6)]
     576        #ff0000 [(1, 6), (2, 5), (3, 4)]
     577        #ff00da [(0, 5), (1, 4), (2, 3)]
     578        #ffda00 [(0, 2), (3, 6), (4, 5)]
     579
     580    TESTS:
     581   
     582    This used to cause an infinite loop when GLPK was not installed (here we
     583    test, whether GLPK is installed or not, by using a try block), trac #7734::
     584
     585        sage: from sage.graphs.graph_coloring import edge_coloring
     586        sage: g = graphs.CirculantGraph(24, [2,3])
     587        sage: try:
     588        ...       v = edge_coloring(g, value_only=False)
     589        ... except ValueError:
     590        ...       pass
     591
    513592    """
    514593    from sage.numerical.mip import MixedIntegerLinearProgram
    515594    from sage.plot.colors import rainbow
     
    526605        for (u, v, c) in r.edge_iterator():
    527606            classes[c].append((vertices[u], vertices[v]))
    528607        if hex_colors:
    529             return zip(rainbow(len(classes)), classes)
     608            return dict(zip(rainbow(len(classes)), classes))
    530609        else:
    531610            return classes             
    532611