# Closeness and Communities: Analyzing Social Networks with Python and NetworkX — Part 3 | by Christine Egan | Jun, 2023

## Learn about communities and closeness centrality in social network analysis with Python and NetworkX

In Part 2, we expanded our understanding of social network analysis by graphing the relationships between the members of the bands Smashing Pumpkins and Zwan. Then, we examined metrics like degree centrality and betweenness centrality to investigate the relationships between the members of the different bands. At the same time, we discussed how domain knowledge helps to inform our understanding of the results.

In Part 3, we will cover the basics of closeness centrality and how it is calculated. Then, we will demonstrate how to calculate closeness centrality with NetworkX using Billy Corgan’s network as an example.

Before you start…

1. Do you have basic knowledge of Python? If not, start here.
2. Are you familiar with basic concepts in social network analysis, like nodes and edges? If not, start here.
3. Are you comfortable with degree centrality and betweenness centrality? If not, start here.

## Closeness Centrality

Closeness centrality is a measure in social network analysis that quantifies how close a node is to all other nodes in a network in terms of the shortest path distance.

Closeness centrality focuses on the efficiency of information or resource flow within a network. The idea is that nodes with higher closeness centrality are able to reach other nodes more quickly and efficiently, as they have shorter average distances to the rest of the network.

The closeness centrality of a node is calculated as the reciprocal of the sum of the shortest path distances (SPD) from that node to all other nodes in the network.

Closeness Centrality = 1 / (Sum of SPD from the node to all other nodes)

Higher values indicate greater centrality and efficiency in information flow within the network.

## Calculating Closeness Centrality

Let’s break it down, using a simple network with eight nodes.

1. Calculate the shortest path distances (SPD) from node A to all other nodes. For our example, we will use simple example distances. In practice, this would be done with a shortest path algorithm like Breadth-First Search or Dijkstra’s algorithm.