📚 The CoCalc Library - books, templates and other resources
License: OTHER
Modeling Scale-Free Networks
Including a new model that generates graphs with small world properties and a lognormal degree distribution.
Code examples from Think Complexity, 2nd edition.
Copyright 2016 Allen Downey, MIT License
Introduction
Real social networks generally have the properties of small world graphs (high clustering and low path lengths) and the characteristics of scale free networks (a heavy-tailed degree distribution).
The Watts-Strogatz (WS) network model has small world characteristics, but the degree distribution is roughly normal, very different from observed distributions.
The Barabasi-Albert (BA) model has low path lengths and a heavy-tailed degree distribution, but
It has low clustering, and
The degree distribution does not fit observed data well.
The Holmes-Kim (HK) model generates graphs with higher clustering, although still not as high as observed values. And the degree distribution is heavy tailed, but it still doesn't fit observed distributions well.
I propose a new model that generates graphs with
Low path lenths,
Clustering coefficients similar to the HK model (but still lower than observed values), and
A degree distribution that fits observed data well.
I test the models with a relatively small dataset from SNAP.
The proposed model is based on a "friend of a friend" growth mechanism that is a plausible description of the way social networks actually grow. The implementation is simple, comparable to BA and HK in both lines of code and run time.
Facebook data
The following function reads a file with one edge per line, specified by two integer node IDs.
We'll read the Facecook data downloaded from SNAP
The average clustering coefficient is about 0.6
The average path length is short.
WS model
Next I'll construct a WS graph with the same number of nodes and average degree as the Facebook network:
With p=0.05
, we get clustering and path lengths similar to the Facebook data:
The clustering coefficient in the model is a little higher than in the data.
And the mean path length is a little smaller.
So the WS model has the small world properties. But the distribution of degrees is very different from the data.
This function extracts the list of degrees, one for each node.
The mean degree is about right:
But the standard deviation isn't even close.
We can see what's going on by plotting the distributions:
The degree distribution in the WS graph is roughly Gaussian. The degree distribution in social networks is emphatically not Gaussian.
And that discrepancy is at least part of the motivation for the BA model.
BA model
Here's a simplified version of the NetworkX function that generates BA graphs.
I'll generate a BA graph with the same number of nodes and edges as the Facebook data:
Providing a random seed means we'll get the same graph every time.
The number of edges is pretty close to what we asked for.
So the mean degree is about right.
The standard deviation of degree is a bit lower than it should be (but much better than the WS model).
Let's take a look at the degree distributions.
Looking at the PMFs on a linear scale, we see one difference, which is that the BA model has no nodes with degree less than k
, which is 22.
If we look at the PMF on a log-log scale, the BA model looks pretty good for values bigger than about 20. And it seems to follow a power law.
The characteristic path length is even smaller in the model than in the data.
But the clustering coefficient isn't even close.
In the BA model, the degree distribution is better than in the WS model, but the clustering coefficient is too low.
Cumulative distributions
We can get a better picture of these distributions, and the differences between the model and the data, by plotting CDFs instead of PMFs.
If we plot them on a log-x scale, we get a sense of how well the models fit the central part of the distribution.
The WS model is hopeless. The BA model is ok for values above the median, but not very good for smaller values.
On a log-log scale, we see that the BA model fits the tail of the distribution reasonably well.
HK model
The Holme-Kim (HK) model is similar to the BA model, but it generates additional triangles in order to increase the clustering coefficient.
Here's a slightly simplified version of the NetworkX function that generates BA graphs:
The clustering coefficient is better than in the BA model, but still subtantially lower than in the dataset.
The path lengths are very short.
The mean degree is about right, but the standard deviation is a little low.
The distribution of degrees is pretty much the same as from the BA model. It doesn't fit the lower and central parts of the distribution well.
But it fits the tail of the distribution reasonably well.
FOF model
The generative model I propose is called FOF for "friends of friends". It is similar to both BA and HK, but it yields a degree distribution that matches observed data better.
It starts with a complete graph with m
nodes, so initially all nodes have degree m
. Each time we generate a node we:
Select a random target uniformly from existing nodes.
Iterate through the friends of the target. For each one, with probability
p
, we form a triangle that includes the source, friend, and a random friend of friend.Finally, we connect the source and target.
Because we choose friends of the target, this process has preferential attachment, but it does not yield a power law tail. Rather, the degree distribution is approximately lognormal with median degree m
.
Because this process forms triangles, it tends to yield a moderately high clustering coefficient.
Here's what my implementation looks like:
The parameter m
is the average degree.
The parameter p
is the probability of adding a triangle. Since each triangle increases the total degree by 4, keeping p
near 1/4
tends to keep m
constant.
The clustering coefficient is similar to what we get from the HK model.
The average path length is low.
The degree distribution fits the observed data well
On a log-log scale, the model fits the data well except in the extreme tail.
In summary, the FOF model has
Short path lengths, like WS, BA, and HK.
Moderate clustering, similar to HK, less than WS, and higher than BA.
Good fit to the tail of the degree distribution, like BA and HK.
Good fit to the rest of the degree distribution, unlike WS, BA, and HK.
Also, the mechanism of growth is plausible: when a person joins the network, they connect to a randomly-chosen friend and then a random subset of "friends of friends". This process has preferential attachment because friends of friends are more likely to have high degree (see The Inspection Paradox is Everywhere). But the resulting distribution is approximately lognormal, which is heavy tailed, but does not have a power law tail.