ó ŸÃÒYc@sgdZdZddlmZddlZddlmZddlmZddl m Z yÁddl m Z m Z mZmZmZmZmZmZmZmZdd lmZmZdd lmZdd lmZmZdd lmZm Z dd l!m"Z"m#Z#dddgZ$Wne%k r2gZ$nXy ddl&m'Z'm(Z(m)Z)Wn;e%k re$r‘eeddƒZ'eZ)d„Z(q‘nXe dƒZ*de+fd„ƒYZ,de+fd„ƒYZ-de+fd„ƒYZ.d„Z/d„Z0d„Z1d„Z2ed ƒd!e3d"d#d$„ƒZ4ed ƒd!e3d"d#d%„ƒZ5d!e3d"d#d&„Z6d'„Z7dS((sB Algebraic connectivity and Fiedler vectors of undirected graphs. s&ysitu iÿÿÿÿ(tpartialN(tnot_implemented_for(treverse_cuthill_mckee_ordering(tcompile( tarraytasmatrixtasarraytdottmatrixtndarraytonestreshapetsqrttzeros(tnormtqr(tnormal(teightinv(t csc_matrixtspdiags(teigshtlobpcgtalgebraic_connectivitytfiedler_vectortspectral_ordering(tdasumtdaxpytddottordicCs|||7}|S(N((txtyta((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR(ss^tracemin(?:_(.*))?$t _PCGSolvercBs)eZdZd„Zd„Zd„ZRS(s.Preconditioned conjugate gradient method. cCs||_|pd„|_dS(NcSs |jƒS(N(tcopy(R((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyt5s(t_At_M(tselftAtM((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyt__init__3s cCsut|ƒ}t|jddƒ}xMt|jdƒD]8}|j|dd…|f|ƒ|dd…|fs&        (t__name__t __module__t__doc__R)R3R.(((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR!/s  t_CholeskySolvercBsTeZdZd„Zd„ZyddlmZeZWnek rQdZnXRS(sCholesky factorization. cCs1|jstjdƒ‚n|j|ƒ|_dS(NsCholesky solver unavailable.(t _choleskytnxt NetworkXErrort_chol(R&R'((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR)Zs cCs |j|ƒS(N(RD(R&R/((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR3_siÿÿÿÿ(tcholeskyN( R=R>R?R)R3tscikits.sparse.cholmodRERAt ImportErrortNone(((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR@Vs    t _LUSolverc BszeZdZd„Zd„Zy@ddlmZeeddddd ied 6ed 6ƒZ Wne k rwd Z nXRS( sLU factorization. cCs1|jstjdƒ‚n|j|ƒ|_dS(NsLU solver unavailable.(t_spluRBRCt_LU(R&R'((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR)ms cCsut|ƒ}t|jddƒ}xMt|jdƒD]8}|jj|dd…|fƒ|dd…|fR?R)R3tscipy.sparse.linalgRLRR4RJRGRH(((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyRIis   csáˆjƒr]tjƒ}|jˆƒ|j‡fd†ˆjdtƒDƒdˆƒ|‰nˆjƒsŽ‡fd†ˆjdtƒDƒ}n‡‡fd†ˆjƒDƒ}tjƒ}|jˆƒ|jd„|Dƒƒ|S(s:Compute edge weights and eliminate zero-weight edges. c3s?|]5\}}}||kr|||jˆdƒfVqdS(gð?N(tget(t.0tutvte(tweight(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pys ‡s tdataRYc3sE|];\}}}||kr||t|jˆdƒƒfVqdS(gð?N(tabsRT(RURVRWRX(RY(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pys Œsc3sT|]J\}}||kr||t‡fd†ˆ||jƒDƒƒfVqdS(c3s'|]}t|jˆdƒƒVqdS(gð?N(R[RT(RURX(RY(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pys sN(tsumtvalues(RURVRW(tGRY(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pys scss3|])\}}}|dkr|||fVqdS(iN((RURVRWRX((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pys “s( t is_directedRBt MultiGraphtadd_nodes_fromtadd_weighted_edges_fromtedgesR4t is_multigraphtGraph(R^RYtHRc((R^RYs{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyt_preprocess_graphs         cCs•|j|ƒ}t|ƒ}t|ƒ}tt|t|ƒƒƒ}t|dtƒ}x(t|ƒD]\}}||||ttracemincCsÂt|ƒdkr$tjdƒ‚nt||ƒ}tj|ƒsFdStj|ƒ}|jddkr~|szd|dSdSt|ƒ}|dkrœdn t ||ƒ}|||||ƒdS( s§Return the algebraic connectivity of an undirected graph. The algebraic connectivity of a connected undirected graph is the second smallest eigenvalue of its Laplacian matrix. Parameters ---------- G : NetworkX graph An undirected graph. weight : object, optional The data key used to determine the weight of each edge. If None, then each edge has unit weight. Default value: None. normalized : bool, optional Whether the normalized Laplacian matrix is used. Default value: False. tol : float, optional Tolerance of relative residual in eigenvalue computation. Default value: 1e-8. method : string, optional Method of eigenvalue computation. It should be one of 'tracemin' (TraceMIN), 'lanczos' (Lanczos iteration) and 'lobpcg' (LOBPCG). Default value: 'tracemin'. The TraceMIN algorithm uses a linear system solver. The following values allow specifying the solver to be used. =============== ======================================== Value Solver =============== ======================================== 'tracemin_pcg' Preconditioned conjugate gradient method 'tracemin_chol' Cholesky factorization 'tracemin_lu' LU factorization =============== ======================================== Returns ------- algebraic_connectivity : float Algebraic connectivity. Raises ------ NetworkXNotImplemented If G is directed. NetworkXError If G has less than two nodes. Notes ----- Edge weights are interpreted by their absolute values. For MultiGraph's, weights of parallel edges are summed. Zero-weighted edges are ignored. To use Cholesky factorization in the TraceMIN algorithm, the :samp:`scikits.sparse` package must be installed. See Also -------- laplacian_matrix isgraph has less than two nodes.gig@R(iiN( RjRBRCRgt is_connectedtlaplacian_matrixR,RŸRHRs(R^RYRyR0RˆR{R“R((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyR1sA !cCsÉt|ƒdkr$tjdƒ‚nt||ƒ}tj|ƒsTtjdƒ‚nt|ƒdkrvtddgƒSt|ƒ}tj|ƒ}|dkr£dn t ||ƒ}|||||ƒdS( sêReturn the Fiedler vector of a connected undirected graph. The Fiedler vector of a connected undirected graph is the eigenvector corresponding to the second smallest eigenvalue of the Laplacian matrix of of the graph. Parameters ---------- G : NetworkX graph An undirected graph. weight : object, optional The data key used to determine the weight of each edge. If None, then each edge has unit weight. Default value: None. normalized : bool, optional Whether the normalized Laplacian matrix is used. Default value: False. tol : float, optional Tolerance of relative residual in eigenvalue computation. Default value: 1e-8. method : string, optional Method of eigenvalue computation. It should be one of 'tracemin' (TraceMIN), 'lanczos' (Lanczos iteration) and 'lobpcg' (LOBPCG). Default value: 'tracemin'. The TraceMIN algorithm uses a linear system solver. The following values allow specifying the solver to be used. =============== ======================================== Value Solver =============== ======================================== 'tracemin_pcg' Preconditioned conjugate gradient method 'tracemin_chol' Cholesky factorization 'tracemin_lu' LU factorization =============== ======================================== Returns ------- fiedler_vector : NumPy array of floats. Fiedler vector. Raises ------ NetworkXNotImplemented If G is directed. NetworkXError If G has less than two nodes or is not connected. Notes ----- Edge weights are interpreted by their absolute values. For MultiGraph's, weights of parallel edges are summed. Zero-weighted edges are ignored. To use Cholesky factorization in the TraceMIN algorithm, the :samp:`scikits.sparse` package must be installed. See Also -------- laplacian_matrix isgraph has less than two nodes.sgraph is not connected.gð?gð¿RiN( RjRBRCRgR¢RRŸR£RHRs(R^RYRyR0RˆR“R{R((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyRsB !c Cst|ƒdkr$tjdƒ‚nt||ƒ}t|ƒ}g}xºtj|ƒD]©}t|ƒ}|dkrñtj||ƒ} |dkr—dn t||ƒ} || | ||ƒd} |j d„t t | t |ƒ|ƒƒDƒƒqU|j |ƒqUW|S(s»Compute the spectral_ordering of a graph. The spectral ordering of a graph is an ordering of its nodes where nodes in the same weakly connected components appear contiguous and ordered by their corresponding elements in the Fiedler vector of the component. Parameters ---------- G : NetworkX graph A graph. weight : object, optional The data key used to determine the weight of each edge. If None, then each edge has unit weight. Default value: None. normalized : bool, optional Whether the normalized Laplacian matrix is used. Default value: False. tol : float, optional Tolerance of relative residual in eigenvalue computation. Default value: 1e-8. method : string, optional Method of eigenvalue computation. It should be one of 'tracemin' (TraceMIN), 'lanczos' (Lanczos iteration) and 'lobpcg' (LOBPCG). Default value: 'tracemin'. The TraceMIN algorithm uses a linear system solver. The following values allow specifying the solver to be used. =============== ======================================== Value Solver =============== ======================================== 'tracemin_pcg' Preconditioned conjugate gradient method 'tracemin_chol' Cholesky factorization 'tracemin_lu' LU factorization =============== ======================================== Returns ------- spectral_ordering : NumPy array of floats. Spectral ordering of nodes. Raises ------ NetworkXError If G is empty. Notes ----- Edge weights are interpreted by their absolute values. For MultiGraph's, weights of parallel edges are summed. Zero-weighted edges are ignored. To use Cholesky factorization in the TraceMIN algorithm, the :samp:`scikits.sparse` package must be installed. See Also -------- laplacian_matrix isgraph is empty.iRicss|]\}}}|VqdS(N((RURtcRV((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pys sN( RjRBRCRgRŸtconnected_componentsR£RHRstextendtsortedRlR-( R^RYRyR0RˆR“R*t componentRR{Rtfiedler((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyRÒs>   !,cCsPddlm}yddl}ddl}Wntk rK|dƒ‚nXdS(Niÿÿÿÿ(tSkipTestsSciPy not available.(tnoseRªtnumpyt scipy.sparseRG(tmoduleRªR¬tscipy((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pyt setup_module%s   (8R?t __author__t functoolsRtnetworkxRBtnetworkx.utilsRRtreRR¬RRRRRR R R R R t numpy.linalgRRt numpy.randomRt scipy.linalgRRR­RRRSRRt__all__RGtscipy.linalg.blasRRRRœtobjectR!R@RIRgRsRRŸR›RRRR°(((s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/linalg/algebraicconnectivity.pytsP F     '   c )  N  O  R