0% found this document useful (0 votes)
5 views

2_EXAMPLE_dendrogram_from_girvan_newman.ipynb - Colab

This document outlines the process of using the Girvan-Newman algorithm for community detection in a graph and how to visualize the results using a dendrogram. It details the steps for generating a network, performing community detection, creating an agglomerative matrix, and plotting the dendrogram using the scipy library. Additionally, it identifies the best community partition based on modularity and highlights it in the dendrogram.

Uploaded by

fracrusan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

2_EXAMPLE_dendrogram_from_girvan_newman.ipynb - Colab

This document outlines the process of using the Girvan-Newman algorithm for community detection in a graph and how to visualize the results using a dendrogram. It details the steps for generating a network, performing community detection, creating an agglomerative matrix, and plotting the dendrogram using the scipy library. Additionally, it identifies the best community partition based on modularity and highlights it in the dendrogram.

Uploaded by

fracrusan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

keyboard_arrow_down Dendrogram from community detection by Girvan-Newman

In this notebook we will show how to use the method dendrogram_from_girvan_newman.py to plot a dendrogram from community detection
performed by the Girvan-Newman algorithm.

The Girvan–Newman algorithm detects communities by progressively removing edges from the original graph. The algorithm removes the
"most valuable" edge, traditionally the edge with the highest betweenness centrality, at each step. As the graph breaks down into pieces, the
tightly knit community structure is exposed and the result can be depicted as a dendrogram.

Usually the scipy.cluster.hierarchy.dendrogram function is used to draw dendrograms. It receives in input a matrix which encodes the
hierarchical clustering to render as a dendrogram. For many cases it is enough to have this matrix generated by the
scipy.cluster.hierarchy.linkage function, but not so, for example, in graph community detection.

The dendrogram_from_girvan_newman module provides functions that generate a matrix of this type (named agglomerative matrix ) starting
from the community reconstruction of a graph, performed with the 'girvan_newman' function from
networkx.algorithms.community.centrality . The agglomerative matrix is suitable to be taken in input by the
scipy.cluster.hierarchy.dendrogram function.

import networkx as nx

Run this cell to load the functions of dendrogram_from_girvan_newman library


keyboard_arrow_down

[ ] ↳ 1 celda oculta

keyboard_arrow_down Start of the practice


# 1) Generate a simple network.

# nodes and edges


nodes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
edges = [(0, 1), (0, 10), (0, 11), (1, 3), (3, 2), (3, 4), (3, 5), (4, 2),
(4, 5), (5, 2), (5, 6), (5, 7), (5, 8), (8, 9)]

# graph
G = nx.Graph()
G.add_nodes_from(nodes)
G.add_edges_from(edges)

# info
nn = nx.number_of_nodes(G)
ne = nx.number_of_edges(G)
print ('The graph G has:\t %d nodes'%nn)
print ('The graph G has:\t %d edges'%ne)

The graph G has: 12 nodes


The graph G has: 14 edges

# 2) Draw the network.


nx.draw_networkx(G)
# 3) Perform community detection using Girvan-Newman.

# create the list of partitions detected by Girvan-Newman


partitions = girvan_newman_partitions(G)

# print the list of partitions


for i, part in enumerate(partitions):
print ('partition number %d:\t'%i, part)

print ('\nN.B: number of partitions: %d\t is equal to\t (number of nodes - 1): %d - 1'
%(len(partitions), nn))

partition number 0: [{0, 1, 10, 11}, {2, 3, 4, 5, 6, 7, 8, 9}]


partition number 1: [{0, 1, 10, 11}, {2, 3, 4, 5, 6, 7}, {8, 9}]
partition number 2: [{0, 1, 10, 11}, {2, 3, 4, 5, 7}, {6}, {8, 9}]
partition number 3: [{0, 1, 10, 11}, {2, 3, 4, 5}, {6}, {7}, {8, 9}]
partition number 4: [{0, 10, 11}, {1}, {2, 3, 4, 5}, {6}, {7}, {8, 9}]
partition number 5: [{0, 11}, {1}, {2, 3, 4, 5}, {6}, {7}, {8, 9}, {10}]
partition number 6: [{0}, {1}, {2, 3, 4, 5}, {6}, {7}, {8, 9}, {10}, {11}]
partition number 7: [{0}, {1}, {2}, {3, 4, 5}, {6}, {7}, {8, 9}, {10}, {11}]
partition number 8: [{0}, {1}, {2}, {3}, {4, 5}, {6}, {7}, {8, 9}, {10}, {11}]
partition number 9: [{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8, 9}, {10}, {11}]
partition number 10: [{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}]

N.B: number of partitions: 11 is equal to (number of nodes - 1): 12 - 1

# 4) Create the 'agglomerative matrix' associated to the community detection.

# agglomerative matrix
agglomerative_mat = agglomerative_matrix(G, partitions)
# columns=[comm.1, comm.2, dist(cluster1,cluster2), n. nodes of new community]

print ('Agglomerative matrix:\n', agglomerative_mat)

Agglomerative matrix:
[[ 8. 9. 1. 2.]
[ 4. 5. 1. 2.]
[ 3. 13. 2. 3.]
[ 2. 14. 3. 4.]
[ 0. 11. 1. 2.]
[10. 16. 2. 3.]
[ 1. 17. 3. 4.]
[ 7. 15. 4. 5.]
[ 6. 19. 5. 6.]
[12. 20. 6. 8.]
[18. 21. 7. 12.]]

# 5) Use the 'agglomerative matrix' to plot the dendrogram.


# To do the plot use 'scipy.cluster.hierarchy.dendrogram'.

from scipy.cluster.hierarchy import dendrogram

# plot dendrogram
dendro = dendrogram(agglomerative_mat, color_threshold=1)
# 6) Find the 'best partitions' among all the partitions detected by Girvan-Newman.
# The best partition is selected according to its modularity.
# (Using 'networkx.algorithms.community.quality.modularity').

# best_partition, position of best partition in the list 'partitions'


bp_G, idx_bp_G = girvan_newman_best_partition(G, partitions)

print ('The best community partition of the graph G is:\n\t', bp_G)


print ('\nWhich is the one with index = %d among the partitions in the list "partitions".'
%(idx_bp_G))

The best community partition of the graph G is:


[{0, 1, 10, 11}, {2, 3, 4, 5, 6, 7}, {8, 9}]

Which is the one with index = 1 among the partitions in the list "partitions".

# Plot the dendrogram highlighting the best partition.

# How many communities in the best partition?


n_communities_bp = len(bp_G)
print ('The best partition splits the graph G into %d communities.'%n_communities_bp)

# Distance of the best partition from the ground level


dist_bp = distance_of_partition(agglomerative_mat, n_communities_bp)
print ('The best partition, which splits the graph G into %d communities, has a distance from the ground level equal to:\t %d'
%(n_communities_bp, dist_bp))

# Plot the dendrogram highlighting the communities which compose the best partition
dendro_bp = dendrogram(agglomerative_mat, color_threshold=dist_bp)

The best partition splits the graph G into 3 communities.


The best partition, which splits the graph G into 3 communities, has a distance from the ground level equal to: 6

You might also like