# Detecting communities in social networks using Girvan Newman algorithm in Python

**Prerequisite**– Python Basics, NetworkX Basics

We are going to divide the nodes of the graph into two or more communities using the Girvan Newman algorithm. The Girvan Newman Algorithm removes the edges with the highest betweenness until there are no edges remain. **Betweenness** is the number of the shortest paths between pairs of nodes that run through it.

We will use a Girvan Newman Algorithm for this task.

**Algorithm:**

- Create a graph of N nodes and its edges or take an inbuilt graph like a barbell graph.
- Calculate the betweenness of all existed edges in the graph.
- Now remove all the edge(s) with the highest betweenness.
- Now recalculate the betweenness of all the edges that got affected by the removal of edges.
- Now repeat steps 3 and 4 until no edges remain.

**Python Code:**

## Python3

`import` `networkx as nx` ` ` ` ` `def` `edge_to_remove(g):` ` ` ` ` `d1 ` `=` `nx.edge_betweenness_centrality(g)` ` ` `list_of_tuples ` `=` `list` `(d1.items())` ` ` ` ` `sorted` `(list_of_tuples, key ` `=` `lambda` `x:x[` `1` `], reverse ` `=` `True` `)` ` ` ` ` `# Will return in the form (a,b)` ` ` `return` `list_of_tuples[` `0` `][` `0` `]` ` ` `def` `girvan(g):` ` ` `a ` `=` `nx.connected_components(g)` ` ` `lena ` `=` `len` `(` `list` `(a))` ` ` `print` `(` `' The number of connected components are '` `, lena)` ` ` `while` `(lena ` `=` `=` `1` `):` ` ` ` ` `# We need (a,b) instead of ((a,b))` ` ` `u, v ` `=` `edge_to_remove(g)` ` ` `g.remove_edge(u, v) ` ` ` ` ` `a ` `=` `nx.connected_components(g)` ` ` `lena` `=` `len` `(` `list` `(a))` ` ` `print` `(` `' The number of connected components are '` `, lena)` ` ` ` ` `return` `a` ` ` `# Driver Code` `g ` `=` `nx.barbell_graph(` `5` `,` `0` `)` `a ` `=` `girvan(g)` `print` `(` `'Barbell Graph'` `)` ` ` `for` `i ` `in` `a:` ` ` `print` `(i.nodes())` ` ` `print` `(` `'.............'` `)` ` ` `g1 ` `=` `nx.karate_club_graph()` `a1 ` `=` `girvan(g1)` ` ` `print` `(` `'Karate Club Graph'` `)` `for` `i ` `in` `a1:` ` ` `print` `(i.nodes())` ` ` `print` `(` `'.............'` `)` |

**Output:**

Barbell Graph

The number of connected components are 1

The number of connected components are 2

[0, 1, 2, 3, 4]

………….

[8, 9, 5, 6, 7]

………….

Karate Club Graph

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 1

The number of connected components are 2

[32, 33, 2, 8, 9, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]

………….

[0, 1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21]

………….

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course. And to begin with your Machine Learning Journey, join the **Machine Learning – Basic Level Course**