Ticket #5793: cliquer-5-ref.patch
File cliquer-5-ref.patch, 22.1 KB (added by , 14 years ago) |
---|
-
sage/graphs/cliquer.pyx
# HG changeset patch # User Robert L. Miller <rlm@rlmiller.org> # Date 1248202547 25200 # Node ID 121192e56a226dd67175a76683f8b255deb7e62b # Parent bd5ab662f750d41d98b34246bd0babf24d2423b4 Editing for trac #5793 diff -r bd5ab662f750 -r 121192e56a22 sage/graphs/cliquer.pyx
a b 94 94 95 95 EXAMPLES:: 96 96 97 sage: C = Graph('DJ{')98 sage: clique_number(C)99 4100 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})101 sage: G.show(figsize=[2,2])102 sage: clique_number(G)103 397 sage: C = Graph('DJ{') 98 sage: clique_number(C) 99 4 100 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 101 sage: G.show(figsize=[2,2]) 102 sage: clique_number(G) 103 3 104 104 """ 105 105 graph=graph.relabel(inplace=False) 106 106 cdef graph_t *g … … 116 116 117 117 118 118 def list_composition(a,b): 119 """ 120 Composes a list ``a`` with a map ``b``. 121 122 EXAMPLE:: 123 124 sage: from sage.graphs.cliquer import list_composition 125 sage: list_composition([1,3,'a'], {'a':'b', 1:2, 2:3, 3:4}) 126 [2, 4, 'b'] 127 128 """ 119 129 value=[] 120 130 for i in a: 121 131 value.append(b[i]) -
sage/graphs/graph.py
diff -r bd5ab662f750 -r 121192e56a22 sage/graphs/graph.py
a b 9755 9755 9756 9756 ### Cliques 9757 9757 9758 def cliques(self): 9759 """ 9760 Returns the list of maximal cliques. Each maximal clique is 9761 represented by a list of vertices. 9762 9763 Currently only implemented for undirected graphs. Use 9764 to_undirected to convert a digraph to an undirected graph. 9765 9766 Maximal cliques are the largest complete subgraphs containing a 9767 given point. This function is based on Networkx's implementation of 9768 the Bron and Kerbosch Algorithm, [1]. 9769 9758 def cliques_maximal(self): 9759 """ 9760 Returns the list of all maximal cliques, with each clique represented 9761 by a list of vertices. A clique is an induced complete subgraph, and a 9762 maximal clique is one not contained in a larger one. 9763 9764 NOTES: 9765 9766 - Currently only implemented for undirected graphs. Use to_undirected 9767 to convert a digraph to an undirected graph. 9768 9769 ALGORITHM: 9770 9771 - This function is based on Networkx's implementation of the Bron and 9772 Kerbosch Algorithm, [1]. 9773 9770 9774 REFERENCE: 9771 9775 9772 9776 - [1] Coen Bron and Joep Kerbosch. (1973). Algorithm 457: … … 9776 9780 9777 9781 EXAMPLES:: 9778 9782 9779 sage: (graphs.ChvatalGraph()).cliques()9783 sage: graphs.ChvatalGraph().cliques_maximal() 9780 9784 [[0, 1], [0, 4], [0, 6], [0, 9], [2, 1], [2, 3], [2, 6], [2, 8], [3, 4], [3, 7], [3, 9], [5, 1], [5, 4], [5, 10], [5, 11], [7, 1], [7, 8], [7, 11], [8, 4], [8, 10], [10, 6], [10, 9], [11, 6], [11, 9]] 9781 9785 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 9782 9786 sage: G.show(figsize=[2,2]) 9783 sage: G.cliques ()9787 sage: G.cliques_maximal() 9784 9788 [[0, 1, 2], [0, 1, 3]] 9789 sage: C=graphs.PetersenGraph() 9790 sage: C.cliques_maximal() 9791 [[0, 1], [0, 4], [0, 5], [2, 1], [2, 3], [2, 7], [3, 4], [3, 8], [6, 1], [6, 8], [6, 9], [7, 5], [7, 9], [8, 5], [9, 4]] 9792 sage: C = Graph('DJ{') 9793 sage: C.cliques_maximal() 9794 [[4, 1, 2, 3], [4, 0]] 9795 9785 9796 """ 9786 9797 import networkx.cliques 9787 9798 return networkx.cliques.find_cliques(self.networkx_graph(copy=False)) 9788 9799 9800 def cliques(self): 9801 """ 9802 (Deprecated) alias for ``cliques_maximal``. See that function for more 9803 details. 9804 9805 EXAMPLE:: 9806 9807 sage: C = Graph('DJ{') 9808 sage: C.cliques() 9809 doctest:...: DeprecationWarning: The function 'cliques' has been deprecated. Use 'cliques_maximal' or 'cliques_maximum'. 9810 [[4, 1, 2, 3], [4, 0]] 9811 9812 """ 9813 from sage.misc.misc import deprecation 9814 deprecation("The function 'cliques' has been deprecated. Use " + \ 9815 "'cliques_maximal' or 'cliques_maximum'.") 9816 return self.cliques_maximal() 9817 9818 def cliques_maximum(self): 9819 """ 9820 Returns the list of all maximum cliques, with each clique represented 9821 by a list of vertices. A clique is an induced complete subgraph, and a 9822 maximum clique is one of maximal order. 9823 9824 NOTES: 9825 9826 - Currently only implemented for undirected graphs. Use to_undirected 9827 to convert a digraph to an undirected graph. 9828 9829 ALGORITHM: 9830 9831 - This function is based on Cliquer, [1]. 9832 9833 REFERENCE: 9834 9835 - [1] Sampo Niskanen and Patric R. J. Östergård, "Cliquer User's Guide, 9836 Version 1.0," Communications Laboratory, Helsinki University of 9837 Technology, Espoo, Finland, Tech. Rep. T48, 2003. 9838 9839 EXAMPLES:: 9840 9841 sage: graphs.ChvatalGraph().cliques_maximum() 9842 [[0, 1], [0, 4], [0, 6], [0, 9], [1, 2], [1, 5], [1, 7], [2, 3], [2, 6], [2, 8], [3, 4], [3, 7], [3, 9], [4, 5], [4, 8], [5, 10], [5, 11], [6, 10], [6, 11], [7, 8], [7, 11], [8, 10], [9, 10], [9, 11]] 9843 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 9844 sage: G.show(figsize=[2,2]) 9845 sage: G.cliques_maximum() 9846 [[0, 1, 2], [0, 1, 3]] 9847 sage: C=graphs.PetersenGraph() 9848 sage: C.cliques_maximum() 9849 [[0, 1], [0, 4], [0, 5], [1, 2], [1, 6], [2, 3], [2, 7], [3, 4], [3, 8], [4, 9], [5, 7], [5, 8], [6, 8], [6, 9], [7, 9]] 9850 sage: C = Graph('DJ{') 9851 sage: C.cliques_maximum() 9852 [[1, 2, 3, 4]] 9853 9854 """ 9855 from sage.graphs.cliquer import all_max_clique 9856 return sorted(all_max_clique(self)) 9857 9858 def clique_maximum(self): 9859 """ 9860 Returns the vertex set of a maximal order complete subgraph. 9861 9862 NOTE: 9863 9864 - Currently only implemented for undirected graphs. Use to_undirected 9865 to convert a digraph to an undirected graph. 9866 9867 ALGORITHM: 9868 9869 - This function is based on Cliquer, [1]. 9870 9871 REFERENCE: 9872 9873 - [1] Sampo Niskanen and Patric R. J. Östergård, "Cliquer User's Guide, 9874 Version 1.0," Communications Laboratory, Helsinki University of 9875 Technology, Espoo, Finland, Tech. Rep. T48, 2003. 9876 9877 EXAMPLES:: 9878 9879 sage: C=graphs.PetersenGraph() 9880 sage: C.clique_maximum() 9881 [7, 9] 9882 sage: C = Graph('DJ{') 9883 sage: C.clique_maximum() 9884 [1, 2, 3, 4] 9885 9886 """ 9887 from sage.graphs.cliquer import max_clique 9888 return max_clique(self) 9889 9890 def clique_number(self, algorithm="cliquer", cliques=None): 9891 """ 9892 Returns the order of the largest clique of the graph (the clique 9893 number). 9894 9895 NOTE: 9896 9897 - Currently only implemented for undirected graphs. Use to_undirected 9898 to convert a digraph to an undirected graph. 9899 9900 INPUT: 9901 9902 - ``algorithm`` - either ``cliquer`` or ``networkx`` 9903 9904 - ``cliquer`` - This wraps the C program Cliquer, [1]. 9905 9906 - ``networkx`` - This function is based on Networkx's implementation 9907 of the Bron and Kerbosch Algorithm, [2]. 9908 9909 - ``cliques'' - an optional list of cliques that can be input if 9910 already computed. Ignored unless ``algorithm=='networkx'``. 9911 9912 REFERENCE: 9913 9914 - [1] Sampo Niskanen and Patric R. J. Östergård, "Cliquer User's Guide, 9915 Version 1.0," Communications Laboratory, Helsinki University of 9916 Technology, Espoo, Finland, Tech. Rep. T48, 2003. 9917 9918 - [2] Coen Bron and Joep Kerbosch. (1973). Algorithm 457: 9919 Finding All Cliques of an Undirected Graph. Commun. ACM. v 9920 16. n 9. pages 575-577. ACM Press. [Online] Available: 9921 http://www.ram.org/computing/rambin/rambin.html 9922 9923 EXAMPLES:: 9924 9925 sage: C = Graph('DJ{') 9926 sage: C.clique_number() 9927 4 9928 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 9929 sage: G.show(figsize=[2,2]) 9930 sage: G.clique_number() 9931 3 9932 9933 """ 9934 if algorithm=="cliquer": 9935 from sage.graphs.cliquer import clique_number 9936 return clique_number(self) 9937 elif algorithm=="networkx": 9938 import networkx.cliques 9939 return networkx.cliques.graph_clique_number(self.networkx_graph(copy=False),cliques) 9940 else: 9941 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 9942 9943 def cliques_number_of(self, vertices=None, cliques=None, with_labels=False): 9944 """ 9945 Returns a list of the number of maximal cliques containing each 9946 vertex. (Returns a single value if only one input vertex). 9947 9948 NOTES: 9949 9950 - Currently only implemented for undirected graphs. Use to_undirected 9951 to convert a digraph to an undirected graph. 9952 9953 INPUT: 9954 9955 - ``vertices`` - the vertices to inspect (default is 9956 entire graph) 9957 9958 - ``with_labels`` - (boolean) default False returns 9959 list as above True returns a dictionary keyed by vertex labels 9960 9961 - ``cliques`` - list of cliques (if already 9962 computed) 9963 9964 9965 EXAMPLES:: 9966 9967 sage: C = Graph('DJ{') 9968 sage: C.cliques_number_of() 9969 [1, 1, 1, 1, 2] 9970 sage: E = C.cliques_maximal() 9971 sage: E 9972 [[4, 1, 2, 3], [4, 0]] 9973 sage: C.cliques_number_of(cliques=E) 9974 [1, 1, 1, 1, 2] 9975 sage: F = graphs.Grid2dGraph(2,3) 9976 sage: X = F.cliques_number_of(with_labels=True) 9977 sage: for v in sorted(X.iterkeys()): 9978 ... print v, X[v] 9979 (0, 0) 2 9980 (0, 1) 3 9981 (0, 2) 2 9982 (1, 0) 2 9983 (1, 1) 3 9984 (1, 2) 2 9985 sage: F.cliques_number_of(vertices=[(0, 1), (1, 2)]) 9986 [3, 2] 9987 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 9988 sage: G.show(figsize=[2,2]) 9989 sage: G.cliques_number_of() 9990 [2, 2, 1, 1] 9991 """ 9992 import networkx.cliques 9993 return networkx.cliques.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques, with_labels) 9994 9789 9995 def cliques_get_max_clique_graph(self, name=''): 9790 9996 """ 9791 9997 Returns a graph constructed with maximal cliques as vertices, and 9792 9998 edges between maximal cliques with common members in the original 9793 9999 graph. 9794 10000 9795 Currently only implemented for undirected graphs. Use 9796 to_undirected to convert a digraph to an undirected graph. 9797 9798 INPUT: 9799 10001 NOTES: 10002 10003 - Currently only implemented for undirected graphs. Use to_undirected 10004 to convert a digraph to an undirected graph. 10005 10006 INPUT: 9800 10007 9801 10008 - ``name`` - The name of the new graph. 9802 10009 9803 9804 10010 EXAMPLES:: 9805 10011 9806 10012 sage: (graphs.ChvatalGraph()).cliques_get_max_clique_graph() … … 9817 10023 9818 10024 def cliques_get_clique_bipartite(self, **kwds): 9819 10025 """ 9820 Returns a bipartite graph constructed such that cliques are the10026 Returns a bipartite graph constructed such that maximal cliques are the 9821 10027 right vertices and the left vertices are retained from the given 9822 10028 graph. Right and left vertices are connected if the bottom vertex 9823 10029 belongs to the clique represented by a top vertex. 9824 10030 9825 Currently only implemented for undirected graphs. Use 9826 to_undirected to convert a digraph to an undirected graph. 10031 NOTES: 10032 10033 - Currently only implemented for undirected graphs. Use to_undirected 10034 to convert a digraph to an undirected graph. 9827 10035 9828 10036 EXAMPLES:: 9829 10037 … … 9840 10048 from bipartite_graph import BipartiteGraph 9841 10049 return BipartiteGraph(networkx.cliques.make_clique_bipartite(self.networkx_graph(copy=False), **kwds)) 9842 10050 9843 9844 def clique_number(self,algorithm="cliquer",cliques=None): 9845 """ 9846 Returns the size of the largest clique of the graph (clique 9847 number). By default, the Cliquer algorithm is used 9848 ( algorithm="cliquer" ) but you can also chose the networkx 9849 algorithm instead ( algorithm="networkx" ) 9850 9851 INPUT : 9852 9853 - ``cliques'' : This parameter is an optional list of cliques that can be input if already computed. 9854 ONLY USED BY NetworkX !!! 9855 9856 Currently only implemented for undirected graphs. Use 9857 to_undirected to convert a digraph to an undirected graph. 9858 9859 EXAMPLES:: 9860 9861 sage: C = Graph('DJ{') 9862 sage: C.clique_number() 9863 4 9864 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) 9865 sage: G.show(figsize=[2,2]) 9866 sage: G.clique_number() 9867 3 9868 """ 9869 if algorithm=="cliquer": 9870 from sage.graphs.cliquer import clique_number 9871 return clique_number(self) 9872 elif algorithm=="networkx": 9873 import networkx.cliques 9874 return networkx.cliques.graph_clique_number(self.networkx_graph(copy=False),cliques) 9875 else: 9876 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 9877 9878 9879 def maximum_clique(self): 9880 """ 9881 Returns the vertex set of a maximum complete subgraph. 9882 9883 Currently only implemented for undirected graphs. Use 9884 to_undirected to convert a digraph to an undirected graph. 10051 def independent_set(self): 10052 """ 10053 Returns a maximal independent set, which is a set of vertices which 10054 induces an empty subgraph. Uses Cliquer. 10055 10056 NOTES: 10057 10058 - Currently only implemented for undirected graphs. Use to_undirected 10059 to convert a digraph to an undirected graph. 9885 10060 9886 10061 EXAMPLES:: 9887 10062 9888 10063 sage: C=graphs.PetersenGraph() 9889 sage: C.maximum_clique() 9890 [7, 9] 9891 """ 9892 from sage.graphs.cliquer import max_clique 9893 return max_clique(self) 9894 9895 def maximum_independent_set(self): 9896 """ 9897 Returns the vertex set of a independent (stable) subgraph. 9898 9899 Currently only implemented for undirected graphs. Use 9900 to_undirected to convert a digraph to an undirected graph. 9901 9902 EXAMPLES:: 9903 9904 sage: C=graphs.PetersenGraph() 9905 sage: C.maximum_independent_set() 10064 sage: C.independent_set() 9906 10065 [0, 3, 6, 7] 9907 10066 9908 10067 """ 9909 10068 from sage.graphs.cliquer import max_clique 9910 10069 return max_clique(self.complement()) 9911 10070 9912 def maximum_cliques(self): 9913 """ 9914 Returns the vertex sets of *ALL* the maximum complete subgraphs. 9915 9916 Currently only implemented for undirected graphs. Use 9917 to_undirected to convert a digraph to an undirected graph. 9918 9919 EXAMPLES:: 9920 9921 sage: C=graphs.PetersenGraph() 9922 sage: C.maximum_cliques() 9923 [[2, 7], [7, 9], [6, 8], [6, 9], [0, 4], [4, 9], [5, 7], [0, 5], [5, 8], [3, 4], [2, 3], [3, 8], [1, 6], [0, 1], [1, 2]] 9924 """ 9925 9926 from sage.graphs.cliquer import all_max_clique 9927 return all_max_clique(self) 9928 9929 def cliques_vertex_clique_number(self, algorithm="cliquer",vertices=None, with_labels=False, cliques=None): 10071 def cliques_vertex_clique_number(self, algorithm="cliquer", vertices=None, 10072 with_labels=False, cliques=None): 9930 10073 r""" 9931 10074 Returns a list of sizes of the largest maximal cliques containing 9932 10075 each vertex. (Returns a single value if only one input vertex). 9933 10076 9934 Currently only implemented for undirected graphs. Use 9935 to_undirected to convert a digraph to an undirected graph. 9936 9937 INPUT: 9938 9939 - ``algorithm'' - "cliquer" or "networkx" 9940 9941 - ``vertices`` - the vertices to inspect (default is 9942 entire graph) 9943 ONLY USED BY NetworkX !!! 9944 9945 - ``with_labels`` - (boolean) default False returns 9946 list as above True returns a dictionary keyed by vertex labels 9947 ONLY USED BY NetworkX !!! 9948 9949 - ``cliques`` - list of cliques (if already 9950 computed) 9951 ONLY USED BY NetworkX !!! 10077 NOTES: 10078 10079 - Currently only implemented for undirected graphs. Use to_undirected 10080 to convert a digraph to an undirected graph. 10081 10082 INPUT: 10083 10084 - ``algorithm`` - either ``cliquer`` or ``networkx`` 10085 10086 - ``cliquer`` - This wraps the C program Cliquer, [1]. 10087 10088 - ``networkx`` - This function is based on Networkx's implementation 10089 of the Bron and Kerbosch Algorithm, [2]. 10090 10091 - ``vertices`` - the vertices to inspect (default is entire graph). 10092 Ignored unless ``algorithm=='networkx'``. 10093 10094 - ``with_labels`` - (boolean) default False returns list as above 10095 True returns a dictionary keyed by vertex labels. Ignored unless 10096 ``algorithm=='networkx'``. 10097 10098 - ``cliques`` - list of cliques (if already computed). Ignored unless 10099 ``algorithm=='networkx'``. 9952 10100 9953 10101 EXAMPLES:: 9954 10102 9955 10103 sage: C = Graph('DJ{') 9956 10104 sage: C.cliques_vertex_clique_number() 9957 10105 [2, 4, 4, 4, 4] 9958 sage: E = C.cliques ()10106 sage: E = C.cliques_maximal() 9959 10107 sage: E 9960 10108 [[4, 1, 2, 3], [4, 0]] 9961 10109 sage: C.cliques_vertex_clique_number(cliques=E,algorithm="networkx") … … 9976 10124 sage: G.show(figsize=[2,2]) 9977 10125 sage: G.cliques_vertex_clique_number() 9978 10126 [3, 3, 3, 3] 10127 9979 10128 """ 9980 10129 9981 10130 if algorithm=="cliquer": … … 9993 10142 else: 9994 10143 raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") 9995 10144 9996 9997 def cliques_number_of(self, vertices=None, cliques=None, with_labels=False):9998 """9999 Returns a list of the number of maximal cliques containing each10000 vertex. (Returns a single value if only one input vertex).10001 10002 Currently only implemented for undirected graphs. Use10003 to_undirected to convert a digraph to an undirected graph.10004 10005 INPUT:10006 10145 def cliques_containing_vertex(self, vertices=None, cliques=None, with_labels=False): 10146 """ 10147 Returns the cliques containing each vertex, represented as a list 10148 of lists. (Returns a single list if only one input vertex). 10149 10150 NOTE: 10151 10152 - Currently only implemented for undirected graphs. Use to_undirected 10153 to convert a digraph to an undirected graph. 10154 10155 INPUT: 10007 10156 10008 10157 - ``vertices`` - the vertices to inspect (default is 10009 10158 entire graph) … … 10014 10163 - ``cliques`` - list of cliques (if already 10015 10164 computed) 10016 10165 10017 10018 EXAMPLES::10019 10020 sage: C = Graph('DJ{')10021 sage: C.cliques_number_of()10022 [1, 1, 1, 1, 2]10023 sage: E = C.cliques()10024 sage: E10025 [[4, 1, 2, 3], [4, 0]]10026 sage: C.cliques_number_of(cliques=E)10027 [1, 1, 1, 1, 2]10028 sage: F = graphs.Grid2dGraph(2,3)10029 sage: X = F.cliques_number_of(with_labels=True)10030 sage: for v in sorted(X.iterkeys()):10031 ... print v, X[v]10032 (0, 0) 210033 (0, 1) 310034 (0, 2) 210035 (1, 0) 210036 (1, 1) 310037 (1, 2) 210038 sage: F.cliques_number_of(vertices=[(0, 1), (1, 2)])10039 [3, 2]10040 sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]})10041 sage: G.show(figsize=[2,2])10042 sage: G.cliques_number_of()10043 [2, 2, 1, 1]10044 """10045 import networkx.cliques10046 return networkx.cliques.number_of_cliques(self.networkx_graph(copy=False), vertices, cliques, with_labels)10047 10048 def cliques_containing_vertex(self, vertices=None, cliques=None, with_labels=False):10049 """10050 Returns the cliques containing each vertex, represented as a list10051 of lists. (Returns a single list if only one input vertex).10052 10053 Currently only implemented for undirected graphs. Use10054 to_undirected to convert a digraph to an undirected graph.10055 10056 INPUT:10057 10058 10059 - ``vertices`` - the vertices to inspect (default is10060 entire graph)10061 10062 - ``with_labels`` - (boolean) default False returns10063 list as above True returns a dictionary keyed by vertex labels10064 10065 - ``cliques`` - list of cliques (if already10066 computed)10067 10068 10069 10166 EXAMPLES:: 10070 10167 10071 10168 sage: C = Graph('DJ{') 10072 10169 sage: C.cliques_containing_vertex() 10073 10170 [[[4, 0]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3]], [[4, 1, 2, 3], [4, 0]]] 10074 sage: E = C.cliques ()10171 sage: E = C.cliques_maximal() 10075 10172 sage: E 10076 10173 [[4, 1, 2, 3], [4, 0]] 10077 10174 sage: C.cliques_containing_vertex(cliques=E) … … 10092 10189 sage: G.show(figsize=[2,2]) 10093 10190 sage: G.cliques_containing_vertex() 10094 10191 [[[0, 1, 2], [0, 1, 3]], [[0, 1, 2], [0, 1, 3]], [[0, 1, 2]], [[0, 1, 3]]] 10192 10095 10193 """ 10096 10194 import networkx.cliques 10097 10195 return networkx.cliques.cliques_containing_node(self.networkx_graph(copy=False), vertices, cliques, with_labels)