Our clustered random graph generation method begins with a random graph and iteratively rewires edges to introduce triangles. Network rewiring, also known as edge swapping, is a wellknown method for generating networks with desired properties [37, 36, 38]. Two edges are called adjacent if they connect to a common node. Each rewiring is performed on two nonadjacent edges of the graph and consists of removing these two edges and replacing them with another pair of edges. Specifically, a pair of edges (i, j) and (k, l) is replaced with either (i, k) and (j, l), or (i, l) and (j, k) (as illustrated in Figure 1c). This change in the graph leaves the degrees of the participating nodes unchanged, thus maintaining the specified degree sequence. Below we describe a rewiring algorithm that increases the level of clustering in a random graph, while preserving the degree sequence.
The algorithm we develop below is implemented in Python as ClustRNet. It is based on Networkx, an opensource Python library available for download at [39], which provides standard graph library functionality (e.g. data structure, input/output, and layouts). The source code for ClustRNet, along with documentation and test network datasets, is available on the web [40]. Our algorithm joins a existing suite of random graph modelbased software tools for the analysis of biological networks and the dynamics on them [41, 42].
Measures of Clustering
We begin with a graph G = (V, E) which is undirected and simple. V is the set of vertices of G and E is the set of the edges. We let N = V and M = E denote the number of nodes and edges in G, respectively. The degree of a node i will be denoted d_{
i
}. The set of degrees for all nodes in the graph makes up the degree sequence, which follows a probability distribution called the degree distribution.
Clustering is the likelihood that two neighbors of a given node are themselves connected. In topological terms, clustering measures the density of triangles in the graph, where a triangle is the existence of the set of edges (i, j), (i, k), (j, k) between any triplet of nodes i, j, k (Figure 1b).
To quantify the local presence of triangles, δ(i) is defined as the number of triangles in which node i participates. Since each triangle consists of three nodes, it is counted thrice when we sum δ(i) for each node in the graph. Thus the total number of triangles in the graph is
A triple is a set of three nodes, i, j, k that are connected by edges (i, j) and (i, k), regardless of the existence of the edge (j, k) (Figure 1a). The number of triples of node i is simply
assuming d_{
i
}≥ 2. To compute the total number of triples in the graph, τ(G), we sum τ(i) for all i ∈ V.
The clustering coefficient was introduced by Watts and Strogatz [1] as a local measure of triadic closure. For a node i with d_{
i
}≥ 2, the clustering coefficient c(i) is the fraction of triples for node i which are closed, and can be measured as δ(i) = τ(i). The clustering coefficient of the graph is then given by:
where N_{2} is the number of nodes with c(i) ≥ 0. Some authors do define the clustering coefficient for all nodes of G [43].
A more global measure of the presence of triangles is called the transitivity of graph G and is defined as:
Although they are often similar, T(G) and C(G) can vary by orders of magnitude [22]. They differ most when the triangles are heterogeneously distributed in the graph.
These traditional measures of clustering are degreedependent and thus can be biased by the degree sequence of the network. The maximum number of possible triangles for a given node i is just its number of triples (τ(i)). For a node which is connected to only low degree neighbors, however, the maximum number of possible triangles may be much smaller than τ(i). To account for this, a new measure for clustering was introduced in [22] that calculates triadic closure as a function of degree and neighbor degree. Specifically, the SofferVasquez clustering coefficient () and transitivity () are given by:
where ω(i) measures the number of possible triangles for node i, and N_{
ω
}is the number of nodes in G for which ω(i) > 0. We note that and are undefined if ω(G) = Σ_{
i
}ω(i) = 0. ω(i) is computed by counting the maximum number of edges that can be drawn among the d_{
i
}neighbors of a node i, given the degree sequence of i's neighbors; this value is often smaller than [22]. For example, consider a star network of five nodes, where four nodes have degree 1 and one node has degree 4. Although the total number of triples is τ(G) = 6, the number of possible triangles is ω(G) = 0 because the degree one nodes preclude their formation. The computation of ω(i) must be done algorithmically and is not possible in closed form. (From here on, we refer to as the SVclustering coefficient and to as the SVtransitivity.)
Generative Model
Here we develop a model to generate a simply connected random graph with a specified degree sequence and a desired level of clustering. Generating random graphs uniformly from the set of simply connected graphs with a prescribed degree sequence is a wellstudied problem with algorithmic solutions [37]. One of the simplest and most popular of these generative algorithms was suggested by Molloy and Reed and is known as the configuration model [27]. Given a specific realizable degree sequence [44], {d_{
i
}}, this method assigns d_{
j
}halfedges to each node j, and then randomly connects pairs halfedges to create edges until there are no halfedges left. (A realizable degree sequence is one which satisfies the Handshake Theorem (the requirement that the sum of the degrees be even) and the ErdosGallai criterion (which requires that for each subset of the k highest degree nodes, the degrees of these nodes can be "absorbed" within the subset and the remaining degrees.) Although the model sometimes produces graphs that are not simple or connected, this can be remedied by subsequently removing multiple edges and self loops from the constructed graph and keeping only the largest connected component [37]. Our method begins by using this approach to generate a simple, connected random graph G, with a specific realizable degree sequence D. We then introduce triangles into G using a Markov Chain process without disturbing the degree sequence until we achieve the desired level of clustering, as follows.
Let G_{
D
}be the set of all simple, connected graphs with degree sequence D. If are the graphs of G_{
D
}, then we let be the states of the Markov chain, P, where X_{
i
}represents the state in which our graph G = G_{
i
}. The states X_{
i
}and X_{i+1}are connected in the Markov Chain if G_{
i
}can be changed to G_{i+1}with the rewiring of one pair of edges. The state space of the Markov chain P is connected because there exists a path from X_{
i
}to X_{
j
}(for any pair i, j) by one or more rewiring moves that leave the degree sequence unchanged [45].
Our clustered graph generation algorithm involves starting with the random graph G (generated with the configuration model above) and transitioning from the state corresponding to G (X_{
G
}) to other states of P until a halting condition is reached. A transition from one state of the Markov chain to another only occurs when the algorithm makes an edge rewiring that both increases the clustering of the graph and leaves the graph connected. Since a rewiring does not alter the degree sequence of the graph, the rewired graph is still in G_{
D
}. The transition probabilities of the Markov chain for a pair of connected states, X_{
i
}to X_{
j
}, are:
where clust(G_{
x
}) is a clustering measure for graph G_{
x
}, which can be replaced by any of the measures introduced in Section. The algorithm continues searching for a feasible rewiring (one that increases the clustering and does not disconnect the graph) until one is found. If a feasible move is not found, a transition is not made and the process remains in the current state.
The Markov chain above is finite and aperiodic, but not irreducible as the process can never transition to a state in which the graph has lower clustering. It does, however, have an absorbing state, X_{*}, in which the transitivity of G_{*} is greater than or equal to the desired transitivity or is the maximum possible transitivity given the particular degree sequence and connectivity constraints.
Algorithm
To generate clustered graphs, we apply the above Markov Chain simulation model by iteratively applying rewirings that increase graph clustering. Each rewiring takes a set of five nodes {x, y_{1}, y_{2}, z_{1}, z_{2}}, connected by four edges {(x, y_{1}), (x, y_{2}), (y_{1}, z_{1}), (y_{2}, z_{2})}, and swaps the outer edges: {(x, y_{1}), (x, y_{2}), (y_{1}, y_{2}), (z_{1}, z_{2})}(illustrated in Figure 1d). This introduces a triangle among nodes {x, y_{1}, and y_{2}}, without perturbing the degree sequence. The algorithm proceeds as follows:
Input: A realizable degree sequence {d_{
i
}} a desired clustering value, target
Initialization: Generate a random graph G with degree sequence {d_{
i
}} (using the configuration model), and measure the clustering of G, clust(G).
while clust(G) <target do
1. uniformly select a random node, x, from the
set of all nodes of G such that d_{
x
}> 1.
2. uniformly select two random neighbors, y_{1}
and y_{2}, of x such that d_{y 1}> 1 and
d_{y 2}> 1 and y_{1}≠y_{2}.
3. uniformly select a random neighbor, z_{1}
of y_{1} and a random neighbor, z_{2} of
y_{2} such that z_{1} ≠ x, z_{2} ≠ x,
z_{1} ≠ z_{2}.
4. G_{
cand
}: = G where G_{
cand
}is the candidate
graph to which the transition may be made.
5. if (y_{1}, y_{2}) and (z_{1}, z_{2}) do not exist then
Rewire two edges of G_{
cand
}: delete (y_{1}, z_{1}) and (y_{2}, z_{2}), add (y_{1}, y_{2}) and (z_{1}, z_{2}).
end
6. Update the value of clust(G_{
cand
}) by measuring
δ (i) (and ω (i) if relevant) for the nodes involved
in the rewiring and their neighbors.
7. if clust(G_{
cand
}) > clust(G) and G_{
cand
}
is connected then
G: = G_{
cand
}
end
end
Output: A random graph, G with degree sequence {d_{
i
}} and clust(G) ≥ target.
The algorithm terminates when the graph attains at least the desired level of clustering or reaches a threshold number of unsuccessful rewiring attempts. In the latter case, the algorithm returns the graph with the maximum clustering achieved. For practical purposes, a threshold is placed on the number of unsuccessful attempts made by the algorithm in ClustRNet for the case that the desired clustering cannot be reached. Due to the random restarts made at every step, the algorithm is prevented from getting trapped in local minima.
The algorithm is designed to increase clustering while preserving both the degree sequence and connectedness of the graph. However, there are some cases where the desired clustering can only be reached by disconnecting the graph; and thus ClustRNet provides the option of removing the connectivity constraint (see Additional file 1, Figure S2).
Choice of Clustering Measure
The algorithm is defined independent of the choice of clustering measure. The term clust(G) in the algorithm above can be replaced by any clustering measure described in Section. ClustRNet includes all four of these clustering measures (C, , T; ).
The algorithm output varies with the choice of clustering measure. The clustering coefficient is a local measure; and thus C and yield networks that are only locally optimized for the desired level of clustering. The algorithm may have difficulty attaining target clustering values when using the absolute clustering measures (C or T) because of joint degree constraints (the degrees of adjacent nodes) on the possible numbers of triangles, as with the example presented in Section. The SofferVasquez clustering measures, which explicitly consider joint degree constraints, provide a way around this difficulty [22]. Although the rewiring in our algorithm changes the joint degree distribution (and thus the degree correlations) of the graph, ω(G) is not altered significantly during network generation (as shown in Additional file 1, Figure S3). Thus, when using or , clustering is increased primarily by the addition of triangles (that is, increasing δ (G)) rather than decreasing ω(G)).
Types of Graph Changes
As shown in Figure 2, there are six types of triangles that can be added or removed for every pair of edges that are rewired. As illustrated in Figure 1d, these additions and removals can occur in combination.

Type A: The addition of the edge between vertices y_{1} and y_{2} guarantees the addition of one triangle in every rewiring event.

Type B: The addition of the edge (y_{1}, y_{2}) could create new triangles with shared neighbors of y_{1} and y_{2}.

Type C: The addition of the edge (z_{1}, z_{2}) could add a triangle if there existed edges between x and z_{1} and x and z_{2}.

Type D: The addition of the edge between vertices z_{1} and z_{2} could create new triangles with shared neighbors of z_{1} and z_{2}.

Type E: The removal of edges (y_{1}, z_{1}) and (y_{2}, z_{2}) removes one triangle each if the edges (x, z_{1}) or (x, z_{2}) exist.

Type F: The removal of the edges between vertices y_{1} and z_{1}, and y_{2} and z_{2} could lead to the removal of existing triangles with shared neighbors of y_{1} and z_{1} or y_{2} and z_{2}.
We note that although the type A addition is a special case of type B, the type C addition is a special case of type D, and the type E removals are a special case of type F, we distinguish them because they have different probabilities of occurrence. Our lookahead strategy only allows rewiring moves when the total number of Type E and F losses is fewer than the total number of Type A, B, C, and D gains.
Computational Complexity
Like many heuristic search methods, the algorithm we propose can be computationally expensive. The method outlined in Section 2.2 requires O(M) steps to generate a connected graph, and up to O(M) steps to randomize the graph, where M is the number of edges in the graph. At each step of randomization, we test that the graph remains connected (an O(M) operation), resulting in an overall O(M^{2}) random network generation process. A naive computation of the transitivity/clustering coefficient requires checking every node for the existence of edges between every pair of neighbors of the node. This step requires O() operations, where N is the number of nodes and d_{
max
}is the maximum degree of any node in the graph. The most expensive step of our algorithm is the introduction of triangles via rewiring. A single rewiring step requires O(M) operations for switching edges, checking for connectivity and updating the clustering measure. Although we cannot analytically calculate the number of attempted rewiring steps required to reach the desired transitivity, we have found it empirically to be O(M). Thus, the average complexity of the clustered network algorithm presented here is O(M^{2}). This complexity has been computed for the most naive versions of our algorithms; and more efficient implementations may improve the complexity greatly. For example, we might improve efficiency by performing connectivity tests once every x rewirings (for some number x) rather than during every rewiring, as proposed in [46].