Generating Simple Graphs to Chaotic Real World Networks with Python | by Inti Beer | Dec, 2020


Before we begin modelling some extra advanced networks, let’s run via some extra fundamental community homes.

import networkx as nxG = ER(30, 0.1)

Network Density

How is density in a community measured? A community with an edge between each node is named an entire graph. What % of the selection of imaginable edges are in truth are there? This fraction is named community density.

Complete community with 30 nodes. (Image by Author)

Here’s how you’ll calculate the community density in Python:

A sooner approach could be to use networkX’s inbuilt serve as:


Network Connectivity


The community connectivity is measure of the way powerful a community is. A community is hooked up if there may be a minimum of one trail from each node to each different node. What’s the minimal selection of nodes I want take away to separate a community? That’s the most straightforward measure of community connectivity.

Take the instance of a rail community:

“How many stations want to be out of motion for it to be unattainable for commuters to shuttle between any two stations?”

Average Shortest Path


A measure of the potency of data/mass shipping on a community. Calculate with the next:

Calculate the shortest trail for all imaginable nodes pairs then moderate those edges to get the typical shortest trail for the entire community.

Network Diameter


Given we now have an inventory of all of the shortest paths between each two nodes in a graph, if we then take the longest shortest trail and rely what number of edges that trail incorporates, that’s the community’s diameter!

Average Clustering Coefficient

clustering_coeffs = nx.clustering(G).values()
average_clustering_coeff = sum(clustering_coeffs)/len(clustering_coeffs)

A better clustering coefficient signifies a better ‘cliquishness’. Think of this like “what number of of a nodes buddies know one any other?” This is an invaluable metric for describing the level of “native densities” — wallet of neighborhood buildings inside of advanced networks.

Random vs common round ladder graph

Let’s see how neatly our random graph squares up in opposition to the common round ladder graph…

Regular Circular Ladder Graph when put next to Random Graph (Image by Author)

The random graph has a better moderate clustering coefficient which varies between sub graphs and nodes inside of it. Therefore, it’s neatly clustered in some portions however weakly in others. The random wiring on this case occurs to produce a greater moderate shortest trail in this community, indicating it’s higher at propagating knowledge/indicators via it than the round common graph.

By distinctive feature of the character of our round ladder graph, there aren’t any “cliques”, so it ratings a nil on avg. cluster coefficient. Our round ladder graph then again is extra “powerful” as you possibly can want to take away three nodes (vs simply 1, node 19 within the our random graph) for the graph to be separated.

Overall, a bit little bit of random noise in our community is simply adequate in all sides, then again regulars graph will reliably carry out neatly on explicit homes they’re optimised for. These neatly appearing homes come on the sacrifice of alternative doubtlessly necessary community homes. In our instance, the round ladder graph is tremendous powerful however horrible at propagating knowledge because of this.

This begs the query, How does mom nature architect the advanced networks round us?

Random graphs are implausible as a result of they allow us to fashion advanced networks via parameterisation and introduce a probabilistic part, then again they’re garbage at modelling issues helpful in actual lifestyles. In actual lifestyles there’s simply too many interactions, too many kinds of causal impact…too advanced. So how are we able to discover a patterns within the chaos of naturally happening techniques? How do we discover the golden laws that some of these actual lifestyles techniques practice? We’ll strive to solution this query by the usage of some actual global information and writing some code.

For this situation, we’re having a look at publicly to be had UK southern rail schedule data. I’ve wiped clean the information up and filtered to simplest routes that perform on the United Kingdom Southern Rail community (to make our lives somewhat more straightforward), right here’s what the data seems like.

Raw community information (Image by Author)

Now let’s visualise the community! We can use networkX’s spring layout.

import pandas as pd
import networkx as nx
import matplotlib.pyplot as plt
information = pd.read_csv("southern_rail_data.csv")edges = [
record(row) for row in information[
['origin_station_name','destination_station_name', 'time']
].values if record(row)[0] != record(row)[1]
nodes = nodes = record(set(information.origin_station_name))
Gr = nx.DiGraph()
for e in edges:
Gr.add_edge(e[0], e[1])
#width= information.time * 10,


Please enter your comment!
Please enter your name here