Ticket #9567: 14602.patch
File 14602.patch, 16.1 KB (added by , 11 years ago) |
---|
-
sage/graphs/generic_graph.py
# HG changeset patch # User Ben Edwards <bedwards@cs.unm.edu> # Date 1280160661 21600 # Node ID 666a3309898bb1e20d28089c62a8dc8b79aa2141 # Parent 426be7b253ad9c3137e733aba26165a3de5b832f This is a set of patches to make sage work with the networkx-1.1 API. This includes the removal of the argument 'with_labels' to several function calls. This is because networkx as a default now returns keyed dictionairies as opposed to lists. The sage API was also changed to return similarly because it seems like a more intuitive way to report the results of function calls. diff -r 426be7b253ad -r 666a3309898b 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 dictionairy 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""" … … 8543 8540 8544 8541 def clustering_coeff(self, nbunch=None, with_labels=False, weights=False): 8545 8542 r""" 8546 Returns the clustering coefficient for each vertex in nbunch as a n8547 ordered list.8543 Returns the clustering coefficient for each vertex in nbunch as a 8544 dictionairy 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 426be7b253ad -r 666a3309898b sage/graphs/graph.py
a b 2409 2409 sage: (graphs.ChvatalGraph()).centrality_betweenness() 2410 2410 {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} 2411 2411 sage: (graphs.ChvatalGraph()).centrality_betweenness(normalized=False) 2412 {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}2412 {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} 2413 2413 sage: D = DiGraph({0:[1,2,3], 1:[2], 3:[0,1]}) 2414 2414 sage: D.show(figsize=[2,2]) 2415 2415 sage: D = D.to_undirected() … … 2447 2447 sage: D.show(figsize=[2,2]) 2448 2448 sage: D.centrality_degree() 2449 2449 {0: 1.0, 1: 1.0, 2: 0.66666666666666663, 3: 0.66666666666666663} 2450 sage: D.centrality_degree( v=1)2450 sage: D.centrality_degree(1) 2451 2451 1.0 2452 2452 """ 2453 2453 import networkx 2454 return networkx.degree_centrality(self.networkx_graph(copy=False), v) 2454 if v==None: 2455 return networkx.degree_centrality(self.networkx_graph(copy=False)) 2456 else: 2457 return networkx.degree_centrality(self.networkx_graph(copy=False))[v] 2455 2458 2456 2459 def centrality_closeness(self, v=None): 2457 2460 r""" … … 2748 2751 else: 2749 2752 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 2750 2753 2751 def cliques_number_of(self, vertices=None, cliques=None , with_labels=False):2754 def cliques_number_of(self, vertices=None, cliques=None): 2752 2755 """ 2753 Returns a list of the number of maximal cliques containing each 2754 vertex. (Returns a single value if only one input vertex). 2756 Returns a dictionairy of the number of maximal cliques containing each 2757 vertex, keyed by vertex. (Returns a single value if 2758 only one input vertex). 2755 2759 2756 2760 .. NOTE:: 2757 2761 … … 2763 2767 - ``vertices`` - the vertices to inspect (default is 2764 2768 entire graph) 2765 2769 2766 - ``with_labels`` - (boolean) default False returns2767 list as above True returns a dictionary keyed by vertex labels2768 2769 2770 - ``cliques`` - list of cliques (if already 2770 2771 computed) 2771 2772 … … 2774 2775 2775 2776 sage: C = Graph('DJ{') 2776 2777 sage: C.cliques_number_of() 2777 [1, 1, 1, 1, 2]2778 {0: 1, 1: 1, 2: 1, 3: 1, 4: 2} 2778 2779 sage: E = C.cliques_maximal() 2779 2780 sage: E 2780 2781 [[4, 0], [4, 1, 2, 3]] 2781 2782 sage: C.cliques_number_of(cliques=E) 2782 [1, 1, 1, 1, 2]2783 {0: 1, 1: 1, 2: 1, 3: 1, 4: 2} 2783 2784 sage: F = graphs.Grid2dGraph(2,3) 2784 sage: X = F.cliques_number_of( with_labels=True)2785 sage: X = F.cliques_number_of() 2785 2786 sage: for v in sorted(X.iterkeys()): 2786 2787 ... print v, X[v] 2787 2788 (0, 0) 2 … … 2791 2792 (1, 1) 3 2792 2793 (1, 2) 2 2793 2794 sage: F.cliques_number_of(vertices=[(0, 1), (1, 2)]) 2794 [3, 2]2795 {(0, 1): 3, (1, 2): 2} 2795 2796 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 2796 2797 sage: G.show(figsize=[2,2]) 2797 2798 sage: G.cliques_number_of() 2798 [2, 2, 1, 1]2799 {0: 2, 1: 2, 2: 1, 3: 1} 2799 2800 """ 2800 2801 import networkx 2801 return networkx.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques , with_labels)2802 return networkx.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques) 2802 2803 2803 2804 def cliques_get_max_clique_graph(self, name=''): 2804 2805 """ … … 2876 2877 return max_clique(self.complement()) 2877 2878 2878 2879 def cliques_vertex_clique_number(self, algorithm="cliquer", vertices=None, 2879 with_labels=False, cliques=None): 2880 r""" 2881 Returns a list of sizes of the largest maximal cliques containing 2882 each vertex. (Returns a single value if only one input vertex). 2880 cliques=None): 2881 """ 2882 Returns a dictionary of sizes of the largest maximal cliques containing 2883 each vertex, keyed by vertex. (Returns a single value if only one 2884 input vertex). 2883 2885 2884 2886 .. NOTE:: 2885 2887 … … 2898 2900 - ``vertices`` - the vertices to inspect (default is entire graph). 2899 2901 Ignored unless ``algorithm=='networkx'``. 2900 2902 2901 - ``with_labels`` - (boolean) default False returns list as above2902 True returns a dictionary keyed by vertex labels. Ignored unless2903 ``algorithm=='networkx'``.2904 2905 2903 - ``cliques`` - list of cliques (if already computed). Ignored unless 2906 2904 ``algorithm=='networkx'``. 2907 2905 … … 2909 2907 2910 2908 sage: C = Graph('DJ{') 2911 2909 sage: C.cliques_vertex_clique_number() 2912 [2, 4, 4, 4, 4]2910 {0: 2, 1: 4, 2: 4, 3: 4, 4: 4} 2913 2911 sage: E = C.cliques_maximal() 2914 2912 sage: E 2915 2913 [[4, 0], [4, 1, 2, 3]] 2916 2914 sage: C.cliques_vertex_clique_number(cliques=E,algorithm="networkx") 2917 [2, 4, 4, 4, 4]2915 {0: 2, 1: 4, 2: 4, 3: 4, 4: 4} 2918 2916 sage: F = graphs.Grid2dGraph(2,3) 2919 sage: X = F.cliques_vertex_clique_number( with_labels=True,algorithm="networkx")2917 sage: X = F.cliques_vertex_clique_number(algorithm="networkx") 2920 2918 sage: for v in sorted(X.iterkeys()): 2921 2919 ... print v, X[v] 2922 2920 (0, 0) 2 … … 2926 2924 (1, 1) 2 2927 2925 (1, 2) 2 2928 2926 sage: F.cliques_vertex_clique_number(vertices=[(0, 1), (1, 2)]) 2929 [2, 2]2927 {(0, 1): 2, (1, 2): 2} 2930 2928 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 2931 2929 sage: G.show(figsize=[2,2]) 2932 2930 sage: G.cliques_vertex_clique_number() 2933 [3, 3, 3, 3]2931 {0: 3, 1: 3, 2: 3, 3: 3} 2934 2932 2935 2933 """ 2936 2934 … … 2938 2936 from sage.graphs.cliquer import clique_number 2939 2937 if vertices==None: 2940 2938 vertices=self 2941 value= []2939 value={} 2942 2940 for v in vertices: 2943 value .append(1+clique_number(self.subgraph(self.neighbors(v))))2941 value[v] = 1+clique_number(self.subgraph(self.neighbors(v))) 2944 2942 self.subgraph(self.neighbors(v)).plot() 2945 2943 return value 2946 2944 elif algorithm=="networkx": 2947 2945 import networkx 2948 return networkx.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques)2946 return networkx.node_clique_number(self.networkx_graph(copy=False),vertices, cliques) 2949 2947 else: 2950 2948 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 2951 2949 2952 def cliques_containing_vertex(self, vertices=None, cliques=None , with_labels=False):2950 def cliques_containing_vertex(self, vertices=None, cliques=None): 2953 2951 """ 2954 Returns the cliques containing each vertex, represented as a list 2955 of lists. (Returns a single list if only one input vertex). 2952 Returns the cliques containing each vertex, represented as a dictionairy 2953 of lists of lists, keyed by vertex. (Returns a single list if only one 2954 input vertex). 2956 2955 2957 2956 .. NOTE:: 2958 2957 … … 2964 2963 - ``vertices`` - the vertices to inspect (default is 2965 2964 entire graph) 2966 2965 2967 - ``with_labels`` - (boolean) default False returns2968 list as above True returns a dictionary keyed by vertex labels2969 2970 2966 - ``cliques`` - list of cliques (if already 2971 2967 computed) 2972 2968 … … 2974 2970 2975 2971 sage: C = Graph('DJ{') 2976 2972 sage: C.cliques_containing_vertex() 2977 [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 0], [4, 1, 2, 3]]]2973 {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]]} 2978 2974 sage: E = C.cliques_maximal() 2979 2975 sage: E 2980 2976 [[4, 0], [4, 1, 2, 3]] 2981 2977 sage: C.cliques_containing_vertex(cliques=E) 2982 [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 0], [4, 1, 2, 3]]]2978 {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]]} 2983 2979 sage: F = graphs.Grid2dGraph(2,3) 2984 sage: X = F.cliques_containing_vertex( with_labels=True)2980 sage: X = F.cliques_containing_vertex() 2985 2981 sage: for v in sorted(X.iterkeys()): 2986 2982 ... print v, X[v] 2987 2983 (0, 0) [[(0, 1), (0, 0)], [(1, 0), (0, 0)]] … … 2991 2987 (1, 1) [[(0, 1), (1, 1)], [(1, 2), (1, 1)], [(1, 0), (1, 1)]] 2992 2988 (1, 2) [[(1, 2), (0, 2)], [(1, 2), (1, 1)]] 2993 2989 sage: F.cliques_containing_vertex(vertices=[(0, 1), (1, 2)]) 2994 [[[(0, 1), (0, 0)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]], [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]]2990 {(0, 1): [[(0, 1), (0, 0)], [(0, 1), (0, 2)], [(0, 1), (1, 1)]], (1, 2): [[(1, 2), (0, 2)], [(1, 2), (1, 1)]]} 2995 2991 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 2996 2992 sage: G.show(figsize=[2,2]) 2997 2993 sage: G.cliques_containing_vertex() 2998 [[[0, 1, 2], [0, 1, 3]], [[0, 1, 2], [0, 1, 3]], [[0, 1, 2]], [[0, 1, 3]]]2994 {0: [[0, 1, 2], [0, 1, 3]], 1: [[0, 1, 2], [0, 1, 3]], 2: [[0, 1, 2]], 3: [[0, 1, 3]]} 2999 2995 3000 2996 """ 3001 2997 import networkx 3002 return networkx.cliques_containing_node(self.networkx_graph(copy=False), vertices, cliques, with_labels)2998 return networkx.cliques_containing_node(self.networkx_graph(copy=False),vertices, cliques) 3003 2999 3004 3000 def clique_complex(self): 3005 3001 """