o
    ˷e	                     @   sJ  d Z ddlmZ ddlZddlZddlmZ ddlZ	dd Z
dddZe	dd	Ze	dd
Ze	dddZeeefD ]XZed ed ee	e ed e ZeeZede e dd eded d ed e Ze	eZede e dd eded d q;ed e	jedd e  dS )a  
====================
Parallel Betweenness
====================

Example of parallel implementation of betweenness centrality using the
multiprocessing module from Python Standard Library.

The function betweenness centrality accepts a bunch of nodes and computes
the contribution of those nodes to the betweenness centrality of the whole
network. Here we divide the network in chunks of nodes and we compute their
contribution to the betweenness centrality of the whole network.

Note: The example output below shows that the non-parallel implementation is
faster. This is a limitation of our CI/CD pipeline running on a single core.

Depending on your setup, you will likely observe a speedup.
    )PoolNc                 c   s,    t | }	 tt||}|sdS |V  q)z(Divide a list of nodes `l` in `n` chunks   N)itertuple	itertoolsislice)lnl_cx r   /var/www/ideatree/venv/lib/python3.10/site-packages/../../../share/doc/networkx-2.8.6/examples/algorithms/plot_parallel_betweenness.pychunks   s   r   c           
   
   C   s   t |d}t|jd }tt|  |  | }t|}|tj	t
| g| |t| g| dg| dg| }|d }|dd D ]}|D ]}	||	  ||	 7  < qFqB|S )z)Parallel betweenness centrality  function)	processes   TNr   r   )r   len_poollistr   nodesorderstarmapnxbetweenness_centrality_subsetzip)
Gr   pnode_divisornode_chunks
num_chunksbt_scbt_cbtr	   r   r   r   betweenness_centrality_parallel%   s(   
r"   i     g{Gz?r   g? z%Computing betweenness centrality for:z	Parallel versionz		Time: z.4Fz secondsz%		Betweenness centrality for node 0: z.5fz	Non-Parallel versiond   )	node_size)N)__doc__multiprocessingr   timer   matplotlib.pyplotpyplotpltnetworkxr   r   r"   barabasi_albert_graphG_bagnp_random_graphG_erconnected_watts_strogatz_graphG_wsr   printinfostartr!   betweenness_centralitydrawshowr   r   r   r   <module>   s8    


