
    wg                         d Z ddlZddlm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)NetworkXError)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      e/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/networkx/algorithms/broadcasting.py	<genexpr>z+_get_max_broadcast_value.<locals>.<genexpr>   s     AAvay1}As      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$     N41afQi!mv6MQNs   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        j                         st        d        j                         dk(  rdt	         j                               fS  j                         dk(  rdt	         j                               fS  j                  D ch c]  \  }}|dk(  s| }}}|D ci c]  }|d c}
 j                         }|j                  |       |j                  D ch c]  \  }}|dk(  s| }}}
j                   fd|D               |j                         dk\  rt        |
fd      }t        |j                  |            }|j                  |       |j                  |       |j                  |       |j                  |      dk(  r0
j                  |t!         ||
      i       |j                  |       |j                         dk\  rt         j"                  j%                  |      }t!         ||
      }	|	t'         |
|	      fS c c}}w c c}w c c}}w )uA  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   F   K   | ]  }|j                   |   d z
  f  yw)r   N)degree)r   wr   s     r   r   z(tree_broadcast_center.<locals>.<genexpr>T   s"     211ahhqkAo&2s   !c                     |    S r   r   )nr   s    r   <lambda>z'tree_broadcast_center.<locals>.<lambda>Y   s     r"   )r	   )nxis_treer   number_of_nodesr   nodesr.   copyremove_nodes_fromupdateminr&   r   addremoveremove_noder!   utilsarbitrary_elementr(   )r   nodedegr   r1   TWr/   r   b_Tr   s   `         @r   r   r   $   s   @ ::a=12a#aggi.  a#aggi.    !xx4)$3!84A4qadF	A  !xx4)$3!84A4
MM222 



"*+Q  	
a		a 88A;!MM16q!QGHIEE!H 



"" 	""1%A
"1aF
3C&q!VS999; 	5
 	5s   HH'
H$H#2H#c                 N    t               \  }}|t         fd|D              z   S t        j                   t	                     }|D ]<  }t        j                   |      j                         D ]  \  }}|||   k  s|||<    > |t        |j                               z   S )aS  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   L   K   | ]  }t        j                  |        y wr   )r3   shortest_path_length)r   r   r   r@   s     r   r   z&tree_broadcast_time.<locals>.<genexpr>   s      J00D!<Js   !$)
r   r:   dictfromkeyslenr3   rG   itemsr   r   )r   r@   rD   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::< 	+GAt&q))&* #	++ %,,.///r"   r   )__doc__networkxr3   r   networkx.utilsr   __all__r!   r(   _dispatchabler   r   r   r"   r   <module>rT      s     " . B
 Z \"C:  # !C:L Z \"+0  # !+0r"   