2_EXAMPLE_dendrogram_from_girvan_newman.ipynb - Colab
2_EXAMPLE_dendrogram_from_girvan_newman.ipynb - Colab
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
[ ] ↳ 1 celda oculta
# 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)
print ('\nN.B: number of partitions: %d\t is equal to\t (number of nodes - 1): %d - 1'
%(len(partitions), nn))
# agglomerative matrix
agglomerative_mat = agglomerative_matrix(G, partitions)
# columns=[comm.1, comm.2, dist(cluster1,cluster2), n. nodes of new community]
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.]]
# 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').
Which is the one with index = 1 among the partitions in the list "partitions".
# Plot the dendrogram highlighting the communities which compose the best partition
dendro_bp = dendrogram(agglomerative_mat, color_threshold=dist_bp)