Ticket #12653: trac_12653-graph_from_dictionary_of_iterables-nt.patch

File trac_12653-graph_from_dictionary_of_iterables-nt.patch, 2.7 KB (added by Nicolas M. Thiéry, 11 years ago)
  • sage/graphs/digraph.py

    # HG changeset patch
    # User Nicolas M. Thiery <nthiery@users.sf.net>
    # Date 1331542839 -3600
    # Node ID 7d04547261248799f0b4c810fc7b0adc2d01a143
    # Parent  188444c101896383cf100c903c409508bf625dad
    #12653: Add support for DiGraph(<dictionary of iterables>)
    
    diff --git a/sage/graphs/digraph.py b/sage/graphs/digraph.py
    a b class DiGraph(GenericGraph): 
    177177       example, 'out' is the label for the edge from 2 to 5. Labels can be
    178178       used as weights, if all the labels share some common parent.
    179179   
    180     #. A dictionary of lists::
     180    #. A dictionary of lists (or iterables)::
    181181
    182182            sage: g = DiGraph({0:[1,2,3], 2:[4]}); g
    183183            Digraph on 5 vertices
     184            sage: g = DiGraph({0:(1,2,3), 2:(4,)}); g
     185            Digraph on 5 vertices
    184186   
    185187    #. A list of vertices and a function describing adjacencies. Note
    186188       that the list of vertices and the function must be enclosed in a
    class DiGraph(GenericGraph): 
    293295            sage: H = DiGraph(g, implementation='networkx')
    294296            sage: G._backend._nxg is H._backend._nxg
    295297            False
    296        
     298
     299    TESTS::
     300
     301            sage: DiGraph({0:[1,2,3], 2:[4]}).edges()
     302            [(0, 1, None), (0, 2, None), (0, 3, None), (2, 4, None)]
     303            sage: DiGraph({0:(1,2,3), 2:(4,)}).edges()
     304            [(0, 1, None), (0, 2, None), (0, 3, None), (2, 4, None)]
     305            sage: DiGraph({0:Set([1,2,3]), 2:Set([4])}).edges()
     306            [(0, 1, None), (0, 2, None), (0, 3, None), (2, 4, None)]
     307
    297308    """
    298309    _directed = True
    299310
    class DiGraph(GenericGraph): 
    383394            keys = data.keys()
    384395            if len(keys) == 0: format = 'dict_of_dicts'
    385396            else:
    386                 if isinstance(data[keys[0]], list):
     397                if isinstance(data[keys[0]], dict):
     398                    format = 'dict_of_dicts'
     399                else:
    387400                    format = 'dict_of_lists'
    388                 elif isinstance(data[keys[0]], dict):
    389                     format = 'dict_of_dicts'
    390401        if format is None and hasattr(data, 'adj'):
    391402            import networkx
    392403            if isinstance(data, (networkx.Graph, networkx.MultiGraph)):
    class DiGraph(GenericGraph): 
    608619                multiedges = True
    609620            num_verts = len(verts)
    610621        elif format == 'dict_of_lists':
     622            # convert to a dict of lists if not already one
    611623            if not all(isinstance(data[u], list) for u in data):
    612                 raise ValueError("Input dict must be a consistent format.")
     624                data = dict([u, list(data[u])] for u in data)
    613625            verts = set(data.keys())
    614626            if loops is None or loops is False:
    615627                for u in data: