Ticket #7314: trac_7314.patch

File trac_7314.patch, 7.4 KB (added by ncohen, 11 years ago)
  • sage/graphs/graph.py

    # HG changeset patch
    # User Nathann Cohen <nathann.cohen@gmail.com>
    # Date 1256573377 -3600
    # Node ID 98d06bec075c3f8dd8d276439115b676438753ce
    # Parent  0c34fdeab7a675a000387395dbce3661012b1979
    Graph class : Function average_distance, zeged index and wiener index
    
    diff -r 0c34fdeab7a6 -r 98d06bec075c sage/graphs/graph.py
    a b  
    29432943            sage: graphs.StarGraph(3).blocks_and_cut_vertices()
    29442944            ([[1, 0], [2, 0], [3, 0]], [0])
    29452945       
    2946         ALGORITHM: 8.3.8 in [1]. Notice that the termination condition on
    2947         line (23) of the algorithm uses "p[v] == 0" which in the book
    2948         means that the parent is undefined; in this case, v must be the
     2946        ALGORITHM: 8.3.8 in [Jungnickel2005]_. Notice that the termination
     2947        condition on line (23) of the algorithm uses "p[v] == 0" which in
     2948        the book means that the parent is undefined; in this case, v must be the
    29492949        root s.  Since our vertex names start with 0, we substitute instead
    29502950        the condition "v == s".  This is the terminating condition used
    29512951        in the general Depth First Search tree in Algorithm 8.2.1.
    29522952       
    29532953        REFERENCE:
    29542954
    2955         - [1] D. Jungnickel, Graphs, Networks and Algorithms,
     2955        .. [Jungnickel2005] D. Jungnickel, Graphs, Networks and Algorithms,
    29562956          Springer, 2005.
    29572957        """
    29582958        G = self.to_undirected()
     
    49924992        (k_i\*(k_i-1)/2) where k_i is the degree of vertex i, [1]. A
    49934993        coefficient for the whole graph is the average of the c_i.
    49944994        Transitivity is the fraction of all possible triangles which are
    4995         triangles, T = 3\*triangles/triads, [1].
     4995        triangles, T = 3\*triangles/triads, [HSSNX]_.
    49964996       
    49974997        INPUT:
    49984998       
     
    50065006       
    50075007        REFERENCE:
    50085008
    5009         - [1] Aric Hagberg, Dan Schult and Pieter Swart. NetworkX
     5009        .. [HSSNX] Aric Hagberg, Dan Schult and Pieter Swart. NetworkX
    50105010          documentation. [Online] Available:
    50115011          https://networkx.lanl.gov/reference/networkx/
    50125012       
     
    59615961       
    59625962        return dist, pred
    59635963
     5964    def wiener_index(self):
     5965        r"""
     5966        Returns the Wiener index of the graph.
     5967
     5968        The Wiener index of a graph `G` can be defined in two equivalent
     5969        ways [KRG96]_ :
     5970
     5971        - `W(G) = \frac 1 2 \sum_{u,v\in G} d(u,v)` where `d(u,v)` denotes the distance between
     5972          vertices `u` and `v`.
     5973
     5974        - Let `\Omega` be a set of `\frac {n(n-1)} 2` paths in `G` such that `\Omega`
     5975          contains exactly one shortest `u-v` path for each set `\{u,v\}` of vertices
     5976          in `G`. Besides, `\forall e\in E(G)`, let `\Omega(e)` denote the paths from `\Omega`
     5977          containing `e`. We then have `W(G) = \sum_{e\in E(G)}|\Omega(e)|`.
     5978       
     5979        EXAMPLE:
     5980       
     5981        From [GYLL93]_, cited in [KRG96]_::
     5982
     5983            sage: g=graphs.PathGraph(10)
     5984            sage: w=lambda x: (x*(x*x -1)/6)
     5985            sage: g.wiener_index()==w(10)
     5986            True
     5987
     5988        REFERENCE:
     5989
     5990        .. [KRG96] Klavzar S., Rajapakse A., Gutman I. (1996). The Szeged and
     5991          the Wiener index of graphs .
     5992          Applied Mathematics Letters, 9 (5), pp. 45-49.
     5993
     5994        .. [GYLL93] I Gutman, YN Yeh, SL Lee, YL Luo (1993),
     5995          Some recent results in the theory of the Wiener number.
     5996          INDIAN JOURNAL OF CHEMISTRY SECTION A
     5997          PUBLICATIONS & INFORMATION DIRECTORATE, CSIR
     5998     
     5999        """
     6000
     6001        distances=self.shortest_path_all_pairs(default_weight=Integer(1))[0]
     6002        return sum([sum(v.itervalues()) for v in distances.itervalues()])/2
     6003
     6004    def average_distance(self):
     6005        r"""
     6006        Returns the average distance between vertices of the graph.
     6007
     6008        Formally, for a graph `G` this value is equal to
     6009        `\frac 1 {n(n-1)} \sum_{u,v\in G} d(u,v)` where `d(u,v)`
     6010        denotes the distance between vertices `u` and `v` and `n`
     6011        is the number of vertices in `G`.
     6012
     6013        EXAMPLE:
     6014       
     6015        From [GYLL93]_::
     6016
     6017            sage: g=graphs.PathGraph(10)
     6018            sage: w=lambda x: (x*(x*x -1)/6)/(x*(x-1)/2)
     6019            sage: g.average_distance()==w(10)
     6020            True
     6021
     6022
     6023        REFERENCE:
     6024
     6025        .. [GYLL93] I Gutman, YN Yeh, SL Lee, YL Luo (1993),
     6026          Some recent results in the theory of the Wiener number.
     6027          INDIAN JOURNAL OF CHEMISTRY SECTION A
     6028          PUBLICATIONS & INFORMATION DIRECTORATE, CSIR
     6029
     6030        """
     6031
     6032        return self.wiener_index()/((self.order()*(self.order()-1))/2)
     6033
     6034    def szeged_index(self):
     6035        r"""
     6036        Returns the Szeged index of the graph.
     6037
     6038        For any `uv\in E(G)`, let
     6039        `N_u(uv) = \{w\in G:d(u,w)<d(v,w)\}, n_u(uv)=|N_u(uv)|`
     6040
     6041        The Szeged index of a graph is then defined as [KRG96]_ :
     6042        `\sum_{uv \in E(G)}n_u(uv)\times n_v(uv)`
     6043
     6044        EXAMPLE:
     6045
     6046        True for any connected graph [KRG96]_::
     6047
     6048            sage: g=graphs.PetersenGraph()
     6049            sage: g.wiener_index()<= g.szeged_index()
     6050            True
     6051
     6052        True for all trees [KRG96]_::
     6053
     6054            sage: g=Graph()
     6055            sage: g.add_edges(graphs.CubeGraph(5).min_spanning_tree())
     6056            sage: g.wiener_index() == g.szeged_index()
     6057            True
     6058
     6059
     6060        REFERENCE:
     6061
     6062        .. [KRG96] Klavzar S., Rajapakse A., Gutman I. (1996). The Szeged and
     6063          the Wiener index of graphs .
     6064          Applied Mathematics Letters, 9 (5), pp. 45-49.
     6065
     6066        """
     6067        distances=self.shortest_path_all_pairs()[0]
     6068        s=0
     6069        for (u,v) in self.edges(labels=None):
     6070            du=distances[u]
     6071            dv=distances[v]
     6072            n1=n2=0
     6073            for w in self:
     6074                if du[w] < dv[w]:
     6075                    n1+=1
     6076                elif dv[w] < du[w]:
     6077                    n2+=1
     6078            s+=(n1*n2)
     6079        return s
     6080               
     6081           
     6082       
     6083
    59646084    ### Searches
    59656085   
    59666086    def breadth_first_search(self, start, ignore_direction=False, distance=None, neighbors=None):
     
    992010040        paths that go through each vertex) as a dictionary keyed by
    992110041        vertices. The betweenness is normalized by default to be in range
    992210042        (0,1). This wraps NetworkX's implementation of the algorithm
    9923         described in [1].
     10043        described in [Brandes2003]_.
    992410044       
    992510045        Measures of the centrality of a vertex within a graph determine the
    992610046        relative importance of that vertex to its graph. Vertices that
     
    993610056       
    993710057        REFERENCE:
    993810058
    9939         - [1] Ulrik Brandes. (2003). Faster Evaluation of
     10059        .. [Brandes2003] Ulrik Brandes. (2003). Faster Evaluation of
    994010060          Shortest-Path Based Centrality Indices. [Online] Available:
    994110061          http://citeseer.nj.nec.com/brandes00faster.html
    994210062       
     
    1000110121        a given vertex from all other vertices... Closeness is an inverse
    1000210122        measure of centrality in that a larger value indicates a less
    1000310123        central actor while a smaller value indicates a more central
    10004         actor,' [1].
     10124        actor,' [Borgatti95]_.
    1000510125       
    1000610126        INPUT:
    1000710127       
     
    1001210132       
    1001310133        REFERENCE:
    1001410134
    10015         - [1] Stephen P Borgatti. (1995). Centrality and AIDS.
     10135        .. [Borgatti95] Stephen P Borgatti. (1995). Centrality and AIDS.
    1001610136          [Online] Available:
    1001710137          http://www.analytictech.com/networks/centaids.htm
    1001810138