ó
 ,µ[c           @   sM   d  Z  d d l Z d d l Z d d „ Z d „  Z e d k rI e ƒ  n  d S(   sÎ   
===========
Beam Search
===========

Beam search with dynamic beam width.

The progressive widening beam search repeatedly executes a beam search
with increasing beam width until the target node is found.
iÿÿÿÿNi   c   
      C   s§   | | ƒ r | St  j t  j t |  ƒ d ƒ ƒ } x` t | ƒ D]R } | t d | ƒ } x6 t j |  | | | ƒ D] \ } }	 | |	 ƒ rp |	 Sqp Wq> Wt j d ƒ ‚ d S(   sã  Progressive widening beam search to find a node.

    The progressive widening beam search involves a repeated beam
    search, starting with a small beam width then extending to
    progressively larger beam widths if the target node is not
    found. This implementation simply returns the first node found that
    matches the termination condition.

    `G` is a NetworkX graph.

    `source` is a node in the graph. The search for the node of interest
    begins here and extends only to those nodes in the (weakly)
    connected component of this node.

    `value` is a function that returns a real number indicating how good
    a potential neighbor node is when deciding which neighbor nodes to
    enqueue in the breadth-first search. Only the best nodes within the
    current beam width will be enqueued at each step.

    `condition` is the termination condition for the search. This is a
    function that takes a node as input and return a Boolean indicating
    whether the node is the target. If no node matches the termination
    condition, this function raises :exc:`NodeNotFound`.

    `initial_width` is the starting beam width for the beam search (the
    default is one). If no node matching the `condition` is found with
    this beam width, the beam search is restarted from the `source` node
    with a beam width that is twice as large (so the beam width
    increases exponentially). The search terminates after the beam width
    exceeds the number of nodes in the graph.

    i   s+   no node satisfied the termination conditionN(	   t   matht   ceilt   logt   lent   ranget   powt   nxt   bfs_beam_edgest   NodeNotFound(
   t   Gt   sourcet   valuet	   conditiont   initial_widtht   log_mt   it   widtht   ut   v(    (    s9   share/doc/networkx-2.2/examples/algorithms/beam_search.pyt   progressive_widening_search   s    #!%c             s˜   t  j d d ƒ }  t  j |  ƒ ‰ t ˆ j ƒ  ƒ t |  ƒ ‰  ‡  ‡ f d †  } d } ˆ j } | } t |  | | | ƒ } ˆ | } d j | | ƒ GHd S(   så   Search for a node with high centrality.

    In this example, we generate a random graph, compute the centrality
    of each node, then perform the progressive widening search in order
    to find a node of high centrality.

    id   g      à?c            s   ˆ |  ˆ  k S(   N(    (   R   (   t   avg_centralityt
   centrality(    s9   share/doc/networkx-2.2/examples/algorithms/beam_search.pyt   has_high_centralityX   s    i    s"   found node {0} with centrality {1}N(	   R   t   gnp_random_grapht   eigenvector_centralityt   sumt   valuesR   t   getR   t   format(   R	   R   R
   R   R   t
   found_nodet   c(    (   R   R   s9   share/doc/networkx-2.2/examples/algorithms/beam_search.pyt   mainL   s    	
t   __main__(   t   __doc__R    t   networkxR   R   R   t   __name__(    (    (    s9   share/doc/networkx-2.2/examples/algorithms/beam_search.pyt   <module>   s   9	