U
    ?h	                     @   s   d Z ddlZddlZddlmZ e Z	e
eje	Zejdd eeeD ddgdZdd	 Zd
d Zejddd\ZZee	ed d ee	ed d ejjded ddd e  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                 C   s&   g | ]}|d  t jtt| gqS )   )nx	community
modularityGcommunities).0k r
   x/var/www/html/venv/lib/python3.8/site-packages/../../../share/doc/networkx-3.1/examples/algorithms/plot_girvan_newman.py
<listcomp>   s   r   r	   r   )columnsc                 C   sd   t |d }dddddgd | }g }| D ]4}d}|D ]&}||krT|||   q*|d7 }q6q*|S )Nr   z#D4FCB1z#CDC5FCz#FFC2C4#F2D140z#BCC6C8r   )lenappend)graphr   Znumber_of_colorscolorsnode_colorsnodeZcurrent_community_indexr   r
   r
   r   create_community_node_colors!   s    r   c              	   C   sx   t | |}ttj| |d}dt| d| }tj| dddd}tdd	| t	| tj
| |d
|dddd d S )N   zCommunity Visualization of z  communities with modularity of g333333?2      )r	   Z
iterationsseed   r   i  T   black)posZ	node_sizeZ
node_colorZwith_labelsZ	font_sizeZ
font_color)r   roundr   r   r   r   Zspring_layoutpltZsubplottitleZdraw)r   r   ir   r   r    r   r
   r
   r   visualize_communities0   s    

r"   r   )   r   )Zfigsizer   r   r   z6Modularity Trend for Girvan-Newman Community Detection)xaxcolorr    )__doc__Znetworkxr   ZpandaspdZmatplotlib.pyplotZpyplotr   Zkarate_club_graphr   listr   Zgirvan_newmanr   Z	DataFrameranger   Zmodularity_dfr   r"   ZsubplotsZfigr%   Zplotbarshowr
   r
   r
   r   <module>   s.   

