ó žÃÒYc@s²dZddlZddddddd d gZdd „Zd „Zdd „Zdd„Zd„Z d„Z dd„Z d„Z dd„Z dd„Zdddd„ZdS(s1 Shortest path algorithms for unweighted graphs. iÿÿÿÿNtbidirectional_shortest_pathtsingle_source_shortest_patht"single_source_shortest_path_lengthtsingle_target_shortest_patht"single_target_shortest_path_lengthtall_pairs_shortest_pathtall_pairs_shortest_path_lengtht predecessorcCsh||kr'tjdj|ƒƒ‚n|dkrBtdƒ}nid|6}tt|j||ƒƒS(sËCompute the shortest path lengths from source to all reachable nodes. Parameters ---------- G : NetworkX graph source : node Starting node for path cutoff : integer, optional Depth to stop the search. Only paths of length <= cutoff are returned. Returns ------- lengths : dict Dict keyed by node to shortest path length to source. Examples -------- >>> G = nx.path_graph(5) >>> length = nx.single_source_shortest_path_length(G, 0) >>> length[4] 4 >>> for node in length: ... print('{}: {}'.format(node, length[node])) 0: 0 1: 1 2: 2 3: 3 4: 4 See Also -------- shortest_path_length sSource {} is not in GtinfiN(tnxt NodeNotFoundtformattNonetfloattdictt_single_shortest_path_lengthtadj(tGtsourcetcutofft nextlevel((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyRs $   ccsŽi}d}|}xr|r†||kr†|}i}xC|D];}||kr:|||<|j||ƒ||fVq:q:W|d7}qW~dS(sTYields (node, level) in a breadth first search Shortest Path Length helper function Parameters ---------- adj : dict Adjacency dict or view firstlevel : dict starting nodes, e.g. {source: 1} or {target: 1} cutoff : int or float level at which we stop the process iiN(tupdate(Rt firstlevelRtseentlevelRt thisleveltv((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyREs    cCs}||kr'tjdjtƒƒ‚n|dkrBtdƒ}n|jƒrW|jn|j}id|6}t |||ƒS(sCompute the shortest path lengths to target from all reachable nodes. Parameters ---------- G : NetworkX graph target : node Target node for path cutoff : integer, optional Depth to stop the search. Only paths of length <= cutoff are returned. Returns ------- lengths : iterator (source, shortest path length) iterator Examples -------- >>> G = nx.path_graph(5, create_using=nx.DiGraph()) >>> length = dict(nx.single_target_shortest_path_length(G, 4)) >>> length[0] 4 >>> for node in range(5): ... print('{}: {}'.format(node, length[node])) 0: 4 1: 3 2: 2 3: 1 4: 0 See Also -------- single_source_shortest_path_length, shortest_path_length sTarget {} is not in GRiN( R R R RR R t is_directedtpredRR(RttargetRRR((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyRbs$   ccs5t}x(|D] }||||d|ƒfVq WdS(sDComputes the shortest path lengths between all nodes in `G`. Parameters ---------- G : NetworkX graph cutoff : integer, optional Depth at which to stop the search. Only paths of length at most `cutoff` are returned. Returns ------- lengths : iterator (source, dictionary) iterator with dictionary keyed by target and shortest path length as the key value. Notes ----- The iterator returned only has reachable node pairs. Examples -------- >>> G = nx.path_graph(5) >>> length = dict(nx.all_pairs_shortest_path_length(G)) >>> for node in [0, 1, 2, 3, 4]: ... print('1 - {}: {}'.format(node, length[1][node])) 1 - 0: 1 1 - 1: 0 1 - 2: 1 1 - 3: 2 1 - 4: 3 >>> length[3][2] 1 >>> length[2][2] 0 RN(R(RRtlengthtn((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyR‘s& c CsÓ||ks||kr<d}tj|j||ƒƒ‚nt|||ƒ}|\}}}g}x'|dk rŒ|j|ƒ||}qfW|jƒ||d}x'|dk rÎ|j|ƒ||}q¨W|S(s)Return a list of nodes in a shortest path between source and target. Parameters ---------- G : NetworkX graph source : node label starting node for path target : node label ending node for path Returns ------- path: list List of nodes in a path from source to target. Raises ------ NetworkXNoPath If no path exists between source and target. See Also -------- shortest_path Notes ----- This algorithm is used by shortest_path(G, source, target). s)Either source {} or target {} is not in GiÿÿÿÿN(R R R t_bidirectional_pred_succR tappendtreverse( RRRtmsgtresultsRtsucctwtpath((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyR½s    c Cs³||kr'id|6id|6|fS|jƒrH|j}|j}n|j}|j}id|6}id|6}|g}|g}x |r•|r•t|ƒt|ƒkr!|} g}xÖ| D]Z} xQ|| D]E} | |krý|j| ƒ| || >> G = nx.path_graph(5) >>> path = nx.single_source_shortest_path(G, 0) >>> path[4] [0, 1, 2, 3, 4] Notes ----- The shortest path is not necessarily unique. So there can be multiple paths between the source and each target node, all of which have the same 'shortest' length. For each target node, this function returns only one of those paths. See Also -------- shortest_path sSource {} not in GcSs||S(N((tp1tp2((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pytjoinQsRiN(R R R R R Rt_single_shortest_pathR(RRRR1Rtpaths((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyR)s%    c Cs˜d}|}x…|r“||kr“|}i}xV|D]N}xE||D]9} | |krE|||| gƒ|| >> G = nx.path_graph(5, create_using=nx.DiGraph()) >>> path = nx.single_target_shortest_path(G, 4) >>> path[0] [0, 1, 2, 3, 4] Notes ----- The shortest path is not necessarily unique. So there can be multiple paths between the source and each target node, all of which have the same 'shortest' length. For each target node, this function returns only one of those paths. See Also -------- shortest_path, single_source_shortest_path sTarget {} not in GcSs||S(N((R/R0((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyR1¢sRiN( R R R RRRRR R RR2(RRRR1RRR3((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyR{s$    ccs/x(|D] }|t||d|ƒfVqWdS(s*Compute shortest paths between all nodes. Parameters ---------- G : NetworkX graph cutoff : integer, optional Depth at which to stop the search. Only paths of length at most `cutoff` are returned. Returns ------- lengths : dictionary Dictionary, keyed by source and target, of shortest paths. Examples -------- >>> G = nx.path_graph(5) >>> path = dict(nx.all_pairs_shortest_path(G)) >>> print(path[0][4]) [0, 1, 2, 3, 4] See Also -------- floyd_warshall() RN(R(RRR((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyR­s c Css||kr'tjdj|ƒƒ‚nd}|g}i||6}ig|6}x³|r|d}|} g}xz| D]r} xi|| D]]} | |krÀ| g|| <||| <|j| ƒq‡|| |kr‡|| j| ƒq‡q‡WqvW|rS||krSPqSqSW|dk r[|r@||kr.gdfS||||fS||krPgS||Sn|rk||fS|SdS(sÉReturns dict of predecessors for the path from source to all nodes in G Parameters ---------- G : NetworkX graph source : node label Starting node for path target : node label, optional Ending node for path. If provided only predecessors between source and target are returned cutoff : integer, optional Depth to stop the search. Only paths of length <= cutoff are returned. Returns ------- pred : dictionary Dictionary, keyed by node, of predecessors in the shortest path. Examples -------- >>> G = nx.path_graph(4) >>> list(G) [0, 1, 2, 3] >>> nx.predecessor(G, 0) {0: [], 1: [0], 2: [1], 3: [2]} sSource {} not in GiiiÿÿÿÿN(R R R R!R ( RRRRt return_seenRRRRRRR&((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyRÎs>!                (t__doc__tnetworkxR t__all__R RRRRRR RR2RRR(((sƒ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/shortest_paths/unweighted.pyt s(   ,  / , 8 4 1 ! 2 !