ó žÃÒYc @sNdZddlmZddlmZddlmZddlmZyddlmZWne k rmnXddl Z ddl m Z d Z d d d d ddddddg Ze dƒd„ƒZe dƒd„ƒZd„Zdd„Zdddd„Zdd„Zdd„Zddd„Zddd„Zddd„ZdS(s@Functions for finding and manipulating cliques. Finding the largest clique in a graph is NP-complete problem, so most of these algorithms have an exponential running time; for more information, see the Wikipedia article on the clique problem [1]_. .. [1] clique problem:: https://en.wikipedia.org/wiki/Clique_problem iÿÿÿÿ(tdeque(tchain(t combinations(tislice(tifilterN(tnot_implemented_fors Dan Schult (dschult@colgate.edu)t find_cliquestfind_cliques_recursivetmake_max_clique_graphtmake_clique_bipartitetgraph_clique_numbertgraph_number_of_cliquestnode_clique_numbertnumber_of_cliquestcliques_containing_nodetenumerate_all_cliquestdirectedc #s÷i‰i‰x<|D]4}tˆƒˆ|<‡fd†||Dƒˆ|. cs"h|]}|ˆkr|’qS(((t.0tv(tindex(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys Us c3s1|]'}|gtˆ|dˆjƒfVqdS(tkeyN(tsortedt __getitem__(Rtu(Rtnbrs(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys WsiN( tlenRtmaptlisttpopleftt enumeratetappendRtfiltert __contains__RtNone(tGRtqueuetbasetcnbrsti((RRsp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR"s. "  c #s„tˆƒdkrdS‡fd†ˆDƒ‰dg}tˆƒ}tˆƒ‰t|d‡‡fd†ƒ}ˆˆ|}g}yíxætrj|rH|jƒ}ˆj|ƒ||d<ˆ|}||@}|s×|Vqgˆ|@} | rg|j|ˆ|fƒ|jdƒ|}| ‰t|d‡‡fd†ƒ}ˆˆ|}qgq…|jƒ|jƒ\}‰}q…WWntk rnXdS(sa Returns all maximal cliques in an undirected graph. For each node *v*, a *maximal clique for v* is a largest complete subgraph containing *v*. The largest maximal clique is sometimes called the *maximum clique*. This function returns an iterator over cliques, each of which is a list of nodes. It is an iterative implementation, so should not suffer from recursion depth issues. Parameters ---------- G : NetworkX graph An undirected graph. Returns ------- iterator An iterator over maximal cliques, each of which is a list of nodes in `G`. The order of cliques is arbitrary. See Also -------- find_cliques_recursive A recursive version of the same algorithm. Notes ----- To obtain a list of all maximal cliques, use `list(find_cliques(G))`. However, be aware that in the worst-case, the length of this list can be exponential in the number of nodes in the graph (for example, when the graph is the complete graph). This function avoids storing all cliques in memory by only keeping current candidate node lists in memory during its search. This implementation is based on the algorithm published by Bron and Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi (2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. It essentially unrolls the recursion used in the references to avoid issues of recursion stack depth (for a recursive implementation, see :func:`find_cliques_recursive`). This algorithm ignores self-loops and parallel edges, since cliques are not conventionally defined with such edges. References ---------- .. [1] Bron, C. and Kerbosch, J. "Algorithm 457: finding all cliques of an undirected graph". *Communications of the ACM* 16, 9 (Sep. 1973), 575--577. .. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi, "The worst-case time complexity for generating all maximal cliques and computational experiments", *Theoretical Computer Science*, Volume 363, Issue 1, Computing and Combinatorics, 10th Annual International Conference on Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42 .. [3] F. Cazals, C. Karande, "A note on the problem of reporting maximal cliques", *Theoretical Computer Science*, Volume 407, Issues 1--3, 6 November 2008, Pages 564--568, iNcs-i|]#‰‡fd†ˆˆDƒˆ“qS(cs"h|]}|ˆkr|’qS(((RR(R(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys ¯s ((R(R"(Rsp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys ¯s Rcstˆˆ|@ƒS(N(R(R(tadjtcand(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyt´siÿÿÿÿcstˆˆ|@ƒS(N(R(R(R'R((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR)És( RR!tsettmaxtTruetpoptremoveRt IndexError( R"tQtsubgRtext_utstacktqtadj_qtsubg_qtcand_q((R"R'R(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyRfs>F              csftˆƒdkrtgƒS‡fd†ˆDƒ‰g‰‡‡‡fd†‰ˆtˆƒtˆƒƒS(s¼ Returns all maximal cliques in a graph. For each node *v*, a *maximal clique for v* is a largest complete subgraph containing *v*. The largest maximal clique is sometimes called the *maximum clique*. This function returns an iterator over cliques, each of which is a list of nodes. It is a recursive implementation, so may suffer from recursion depth issues. Parameters ---------- G : NetworkX graph Returns ------- iterator An iterator over maximal cliques, each of which is a list of nodes in `G`. The order of cliques is arbitrary. See Also -------- find_cliques An iterative version of the same algorithm. Notes ----- To obtain a list of all maximal cliques, use `list(find_cliques_recursive(G))`. However, be aware that in the worst-case, the length of this list can be exponential in the number of nodes in the graph (for example, when the graph is the complete graph). This function avoids storing all cliques in memory by only keeping current candidate node lists in memory during its search. This implementation is based on the algorithm published by Bron and Kerbosch (1973) [1]_, as adapted by Tomita, Tanaka and Takahashi (2006) [2]_ and discussed in Cazals and Karande (2008) [3]_. For a non-recursive implementation, see :func:`find_cliques`. This algorithm ignores self-loops and parallel edges, since cliques are not conventionally defined with such edges. References ---------- .. [1] Bron, C. and Kerbosch, J. "Algorithm 457: finding all cliques of an undirected graph". *Communications of the ACM* 16, 9 (Sep. 1973), 575--577. .. [2] Etsuji Tomita, Akira Tanaka, Haruhisa Takahashi, "The worst-case time complexity for generating all maximal cliques and computational experiments", *Theoretical Computer Science*, Volume 363, Issue 1, Computing and Combinatorics, 10th Annual International Conference on Computing and Combinatorics (COCOON 2004), 25 October 2006, Pages 28--42 .. [3] F. Cazals, C. Karande, "A note on the problem of reporting maximal cliques", *Theoretical Computer Science*, Volume 407, Issues 1--3, 6 November 2008, Pages 564--568, ics-i|]#‰‡fd†ˆˆDƒˆ“qS(cs"h|]}|ˆkr|’qS(((RR(R(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys s ((R(R"(Rsp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys s c3s´t|d‡‡fd†ƒ}xˆˆ|D]}ˆj|ƒˆj|ƒˆ|}||@}|spˆVn2ˆ|@}|r¢xˆ||ƒD] }|VqWnˆjƒq-WdS(NRcstˆˆ|@ƒS(N(R(R(R'R((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR)s(R+R.RR-(R1R(RR4R5R6R7tclique(R0R'texpand(R(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR9s       (RtiterR*(R"((R"R0R'R9sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyRÓs B cCs‹|dk r|n tjƒ}|jƒttd„t|ƒDƒƒƒ}|jd„|Dƒƒt|dƒ}|j d„|Dƒƒ|S(sÙReturns the maximal clique graph of the given graph. The nodes of the maximal clique graph of `G` are the cliques of `G` and an edge joins two cliques if the cliques are not disjoint. Parameters ---------- G : NetworkX graph create_using : NetworkX graph If provided, this graph will be cleared and the nodes and edges of the maximal clique graph will be added to this graph. Returns ------- NetworkX graph A graph whose nodes are the cliques of `G` and whose edges join two cliques if they are not disjoint. Notes ----- This function behaves like the following code:: import networkx as nx G = nx.make_clique_bipartite(G) cliques = [v for v in G.nodes() if G.nodes[v]['bipartite'] == 0] G = nx.bipartite.project(G, cliques) G = nx.relabel_nodes(G, {-v: v - 1 for v in G}) It should be faster, though, since it skips all the intermediate steps. css|]}t|ƒVqdS(N(R*(Rtc((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys Rscss|]\}}|VqdS(N((RR&R;((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys Tsicss7|]-\\}}\}}||@r||fVqdS(N((RR&tc1tjtc2((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys WsN( R!tnetworkxtGraphtclearRRRtadd_nodes_fromRtadd_edges_from(R"t create_usingtBtcliquest clique_pairs((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR.s" "cs|dk r|n tjƒ}|jƒ|j|ddƒx[tt|ƒƒD]G\}}| d‰|jˆddƒ|j‡fd†|DƒƒqNW|S(sÕReturns the bipartite clique graph corresponding to `G`. In the returned bipartite graph, the "bottom" nodes are the nodes of `G` and the "top" nodes represent the maximal cliques of `G`. There is an edge from node *v* to clique *C* in the returned graph if and only if *v* is an element of *C*. Parameters ---------- G : NetworkX graph An undirected graph. fpos : bool If True or not None, the returned graph will have an additional attribute, `pos`, a dictionary mapping node to position in the Euclidean plane. create_using : NetworkX graph If provided, this graph will be cleared and the nodes and edges of the bipartite graph will be added to this graph. Returns ------- NetworkX graph A bipartite graph whose "bottom" set is the nodes of the graph `G`, whose "top" set is the cliques of `G`, and whose edges join nodes of `G` to the cliques that contain them. The nodes of the graph `G` have the node attribute 'bipartite' set to 1 and the nodes representing cliques have the node attribute 'bipartite' set to 0, as is the convention for bipartite graphs in NetworkX. t bipartiteiic3s|]}|ˆfVqdS(N((RR(tname(sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys ˆsN( R!R?R@RARBRRtadd_nodeRC(R"tfposRDRIRER&tcl((RIsp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR [s#  !cCs>|dkrt|ƒ}ntg|D]}t|ƒ^q%ƒS(s¢Returns the clique number of the graph. The *clique number* of a graph is the size of the largest clique in the graph. Parameters ---------- G : NetworkX graph An undirected graph. cliques : list A list of cliques, each of which is itself a list of nodes. If not specified, the list of all cliques will be computed, as by :func:`find_cliques`. Returns ------- int The size of the largest clique in `G`. Notes ----- You should provide `cliques` if you have already computed the list of maximal cliques, in order to avoid an exponential time search for maximal cliques. N(R!RR+R(R"RFR;((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR Œs cCs+|dkr!tt|ƒƒ}nt|ƒS(sUReturns the number of maximal cliques in the graph. Parameters ---------- G : NetworkX graph An undirected graph. cliques : list A list of cliques, each of which is itself a list of nodes. If not specified, the list of all cliques will be computed, as by :func:`find_cliques`. Returns ------- int The number of maximal cliques in `G`. Notes ----- You should provide `cliques` if you have already computed the list of maximal cliques, in order to avoid an exponential time search for maximal cliques. N(R!RRR(R"RF((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR ­s cCst|dkrº|dk r¥t|tƒrsi}xq|D]8}tj||ƒ}td„t|ƒDƒƒ||Ùscss|]}t|ƒVqdS(N(R(RR;((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pys ÜsN( R!t isinstanceRR?t ego_graphR+RtnodesR(R"RORFtdtntHRR;((sp/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/clique.pyR Ës(   ' 4 9cCsÏ|dkr!tt|ƒƒ}n|dkrBt|jƒƒ}nt|tƒs…|}tg|D]}||krad^qaƒ}nFi}x=|D]5}tg|D]}||kr¢d^q¢ƒ||s4    Dm [ -1 ! 3