Simple Graph Metrics

Simple Graph Metrics #

Networks come in all different shapes and sizes. Some are quite simple while others are more complex. For this reason, knowing the right metrics is very important in order to better understand what is going on in the network.

This guide features some of the most simple yet important graph-based metrics for understanding structure.

Degree (in/out) #

Basically, the degree of a node is just the number of connections or edges it has. In a directed graph, we break it down into two parts: in-degree (edges coming in) and out-degree (edges going out). Usually, we look at this as a distribution, which comes from the degree sequence.

Knowing a network’s degree sequence (or a node’s degree) helps us understand how many connections each node has. In many networks—especially social ones—most nodes will have a degree around one, since they need at least one connection to be part of the network.

To figure out the degree of a node using NetworkX, you can use the following…

>>> G.degree('A')

For directed graphs, this would look like…

>>> G.in_degree('A')
3
>>> G.out_degree('A')
4

Or if you want to know the degree for all nodes just use the following without any parameter…

>>> G.degree()

Density #

Density (or clustering coefficient) is a handy metric for figuring out how clustered a network is. It’s basically the ratio of how many edges the network actually has compared to the total number of edges it could have if every node was connected to every other node.

This metric gives you a quick snapshot of how many edges are filled in the network. In big, complex networks, this number is usually pretty low. But in smaller, sparse networks, you often get a higher density because there’s a better chance that most of the possible edges are filled.

Calculating the density of a network with NetworkX is super simple…

>>> nx.density(G)

Reciprocity #

In social network analysis, one useful way to spot meaningful connections is by looking at reciprocity. This only applies to directed networks because we’re talking about ties that go both ways—edges that point in both directions between two nodes.

Why should you care about reciprocity?

Reciprocity is a big deal when it comes to understanding how people interact. Think of it like this: in a social setting, a reciprocated edge might show that one user is replying to another or returning a favour. This can hint that these users prefer to engage with each other more than with others.

With NetworkX, you can measure this easily using a built-in function that gives you a value representing the proportion of reciprocated edges to the total number of edges in the network. The higher the value, the more back-and-forth ties there are. For example, reciprocity is calculated with the following…

>>> nx.reciprocity(G)

Transitivity #

In network analysis, a triad (basically a group of three nodes) is seen as one of the key building blocks of any network. Transitivity is like the likelihood that connected nodes are all linked together within the network.

It helps show how complete a network is. For example, social interactions like indirect reciprocity (think “the enemy of my enemy is my friend”) depend on triads—where three people or entities are involved. Similar to density, transitivity gives a good sense of how interconnected the whole network is.

In NetworkX, you can use it like this…

>>> nx.transitivity(G)

Modularity #

Modularity is a handy metric for figuring out how well a network can be split into different clusters. The basic idea is that the higher the modularity, the more tightly-knit groups you have, with fewer connections between them.

I’ve also used this in another blog post to get a quick estimate of how many communities are in the network. While there are more advanced methods for detecting communities, modularity is a simple way to check if your network has distinct groups of connected users.

You can calculate the overall modularity of all nodes using the following…

>>> import networkx.algorithms.community as nx_comm
>>> nx_comm.modularity(G, G.nodes())

Final Thoughts and Conclusions #

In this guide, we’ve covered some of the basic techniques to analyse a network using simple metrics. This isn’t an exhaustive list by any means—there are plenty of other metrics out there—but these are the ones you’ll likely use the most.

Prev: Reading and Exporting Graphs Next: Graph Types

Created by James Ashford