<jupyter_start><jupyter_text>Function to plot communities<jupyter_code># Plot graph with communities, receives a list of communities, where each community is a list of nodes (ints)
def show_communities(G, communities, name='title'):
    pos=nx.spring_layout(G)
    colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k', 'w', 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w', 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w', 'b', 'g', 'r', 'c', 'm', 'y', 'k', 'w']
    plt.figure()
    plt.title(name, fontsize=20)
    aux = 0
    for community in communities:
        nx.draw_networkx_nodes(G, pos, community, node_size = 50, node_color = colors[aux])
        aux = aux + 1
    nx.draw_networkx_edges(G, pos, alpha=0.5)
    plt.show(block=True)

#url = "https://dl.dropboxusercontent.com/s/q9geru5vtmz5b55/vk.adjlist?dl=1"
#with urllib.request.urlopen(url) as f:
#    data = f.read().decode('utf-8')
#with open('vk.adjlist', 'w') as f:
#    f.write(data) 
    
G = nx.read_adjlist('vk.adjlist')
#coord = nx.spring_layout(G, k = 0.15)
G = nx.read_gml(path = 'karate.gml')
nx.draw_networkx(G)
k = dict (G.degree ())
k2 = {key: value for key, value in k.items() if value > 0}
G2 = G.subgraph(list(k2.keys()))
# take only giant connected component for the further analysis: 
G = max(nx.connected_component_subgraphs(G), key=len)
print("Number of nodes -", len(G.nodes()))
print("Number of edges -", len(G.edges()))
methodNames = [
    'Louvain',
    #'<NAME>',
    'Fast Greedy',
    'Label Propagation',
    'Modularity'
]

methods = [
    detect_communities_louvain,
    #detect_communities_girvan_newman,
    detect_communities_greedy,
    detect_communities_label_propagation,
    greedy_modularity_communities
]

for i in range(len(methods)):
    result = methods[i](G)
    show_communities(G, result, name=methodNames[i])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18201754385964913, 'fraction_numerical': 0.01206140350877193, 'mean_word_length': 4.259365994236311, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '347730', 'license': 'Apache-2.0', 'n_tokens_mistral': 730, 'n_tokens_neox': 711, 'n_words': 166, 'path': 'social-network-analysis/Social network Analysis/4th class/Communinites.ipynb', 'repo_name': 'alexlaw19/hse-coursera', 'size': '1831'}