Ticket #7314: trac_7314.patch
File trac_7314.patch, 7.4 KB (added by , 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 2943 2943 sage: graphs.StarGraph(3).blocks_and_cut_vertices() 2944 2944 ([[1, 0], [2, 0], [3, 0]], [0]) 2945 2945 2946 ALGORITHM: 8.3.8 in [ 1]. Notice that the termination conditionon2947 line (23) of the algorithm uses "p[v] == 0" which in the book2948 means that the parent is undefined; in this case, v must be the2946 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 2949 2949 root s. Since our vertex names start with 0, we substitute instead 2950 2950 the condition "v == s". This is the terminating condition used 2951 2951 in the general Depth First Search tree in Algorithm 8.2.1. 2952 2952 2953 2953 REFERENCE: 2954 2954 2955  [1] D. Jungnickel, Graphs, Networks and Algorithms,2955 .. [Jungnickel2005] D. Jungnickel, Graphs, Networks and Algorithms, 2956 2956 Springer, 2005. 2957 2957 """ 2958 2958 G = self.to_undirected() … … 4992 4992 (k_i\*(k_i1)/2) where k_i is the degree of vertex i, [1]. A 4993 4993 coefficient for the whole graph is the average of the c_i. 4994 4994 Transitivity is the fraction of all possible triangles which are 4995 triangles, T = 3\*triangles/triads, [ 1].4995 triangles, T = 3\*triangles/triads, [HSSNX]_. 4996 4996 4997 4997 INPUT: 4998 4998 … … 5006 5006 5007 5007 REFERENCE: 5008 5008 5009  [1] Aric Hagberg, Dan Schult and Pieter Swart. NetworkX5009 .. [HSSNX] Aric Hagberg, Dan Schult and Pieter Swart. NetworkX 5010 5010 documentation. [Online] Available: 5011 5011 https://networkx.lanl.gov/reference/networkx/ 5012 5012 … … 5961 5961 5962 5962 return dist, pred 5963 5963 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(n1)} 2` paths in `G` such that `\Omega` 5975 contains exactly one shortest `uv` 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. 4549. 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(n1)} \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*(x1)/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. 4549. 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 5964 6084 ### Searches 5965 6085 5966 6086 def breadth_first_search(self, start, ignore_direction=False, distance=None, neighbors=None): … … 9920 10040 paths that go through each vertex) as a dictionary keyed by 9921 10041 vertices. The betweenness is normalized by default to be in range 9922 10042 (0,1). This wraps NetworkX's implementation of the algorithm 9923 described in [ 1].10043 described in [Brandes2003]_. 9924 10044 9925 10045 Measures of the centrality of a vertex within a graph determine the 9926 10046 relative importance of that vertex to its graph. Vertices that … … 9936 10056 9937 10057 REFERENCE: 9938 10058 9939  [1] Ulrik Brandes. (2003). Faster Evaluation of10059 .. [Brandes2003] Ulrik Brandes. (2003). Faster Evaluation of 9940 10060 ShortestPath Based Centrality Indices. [Online] Available: 9941 10061 http://citeseer.nj.nec.com/brandes00faster.html 9942 10062 … … 10001 10121 a given vertex from all other vertices... Closeness is an inverse 10002 10122 measure of centrality in that a larger value indicates a less 10003 10123 central actor while a smaller value indicates a more central 10004 actor,' [ 1].10124 actor,' [Borgatti95]_. 10005 10125 10006 10126 INPUT: 10007 10127 … … 10012 10132 10013 10133 REFERENCE: 10014 10134 10015  [1] Stephen P Borgatti. (1995). Centrality and AIDS.10135 .. [Borgatti95] Stephen P Borgatti. (1995). Centrality and AIDS. 10016 10136 [Online] Available: 10017 10137 http://www.analytictech.com/networks/centaids.htm 10018 10138