
    9in                         S r SSKrSSKJr  SS/rS rS r\" S5      \" S	5      \R                  S
 5       5       5       r	\" S5      \" S	5      \R                  SS j5       5       5       r
g)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                    ^ [        [        U R                  U5      5      U-  TR                  SS9n[	        U4S j[        USS9 5       5      $ )NTkeyreversec              3   8   >#    U  H  u  pTU   U-   v   M     g 7fN ).0iuvaluess      `/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/networkx/algorithms/broadcasting.py	<genexpr>+_get_max_broadcast_value.<locals>.<genexpr>   s     A)@vay1})@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                    ^^ [        U R                  U5      TR                  SS9n[        UU4S j[	        USS9 5       5      n[        U/US U -   5      $ )NTr   c              3   H   >#    U  H  u  pTU   U-   T:X  d  M  Uv   M     g 7fr   r   )r   r   r   targetr   s      r   r   )_get_broadcast_centers.<locals>.<genexpr>   s&     N241fQi!mv6MQQ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           	        ^  [         R                  " T 5      (       d  [         R                  " S5      e[        T 5      =nS:  a  US-
  [	        T 5      4$ T R
                   VVs1 s H  u  p#US:X  d  M  UiM     nnnU Vs0 s H  oS_M     nnT R                  5       nUR                  U5        UR
                   VVs1 s H  u  p#US:X  d  M  UiM     nnnUR                  U 4S jU 5       5        [        U5      S:  a  [        XuR                  S9n[        UR                  U5      5      n	UR                  U5        UR                  U5        UR                  U5        UR                  U	5      S:X  a/  UR                  U	[!        T XIU5      05        UR                  U	5        [        U5      S:  a  M  [         R"                  R%                  U5      n	[!        T XIU5      n
U
['        T XU
5      4$ s  snnf s  snf s  snnf )u7  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   J   >#    U  H  oTR                   U   S -
  4v   M     g7fr   N)degree)r   wr   s     r   r   (tree_broadcast_center.<locals>.<genexpr>U   s      21ahhqkAo&s    #   )r   )nxis_treeNotATreelenr   r0   copyremove_nodes_fromupdateminr   r'   r   addremoveremove_noder!   utilsarbitrary_elementr)   )r   nnodedegr   r   TWr1   r   b_Ts   `          r   r   r   #   s   H ::a==kk+,,Vq1uc!f}  !xx4x)$3!8xA4AqdAF	A  !xx4x)$3!8xA4
MM222 a&A+zz"Q  	
a		a 88A;!MM16q!GHIEE!H a&A+" 	""1%A
"1aF
3C&q!S999; 	5
 	5s   #G13G1 G7>G<G<c           
      0  ^ Tb"  TU ;  a  ST S3n[         R                  " U5      e[        U 5      u  p4Tc,  U[        S [         R                  " X5       5       5      -   S-
  $ U[        U4S j[        [         R                  " X5      5       5       5      -   $ )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   &   #    U  H  nS v   M	     g7fr/   r   )r   _s     r   r   &tree_broadcast_time.<locals>.<genexpr>   s     :$9q$9s   r   c              3   <   >#    U  H  u  pTU;   d  M  Uv   M     g 7fr   r   )r   dlayerrB   s      r   r   rJ      s      :hadem:s   	)r4   NodeNotFoundr   sum
bfs_layersr'   r   )r   rB   errrF   b_Cs    `   r   r   r   n   s    \ DMdV9%ooc""$Q'HC|S:BMM!$9:::Q>> #BMM!$9:   r"   r   )__doc__networkxr4   networkx.utilsr   __all__r!   r)   _dispatchabler   r   r   r"   r   <module>rX      s     . B
 Z \"E:  # !E:P Z \"3  # !3r"   