summaryrefslogtreecommitdiff
blob: 51ba3fbd72b5f39bc86ab8f024c4d2bf13767c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# HG changeset patch
# User Ben Edwards <bedwards@cs.unm.edu>
# Date 1281372187 21600
# Node ID 2f4945743e05e93fdbae5ec75777060bea4a6670
# Parent  5b338f2e484f2065d3d30d47bc204d6e9ed13d12
trac 9567: Updates to sage to interface with new version of networkx(1.2)

This also includes several changes to the sage graph API to return
keyed dictionaries instead of lists, as this is preferred in many
situations.

diff -r 5b338f2e484f -r 2f4945743e05 sage/graphs/generic_graph.py
--- a/sage/graphs/generic_graph.py	Thu Aug 05 03:35:44 2010 -0700
+++ b/sage/graphs/generic_graph.py	Mon Aug 09 10:43:07 2010 -0600
@@ -8478,8 +8478,8 @@
     
     def cluster_triangles(self, nbunch=None, with_labels=False):
         r"""
-        Returns the number of triangles for nbunch of vertices as an
-        ordered list.
+        Returns the number of triangles for nbunch of vertices as a
+        dictionary keyed by vertex.
         
         The clustering coefficient of a graph is the fraction of possible
         triangles that are triangles, `c_i = triangles_i /
@@ -8494,9 +8494,6 @@
         -  ``nbunch`` - The vertices to inspect. If
            nbunch=None, returns data for all vertices in the graph
         
-        -  ``with_labels`` - (boolean) default False
-           returns list as above True returns dict keyed by vertex labels.
-        
         
         REFERENCE:
 
@@ -8506,15 +8503,15 @@
         
         EXAMPLES::
         
+            sage: (graphs.FruchtGraph()).cluster_triangles().values()
+            [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0]
             sage: (graphs.FruchtGraph()).cluster_triangles()
-            [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0]
-            sage: (graphs.FruchtGraph()).cluster_triangles(with_labels=True)
             {0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 1, 11: 0}
             sage: (graphs.FruchtGraph()).cluster_triangles(nbunch=[0,1,2])
-            [1, 1, 0]
+            {0: 1, 1: 1, 2: 0}
         """
         import networkx
-        return networkx.triangles(self.networkx_graph(copy=False), nbunch, with_labels)
+        return networkx.triangles(self.networkx_graph(copy=False), nbunch)
         
     def clustering_average(self):
         r"""
@@ -8541,10 +8538,10 @@
         import networkx
         return networkx.average_clustering(self.networkx_graph(copy=False))
         
-    def clustering_coeff(self, nbunch=None, with_labels=False, weights=False):
-        r"""
-        Returns the clustering coefficient for each vertex in nbunch as an
-        ordered list.
+    def clustering_coeff(self, nbunch=None, weights=False):
+        r"""
+        Returns the clustering coefficient for each vertex in nbunch as a
+        dictionary keyed by vertex.
         
         The clustering coefficient of a graph is the fraction of possible
         triangles that are triangles, `c_i = triangles_i /
@@ -8558,9 +8555,6 @@
         -  ``nbunch`` - the vertices to inspect (default
            None returns data on all vertices in graph)
         
-        -  ``with_labels`` - (boolean) default False
-           returns list as above True returns dict keyed by vertex labels.
-        
         -  ``weights`` - default is False. If both
            with_labels and weights are True, then returns a clustering
            coefficient dict and a dict of weights based on degree. Weights are
@@ -8576,19 +8570,19 @@
         
         EXAMPLES::
         
+            sage: (graphs.FruchtGraph()).clustering_coeff().values()
+            [0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.0]
             sage: (graphs.FruchtGraph()).clustering_coeff()
-            [0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.0]
-            sage: (graphs.FruchtGraph()).clustering_coeff(with_labels=True)
             {0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.0, 3: 0.33333333333333331, 4: 0.33333333333333331, 5: 0.33333333333333331, 6: 0.33333333333333331, 7: 0.33333333333333331, 8: 0.0, 9: 0.33333333333333331, 10: 0.33333333333333331, 11: 0.0}
-            sage: (graphs.FruchtGraph()).clustering_coeff(with_labels=True,weights=True)
+            sage: (graphs.FruchtGraph()).clustering_coeff(weights=True)
             ({0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.0, 3: 0.33333333333333331, 4: 0.33333333333333331, 5: 0.33333333333333331, 6: 0.33333333333333331, 7: 0.33333333333333331, 8: 0.0, 9: 0.33333333333333331, 10: 0.33333333333333331, 11: 0.0}, {0: 0.083333333333333329, 1: 0.083333333333333329, 2: 0.083333333333333329, 3: 0.083333333333333329, 4: 0.083333333333333329, 5: 0.083333333333333329, 6: 0.083333333333333329, 7: 0.083333333333333329, 8: 0.083333333333333329, 9: 0.083333333333333329, 10: 0.083333333333333329, 11: 0.083333333333333329})
             sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2])
-            [0.33333333333333331, 0.33333333333333331, 0.0]
-            sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2],with_labels=True,weights=True)
+            {0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.0}
+            sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2],weights=True)
             ({0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.0}, {0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.33333333333333331})
         """
         import networkx
-        return networkx.clustering(self.networkx_graph(copy=False), nbunch, with_labels, weights)
+        return networkx.clustering(self.networkx_graph(copy=False), nbunch, weights)
         
     def cluster_transitivity(self):
         r"""
diff -r 5b338f2e484f -r 2f4945743e05 sage/graphs/graph.py
--- a/sage/graphs/graph.py	Thu Aug 05 03:35:44 2010 -0700
+++ b/sage/graphs/graph.py	Mon Aug 09 10:43:07 2010 -0600
@@ -2531,7 +2531,7 @@
             sage: (graphs.ChvatalGraph()).centrality_betweenness()
             {0: 0.069696969696969688, 1: 0.069696969696969688, 2: 0.060606060606060601, 3: 0.060606060606060601, 4: 0.069696969696969688, 5: 0.069696969696969688, 6: 0.060606060606060601, 7: 0.060606060606060601, 8: 0.060606060606060601, 9: 0.060606060606060601, 10: 0.060606060606060601, 11: 0.060606060606060601}
             sage: (graphs.ChvatalGraph()).centrality_betweenness(normalized=False)
-            {0: 7.6666666666666661, 1: 7.6666666666666661, 2: 6.6666666666666661, 3: 6.6666666666666661, 4: 7.6666666666666661, 5: 7.6666666666666661, 6: 6.6666666666666661, 7: 6.6666666666666661, 8: 6.6666666666666661, 9: 6.6666666666666661, 10: 6.6666666666666661, 11: 6.6666666666666661}
+            {0: 3.833333333333333, 1: 3.833333333333333, 2: 3.333333333333333, 3: 3.333333333333333, 4: 3.833333333333333, 5: 3.833333333333333, 6: 3.333333333333333, 7: 3.333333333333333, 8: 3.333333333333333, 9: 3.333333333333333, 10: 3.333333333333333, 11: 3.333333333333333}
             sage: D = DiGraph({0:[1,2,3], 1:[2], 3:[0,1]})
             sage: D.show(figsize=[2,2])
             sage: D = D.to_undirected()
@@ -2573,7 +2573,10 @@
             1.0
         """
         import networkx
-        return networkx.degree_centrality(self.networkx_graph(copy=False), v)
+        if v==None:
+            return networkx.degree_centrality(self.networkx_graph(copy=False))
+        else:
+            return networkx.degree_centrality(self.networkx_graph(copy=False))[v]
             
     def centrality_closeness(self, v=None):
         r"""
@@ -2870,10 +2873,11 @@
         else:
             raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.")
 
-    def cliques_number_of(self, vertices=None, cliques=None, with_labels=False):
+    def cliques_number_of(self, vertices=None, cliques=None):
         """
-        Returns a list of the number of maximal cliques containing each
-        vertex. (Returns a single value if only one input vertex).
+        Returns a dictionary of the number of maximal cliques containing each
+        vertex, keyed by vertex. (Returns a single value if
+        only one input vertex).
         
         .. NOTE::
         
@@ -2885,9 +2889,6 @@
         -  ``vertices`` - the vertices to inspect (default is
            entire graph)
         
-        -  ``with_labels`` - (boolean) default False returns
-           list as above True returns a dictionary keyed by vertex labels
-        
         -  ``cliques`` - list of cliques (if already
            computed)
         
@@ -2896,14 +2897,14 @@
         
             sage: C = Graph('DJ{')
             sage: C.cliques_number_of()
-            [1, 1, 1, 1, 2]
+            {0: 1, 1: 1, 2: 1, 3: 1, 4: 2}
             sage: E = C.cliques_maximal()
             sage: E
             [[4, 0], [4, 1, 2, 3]]
             sage: C.cliques_number_of(cliques=E)
-            [1, 1, 1, 1, 2]
+            {0: 1, 1: 1, 2: 1, 3: 1, 4: 2}
             sage: F = graphs.Grid2dGraph(2,3)
-            sage: X = F.cliques_number_of(with_labels=True)
+            sage: X = F.cliques_number_of()
             sage: for v in sorted(X.iterkeys()):
             ...    print v, X[v]
             (0, 0) 2
@@ -2913,14 +2914,14 @@
             (1, 1) 3
             (1, 2) 2
             sage: F.cliques_number_of(vertices=[(0, 1), (1, 2)])
-            [3, 2]
+            {(0, 1): 3, (1, 2): 2}
             sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
             sage: G.show(figsize=[2,2])
             sage: G.cliques_number_of()
-            [2, 2, 1, 1]
+            {0: 2, 1: 2, 2: 1, 3: 1}
         """
         import networkx
-        return networkx.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques, with_labels)
+        return networkx.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques)
 
     def cliques_get_max_clique_graph(self, name=''):
         """
@@ -2996,12 +2997,13 @@
         """
         from sage.graphs.cliquer import max_clique
         return max_clique(self.complement())
-
+    
     def cliques_vertex_clique_number(self, algorithm="cliquer", vertices=None,
-                                     with_labels=False, cliques=None):
-        r"""
-        Returns a list of sizes of the largest maximal cliques containing
-        each vertex. (Returns a single value if only one input vertex).
+                                     cliques=None):
+        """
+        Returns a dictionary of sizes of the largest maximal cliques containing
+        each vertex, keyed by vertex. (Returns a single value if only one
+        input vertex).
         
         .. NOTE::
         
@@ -3020,10 +3022,6 @@
         -  ``vertices`` - the vertices to inspect (default is entire graph).
            Ignored unless ``algorithm=='networkx'``.
         
-        -  ``with_labels`` - (boolean) default False returns list as above
-           True returns a dictionary keyed by vertex labels. Ignored unless
-           ``algorithm=='networkx'``.
-        
         -  ``cliques`` - list of cliques (if already computed).  Ignored unless
            ``algorithm=='networkx'``.
         
@@ -3031,14 +3029,14 @@
         
             sage: C = Graph('DJ{')
             sage: C.cliques_vertex_clique_number()
-            [2, 4, 4, 4, 4]
+            {0: 2, 1: 4, 2: 4, 3: 4, 4: 4}
             sage: E = C.cliques_maximal()
             sage: E
             [[4, 0], [4, 1, 2, 3]]
             sage: C.cliques_vertex_clique_number(cliques=E,algorithm="networkx")
-            [2, 4, 4, 4, 4]
+            {0: 2, 1: 4, 2: 4, 3: 4, 4: 4}
             sage: F = graphs.Grid2dGraph(2,3)
-            sage: X = F.cliques_vertex_clique_number(with_labels=True,algorithm="networkx")
+            sage: X = F.cliques_vertex_clique_number(algorithm="networkx")
             sage: for v in sorted(X.iterkeys()):
             ...    print v, X[v]
             (0, 0) 2
@@ -3048,11 +3046,11 @@
             (1, 1) 2
             (1, 2) 2
             sage: F.cliques_vertex_clique_number(vertices=[(0, 1), (1, 2)])
-            [2, 2]
+            {(0, 1): 2, (1, 2): 2}
             sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
             sage: G.show(figsize=[2,2])
             sage: G.cliques_vertex_clique_number()
-            [3, 3, 3, 3]
+            {0: 3, 1: 3, 2: 3, 3: 3}
 
         """
 
@@ -3060,21 +3058,22 @@
             from sage.graphs.cliquer import clique_number
             if vertices==None:
                 vertices=self
-            value=[]
+            value={}
             for v in vertices:
-                value.append(1+clique_number(self.subgraph(self.neighbors(v))))
+                value[v] = 1+clique_number(self.subgraph(self.neighbors(v)))
                 self.subgraph(self.neighbors(v)).plot()
             return value
         elif algorithm=="networkx":
             import networkx
-            return networkx.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques)
+            return networkx.node_clique_number(self.networkx_graph(copy=False),vertices, cliques)
         else:
             raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.")
 
-    def cliques_containing_vertex(self, vertices=None, cliques=None, with_labels=False):
+    def cliques_containing_vertex(self, vertices=None, cliques=None):
         """
-        Returns the cliques containing each vertex, represented as a list
-        of lists. (Returns a single list if only one input vertex).
+        Returns the cliques containing each vertex, represented as a dictionary
+        of lists of lists, keyed by vertex. (Returns a single list if only one
+        input vertex).
         
         .. NOTE::
         
@@ -3086,9 +3085,6 @@
         -  ``vertices`` - the vertices to inspect (default is
            entire graph)
         
-        -  ``with_labels`` - (boolean) default False returns
-           list as above True returns a dictionary keyed by vertex labels
-        
         -  ``cliques`` - list of cliques (if already
            computed)
         
@@ -3096,14 +3092,14 @@
         
             sage: C = Graph('DJ{')
             sage: C.cliques_containing_vertex()
-            [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 0], [4, 1, 2, 3]]]
+            {0: [[4, 0]], 1: [[4, 1, 2, 3]], 2: [[4, 1, 2, 3]], 3: [[4, 1, 2, 3]], 4: [[4, 0], [4, 1, 2, 3]]}
             sage: E = C.cliques_maximal()
             sage: E
             [[4, 0], [4, 1, 2, 3]]
             sage: C.cliques_containing_vertex(cliques=E)
-            [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 0], [4, 1, 2, 3]]]
+            {0: [[4, 0]], 1: [[4, 1, 2, 3]], 2: [[4, 1, 2, 3]], 3: [[4, 1, 2, 3]], 4: [[4, 0], [4, 1, 2, 3]]}
             sage: F = graphs.Grid2dGraph(2,3)
-            sage: X = F.cliques_containing_vertex(with_labels=True)
+            sage: X = F.cliques_containing_vertex()
             sage: for v in sorted(X.iterkeys()):
             ...    print v, X[v]
             (0, 0) [[(0, 1), (0, 0)], [(1, 0), (0, 0)]]
@@ -3113,15 +3109,15 @@
             (1, 1) [[(0, 1), (1, 1)], [(1, 2), (1, 1)], [(1, 0), (1, 1)]]
             (1, 2) [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]
             sage: F.cliques_containing_vertex(vertices=[(0, 1), (1, 2)])
-            [[[(0, 1), (0, 0)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]], [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]]
+            {(0, 1): [[(0, 1), (0, 0)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]], (1, 2): [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]}
             sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})
             sage: G.show(figsize=[2,2])
             sage: G.cliques_containing_vertex()
-            [[[0, 1, 2], [0, 1, 3]], [[0, 1, 2], [0, 1, 3]], [[0, 1, 2]], [[0, 1, 3]]]
+            {0: [[0, 1, 2], [0, 1, 3]], 1: [[0, 1, 2], [0, 1, 3]], 2: [[0, 1, 2]], 3: [[0, 1, 3]]}
 
         """
         import networkx
-        return networkx.cliques_containing_node(self.networkx_graph(copy=False), vertices, cliques, with_labels)
+        return networkx.cliques_containing_node(self.networkx_graph(copy=False),vertices, cliques)
 
     def clique_complex(self):
         """