U
    ?hŒ	  ã                   @   sD  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 ]ªZedƒ edƒ 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›ƒ qve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dS )z(Divide a list of nodes `l` in `n` chunksN)ÚiterÚtupleÚ	itertoolsÚislice)ÚlÚnZl_cÚx© r
   ú/var/www/html/venv/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/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  < qŒq„|S )z)Parallel betweenness centrality  function)Ú	processesé   TNr   é   )r   ÚlenÚ_poolÚlistr   ZnodesÚorderÚstarmapÚnxZbetweenness_centrality_subsetÚzip)
ÚGr   ÚpZnode_divisorZnode_chunksZ
num_chunksZbt_scZbt_cÚbtr   r
   r
   r   Úbetweenness_centrality_parallel%   s&    
ûþr   iè  é   g{®Gáz„?r   gš™™™™™¹?Ú z%Computing betweenness centrality for:z	Parallel versionz		Time: z.4Fz secondsz%		Betweenness centrality for node 0: z.5fz	Non-Parallel versionéd   )Z	node_size)N)Ú__doc__Úmultiprocessingr   Útimer   Zmatplotlib.pyplotZpyplotZpltZnetworkxr   r   r   Zbarabasi_albert_graphZG_baZgnp_random_graphZG_erZconnected_watts_strogatz_graphZG_wsr   ÚprintÚstartr   Zbetweenness_centralityZdrawÚshowr
   r
   r
   r   Ú<module>   s6   

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