U
    ?h                     @   s   d Z ddlZddlmZ ddlZdddZdZej	dded	Z
ee
Zee ee
 Zd
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d	ZddddddZeje
efe eje
eegddd e  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           
      C   sx   ||r|S t t t| }t|D ]@}|td| }t| |||D ]\}}	||	rJ|	    S qJq(tddS )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 conditionN)	mathceillog2lenrangepownxZbfs_beam_edgesZNodeNotFound)
Gsourcevalue	conditionZinitial_widthZlog_miwidthuv r   v/var/www/html/venv/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/examples/algorithms/plot_beam_search.pyprogressive_widening_search   s    #r   Y   d   g      ?)seedc                 C   s   t |  tkS )N)
centralityavg_centrality)r   r   r   r   has_high_centralityW   s    r   zfound node z with centrality blue   Zgreyg?)
node_color	node_sizeZ
edge_colorZ
linewidthsr   r)Znodelistr   r   )r   )__doc__r   Zmatplotlib.pyplotZpyplotZpltZnetworkxr
   r   r   Zgnp_random_graphr   Zeigenvector_centralityr   sumvaluesr   r   r   r   getr   r   Z
found_nodecprintZspring_layoutposoptionsZdrawZdraw_networkx_nodesshowr   r   r   r   <module>   s2   

?
