ó žÃÒYc@s±dZddlZddlmZddlmZddddd d gZed ƒd „ƒZeZ ddd „Z ddd„Z ddd„Z ddd„Zdd„ZdS(sá Find the k-cores of a graph. The k-core is found by recursively pruning nodes with degrees less than k. See the following references for details: An O(m) Algorithm for Cores Decomposition of Networks Vladimir Batagelj and Matjaz Zaversnik, 2003. https://arxiv.org/abs/cs.DS/0310049 Generalized Cores Vladimir Batagelj and Matjaz Zaversnik, 2002. https://arxiv.org/pdf/cs/0202039 For directed graphs a more general notion is that of D-cores which looks at (k, l) restrictions on (in, out) degree. The (k, k) D-core is the k-core. D-cores: Measuring Collaboration of Directed Graphs Based on Degeneracy Christos Giatsidis, Dimitrios M. Thilikos, Michalis Vazirgiannis, ICDM 2011. http://www.graphdegeneracy.org/dcores_ICDM_2011.pdf iÿÿÿÿN(t NetworkXError(tnot_implemented_fort core_numbert find_corestk_coretk_shelltk_crusttk_coronat multigraphcs«tjˆƒdkr*d}t|ƒ‚ntˆjƒƒ}t|d|jƒ}dg}d}xSt|ƒD]E\}}|||krm|j|g|||ƒ||}qmqmWd„t|ƒDƒ}|} ‡fd†ˆDƒ} x¼|D]´}x«| |D]Ÿ} | | | |kr| | j |ƒ|| } || | } | || <| ||| <|| || || <|| <|| | cd7<| | cd8]s cs+i|]!}ttjˆ|ƒƒ|“qS((tlisttnxt all_neighbors(R R (tG(sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pys `s i( Rtnumber_of_selfloopsRtdicttdegreetsortedtgett enumeratetextendtremove(Rtmsgtdegreestnodestbin_boundariest curr_degreetiR tnode_postcoretnbrstuR t bin_start((Rsn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyR*s4&    cskˆdkrt|ƒ‰nˆdkr<tˆjƒƒ‰n‡‡‡fd†ˆDƒ}|j|ƒjƒS(s½Returns the subgraph induced by nodes passing filter `k_filter`. Parameters ---------- G : NetworkX graph The graph or directed graph to process k_filter : filter function This function filters the nodes chosen. It takes three inputs: A node of G, the filter's cutoff, and the core dict of the graph. The function should return a Boolean value. k : int, optional The order of the core. If not specified use the max core number. This value is used as the cutoff for the filter. core : dict, optional Precomputed core numbers keyed by node for the graph `G`. If not specified, the core numbers will be computed from `G`. c3s'|]}ˆ|ˆˆƒr|VqdS(N((R R (R tktk_filter(sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pys ‰sN(tNoneRtmaxtvaluestsubgraphtcopy(RR%R$R R((R R$R%sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyt_core_subgraphrs   cCsd„}t||||ƒS(sIReturn the k-core of G. A k-core is a maximal subgraph that contains nodes of degree k or more. Parameters ---------- G : NetworkX graph A graph or directed graph k : int, optional The order of the core. If not specified return the main core. core_number : dictionary, optional Precomputed core numbers for the graph G. Returns ------- G : NetworkX graph The k-core subgraph Raises ------ NetworkXError The k-core is not defined for graphs with self loops or parallel edges. Notes ----- The main core is the core with the largest degree. Not implemented for graphs with parallel edges or self loops. For directed graphs the node degree is defined to be the in-degree + out-degree. Graph, node, and edge attributes are copied to the subgraph. See Also -------- core_number References ---------- .. [1] An O(m) Algorithm for Cores Decomposition of Networks Vladimir Batagelj and Matjaz Zaversnik, 2003. https://arxiv.org/abs/cs.DS/0310049 cSs|||kS(N((R R$tc((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyR%ºs(R+(RR$RR%((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyRs- cCsd„}t||||ƒS(s6Return the k-shell of G. The k-shell is the subgraph induced by nodes with core number k. That is, nodes in the k-core that are not in the (k+1)-core. Parameters ---------- G : NetworkX graph A graph or directed graph. k : int, optional The order of the shell. If not specified return the outer shell. core_number : dictionary, optional Precomputed core numbers for the graph G. Returns ------- G : NetworkX graph The k-shell subgraph Raises ------ NetworkXError The k-shell is not implemented for graphs with self loops or parallel edges. Notes ----- This is similar to k_corona but in that case only neighbors in the k-core are considered. Not implemented for graphs with parallel edges or self loops. For directed graphs the node degree is defined to be the in-degree + out-degree. Graph, node, and edge attributes are copied to the subgraph. See Also -------- core_number k_corona References ---------- .. [1] A model of Internet topology using k-shell decomposition Shai Carmi, Shlomo Havlin, Scott Kirkpatrick, Yuval Shavitt, and Eran Shir, PNAS July 3, 2007 vol. 104 no. 27 11150-11154 http://www.pnas.org/content/104/27/11150.full cSs|||kS(N((R R$R,((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyR%ós(R+(RR$RR%((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyR¿s4 cslˆdkrt|ƒ‰nˆdkr@tˆjƒƒd‰n‡‡fd†ˆDƒ}|j|ƒjƒS(s Return the k-crust of G. The k-crust is the graph G with the k-core removed. Parameters ---------- G : NetworkX graph A graph or directed graph. k : int, optional The order of the shell. If not specified return the main crust. core_number : dictionary, optional Precomputed core numbers for the graph G. Returns ------- G : NetworkX graph The k-crust subgraph Raises ------ NetworkXError The k-crust is not implemented for graphs with self loops or parallel edges. Notes ----- This definition of k-crust is different than the definition in [1]_. The k-crust in [1]_ is equivalent to the k+1 crust of this algorithm. Not implemented for graphs with parallel edges or self loops. For directed graphs the node degree is defined to be the in-degree + out-degree. Graph, node, and edge attributes are copied to the subgraph. See Also -------- core_number References ---------- .. [1] A model of Internet topology using k-shell decomposition Shai Carmi, Shlomo Havlin, Scott Kirkpatrick, Yuval Shavitt, and Eran Shir, PNAS July 3, 2007 vol. 104 no. 27 11150-11154 http://www.pnas.org/content/104/27/11150.full ic3s%|]}ˆ|ˆkr|VqdS(N((R R (RR$(sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pys .sN(R&RR'R(R)R*(RR$RR((RR$sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyRøs 2  cs"‡fd†}tˆ|||ƒS(sReturn the k-corona of G. The k-corona is the subgraph of nodes in the k-core which have exactly k neighbours in the k-core. Parameters ---------- G : NetworkX graph A graph or directed graph k : int The order of the corona. core_number : dictionary, optional Precomputed core numbers for the graph G. Returns ------- G : NetworkX graph The k-corona subgraph Raises ------ NetworkXError The k-cornoa is not defined for graphs with self loops or parallel edges. Notes ----- Not implemented for graphs with parallel edges or self loops. For directed graphs the node degree is defined to be the in-degree + out-degree. Graph, node, and edge attributes are copied to the subgraph. See Also -------- core_number References ---------- .. [1] k -core (bootstrap) percolation on complex networks: Critical phenomena and nonlocal effects, A. V. Goltsev, S. N. Dorogovtsev, and J. F. F. Mendes, Phys. Rev. E 73, 056101 (2006) http://link.aps.org/doi/10.1103/PhysRevE.73.056101 cs7ˆ|ˆko6ˆt‡‡fd†ˆ|DƒƒkS(Nc3s%|]}ˆ|ˆkrdVqdS(iN((R tw(R,R$(sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pys bs(tsum(R R$R,(R(R,R$sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pytfuncas(R+(RR$RR/((Rsn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyR2s/(t__doc__tnetworkxRtnetworkx.exceptionRtnetworkx.utilsRt__all__RRR&R+RRRR(((sn/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/core.pyt!s  E29: