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]) |
9789 | | def cliques_get_max_clique_graph(self, name=''): |
9790 | | """ |
9791 | | Returns a graph constructed with maximal cliques as vertices, and |
9792 | | edges between maximal cliques with common members in the original |
9793 | | graph. |
9794 | | |
9795 | | Currently only implemented for undirected graphs. Use |
9796 | | to_undirected to convert a digraph to an undirected graph. |
9797 | | |
9798 | | INPUT: |
9799 | | |
9800 | | |
9801 | | - ``name`` - The name of the new graph. |
9802 | | |
9803 | | |
9804 | | EXAMPLES:: |
9805 | | |
9806 | | sage: (graphs.ChvatalGraph()).cliques_get_max_clique_graph() |
9807 | | Graph on 24 vertices |
9808 | | sage: ((graphs.ChvatalGraph()).cliques_get_max_clique_graph()).show(figsize=[2,2], vertex_size=20, vertex_labels=False) |
| 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]] |
9811 | | sage: G.cliques_get_max_clique_graph() |
9812 | | Graph on 2 vertices |
9813 | | sage: (G.cliques_get_max_clique_graph()).show(figsize=[2,2]) |
9814 | | """ |
9815 | | import networkx.cliques |
9816 | | return Graph(networkx.cliques.make_max_clique_graph(self.networkx_graph(copy=False), name=name, create_using=networkx.xgraph.XGraph())) |
9817 | | |
9818 | | def cliques_get_clique_bipartite(self, **kwds): |
9819 | | """ |
9820 | | Returns a bipartite graph constructed such that cliques are the |
9821 | | right vertices and the left vertices are retained from the given |
9822 | | graph. Right and left vertices are connected if the bottom vertex |
9823 | | belongs to the clique represented by a top vertex. |
9824 | | |
9825 | | Currently only implemented for undirected graphs. Use |
9826 | | to_undirected to convert a digraph to an undirected graph. |
9827 | | |
9828 | | EXAMPLES:: |
9829 | | |
9830 | | sage: (graphs.ChvatalGraph()).cliques_get_clique_bipartite() |
9831 | | Bipartite graph on 36 vertices |
9832 | | sage: ((graphs.ChvatalGraph()).cliques_get_clique_bipartite()).show(figsize=[2,2], vertex_size=20, vertex_labels=False) |
9833 | | sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) |
9834 | | sage: G.show(figsize=[2,2]) |
9835 | | sage: G.cliques_get_clique_bipartite() |
9836 | | Bipartite graph on 6 vertices |
9837 | | sage: (G.cliques_get_clique_bipartite()).show(figsize=[2,2]) |
9838 | | """ |
9839 | | import networkx.cliques |
9840 | | from bipartite_graph import BipartiteGraph |
9841 | | return BipartiteGraph(networkx.cliques.make_clique_bipartite(self.networkx_graph(copy=False), **kwds)) |
9842 | | |
9843 | | def clique_number(self, cliques=None): |
9844 | | """ |
9845 | | Returns the size of the largest clique of the graph (clique |
| 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 |
9847 | | |
9848 | | Currently only implemented for undirected graphs. Use |
9849 | | to_undirected to convert a digraph to an undirected graph. |
9850 | | |
9851 | | INPUT: |
9852 | | |
9853 | | |
9854 | | - ``cliques`` - list of cliques (if already |
9855 | | computed) |
9856 | | |
| 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 |
9872 | | """ |
9873 | | import networkx.cliques |
9874 | | return networkx.cliques.graph_clique_number(self.networkx_graph(copy=False), cliques) |
9875 | | |
9876 | | def cliques_vertex_clique_number(self, vertices=None, with_labels=False, cliques=None): |
9877 | | r""" |
9878 | | Returns a list of sizes of the largest maximal cliques containing |
9879 | | each vertex. (Returns a single value if only one input vertex). |
9880 | | |
9881 | | Currently only implemented for undirected graphs. Use |
9882 | | to_undirected to convert a digraph to an undirected graph. |
9883 | | |
9884 | | INPUT: |
9885 | | |
9886 | | |
9887 | | - ``vertices`` - the vertices to inspect (default is |
9888 | | entire graph) |
9889 | | |
9890 | | - ``with_labels`` - (boolean) default False returns |
9891 | | list as above True returns a dictionary keyed by vertex labels |
9892 | | |
9893 | | - ``cliques`` - list of cliques (if already |
9894 | | computed) |
9895 | | |
9896 | | |
9897 | | EXAMPLES:: |
9898 | | |
9899 | | sage: C = Graph('DJ{') |
9900 | | sage: C.cliques_vertex_clique_number() |
9901 | | [2, 4, 4, 4, 4] |
9902 | | sage: E = C.cliques() |
9903 | | sage: E |
9904 | | [[4, 1, 2, 3], [4, 0]] |
9905 | | sage: C.cliques_vertex_clique_number(cliques=E) |
9906 | | [2, 4, 4, 4, 4] |
9907 | | sage: F = graphs.Grid2dGraph(2,3) |
9908 | | sage: X = F.cliques_vertex_clique_number(with_labels=True) |
9909 | | sage: for v in sorted(X.iterkeys()): |
9910 | | ... print v, X[v] |
9911 | | (0, 0) 2 |
9912 | | (0, 1) 2 |
9913 | | (0, 2) 2 |
9914 | | (1, 0) 2 |
9915 | | (1, 1) 2 |
9916 | | (1, 2) 2 |
9917 | | sage: F.cliques_vertex_clique_number(vertices=[(0, 1), (1, 2)]) |
9918 | | [2, 2] |
9919 | | sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) |
9920 | | sage: G.show(figsize=[2,2]) |
9921 | | sage: G.cliques_vertex_clique_number() |
9922 | | [3, 3, 3, 3] |
9923 | | """ |
9924 | | import networkx.cliques |
9925 | | return networkx.cliques.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques) |
9926 | | |
| 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 | |
| 9995 | def cliques_get_max_clique_graph(self, name=''): |
| 9996 | """ |
| 9997 | Returns a graph constructed with maximal cliques as vertices, and |
| 9998 | edges between maximal cliques with common members in the original |
| 9999 | graph. |
| 10000 | |
| 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: |
| 10007 | |
| 10008 | - ``name`` - The name of the new graph. |
| 10009 | |
| 10010 | EXAMPLES:: |
| 10011 | |
| 10012 | sage: (graphs.ChvatalGraph()).cliques_get_max_clique_graph() |
| 10013 | Graph on 24 vertices |
| 10014 | sage: ((graphs.ChvatalGraph()).cliques_get_max_clique_graph()).show(figsize=[2,2], vertex_size=20, vertex_labels=False) |
| 10015 | sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) |
| 10016 | sage: G.show(figsize=[2,2]) |
| 10017 | sage: G.cliques_get_max_clique_graph() |
| 10018 | Graph on 2 vertices |
| 10019 | sage: (G.cliques_get_max_clique_graph()).show(figsize=[2,2]) |
| 10020 | """ |
| 10021 | import networkx.cliques |
| 10022 | return Graph(networkx.cliques.make_max_clique_graph(self.networkx_graph(copy=False), name=name, create_using=networkx.xgraph.XGraph())) |
| 10023 | |
| 10024 | def cliques_get_clique_bipartite(self, **kwds): |
| 10025 | """ |
| 10026 | Returns a bipartite graph constructed such that maximal cliques are the |
| 10027 | right vertices and the left vertices are retained from the given |
| 10028 | graph. Right and left vertices are connected if the bottom vertex |
| 10029 | belongs to the clique represented by a top vertex. |
| 10030 | |
| 10031 | NOTES: |
| 10032 | |
| 10033 | - Currently only implemented for undirected graphs. Use to_undirected |
| 10034 | to convert a digraph to an undirected graph. |
| 10035 | |
| 10036 | EXAMPLES:: |
| 10037 | |
| 10038 | sage: (graphs.ChvatalGraph()).cliques_get_clique_bipartite() |
| 10039 | Bipartite graph on 36 vertices |
| 10040 | sage: ((graphs.ChvatalGraph()).cliques_get_clique_bipartite()).show(figsize=[2,2], vertex_size=20, vertex_labels=False) |
| 10041 | sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) |
| 10042 | sage: G.show(figsize=[2,2]) |
| 10043 | sage: G.cliques_get_clique_bipartite() |
| 10044 | Bipartite graph on 6 vertices |
| 10045 | sage: (G.cliques_get_clique_bipartite()).show(figsize=[2,2]) |
| 10046 | """ |
| 10047 | import networkx.cliques |
| 10048 | from bipartite_graph import BipartiteGraph |
| 10049 | return BipartiteGraph(networkx.cliques.make_clique_bipartite(self.networkx_graph(copy=False), **kwds)) |
| 10050 | |
| 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. |
| 10060 | |
| 10061 | EXAMPLES:: |
| 10062 | |
| 10063 | sage: C=graphs.PetersenGraph() |
| 10064 | sage: C.independent_set() |
| 10065 | [0, 3, 6, 7] |
| 10066 | |
| 10067 | """ |
| 10068 | from sage.graphs.cliquer import max_clique |
| 10069 | return max_clique(self.complement()) |
| 10070 | |
| 10071 | def cliques_vertex_clique_number(self, algorithm="cliquer", vertices=None, |
| 10072 | with_labels=False, cliques=None): |
| 10073 | r""" |
| 10074 | Returns a list of sizes of the largest maximal cliques containing |
| 10075 | each vertex. (Returns a single value if only one input vertex). |
| 10076 | |
| 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'``. |
| 10100 | |
| 10101 | EXAMPLES:: |
| 10102 | |
| 10103 | sage: C = Graph('DJ{') |
| 10104 | sage: C.cliques_vertex_clique_number() |
| 10105 | [2, 4, 4, 4, 4] |
| 10106 | sage: E = C.cliques_maximal() |
| 10107 | sage: E |
| 10108 | [[4, 1, 2, 3], [4, 0]] |
| 10109 | sage: C.cliques_vertex_clique_number(cliques=E,algorithm="networkx") |
| 10110 | [2, 4, 4, 4, 4] |
| 10111 | sage: F = graphs.Grid2dGraph(2,3) |
| 10112 | sage: X = F.cliques_vertex_clique_number(with_labels=True,algorithm="networkx") |
| 10113 | sage: for v in sorted(X.iterkeys()): |
| 10114 | ... print v, X[v] |
| 10115 | (0, 0) 2 |
| 10116 | (0, 1) 2 |
| 10117 | (0, 2) 2 |
| 10118 | (1, 0) 2 |
| 10119 | (1, 1) 2 |
| 10120 | (1, 2) 2 |
| 10121 | sage: F.cliques_vertex_clique_number(vertices=[(0, 1), (1, 2)]) |
| 10122 | [2, 2] |
| 10123 | sage: G = Graph({0:[1,2,3], 1:[2], 3:[0,1]}) |
| 10124 | sage: G.show(figsize=[2,2]) |
| 10125 | sage: G.cliques_vertex_clique_number() |
| 10126 | [3, 3, 3, 3] |
| 10127 | |
| 10128 | """ |
| 10129 | |
| 10130 | if algorithm=="cliquer": |
| 10131 | from sage.graphs.cliquer import clique_number |
| 10132 | if vertices==None: |
| 10133 | vertices=self |
| 10134 | value=[] |
| 10135 | for v in vertices: |
| 10136 | value.append(1+clique_number(self.subgraph(self.neighbors(v)))) |
| 10137 | self.subgraph(self.neighbors(v)).plot() |
| 10138 | return value |
| 10139 | elif algorithm=="networkx": |
| 10140 | import networkx.cliques |
| 10141 | return networkx.cliques.node_clique_number(self.networkx_graph(copy=False), vertices, with_labels, cliques) |
| 10142 | else: |
| 10143 | raise NotImplementedError("Only 'networkx' and 'cliquer' are supported.") |
| 10144 | |