# HG changeset patch
# User Nathann Cohen
# 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/sage/graphs/graph.py Mon Oct 26 19:47:58 2009 +0100
+++ b/sage/graphs/graph.py Mon Oct 26 17:09:37 2009 +0100
@@ -2943,16 +2943,16 @@
sage: graphs.StarGraph(3).blocks_and_cut_vertices()
([[1, 0], [2, 0], [3, 0]], [0])
- ALGORITHM: 8.3.8 in [1]. Notice that the termination condition on
- line (23) of the algorithm uses "p[v] == 0" which in the book
- means that the parent is undefined; in this case, v must be the
+ ALGORITHM: 8.3.8 in [Jungnickel2005]_. Notice that the termination
+ condition on line (23) of the algorithm uses "p[v] == 0" which in
+ the book means that the parent is undefined; in this case, v must be the
root s. Since our vertex names start with 0, we substitute instead
the condition "v == s". This is the terminating condition used
in the general Depth First Search tree in Algorithm 8.2.1.
REFERENCE:
- - [1] D. Jungnickel, Graphs, Networks and Algorithms,
+ .. [Jungnickel2005] D. Jungnickel, Graphs, Networks and Algorithms,
Springer, 2005.
"""
G = self.to_undirected()
@@ -4992,7 +4992,7 @@
(k_i\*(k_i-1)/2) where k_i is the degree of vertex i, [1]. A
coefficient for the whole graph is the average of the c_i.
Transitivity is the fraction of all possible triangles which are
- triangles, T = 3\*triangles/triads, [1].
+ triangles, T = 3\*triangles/triads, [HSSNX]_.
INPUT:
@@ -5006,7 +5006,7 @@
REFERENCE:
- - [1] Aric Hagberg, Dan Schult and Pieter Swart. NetworkX
+ .. [HSSNX] Aric Hagberg, Dan Schult and Pieter Swart. NetworkX
documentation. [Online] Available:
https://networkx.lanl.gov/reference/networkx/
@@ -5961,6 +5961,126 @@
return dist, pred
+ def wiener_index(self):
+ r"""
+ Returns the Wiener index of the graph.
+
+ The Wiener index of a graph `G` can be defined in two equivalent
+ ways [KRG96]_ :
+
+ - `W(G) = \frac 1 2 \sum_{u,v\in G} d(u,v)` where `d(u,v)` denotes the distance between
+ vertices `u` and `v`.
+
+ - Let `\Omega` be a set of `\frac {n(n-1)} 2` paths in `G` such that `\Omega`
+ contains exactly one shortest `u-v` path for each set `\{u,v\}` of vertices
+ in `G`. Besides, `\forall e\in E(G)`, let `\Omega(e)` denote the paths from `\Omega`
+ containing `e`. We then have `W(G) = \sum_{e\in E(G)}|\Omega(e)|`.
+
+ EXAMPLE:
+
+ From [GYLL93]_, cited in [KRG96]_::
+
+ sage: g=graphs.PathGraph(10)
+ sage: w=lambda x: (x*(x*x -1)/6)
+ sage: g.wiener_index()==w(10)
+ True
+
+ REFERENCE:
+
+ .. [KRG96] Klavzar S., Rajapakse A., Gutman I. (1996). The Szeged and
+ the Wiener index of graphs .
+ Applied Mathematics Letters, 9 (5), pp. 45-49.
+
+ .. [GYLL93] I Gutman, YN Yeh, SL Lee, YL Luo (1993),
+ Some recent results in the theory of the Wiener number.
+ INDIAN JOURNAL OF CHEMISTRY SECTION A
+ PUBLICATIONS & INFORMATION DIRECTORATE, CSIR
+
+ """
+
+ distances=self.shortest_path_all_pairs(default_weight=Integer(1))[0]
+ return sum([sum(v.itervalues()) for v in distances.itervalues()])/2
+
+ def average_distance(self):
+ r"""
+ Returns the average distance between vertices of the graph.
+
+ Formally, for a graph `G` this value is equal to
+ `\frac 1 {n(n-1)} \sum_{u,v\in G} d(u,v)` where `d(u,v)`
+ denotes the distance between vertices `u` and `v` and `n`
+ is the number of vertices in `G`.
+
+ EXAMPLE:
+
+ From [GYLL93]_::
+
+ sage: g=graphs.PathGraph(10)
+ sage: w=lambda x: (x*(x*x -1)/6)/(x*(x-1)/2)
+ sage: g.average_distance()==w(10)
+ True
+
+
+ REFERENCE:
+
+ .. [GYLL93] I Gutman, YN Yeh, SL Lee, YL Luo (1993),
+ Some recent results in the theory of the Wiener number.
+ INDIAN JOURNAL OF CHEMISTRY SECTION A
+ PUBLICATIONS & INFORMATION DIRECTORATE, CSIR
+
+ """
+
+ return self.wiener_index()/((self.order()*(self.order()-1))/2)
+
+ def szeged_index(self):
+ r"""
+ Returns the Szeged index of the graph.
+
+ For any `uv\in E(G)`, let
+ `N_u(uv) = \{w\in G:d(u,w)