K idZddlZddlmZddlmZddgZdZdZed ed ejd Z ed ed ejd d Z y)acRoutines 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_fortree_broadcast_centertree_broadcast_timectt|j||zjd}t fdt |dDS)NTkeyreversec34K|]\}}||zywN).0iuvaluess f/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/networkx/algorithms/broadcasting.py z+_get_max_broadcast_value..sAAvay1}Asstart)sortedset neighborsgetmax enumerate)GUvradjs ` r_get_max_broadcast_valuer!s? Q[[^$q(fjj$ GC A3a)@A AAct|j|jd}tfdt |dD}t |g|d|zS)NTrc3@K|]\}}||zk(s|ywr r )rrrtargetrs rrz)_get_broadcast_centers.. s$ N41afQi!mv6MQ Nsrr)rrrnextrr)rrrr%r js `` r_get_broadcast_centersr(sK QVZZ >C N9S2 NNA sS!W} r"directed multigraphc R tjs tdjdk(rdt j fSjdk(rdt j fSj Dchc] \}}|dk(s |}}}tj|d j}|j||j Dchc] \}}|dk(s |}}} jfd|D|jdk\rt| fd}t|j|}|j||j!||j#||j |dk(r0 j|t%|| i|j||jdk\rtj&j)|}t%|| }|t+| |fScc}}wcc}}w)uAReturn 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) zInput graph is not a treerrc3FK|]}|j|dz fyw)rN)degree)rwrs rrz(tree_broadcast_center..Ts"211ahhqkAo&2s!c|Sr r )nrs rz'tree_broadcast_center..Ys r")r )nxis_treernumber_of_nodesrnodesr.dictfromkeyscopyremove_nodes_fromupdateminr&raddremove remove_noder!utilsarbitrary_elementr() rnodedegrTWr/rb_Trs ` @rrr$s@ ::a=12a#aggi.  a#aggi.   !xx4)$3!84A4 ]]1a F A !xx4)$3!84A4 MM222    " * + Q  a   a 88A;!  MM16q!QGH I EE!H    "" ""1%A "1aF 3C &q!VS9 99; 5 5s HH) H#7H#cNt\}}|tfd|DzStjt }|D]<}t j |jD]\}}|||ks|||<>|t|jzS)aSReturn 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. c3LK|]}tj|ywr )r3shortest_path_length)rrrrBs rrz&tree_broadcast_time..s J00D!<Js!$) rr<r7r8lenr3rIitemsrr)rrBrFb_Cdist_from_centerrrdists`` rrrmsL%Q'HC SJcJJJJ}}QA/ +..q!4::< +GAt&q))&* # ++ %,,./ //r"r ) __doc__networkxr3rnetworkx.utilsr__all__r!r( _dispatchablerrr r"rrTs ".  B  Z \"C:#!C:LZ \"+0#!+0r"