Ticket #9567: trac_9567-networkx-1.2.patch
File trac_9567-networkx-1.2.patch, 16.0 KB (added by , 10 years ago) |
---|
-
sage/graphs/generic_graph.py
# 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 b 8478 8478 8479 8479 def cluster_triangles(self, nbunch=None, with_labels=False): 8480 8480 r""" 8481 Returns the number of triangles for nbunch of vertices as a n8482 ordered list.8481 Returns the number of triangles for nbunch of vertices as a 8482 dictionary keyed by vertex. 8483 8483 8484 8484 The clustering coefficient of a graph is the fraction of possible 8485 8485 triangles that are triangles, `c_i = triangles_i / … … 8494 8494 - ``nbunch`` - The vertices to inspect. If 8495 8495 nbunch=None, returns data for all vertices in the graph 8496 8496 8497 - ``with_labels`` - (boolean) default False8498 returns list as above True returns dict keyed by vertex labels.8499 8500 8497 8501 8498 REFERENCE: 8502 8499 … … 8506 8503 8507 8504 EXAMPLES:: 8508 8505 8506 sage: (graphs.FruchtGraph()).cluster_triangles().values() 8507 [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0] 8509 8508 sage: (graphs.FruchtGraph()).cluster_triangles() 8510 [1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0]8511 sage: (graphs.FruchtGraph()).cluster_triangles(with_labels=True)8512 8509 {0: 1, 1: 1, 2: 0, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 0, 9: 1, 10: 1, 11: 0} 8513 8510 sage: (graphs.FruchtGraph()).cluster_triangles(nbunch=[0,1,2]) 8514 [1, 1, 0]8511 {0: 1, 1: 1, 2: 0} 8515 8512 """ 8516 8513 import networkx 8517 return networkx.triangles(self.networkx_graph(copy=False), nbunch , with_labels)8514 return networkx.triangles(self.networkx_graph(copy=False), nbunch) 8518 8515 8519 8516 def clustering_average(self): 8520 8517 r""" … … 8541 8538 import networkx 8542 8539 return networkx.average_clustering(self.networkx_graph(copy=False)) 8543 8540 8544 def clustering_coeff(self, nbunch=None, w ith_labels=False, weights=False):8545 r""" 8546 Returns the clustering coefficient for each vertex in nbunch as a n8547 ordered list.8541 def clustering_coeff(self, nbunch=None, weights=False): 8542 r""" 8543 Returns the clustering coefficient for each vertex in nbunch as a 8544 dictionary keyed by vertex. 8548 8545 8549 8546 The clustering coefficient of a graph is the fraction of possible 8550 8547 triangles that are triangles, `c_i = triangles_i / … … 8558 8555 - ``nbunch`` - the vertices to inspect (default 8559 8556 None returns data on all vertices in graph) 8560 8557 8561 - ``with_labels`` - (boolean) default False8562 returns list as above True returns dict keyed by vertex labels.8563 8564 8558 - ``weights`` - default is False. If both 8565 8559 with_labels and weights are True, then returns a clustering 8566 8560 coefficient dict and a dict of weights based on degree. Weights are … … 8576 8570 8577 8571 EXAMPLES:: 8578 8572 8573 sage: (graphs.FruchtGraph()).clustering_coeff().values() 8574 [0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.0] 8579 8575 sage: (graphs.FruchtGraph()).clustering_coeff() 8580 [0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.33333333333333331, 0.0, 0.33333333333333331, 0.33333333333333331, 0.0]8581 sage: (graphs.FruchtGraph()).clustering_coeff(with_labels=True)8582 8576 {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} 8583 sage: (graphs.FruchtGraph()).clustering_coeff(w ith_labels=True,weights=True)8577 sage: (graphs.FruchtGraph()).clustering_coeff(weights=True) 8584 8578 ({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}) 8585 8579 sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2]) 8586 [0.33333333333333331, 0.33333333333333331, 0.0]8587 sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2],w ith_labels=True,weights=True)8580 {0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.0} 8581 sage: (graphs.FruchtGraph()).clustering_coeff(nbunch=[0,1,2],weights=True) 8588 8582 ({0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.0}, {0: 0.33333333333333331, 1: 0.33333333333333331, 2: 0.33333333333333331}) 8589 8583 """ 8590 8584 import networkx 8591 return networkx.clustering(self.networkx_graph(copy=False), nbunch, w ith_labels, weights)8585 return networkx.clustering(self.networkx_graph(copy=False), nbunch, weights) 8592 8586 8593 8587 def cluster_transitivity(self): 8594 8588 r""" -
sage/graphs/graph.py
diff -r 5b338f2e484f -r 2f4945743e05 sage/graphs/graph.py
a b 2531 2531 sage: (graphs.ChvatalGraph()).centrality_betweenness() 2532 2532 {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} 2533 2533 sage: (graphs.ChvatalGraph()).centrality_betweenness(normalized=False) 2534 {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}2534 {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} 2535 2535 sage: D = DiGraph({0:[1,2,3], 1:[2], 3:[0,1]}) 2536 2536 sage: D.show(figsize=[2,2]) 2537 2537 sage: D = D.to_undirected() … … 2573 2573 1.0 2574 2574 """ 2575 2575 import networkx 2576 return networkx.degree_centrality(self.networkx_graph(copy=False), v) 2576 if v==None: 2577 return networkx.degree_centrality(self.networkx_graph(copy=False)) 2578 else: 2579 return networkx.degree_centrality(self.networkx_graph(copy=False))[v] 2577 2580 2578 2581 def centrality_closeness(self, v=None): 2579 2582 r""" … … 2870 2873 else: 2871 2874 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 2872 2875 2873 def cliques_number_of(self, vertices=None, cliques=None , with_labels=False):2876 def cliques_number_of(self, vertices=None, cliques=None): 2874 2877 """ 2875 Returns a list of the number of maximal cliques containing each 2876 vertex. (Returns a single value if only one input vertex). 2878 Returns a dictionary of the number of maximal cliques containing each 2879 vertex, keyed by vertex. (Returns a single value if 2880 only one input vertex). 2877 2881 2878 2882 .. NOTE:: 2879 2883 … … 2885 2889 - ``vertices`` - the vertices to inspect (default is 2886 2890 entire graph) 2887 2891 2888 - ``with_labels`` - (boolean) default False returns2889 list as above True returns a dictionary keyed by vertex labels2890 2891 2892 - ``cliques`` - list of cliques (if already 2892 2893 computed) 2893 2894 … … 2896 2897 2897 2898 sage: C = Graph('DJ{') 2898 2899 sage: C.cliques_number_of() 2899 [1, 1, 1, 1, 2]2900 {0: 1, 1: 1, 2: 1, 3: 1, 4: 2} 2900 2901 sage: E = C.cliques_maximal() 2901 2902 sage: E 2902 2903 [[4, 0], [4, 1, 2, 3]] 2903 2904 sage: C.cliques_number_of(cliques=E) 2904 [1, 1, 1, 1, 2]2905 {0: 1, 1: 1, 2: 1, 3: 1, 4: 2} 2905 2906 sage: F = graphs.Grid2dGraph(2,3) 2906 sage: X = F.cliques_number_of( with_labels=True)2907 sage: X = F.cliques_number_of() 2907 2908 sage: for v in sorted(X.iterkeys()): 2908 2909 ... print v, X[v] 2909 2910 (0, 0) 2 … … 2913 2914 (1, 1) 3 2914 2915 (1, 2) 2 2915 2916 sage: F.cliques_number_of(vertices=[(0, 1), (1, 2)]) 2916 [3, 2]2917 {(0, 1): 3, (1, 2): 2} 2917 2918 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 2918 2919 sage: G.show(figsize=[2,2]) 2919 2920 sage: G.cliques_number_of() 2920 [2, 2, 1, 1]2921 {0: 2, 1: 2, 2: 1, 3: 1} 2921 2922 """ 2922 2923 import networkx 2923 return networkx.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques , with_labels)2924 return networkx.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques) 2924 2925 2925 2926 def cliques_get_max_clique_graph(self, name=''): 2926 2927 """ … … 2996 2997 """ 2997 2998 from sage.graphs.cliquer import max_clique 2998 2999 return max_clique(self.complement()) 2999 3000 3000 3001 def cliques_vertex_clique_number(self, algorithm="cliquer", vertices=None, 3001 with_labels=False, cliques=None): 3002 r""" 3003 Returns a list of sizes of the largest maximal cliques containing 3004 each vertex. (Returns a single value if only one input vertex). 3002 cliques=None): 3003 """ 3004 Returns a dictionary of sizes of the largest maximal cliques containing 3005 each vertex, keyed by vertex. (Returns a single value if only one 3006 input vertex). 3005 3007 3006 3008 .. NOTE:: 3007 3009 … … 3020 3022 - ``vertices`` - the vertices to inspect (default is entire graph). 3021 3023 Ignored unless ``algorithm=='networkx'``. 3022 3024 3023 - ``with_labels`` - (boolean) default False returns list as above3024 True returns a dictionary keyed by vertex labels. Ignored unless3025 ``algorithm=='networkx'``.3026 3027 3025 - ``cliques`` - list of cliques (if already computed). Ignored unless 3028 3026 ``algorithm=='networkx'``. 3029 3027 … … 3031 3029 3032 3030 sage: C = Graph('DJ{') 3033 3031 sage: C.cliques_vertex_clique_number() 3034 [2, 4, 4, 4, 4]3032 {0: 2, 1: 4, 2: 4, 3: 4, 4: 4} 3035 3033 sage: E = C.cliques_maximal() 3036 3034 sage: E 3037 3035 [[4, 0], [4, 1, 2, 3]] 3038 3036 sage: C.cliques_vertex_clique_number(cliques=E,algorithm="networkx") 3039 [2, 4, 4, 4, 4]3037 {0: 2, 1: 4, 2: 4, 3: 4, 4: 4} 3040 3038 sage: F = graphs.Grid2dGraph(2,3) 3041 sage: X = F.cliques_vertex_clique_number( with_labels=True,algorithm="networkx")3039 sage: X = F.cliques_vertex_clique_number(algorithm="networkx") 3042 3040 sage: for v in sorted(X.iterkeys()): 3043 3041 ... print v, X[v] 3044 3042 (0, 0) 2 … … 3048 3046 (1, 1) 2 3049 3047 (1, 2) 2 3050 3048 sage: F.cliques_vertex_clique_number(vertices=[(0, 1), (1, 2)]) 3051 [2, 2]3049 {(0, 1): 2, (1, 2): 2} 3052 3050 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 3053 3051 sage: G.show(figsize=[2,2]) 3054 3052 sage: G.cliques_vertex_clique_number() 3055 [3, 3, 3, 3]3053 {0: 3, 1: 3, 2: 3, 3: 3} 3056 3054 3057 3055 """ 3058 3056 … … 3060 3058 from sage.graphs.cliquer import clique_number 3061 3059 if vertices==None: 3062 3060 vertices=self 3063 value= []3061 value={} 3064 3062 for v in vertices: 3065 value .append(1+clique_number(self.subgraph(self.neighbors(v))))3063 value[v] = 1+clique_number(self.subgraph(self.neighbors(v))) 3066 3064 self.subgraph(self.neighbors(v)).plot() 3067 3065 return value 3068 3066 elif algorithm=="networkx": 3069 3067 import networkx 3070 return networkx.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques)3068 return networkx.node_clique_number(self.networkx_graph(copy=False),vertices, cliques) 3071 3069 else: 3072 3070 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 3073 3071 3074 def cliques_containing_vertex(self, vertices=None, cliques=None , with_labels=False):3072 def cliques_containing_vertex(self, vertices=None, cliques=None): 3075 3073 """ 3076 Returns the cliques containing each vertex, represented as a list 3077 of lists. (Returns a single list if only one input vertex). 3074 Returns the cliques containing each vertex, represented as a dictionary 3075 of lists of lists, keyed by vertex. (Returns a single list if only one 3076 input vertex). 3078 3077 3079 3078 .. NOTE:: 3080 3079 … … 3086 3085 - ``vertices`` - the vertices to inspect (default is 3087 3086 entire graph) 3088 3087 3089 - ``with_labels`` - (boolean) default False returns3090 list as above True returns a dictionary keyed by vertex labels3091 3092 3088 - ``cliques`` - list of cliques (if already 3093 3089 computed) 3094 3090 … … 3096 3092 3097 3093 sage: C = Graph('DJ{') 3098 3094 sage: C.cliques_containing_vertex() 3099 [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 0], [4, 1, 2, 3]]]3095 {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]]} 3100 3096 sage: E = C.cliques_maximal() 3101 3097 sage: E 3102 3098 [[4, 0], [4, 1, 2, 3]] 3103 3099 sage: C.cliques_containing_vertex(cliques=E) 3104 [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 0], [4, 1, 2, 3]]]3100 {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]]} 3105 3101 sage: F = graphs.Grid2dGraph(2,3) 3106 sage: X = F.cliques_containing_vertex( with_labels=True)3102 sage: X = F.cliques_containing_vertex() 3107 3103 sage: for v in sorted(X.iterkeys()): 3108 3104 ... print v, X[v] 3109 3105 (0, 0) [[(0, 1), (0, 0)], [(1, 0), (0, 0)]] … … 3113 3109 (1, 1) [[(0, 1), (1, 1)], [(1, 2), (1, 1)], [(1, 0), (1, 1)]] 3114 3110 (1, 2) [[(1, 2), (0, 2)], [(1, 2), (1, 1)]] 3115 3111 sage: F.cliques_containing_vertex(vertices=[(0, 1), (1, 2)]) 3116 [[[(0, 1), (0, 0)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]], [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]]3112 {(0, 1): [[(0, 1), (0, 0)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]], (1, 2): [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]} 3117 3113 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 3118 3114 sage: G.show(figsize=[2,2]) 3119 3115 sage: G.cliques_containing_vertex() 3120 [[[0, 1, 2], [0, 1, 3]], [[0, 1, 2], [0, 1, 3]], [[0, 1, 2]], [[0, 1, 3]]]3116 {0: [[0, 1, 2], [0, 1, 3]], 1: [[0, 1, 2], [0, 1, 3]], 2: [[0, 1, 2]], 3: [[0, 1, 3]]} 3121 3117 3122 3118 """ 3123 3119 import networkx 3124 return networkx.cliques_containing_node(self.networkx_graph(copy=False), vertices, cliques, with_labels)3120 return networkx.cliques_containing_node(self.networkx_graph(copy=False),vertices, cliques) 3125 3121 3126 3122 def clique_complex(self): 3127 3123 """