3
Md'                 @   s@   d Z ddlmZ ddlmZ dddgZd
ddZd	d ZeZdS )z9Functions for detecting communities based on modularity.
    )
modularity)MappedQueuegreedy_modularity_communities#naive_greedy_modularity_communities$_naive_greedy_modularity_communitiesNc                s&  t  j }tdd  jddD }dd|  dd t j D fd	dt|D  j j |d
fddt|D dd t|D }g }fdd|j D }t |}fddt|D } fddt|D fddt|D t	fddt|D }	xt |	dkr y|	j
 \}
}}W n tk
r`   P Y nX |
 }
| j
  t | dkr|	j| jd  | jd |
 ||fkr|	j|
 ||f | j|
 ||f t | dkr|	j| jd  n| j|
 ||f |
dkr(P t|| || B ||< ||= |j|||
f ||
7 }t| j }t| j }||B ||h }||@ }xl|D ]b|krȈ|  |   }nL|kr|  d||  |   }n |  d||  |   }x|f|fgD ]\}}|krN| |  ||f}nd}|| |< t | dkr| jd }nd}| ||f}|dkr| j| n| j|| |dkr|	j| n*| jd |kr&|	j|| jd  q&W qW | j }x|D ] | } |= |krx|f|fgD ]x\}}| ||f}| jd |kr| j| |	j| t | dkr|	j| jd  n| j| qNW qW |= t	 |< ||  || 7  < d||< q(W fdd|j D }t|t ddS )a   Find communities in graph using Clauset-Newman-Moore greedy modularity
    maximization. This method currently supports the Graph class and does not
    consider edge weights.

    Greedy modularity maximization begins with each node in its own community
    and joins the pair of communities that most increases modularity until no
    such pair exists.

    Parameters
    ----------
    G : NetworkX graph

    Returns
    -------
    Yields sets of nodes, one for each community.

    Examples
    --------
    >>> from networkx.algorithms.community import greedy_modularity_communities
    >>> G = nx.karate_club_graph()
    >>> c = list(greedy_modularity_communities(G))
    >>> sorted(c[0])
    [8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33]

    References
    ----------
    .. [1] M. E. J Newman 'Networks: An Introduction', page 224
       Oxford University Press 2011.
    .. [2] Clauset, A., Newman, M. E., & Moore, C.
       "Finding community structure in very large networks."
       Physical Review E 70(6), 2004.
    c             S   s   g | ]\}}}|j d dqS )weight   )get).0uvd r   ^/var/www/html/virt/lib/python3.6/site-packages/networkx/algorithms/community/modularity_max.py
<listcomp>6   s    z1greedy_modularity_communities.<locals>.<listcomp>T)datag      ?g       @c             S   s   i | ]\}}||qS r   r   )r
   ir   r   r   r   
<dictcomp>:   s    z1greedy_modularity_communities.<locals>.<dictcomp>c                s   i | ]}| | qS r   r   )r
   r   )label_for_noder   r   r   ;   s    )r   c                s   g | ]} |  qS r   r   )r
   r   )k_for_labelr   r   r   r   ?   s    c             S   s   i | ]}t |g|qS r   )	frozenset)r
   r   r   r   r   r   B   s    c                s   g | ]} fd d|D qS )c                s   g | ]} | qS r   r   )r
   x)r   r   r   r   F   s    z<greedy_modularity_communities.<locals>.<listcomp>.<listcomp>r   )r
   c)r   r   r   r   F   s    c                s   g | ]} |  qS r   r   )r
   r   )kq0r   r   r   O   s    c                s<   i | ]4  fd dfddj   D D  qS )c                s:   i | ]2}| krd  d    |     |qS )   r   )r
   j)r   r   r   r   r   r   Q   s   z<greedy_modularity_communities.<locals>.<dictcomp>.<dictcomp>c                s   g | ]} | qS r   r   )r
   r   )node_for_labelr   r   r   S   s    z<greedy_modularity_communities.<locals>.<dictcomp>.<listcomp>)Z	neighbors)r
   )Gr   r   r   r   )r   r   r   P   s   c                s*   g | ]" t  fd d  j D qS )c                s   g | ]\}}|  |fqS r   r   )r
   r   dq)r   r   r   r   Y   s    z<greedy_modularity_communities.<locals>.<listcomp>.<listcomp>)r   items)r
   )dq_dict)r   r   r   Y   s    c                s*   g | ]"}t  | d kr | jd  qS )r   )lenh)r
   r   )dq_heapr   r   r   [   s    r   r   Nc                s"   g | ]}t  fd d|D qS )c                s   g | ]} | qS r   r   )r
   r   )r   r   r   r      s    z<greedy_modularity_communities.<locals>.<listcomp>.<listcomp>)r   )r
   r   )r   r   r   r      s    )keyreverse)r"   nodessumedges	enumeraterangeZdegreevaluesr   r   pop
IndexErrorpushr#   remover   appendsetkeysupdatesorted)r   r   Nmcommunitiesmerges	partitionZq_cnmaHr   r   r   Zi_setZj_setZall_setZboth_setZdq_jkrowcolZd_oldZd_oldmaxr   Zi_neighborsZdq_oldr   )r   r!   r$   r   r   r   r   r   r   r      s    #



" 


"




c             c   s  t dd | j D }g }d}t| |}xF|dks>||krp|}t |}d}xt|D ]\}}xt|D ]\}	}
|	|ksjt|dksjt|
dkrqj||
B ||	< tg ||< t| |}||kr||kr|}||	|| f}n4|ot||	t|d |d k r|}||	|| f}|||< |
||	< qjW qXW |dk	r,|j| |\}}	}|| ||	  }}
||
B ||	< tg ||< q,W dd |D }t|dd d	d
E dH  dS )zFind communities in graph using the greedy modularity maximization.
    This implementation is O(n^4), much slower than alternatives, but it is
    provided as an easy-to-understand reference implementation.
    c             S   s   g | ]}t |gqS r   )r   )r
   r   r   r   r   r      s    z7naive_greedy_modularity_communities.<locals>.<listcomp>Nr   r   c             S   s   g | ]}t |d kr|qS )r   )r"   )r
   r   r   r   r   r     s    c             S   s   t | S )N)r"   )r   r   r   r   <lambda>  s    z5naive_greedy_modularity_communities.<locals>.<lambda>T)r%   r&   )	listr'   r   r*   r"   r   minr1   r5   )r   r8   r9   Zold_modularityZnew_modularityZtrial_communitiesto_merger   r   r   r   Ztrial_modularityr   r   r   r   r      s@    
 

"

)N)	__doc__Z%networkx.algorithms.community.qualityr   Znetworkx.utils.mapped_queuer   __all__r   r   r   r   r   r   r   <module>   s   
 C5