ó žÃÒYc@s…dZddlZddddddgZdd „Zddd „Zded „Zded „Zded „Z ded„Z dS(s:Graph diameter, radius, eccentricity and other properties.iÿÿÿÿNtextrema_boundingt eccentricitytdiametertradiust peripherytcentercst|jƒƒ}t|d|jƒ}t|ƒ}t}tj|dƒ‰tj||ƒ‰t|ƒ}|‰d‰|‰d‰xÞ|ra|r™|}n|}| }ttj ||ƒƒ} t| ƒ|krèd} tj | ƒ‚nt| j ƒƒ} d}d}x«|D]£} | | } tˆ| t| | | ƒƒˆ| <}t ˆ| | | ƒˆ| <}t ˆ| ˆƒ‰tˆ| ˆƒ‰t ˆ| ˆƒ‰tˆ| ˆƒ‰q W|dkr⇇‡‡fd†|Dƒ}nŸ|dkr‡‡‡‡fd†|Dƒ}nq|dkr>‡‡‡‡fd †|Dƒ}nC|d krl‡‡‡‡fd †|Dƒ}n|d kri}n|j‡‡fd †|Dƒƒ||8}x°|D]¨} |dksˆ| ˆ|krì|| ||ksˆ| ˆ|kr | }n|dksQˆ| ˆ|kr=|| ||ksQˆ| ˆ|kr²| }q²q²Wq„W|dkrrˆS|dkr‚ˆS|dkr»g|D]}ˆ|ˆkr•|^q•}|S|d krôg|D]}ˆ|ˆkrÎ|^qÎ}|S|d krˆSdS(sÉCompute requested extreme distance metric of undirected graph G Computation is based on smart lower and upper bounds, and in practice linear in the number of nodes, rather than quadratic (except for some border cases such as complete graphs or circle shaped graphs). Parameters ---------- G : NetworkX graph An undirected graph compute : string denoting the requesting metric "diameter" for the maximal eccentricity value, "radius" for the minimal eccentricity value, "periphery" for the set of nodes with eccentricity equal to the diameter "center" for the set of nodes with eccentricity equal to the radius Returns ------- value : value of the requested metric int for "diameter" and "radius" or list of nodes for "center" and "periphery" Raises ------ NetworkXError If the graph consists of multiple components Notes ----- This algorithm was proposed in the following papers: F.W. Takes and W.A. Kosters, Determining the Diameter of Small World Networks, in Proceedings of the 20th ACM International Conference on Information and Knowledge Management (CIKM 2011), pp. 1191-1196, 2011. doi: http://dx.doi.org/10.1145/2063576.2063748 F.W. Takes and W.A. Kosters, Computing the Eccentricity Distribution of Large Graphs, Algorithms 6(1): 100-118, 2013. doi: http://dx.doi.org/10.3390/a6010100 M. Borassi, P. Crescenzi, M. Habib, W.A. Kosters, A. Marino and F.W. Takes, Fast Graph Diameter and Radius BFS-Based Computation in (Weakly Connected) Real-World Graphs, Theoretical Computer Science 586: 59-80, 2015. doi: http://dx.doi.org/10.1016/j.tcs.2015.02.033 tkeyis5Cannot compute metric because graph is not connected.Rcs:h|]0}ˆ|ˆkrdˆ|ˆkr|’qS(i((t.0ti(t ecc_lowert ecc_uppertmaxlowertmaxupper(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/distance_measures.pys {s Rcs>h|]4}ˆ|ˆkrˆ|ddˆkr|’qS(ii((RR(R R tminlowertminupper(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/distance_measures.pys s RcsBh|]8}ˆ|ˆkrˆˆks8ˆ|ˆkr|’qS(((RR(R R R R (s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/distance_measures.pys ƒs RcsJh|]@}ˆ|ˆkrˆˆks@ˆ|ddˆkr|’qS(ii((RR(R R R R(s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/distance_measures.pys ‡s teccentricitiesc3s)|]}ˆ|ˆ|kr|VqdS(N((RR(R R (s{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/distance_measures.pys sN(tdicttdegreetmaxtgettlentFalsetfromkeystsettnetworkxt"single_source_shortest_path_lengtht NetworkXErrortvaluestNonetmintupdate(tGtcomputetdegreest minlowernodetNthight candidatest maxuppernodetcurrenttdisttmsgt current_eccRtdtlowtuppt ruled_outtvtptc((R R R R R Rs{/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/distance_measures.pyRs†1       ( " " " "          ) ) c Cs|jƒ}i}xÐ|j|ƒD]¿}|dkrUtj||ƒ}t|ƒ}n=y||}t|ƒ}Wn tk r‘tjdƒ‚nX||krË|jƒr³d}nd}tj|ƒ‚nt |j ƒƒ|| s   ¯: