
    ܛ7in                         d Z ddlZddlmZ ddgZd Zd Z ed       ed	      ej                  d
                      Z	 ed       ed	      ej                  dd                     Z
y)ac  Routines to calculate the broadcast time of certain graphs.

Broadcasting is an information dissemination problem in which a node in a graph,
called the originator, must distribute a message to all other nodes by placing
a series of calls along the edges of the graph. Once informed, other nodes aid
the originator in distributing the message.

The broadcasting must be completed as quickly as possible subject to the
following constraints:
- Each call requires one unit of time.
- A node can only participate in one call per unit of time.
- Each call only involves two adjacent nodes: a sender and a receiver.
    N)not_implemented_fortree_broadcast_centertree_broadcast_timec                     t        t        | j                  |            |z  j                  d      }t	        fdt        |d      D              S )NTkeyreversec              3   4   K   | ]  \  }}|   |z     y wN ).0iuvaluess      ]/home/rose/Desktop/poly/venv/lib/python3.12/site-packages/networkx/algorithms/broadcasting.py	<genexpr>z+_get_max_broadcast_value.<locals>.<genexpr>   s      A)@Avay1})@s      start)sortedset	neighborsgetmax	enumerate)GUvr   adjs      ` r   _get_max_broadcast_valuer       s?    
Q[[^$q(fjj$
GCA3a)@AAA    c                     t        | j                  |      j                  d      }t        fdt	        |d      D              }t        |g|d | z         S )NTr   c              3   @   K   | ]  \  }}|   |z   k(  s|  y wr   r   )r   r   r   targetr   s      r   r   z)_get_broadcast_centers.<locals>.<genexpr>   s'     N241afQi!mv6MQ2s   r   r   )r   r   r   nextr   r   )r   r   r   r$   r   js     ``  r   _get_broadcast_centersr'      sK    
QVZZ
>CN9S2NNAsS!W}r!   directed
multigraphc           	          t        j                         st        j                  d      t               x}dk  r|dz
  t	               fS  j
                  D ch c]  \  }}|dk(  s| }}}|D ci c]  }|d }} j                         }|j                  |       |j
                  D ch c]  \  }}|dk(  s| }}}|j                   fd|D               t        |      dk\  rt        ||j                        }t        |j                  |            }	|j                  |       |j                  |       |j                  |       |j                  |	      dk(  r0|j                  |	t!         ||	|      i       |j                  |	       t        |      dk\  rt         j"                  j%                  |      }	t!         ||	|      }
|
t'         |	||
      fS c c}}w c c}w c c}}w )u  Return the broadcast center of a tree.

    The broadcast center of a graph `G` denotes the set of nodes having
    minimum broadcast time [1]_. This function implements a linear algorithm
    for determining the broadcast center of a tree with ``n`` nodes. As a
    by-product, it also determines the broadcast time from the broadcast center.

    Parameters
    ----------
    G : Graph
        The graph should be an undirected tree.

    Returns
    -------
    b_T, b_C : (int, set) tuple
        Minimum broadcast time of the broadcast center in `G`, set of nodes
        in the broadcast center.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is directed or is a multigraph.

    NotATree
        If `G` is not a tree.

    References
    ----------
    .. [1] Slater, P.J., Cockayne, E.J., Hedetniemi, S.T,
       Information dissemination in trees. SIAM J.Comput. 10(4), 692–701 (1981)
    zG is not a tree   r   r   c              3   F   K   | ]  }|j                   |   d z
  f  ywr   N)degree)r   wr   s     r   r   z(tree_broadcast_center.<locals>.<genexpr>U   s#     211ahhqkAo&s   !   )r   )nxis_treeNotATreelenr   r.   copyremove_nodes_fromupdateminr   r%   r   addremoveremove_noder    utilsarbitrary_elementr'   )r   nnodedegr   r   TWr/   r   b_Ts   `          r   r   r   #   s   H ::a=kk+,,Vq1uc!f}  !xx4x)$3!8xA4AqadAF	A  !xx4x)$3!8xA4
MM222 a&A+vzz"Q  	
a		a 88A;!MM16q!QGHIEE!H a&A+" 	""1%A
"1aF
3C&q!VS999; 	5
 	5s   G!)G!4
G'0G,>G,c           
      &   | vrd d}t        j                  |      t        |       \  }},|t        d t        j                  | |      D              z   dz
  S |t        fdt        t        j                  | |            D              z   S )a  Return the minimum broadcast time of a (node in a) tree.

    The minimum broadcast time of a node is defined as the minimum amount
    of time required to complete broadcasting starting from that node.
    The broadcast time of a graph is the maximum over
    all nodes of the minimum broadcast time from that node [1]_.
    This function returns the minimum broadcast time of `node`.
    If `node` is `None`, the broadcast time for the graph is returned.

    Parameters
    ----------
    G : Graph
        The graph should be an undirected tree.

    node : node, optional (default=None)
        Starting node for the broadcasting. If `None`, the algorithm
        returns the broadcast time of the graph instead.

    Returns
    -------
    int
        Minimum broadcast time of `node` in `G`, or broadcast time of `G`
        if no node is provided.

    Raises
    ------
    NetworkXNotImplemented
        If `G` is directed or is a multigraph.

    NodeNotFound
        If `node` is not a node in `G`.

    NotATree
        If `G` is not a tree.

    References
    ----------
    .. [1] Harutyunyan, H. A. and Li, Z.
        "A Simple Construction of Broadcast Graphs."
        In Computing and Combinatorics. COCOON 2019
        (Ed. D. Z. Du and C. Tian.) Springer, pp. 240-253, 2019.
    znode z	 not in Gc              3       K   | ]  }d   ywr-   r   )r   _s     r   r   z&tree_broadcast_time.<locals>.<genexpr>   s     :$9q$9s   r   c              3   2   K   | ]  \  }}|v s|  y wr   r   )r   dlayerr?   s      r   r   z&tree_broadcast_time.<locals>.<genexpr>   s       :hadem:s   )r1   NodeNotFoundr   sum
bfs_layersr%   r   )r   r?   errrC   b_Cs    `   r   r   r   n   s    \ DMdV9%ooc""$Q'HC|S:BMM!S$9:::Q>> #BMM!S$9:   r!   r   )__doc__networkxr1   networkx.utilsr   __all__r    r'   _dispatchabler   r   r   r!   r   <module>rT      s     . B
 Z \"E:  # !E:P Z \"3  # !3r!   