ó žÃÒYc@ s€dZddlmZddlZdddddgZd „Zdddd „Zdddd „Z dd „Z dd „Z dS(s† Compute the shortest paths and path lengths between nodes in the graph. These algorithms work with undirected and directed graphs. iÿÿÿÿ(tdivisionNt shortest_pathtall_shortest_pathstshortest_path_lengthtaverage_shortest_path_lengththas_pathcC s5ytj|||ƒ}Wntjk r0tSXtS(sÕReturn *True* if *G* has a path from *source* to *target*. Parameters ---------- G : NetworkX graph source : node Starting node for path target : node Ending node for path (tnxRtNetworkXNoPathtFalsetTrue(tGtsourcettargettsp((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyRs c C sb|dkrÚ|dkrZ|dkr<ttj|ƒƒ}q×ttj|d|ƒƒ}q^tjj|ƒi|dkrŽtj||ƒ}ntj||d|ƒ}x(|D] }t t||ƒƒ||>> G = nx.path_graph(5) >>> print(nx.shortest_path(G, source=0, target=4)) [0, 1, 2, 3, 4] >>> p = nx.shortest_path(G, source=0) # target not specified >>> p[4] [0, 1, 2, 3, 4] >>> p = nx.shortest_path(G, target=4) # source not specified >>> p[0] [0, 1, 2, 3, 4] >>> p = nx.shortest_path(G) # source, target not specified >>> p[0][4] [0, 1, 2, 3, 4] Notes ----- There may be more than one shortest path between a source and target. This returns only one of them. See Also -------- all_pairs_shortest_path() all_pairs_dijkstra_path() single_source_shortest_path() single_source_dijkstra_path() tweightN( tNonetdictRtall_pairs_shortest_pathtall_pairs_dijkstra_pathtutilstreversedtsingle_source_shortest_pathtsingle_source_dijkstra_pathtlisttbidirectional_shortest_patht dijkstra_path(R R R Rtpaths((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyR.s*B      '    c C sn|dkr¯|dkrN|dkr6tj|ƒ}q¬tj|d|ƒ}qjtjj|ƒJ|dkrˆtj}|||ƒ}ntj}|||d|ƒ}WdQXn»||krÖtjdj |ƒƒ‚n|dkr|dkrtj||ƒ}qjtj||d|ƒ}nL|dkrRtj |||ƒ}t |ƒd}ntj ||||ƒ}|S(s) Compute shortest path lengths in the graph. Parameters ---------- G : NetworkX graph source : node, optional Starting node for path. If not specified, compute shortest path lengths using all nodes as source nodes. target : node, optional Ending node for path. If not specified, compute shortest path lengths using all nodes as target nodes. weight : None or string, optional (default = None) If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1. Returns ------- length: int or iterator If the source and target are both specified, return the length of the shortest path from the source to the target. If only the source is specified, return a dict keyed by target to the shortest path length from the source to that target. If only the target is specified, return a dict keyed by source to the shortest path length from that source to the target. If neither the source nor target are specified, return an iterator over (source, dictionary) where dictionary is keyed by target to shortest path length from source to that target. Raises ------ NetworkXNoPath If no path exists between source and target. Examples -------- >>> G = nx.path_graph(5) >>> nx.shortest_path_length(G, source=0, target=4) 4 >>> p = nx.shortest_path_length(G, source=0) # target not specified >>> p[4] 4 >>> p = nx.shortest_path_length(G, target=4) # source not specified >>> p[0] 4 >>> p = dict(nx.shortest_path_length(G)) # source,target not specified >>> p[0][4] 4 Notes ----- The length of the path is always 1 less than the number of nodes involved in the path since the length measures the number of edges followed. For digraphs this returns the shortest directed path length. To find path lengths in the reverse direction use G.reverse(copy=False) first to flip the edge orientation. See Also -------- all_pairs_shortest_path_length() all_pairs_dijkstra_path_length() single_source_shortest_path_length() single_source_dijkstra_path_length() RNsSource {} not in Gi( RRtall_pairs_shortest_path_lengthtall_pairs_dijkstra_path_lengthRRt"single_source_shortest_path_lengtht"single_source_dijkstra_path_lengtht NodeNotFoundtformatRtlentdijkstra_path_length(R R R RRt path_lengthtp((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyR•s.K           c stˆƒ}|dkr0d}tj|ƒ‚n|dkr@dSˆjƒrntjˆƒ rntjdƒ‚nˆjƒ rtjˆƒ rtjdƒ‚nˆd kr»‡fd†‰ntj‰‡‡‡fd†‰t ‡fd†ˆDƒƒ}|||dS( s¢Return the average shortest path length. The average shortest path length is .. math:: a =\sum_{s,t \in V} \frac{d(s, t)}{n(n-1)} where `V` is the set of nodes in `G`, `d(s, t)` is the shortest path from `s` to `t`, and `n` is the number of nodes in `G`. Parameters ---------- G : NetworkX graph weight : None or string, optional (default = None) If None, every edge has weight/distance/cost 1. If a string, use this edge attribute as the edge weight. Any edge attribute not present defaults to 1. Raises ------ NetworkXPointlessConcept If `G` is the null graph (that is, the graph on zero nodes). NetworkXError If `G` is not connected (or not weakly connected, in the case of a directed graph). Examples -------- >>> G = nx.path_graph(5) >>> nx.average_shortest_path_length(G) 2.0 For disconnected graphs, you can compute the average shortest path length for each component >>> G = nx.Graph([(1, 2), (3, 4)]) >>> for C in nx.connected_component_subgraphs(G): ... print(nx.average_shortest_path_length(C)) 1.0 1.0 isIthe null graph has no paths, thus there is no averageshortest path lengthisGraph is not weakly connected.sGraph is not connected.c stjˆ|ƒS(N(RR(tv(R (s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pytGsc sˆˆ|dˆƒS(NR((R%(R tssdplR(s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyR&Jsc3 s.|]$}ˆ|ƒjƒD] }|VqqdS(N(tvalues(t.0tutl(R#(s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pys LsN( R!RtNetworkXPointlessConceptt is_directedtis_weakly_connectedt NetworkXErrort is_connectedRRtsum(R Rtntmsgts((R R#R'Rs€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyRs /     c c s…|dk r-tj||d|ƒ\}}ntj||ƒ}||krftjdj|ƒƒ‚n||krtjƒ‚n|dgg}d}xè|dkr€||\}} ||krðgt||d ƒD]\} } | ^qÖVnt||ƒ| kr[|d7}|t|ƒkr@|j ||| dgƒq}||| dg||>> G = nx.Graph() >>> nx.add_path(G, [0, 1, 2]) >>> nx.add_path(G, [0, 10, 2]) >>> print([p for p in nx.all_shortest_paths(G, source=0, target=2)]) [[0, 1, 2], [0, 10, 2]] Notes ----- There may be many shortest paths between the source and target. See Also -------- shortest_path() single_source_shortest_path() all_pairs_shortest_path() RsSource {} is not in GiiN( RRt!dijkstra_predecessor_and_distancet predecessorRR RRR!tappend( R R R RtpredtdisttstackttoptnodetiR$R2((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyRPs*)    / ( t__doc__t __future__RtnetworkxRt__all__RRRRRR(((s€/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/generic.pyts   gr I