Ticket #13785: trac_13785.patch

File trac_13785.patch, 6.8 KB (added by ncohen, 8 years ago)
  • sage/graphs/generic_graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1354365564 -3600
    # Node ID fcf9fbaca7d88535d754a6c44a74e8041b7fba95
    # Parent  76c0c1b4aee6a503ca4569e9b3de9b7cc468cb3f
    Export a graph to a dictionary
    
    diff --git a/sage/graphs/generic_graph.py b/sage/graphs/generic_graph.py
    a b  
    1212    :delim: |
    1313
    1414    :meth:`~GenericGraph.networkx_graph` | Creates a new NetworkX graph from the Sage graph
     15    :meth:`~GenericGraph.to_dictionary` | Creates a dictionary encoding the graph.
    1516    :meth:`~GenericGraph.adjacency_matrix` | Returns the adjacency matrix of the (di)graph.
    1617    :meth:`~GenericGraph.incidence_matrix` | Returns an incidence matrix of the (di)graph
    1718    :meth:`~GenericGraph.weighted_adjacency_matrix` | Returns the weighted adjacency matrix of the graph
     
    852853                        N.add_edge(u,v,weight=l)
    853854            return N
    854855
     856    def to_dictionary(self, edge_labels=False, multiple_edges=False):
     857        r"""
     858        Returns the graph as a dictionary.
     859
     860        INPUT:
     861
     862        - ``edge_labels`` (boolean) -- whether to include edge labels in the
     863          output.
     864
     865        - ``multiple_edges`` (boolean) -- whether to include multiple edges in
     866          the output.
     867
     868        OUTPUT:
     869
     870        The output depends on the input:
     871
     872        * If ``edge_labels == False`` and ``multiple_edges == False``, the
     873          output is a dictionary associating to each vertex the list of its
     874          neighbors.
     875
     876        * If ``edge_labels == False`` and ``multiple_edges == True``, the output
     877          is a dictionary the same as previously with one difference : the
     878          neighbors are listed with multiplicity.
     879
     880        * If ``edge_labels == True`` and ``multiple_edges == False``, the output
     881          is a dictionary associating to each vertex `u` [a dictionary
     882          associating to each vertex `v` incident to `u` the label of edge
     883          `(u,v)`].
     884
     885        * If ``edge_labels == True`` and ``multiple_edges == True``, the output
     886          is a dictionary associating to each vertex `u` [a dictionary
     887          associating to each vertex `v` incident to `u` [the list of labels of
     888          all edges between `u` and `v`]].
     889
     890        .. NOTE::
     891
     892          When used on directed graphs, the explanations above can be understood
     893          by replacing the word "neigbours" by "out-neighbors"
     894
     895        EXAMPLES::
     896
     897            sage: graphs.PetersenGraph().to_dictionary()
     898            {0: [1, 4, 5], 1: [0, 2, 6],
     899             2: [1, 3, 7], 3: [8, 2, 4],
     900             4: [0, 9, 3], 5: [0, 8, 7],
     901             6: [8, 1, 9], 7: [9, 2, 5],
     902             8: [3, 5, 6], 9: [4, 6, 7]}
     903            sage: graphs.PetersenGraph().to_dictionary(multiple_edges=True)
     904            {0: [1, 4, 5], 1: [0, 2, 6],
     905             2: [1, 3, 7], 3: [2, 4, 8],
     906             4: [0, 3, 9], 5: [0, 7, 8],
     907             6: [1, 8, 9], 7: [2, 5, 9],
     908             8: [3, 5, 6], 9: [4, 6, 7]}
     909            sage: graphs.PetersenGraph().to_dictionary(edge_labels=True)
     910            {0: {1: None, 4: None, 5: None},
     911             1: {0: None, 2: None, 6: None},
     912             2: {1: None, 3: None, 7: None},
     913             3: {8: None, 2: None, 4: None},
     914             4: {0: None, 9: None, 3: None},
     915             5: {0: None, 8: None, 7: None},
     916             6: {8: None, 1: None, 9: None},
     917             7: {9: None, 2: None, 5: None},
     918             8: {3: None, 5: None, 6: None},
     919             9: {4: None, 6: None, 7: None}}
     920            sage: graphs.PetersenGraph().to_dictionary(edge_labels=True,multiple_edges=True)
     921            {0: {1: [None], 4: [None], 5: [None]},
     922             1: {0: [None], 2: [None], 6: [None]},
     923             2: {1: [None], 3: [None], 7: [None]},
     924             3: {8: [None], 2: [None], 4: [None]},
     925             4: {0: [None], 9: [None], 3: [None]},
     926             5: {0: [None], 8: [None], 7: [None]},
     927             6: {8: [None], 1: [None], 9: [None]},
     928             7: {9: [None], 2: [None], 5: [None]},
     929             8: {3: [None], 5: [None], 6: [None]},
     930             9: {4: [None], 6: [None], 7: [None]}}
     931        """
     932
     933        # Returning the resuls as a dictionary of lists
     934        #
     935        # dictionary :
     936        # {vertex : [list of (out-)neighbors]}
     937
     938        if not edge_labels and not multiple_edges:
     939            d = {}
     940
     941            if self.is_directed():
     942                for u in self:
     943                    d[u]=self.neighbors_out(u)
     944            else:
     945                for u in self:
     946                    d[u]=self.neighbors(u)
     947
     948
     949        # Returning the result as a dictionary of lists
     950        #
     951        # dictionary :
     952        # {vertex : [list of (out-)neighbors, with multiplicity]}
     953        elif not edge_labels and multiple_edges:
     954            d={v:[] for v in self}
     955
     956            if self.is_directed():
     957                for u,v in self.edge_iterator(labels = False):
     958                    d[u].append(v)
     959
     960            else:
     961                for u,v in self.edge_iterator(labels = False):
     962                    d[u].append(v)
     963                    d[v].append(u)
     964
     965        # Returning the result as a dictionary of dictionaries
     966        #
     967        # Each vertex is associated with the dictionary associating to each of
     968        # its neighbors the corresponding edge label.
     969        #
     970        # dictionary :
     971        # {v : dictionary                          }
     972        #      {neighbor u of v : label of edge u,v}
     973
     974        elif edge_labels and not multiple_edges:
     975            d={v:{} for v in self}
     976
     977            if self.is_directed():
     978                for u,v,l in self.edge_iterator():
     979                    d[u][v] = l
     980
     981            else:
     982                for u,v,l in self.edge_iterator():
     983                    d[u][v] = l
     984                    d[v][u] = l
     985
     986        # Returning the result as a dictionary of dictionaries
     987        #
     988        # Each vertex is associated with the dictionary associating to each of
     989        # its neighbors the list of edge labels between the two vertices
     990        #
     991        # dictionary :
     992        # {v : dictionary                                          }
     993        #      {neighbor u of v : [labels of edges between u and v]}
     994
     995        elif edge_labels and multiple_edges:
     996            d={v:{} for v in self}
     997
     998            if self.is_directed():
     999                for u,v,l in self.edge_iterator():
     1000                    if not v in d[u]:
     1001                        d[u][v] = []
     1002                    d[u][v].append(l)
     1003
     1004            else:
     1005                for u,v,l in self.edge_iterator():
     1006                    if not v in d[u]:
     1007                        d[u][v] = []
     1008                        d[v][u] = []
     1009
     1010                    d[u][v].append(l)
     1011                    d[v][u].append(l)
     1012
     1013        return d
     1014
    8551015    def adjacency_matrix(self, sparse=None, boundary_first=False):
    8561016        """
    8571017        Returns the adjacency matrix of the (di)graph.