ó
 ,µ[c           @   s;   d  Z  d d l Z d d l m Z d g Z d d „ Z d S(   sB   Basic algorithms for breadth-first searching the nodes of a graph.iÿÿÿÿNi   (   t   generic_bfs_edgest   bfs_beam_edgesc         #   sV   ˆ d k r t ˆ  ƒ ‰ n  ‡  ‡ ‡ f d †  } x t ˆ  | | ƒ D] } | VqC Wd S(   s°  Iterates over edges in a beam search.

    The beam search is a generalized breadth-first search in which only
    the "best" *w* neighbors of the current node are enqueued, where *w*
    is the beam width and "best" is an application-specific
    heuristic. In general, a beam search with a small beam width might
    not visit each node in the graph.

    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.

    value : function
        A function that takes a node of the graph as input and returns a
        real number indicating how "good" it is. A higher value means it
        is more likely to be visited sooner during the search. When
        visiting a new node, only the `width` neighbors with the highest
        `value` are enqueued (in decreasing order of `value`).

    width : int (default = None)
        The beam width for the search. This is the number of neighbors
        (ordered by `value`) to enqueue when visiting each new node.

    Yields
    ------
    edge
        Edges in the beam search starting from `source`, given as a pair
        of nodes.

    Examples
    --------
    To give nodes with, for example, a higher centrality precedence
    during the search, set the `value` function to return the centrality
    value of the node::

        >>> G = nx.karate_club_graph()
        >>> centrality = nx.eigenvector_centrality(G)
        >>> source = 0
        >>> width = 5
        >>> for u, v in nx.bfs_beam_edges(G, source, centrality.get, width):
        ...     print((u, v))  # doctest: +SKIP

    c            s)   t  t ˆ  j |  ƒ d ˆ d t ƒˆ  ƒ S(   sv  Returns a list of the best neighbors of a node.

        `v` is a node in the graph `G`.

        The "best" neighbors are chosen according to the `value`
        function (higher is better). Only the `width` best neighbors of
        `v` are returned.

        The list returned by this function is in decreasing value as
        measured by the `value` function.

        t   keyt   reverse(   t   itert   sortedt	   neighborst   True(   t   v(   t   Gt   valuet   width(    sG   lib/python2.7/site-packages/networkx/algorithms/traversal/beamsearch.pyt
   successorsF   s    N(   t   Nonet   lenR    (   R	   t   sourceR
   R   R   t   e(    (   R	   R
   R   sG   lib/python2.7/site-packages/networkx/algorithms/traversal/beamsearch.pyR      s
    2(   t   __doc__t   networkxt   nxt   breadth_first_searchR    t   __all__R   R   (    (    (    sG   lib/python2.7/site-packages/networkx/algorithms/traversal/beamsearch.pyt   <module>	   s   	