ó
 ,µ[c           @   s~  d  Z  d d l m Z d d l Z d d l Z d d l j Z d d l Z	 d „  Z
 d „  Z d d „ Z e d k rze	 j d d	 ƒ Z e	 j d d
 ƒ Z e	 j d d d ƒ Z x¬ e e e g D]› Z d GHd GHe	 j e ƒ GHd GHe j ƒ  Z e e ƒ Z d e j ƒ  e GHd e d GHd GHe j ƒ  Z e	 j e ƒ Z d e j ƒ  e GHd e d GHq¼ Wd GHe	 j e ƒ e j ƒ  n  d S(   s¸  
====================
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.

This doesn't work in python2.7.13. It does work in 3.6, 3.5, 3.4, and 3.3.

It may be related to this:
https://stackoverflow.com/questions/1816958/cant-pickle-type-instancemethod-when-using-multiprocessing-pool-map
iÿÿÿÿ(   t   PoolNc         c   s>   t  |  ƒ } x+ t t j | | ƒ ƒ } | s1 d S| Vq Wd S(   s(   Divide a list of nodes `l` in `n` chunksN(   t   itert   tuplet	   itertoolst   islice(   t   lt   nt   l_ct   x(    (    sE   share/doc/networkx-2.2/examples/advanced/plot_parallel_betweenness.pyt   chunks   s    c         C   s   t  j |  Œ  S(   sø   Pool for multiprocess only accepts functions with one argument.
    This function uses a tuple as its only argument. We use a named tuple for
    python 3 compatibility, and then unpack it when we send it to
    `betweenness_centrality_source`
    (   t   nxt   betweenness_centrality_source(   t!   G_normalized_weight_sources_tuple(    (    sE   share/doc/networkx-2.2/examples/advanced/plot_parallel_betweenness.pyt   _betmap&   s    c   
      C   s×   t  d | ƒ } t | j ƒ d } t t |  j ƒ  t |  j ƒ  | ƒ ƒ ƒ } t | ƒ } | j t	 t
 |  g | t g | d g | | ƒ ƒ } | d } x7 | d D]+ } x" | D] }	 | |	 c | |	 7<q± Wq¤ W| S(   s)   Parallel betweenness centrality  functiont	   processesi   i    i   N(   R    t   lent   _poolt   listR	   t   nodest   intt   ordert   mapR   t   zipt   Truet   None(
   t   GR   t   pt   node_divisort   node_chunkst
   num_chunkst   bt_sct   bt_ct   btR   (    (    sE   share/doc/networkx-2.2/examples/advanced/plot_parallel_betweenness.pyt   betweenness_centrality_parallel/   s    +	


t   __main__iè  i   g{®Gáz„?i   gš™™™™™¹?t    s%   Computing betweenness centrality for:s   	Parallel versions   		Time: %.4Fs)   		Betweenness centrality for node 0: %.5fi    s   	Non-Parallel versions   		Time: %.4F seconds(   t   __doc__t   multiprocessingR    t   timeR   t   matplotlib.pyplott   pyplott   pltt   networkxR
   R	   R   R   R!   t   __name__t   barabasi_albert_grapht   G_bat   gnp_random_grapht   G_ert   connected_watts_strogatz_grapht   G_wsR   t   infot   startR    t   betweenness_centralityt   drawt   show(    (    (    sE   share/doc/networkx-2.2/examples/advanced/plot_parallel_betweenness.pyt   <module>   s:   	
		