
    Gd                        d Z ddlZddlmZ ddlZddZdZ ej	        dde          Z
 ej        e
          Z ee                                           ee
          z  Zd	 ZdZej        ZeZ ee
eee          Zee         Z ed
e de             ej        e
e          ZddddddZ ej        e
efi e  ej        e
eegdd            ej                     dS )z
===========
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.
    N   c                 P    ||          r|S t          j        t          j        t          |                               }t	          |          D ]C}|t          d|          z  }t          j        | |||          D ]\  }}	 ||	          r|	c c S Dt          j        d          )a  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.

       z+no node satisfied the termination condition)	mathceillog2lenrangepownxbfs_beam_edgesNodeNotFound)
Gsourcevalue	conditioninitial_widthlog_miwidthuvs
             >share/doc/networkx-3.1/examples/algorithms/plot_beam_search.pyprogressive_widening_searchr      s    F y 
 IdiA''((E5\\  Aq		) %a>> 	 	DAqy|| 	
 /G
H
HH    Y   d   g      ?)seedc                 .    t           |          t          k    S )N)
centralityavg_centrality)r   s    r   has_high_centralityr"   W   s    a=N**r   zfound node z with centrality blue   greyg?)
node_color	node_size
edge_color
linewidthsr   r)nodelistr'   r&   )r   )__doc__r   matplotlib.pyplotpyplotpltnetworkxr   r   r   gnp_random_graphr   eigenvector_centralityr    sumvaluesr	   r!   r"   r   getr   r   
found_nodecprintspring_layoutposoptionsdrawdraw_networkx_nodesshow r   r   <module>r@      s  	 	           4I 4I 4I 4I~ 
BSt,,,&R&q))
Z&&(())CCFF2+ + + 
	((FE9EE
z 4J4444 5 5 5 bqt$$$  3  '     q#PS T T T T 




r   