
    Gd	                        d Z ddlZddlZddlmZ  ej                    Z	 e
ej                            e	                    Z ej        d  e ee                    D             ddg          Zd Zd Z ej        d	d
          \  ZZ ee	ed         d            ee	ed	         d           ej                            ded         dd            ej                     dS )a  
=======================================
Community Detection using Girvan-Newman
=======================================

This example shows the detection of communities in the Zachary Karate
Club dataset using the Girvan-Newman method.

We plot the change in modularity as important edges are removed. 
Graph is coloured and plotted based on community detection when number 
of iterations are 1 and 4 respectively.
    Nc                 x    g | ]7}|d z   t           j                            t          t          |                   g8S )   )nx	community
modularityGcommunities).0ks     @share/doc/networkx-3.1/examples/algorithms/plot_girvan_newman.py
<listcomp>r      sG        
Q'';q>::;      r   r   )columnsc                     t          |d                   }g dd |         }g }| D ]/}d}|D ](}||v r|                    ||                     n|dz  })0|S )Nr   )z#D4FCB1z#CDC5FCz#FFC2C4#F2D140z#BCC6C8r   )lenappend)graphr	   number_of_colorscolorsnode_colorsnodecurrent_community_indexr   s           r   create_community_node_colorsr   !   s    ;q>**DDDEVFVEVWFK ) )"#$ 	) 	)Iy  ""6*A#BCCC#q(##r   c           	      f   t          | |          }t          t          j                            | |          d          }dt          |           d| }t          j        | ddd          }t          j        dd	|           t          j	        |           t          j
        | |d
|ddd           d S )N   zCommunity Visualization of z  communities with modularity of g333333?2      )r   
iterationsseed   r   i  T   black)pos	node_size
node_colorwith_labels	font_size
font_color)r   roundr   r   r   r   spring_layoutpltsubplottitledraw)r   r	   ir   r   r.   r$   s          r   visualize_communitiesr1   0   s    .ukBBKr|..ukBBAFFJh#k*:*:hh\fhhE

5CBQ
?
?
?CK1aIeG     r   r!   )   r"   )figsizer   r   r   z6Modularity Trend for Girvan-Newman Community Detection)xaxcolorr.   )__doc__networkxr   pandaspdmatplotlib.pyplotpyplotr,   karate_club_graphr   listr   girvan_newmanr	   	DataFrameranger   modularity_dfr   r1   subplotsfigr5   plotbarshow r   r   <module>rI      ss                  Bd2<--a0011  ss;''((   ,      $ #,q(
+
+
+R  aQ + + +  aQ + + +    		!u

B	     	




r   