
    h                         S r SSKrSSKJ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)NetworkXError)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      R/var/www/html/env/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           	      x  ^ ^
 [         R                  " T 5      (       d  [        S5        T R                  5       S:X  a  S[	        T R                  5       5      4$ T R                  5       S:X  a  S[	        T R                  5       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  oDS_M     snm
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T
R                  U 4S jU 5       5        UR                  5       S:  a  [        UU
4S j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/  T
R                  U[!        T X8T
5      05        UR                  U5        UR                  5       S:  a  M  [         R"                  R%                  U5      n[!        T X8T
5      n	U	['        T UT
U	5      4$ s  snnf s  snf s  snnf )u  Return the Broadcast Center of the tree `G`.

The broadcast center of a graph G denotes the set of nodes having
minimum broadcast time [1]_. This is 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 : undirected graph
    The graph should be an undirected tree

Returns
-------
BC : (int, set) tuple
    minimum broadcast number of the tree, set of broadcast centers

Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.

References
----------
.. [1] Slater, P.J., Cockayne, E.J., Hedetniemi, S.T,
   Information dissemination in trees. SIAM J.Comput. 10(4), 692–701 (1981)
zInput graph 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>T   s      21ahhqkAo&s    #c                    > TU    $ r   r   )nr   s    r   <lambda>'tree_broadcast_center.<locals>.<lambda>Y   s	    r#   )r	   )nxis_treer   number_of_nodesr   nodesr0   copyremove_nodes_fromupdateminr(   r   addremoveremove_noder"   utilsarbitrary_elementr*   )r   nodedegr   r4   TWr1   r    b_Tr   s   `         @r   r   r   $   s   @ ::a==12a#aggi.  a#aggi.    !xx4x)$3!8xA4AqdAF	A  !xx4x)$3!8xA4
MM222 



"*+Q  	
a		a 88A;!MM16q!GHIEE!H 



"" 	""1%A
"1aF
3C&q!VS999; 	5
 	5s   H+&H+3H12H6H6c                 X  ^ ^ [        T 5      u  p#Tb  U[        U U4S jU 5       5      -   $ [        R                  T [	        T 5      5      nU H?  n[
        R                  " T U5      R                  5        H  u  pgXtU   :  d  M  XtU'   M     MA     U[        UR                  5       5      -   $ )a  Return the Broadcast Time of the tree `G`.

The minimum broadcast time of a node is defined as the minimum amount
of time required to complete broadcasting starting from the
originator. 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 : undirected graph
    The graph should be an undirected tree
node: int, optional
    index of starting node. If `None`, the algorithm returns the broadcast
    time of the tree.

Returns
-------
BT : int
    Broadcast Time of a node in a tree

Raises
------
NetworkXNotImplemented
    If the graph is directed or is a multigraph.

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.
c              3   T   >#    U  H  n[         R                  " TTU5      v   M     g 7fr   )r7   shortest_path_length)r   r   r   rD   s     r   r   &tree_broadcast_time.<locals>.<genexpr>   s#     Jc00D!<<cs   %()
r   r>   dictfromkeyslenr7   rK   itemsr   r   )r   rD   rH   b_Cdist_from_centerr   r    dists   ``      r   r   r   m   s    L %Q'HCSJcJJJJ}}QA/..q!4::<GAq))&*# =  %,,.///r#   r   )__doc__networkxr7   r   networkx.utilsr   __all__r"   r*   _dispatchabler   r   r   r#   r   <module>rY      s     " . B
 Z \"C:  # !C:L Z \"+0  # !+0r#   