ó žÃÒYc@sndZddlZddlmZddddgZdd„Zed „Z ed „Z d „Z d „Z dS( sBBasic algorithms for breadth-first searching the nodes of a graph.iÿÿÿÿN(tdequet bfs_edgestbfs_treetbfs_predecessorstbfs_successorsccs³|h}t|||ƒfgƒ}xˆ|r®|d\}}yPt|ƒ}||krŒ||fV|j|ƒ|j|||ƒfƒnWq'tk rª|jƒq'Xq'WdS(sãIterate over edges in a breadth-first search. The breadth-first search begins at `source` and enqueues the neighbors of newly visited nodes specified by the `neighbors` function. Parameters ---------- G : NetworkX graph source : node Starting node for the breadth-first search; this function iterates over only those edges in the component reachable from this node. neighbors : function A function that takes a newly visited node of the graph as input and returns an *iterator* (not just a list) of nodes that are neighbors of that node. If not specified, this is just the ``G.neighbors`` method, but in general it can be any function that returns an iterator over some or all of the neighbors of a given node, in any order. Yields ------ edge Edges in the breadth-first search starting from `source`. Examples -------- >>> G = nx.path_graph(3) >>> print(list(nx.bfs_edges(G,0))) [(0, 1), (1, 2)] Notes ----- This implementation is from `PADS`_, which was in the public domain when it was first accessed in July, 2004. .. _PADS: http://www.ics.uci.edu/~eppstein/PADS/BFS.py iN(Rtnexttaddtappendt StopIterationtpopleft(tGtsourcet neighborstvisitedtqueuetparenttchildrentchild((sˆ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/traversal/breadth_first_search.pytgeneric_bfs_edgess+        ccsM|r|jƒr|j}n |j}xt|||ƒD] }|Vq:WdS(sÕIterate over edges in a breadth-first-search starting at source. Parameters ---------- G : NetworkX graph source : node Specify starting node for breadth-first search and return edges in the component reachable from source. reverse : bool, optional If True traverse a directed graph in the reverse direction Returns ------- edges: generator A generator of edges in the breadth-first-search. Examples -------- To get the edges in a breadth-first search:: >>> G = nx.path_graph(3) >>> list(nx.bfs_edges(G, 0)) [(0, 1), (1, 2)] To get the nodes in a breadth-first search order:: >>> G = nx.path_graph(3) >>> root = 2 >>> edges = nx.bfs_edges(G, root) >>> nodes = [root] + [v for u, v in edges] >>> nodes [2, 1, 0] Notes ----- Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py by D. Eppstein, July 2004. N(t is_directedt predecessorsR R(R R treverset successorste((sˆ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/traversal/breadth_first_search.pyRPs )  cCs9tjƒ}|j|ƒ|jt||d|ƒƒ|S(s°Return an oriented tree constructed from of a breadth-first-search starting at source. Parameters ---------- G : NetworkX graph source : node Specify starting node for breadth-first search and return edges in the component reachable from source. reverse : bool, optional If True traverse a directed graph in the reverse direction Returns ------- T: NetworkX DiGraph An oriented tree Examples -------- >>> G = nx.path_graph(3) >>> print(list(nx.bfs_tree(G,1).edges())) [(1, 0), (1, 2)] Notes ----- Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py by D. Eppstein, July 2004. R(tnxtDiGraphtadd_nodetadd_edges_fromR(R R RtT((sˆ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/traversal/breadth_first_search.pyR‚s  ccs/x(t||ƒD]\}}||fVqWdS(s;Returns an iterator of predecessors in breadth-first-search from source. Parameters ---------- G : NetworkX graph source : node Specify starting node for breadth-first search and return edges in the component reachable from source. Returns ------- pred: iterator (node, predecessors) iterator where predecessors is the list of predecessors of the node. Examples -------- >>> G = nx.path_graph(3) >>> print(dict(nx.bfs_predecessors(G, 0))) {1: 0, 2: 1} >>> H = nx.Graph() >>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]) >>> dict(nx.bfs_predecessors(H, 0)) {1: 0, 2: 0, 3: 1, 4: 1, 5: 2, 6: 2} Notes ----- Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py by D. Eppstein, July 2004. N(R(R R tstt((sˆ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/traversal/breadth_first_search.pyR§s ccst|}g}xVt||ƒD]E\}}||krG|j|ƒqn||fV|g}|}qW||fVdS(s.Returns an iterator of successors in breadth-first-search from source. Parameters ---------- G : NetworkX graph source : node Specify starting node for breadth-first search and return edges in the component reachable from source. Returns ------- succ: iterator (node, successors) iterator where successors is the list of successors of the node. Examples -------- >>> G = nx.path_graph(3) >>> print(dict(nx.bfs_successors(G,0))) {0: [1], 1: [2]} >>> H = nx.Graph() >>> H.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)]) >>> dict(nx.bfs_successors(H, 0)) {0: [1, 2], 1: [3, 4], 2: [5, 6]} Notes ----- Based on http://www.ics.uci.edu/~eppstein/PADS/BFS.py by D. Eppstein, July 2004. N(RR(R R RRtptc((sˆ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/traversal/breadth_first_search.pyRËs!     ( t__doc__tnetworkxRt collectionsRt__all__tNoneRtFalseRRRR(((sˆ/private/var/folders/w6/vb91730s7bb1k90y_rnhql1dhvdd44/T/pip-build-w4MwvS/networkx/networkx/algorithms/traversal/breadth_first_search.pyts  9 2 % $