| random_graph (N, deg_sampler[, deg_corr, directed, ...]) | Generate a random graph, with a given degree distribution and correlation. |
| random_rewire (g[, strat, parallel_edges, ...]) | Shuffle the graph in-place. If strat != “erdos”, the degrees (either in or out) of each vertex are always the same, but otherwise the edges are randomly placed. If strat = “correlated”, the degree correlations are also maintained: The new source and target of each edge both have the same in and out-degree. If strat = “probabilistic”, than edges are rewired according to the degree correlation given by the parameter deg_corr. |
| predecessor_tree (g, pred_map) | Return a graph from a list of predecessors given by the ‘pred_map’ vertex property. |
| line_graph (g) | Return the line graph of the given graph g. |
| graph_union (g1, g2[, props, include]) | Return the union of graphs g1 and g2, composed of all edges and vertices of g1 and g2, without overlap. |
| triangulation (points[, type, periodic]) | Generate a 2D or 3D triangulation graph from a given point set. |
Generate a random graph, with a given degree distribution and correlation.
| Parameters: | N : int
deg_sampler : function
deg_corr : function (optional, default: None)
directed : bool (optional, default: True)
parallel_edges : bool (optional, default: False)
self_loops : bool (optional, default: False)
random : bool (optional, default: True)
verbose : bool (optional, default: False)
|
|---|---|
| Returns: | random_graph : Graph
|
See also
Notes
The algorithm makes sure the degree sequence is graphical (i.e. realizable) and keeps re-sampling the degrees if is not. With a valid degree sequence, the edges are placed deterministically, and later the graph is shuffled with the random_rewire() function.
The complexity is O(V+E) if parallel edges are allowed, and O(V+E \times \log N_k) if parallel edges are not allowed, where N_k < V is the number of different degrees sampled (or in,out-degree pairs).
References
[deg-sequence] http://en.wikipedia.org/wiki/Degree_%28graph_theory%29#Degree_sequence
Examples
>>> from numpy.random import randint, random, seed, poisson
>>> from pylab import *
>>> seed(42)
This is a degree sampler which uses rejection sampling to sample from the distribution P(k)\propto 1/k, up to a maximum.
>>> def sample_k(max):
... accept = False
... while not accept:
... k = randint(1,max+1)
... accept = random() < 1.0/k
... return k
...
The following generates a random undirected graph with degree distribution P(k)\propto 1/k (with k_max=40) and an assortative degree correlation of the form:
>>> g = gt.random_graph(1000, lambda: sample_k(40),
... lambda i,k: 1.0/(1+abs(i-k)), directed=False)
>>> gt.scalar_assortativity(g, "out")
(0.62318897995178757, 0.011431222500824638)
The following samples an in,out-degree pair from the joint distribution:
with m_1 = 4 and m_2 = 20.
>>> def deg_sample():
... if random() > 0.5:
... return poisson(4), poisson(4)
... else:
... return poisson(20), poisson(20)
...
The following generates a random directed graph with this distribution, and plots the combined degree correlation.
>>> g = gt.random_graph(20000, deg_sample)
>>>
>>> hist = gt.combined_corr_hist(g, "in", "out")
>>> imshow(hist[0], interpolation="nearest")
<...>
>>> colorbar()
<...>
>>> xlabel("in-degree")
<...>
>>> ylabel("out-degree")
<...>
>>> savefig("combined-deg-hist.png")
Combined degree histogram.
A correlated directed graph can be build as follows. Consider the following degree correlation:
i.e., the in->out correlation is “disassortative”, the out->in correlation is “assortative”, and everything else is uncorrelated. We will use a flat degree distribution in the range [1,20).
>>> p = scipy.stats.poisson
>>> g = gt.random_graph(20000, lambda: (sample_k(19), sample_k(19)),
... lambda a,b: (p.pmf(a[0],b[1])*
... p.pmf(a[1],20-b[0])))
Lets plot the average degree correlations to check.
>>> figure(figsize=(6,3))
<...>
>>> axes([0.1,0.15,0.63,0.8])
<...>
>>> corr = gt.avg_neighbour_corr(g, "in", "in")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{in}\right>$ vs in")
(...)
>>> corr = gt.avg_neighbour_corr(g, "in", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{out}\right>$ vs in")
(...)
>>> corr = gt.avg_neighbour_corr(g, "out", "in")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{in}\right>$ vs out")
(...)
>>> corr = gt.avg_neighbour_corr(g, "out", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{out}\right>$ vs out")
(...)
>>> legend(loc=(1.05,0.5))
<...>
>>> xlabel("source degree")
<...>
>>> ylabel("average target degree")
<...>
>>> savefig("deg-corr-dir.png")
Average nearest neighbour correlations.
Shuffle the graph in-place. If strat != “erdos”, the degrees (either in or out) of each vertex are always the same, but otherwise the edges are randomly placed. If strat = “correlated”, the degree correlations are also maintained: The new source and target of each edge both have the same in and out-degree. If strat = “probabilistic”, than edges are rewired according to the degree correlation given by the parameter deg_corr.
| Parameters: | g : Graph
strat : string (optional, default: “uncorrelated”)
parallel : bool (optional, default: False)
self_loops : bool (optional, default: False)
deg_corr : function (optional, default: None)
verbose : bool (optional, default: False)
|
|---|
See also
Notes
This algorithm iterates through all the edges in the network and tries to swap its target our edge with another edge.
Note
If parallel_edges = False, parallel edges are not placed during rewiring. In this case, for some special graphs it may be necessary to call the function more than once to obtain a graph which corresponds to a uniform sample from the ensemble. But typically, if the graph is sufficiently large, a single call should be enough.
Each edge gets swapped at least once, so the overall complexity is O(E). If strat = “probabilistic” the complexity is O(E\log N_k), where N_k < V is the number of different degrees (or in,out-degree pairs).
Examples
Some small graphs for visualization.
>>> from numpy.random import random, seed
>>> from pylab import *
>>> seed(42)
>>> g, pos = gt.triangulation(random((1000,2)))
>>> gt.graph_draw(g, layout="arf", output="rewire_orig.png", size=(6,6))
<...>
>>> gt.random_rewire(g, "correlated")
>>> gt.graph_draw(g, layout="arf", output="rewire_corr.png", size=(6,6))
<...>
>>> gt.random_rewire(g)
>>> gt.graph_draw(g, layout="arf", output="rewire_uncorr.png", size=(6,6))
<...>
>>> gt.random_rewire(g, "erdos")
>>> gt.graph_draw(g, layout="arf", output="rewire_erdos.png", size=(6,6))
<...>
Some ridiculograms :
From left to right: Original graph; Shuffled graph, with degree correlations; Shuffled graph, without degree correlations; Shuffled graph, with random degrees.
We can try some larger graphs to get better statistics.
>>> figure()
<...>
>>> g = gt.random_graph(30000, lambda: sample_k(20),
... lambda i,j: exp(abs(i-j)), directed=False)
>>> corr = gt.avg_neighbour_corr(g, "out", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-", label="original")
(...)
>>> gt.random_rewire(g, "correlated")
>>> corr = gt.avg_neighbour_corr(g, "out", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="*", label="correlated")
(...)
>>> gt.random_rewire(g)
>>> corr = gt.avg_neighbour_corr(g, "out", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-", label="uncorrelated")
(...)
>>> gt.random_rewire(g, "erdos")
>>> corr = gt.avg_neighbour_corr(g, "out", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-", label="Erdos")
(...)
>>> xlabel("$k$")
<...>
>>> ylabel(r"$\left<k_{nn}\right>$")
<...>
>>> legend(loc="best")
<...>
>>> savefig("shuffled-stats.png")
Average degree correlations for the different shuffled and non-shuffled graphs. The shuffled graph with correlations displays exactly the same correlation as the original graph.
Now let’s do it for a directed graph. See random_graph() for more details.
>>> p = scipy.stats.poisson
>>> g = gt.random_graph(20000, lambda: (sample_k(19), sample_k(19)),
... lambda a,b: (p.pmf(a[0],b[1])*p.pmf(a[1],20-b[0])))
>>> figure(figsize=(6,3))
<...>
>>> axes([0.1,0.15,0.6,0.8])
<...>
>>> corr = gt.avg_neighbour_corr(g, "in", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{o}\right>$ vs i")
(...)
>>> corr = gt.avg_neighbour_corr(g, "out", "in")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{i}\right>$ vs o")
(...)
>>> gt.random_rewire(g, "correlated")
>>> corr = gt.avg_neighbour_corr(g, "in", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{o}\right>$ vs i, corr.")
(...)
>>> corr = gt.avg_neighbour_corr(g, "out", "in")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{i}\right>$ vs o, corr.")
(...)
>>> gt.random_rewire(g, "uncorrelated")
>>> corr = gt.avg_neighbour_corr(g, "in", "out")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{o}\right>$ vs i, uncorr.")
(...)
>>> corr = gt.avg_neighbour_corr(g, "out", "in")
>>> errorbar(corr[2], corr[0], yerr=corr[1], fmt="o-",
... label=r"$\left<\text{i}\right>$ vs o, uncorr.")
(...)
>>> legend(loc=(1.05,0.45))
<...>
>>> xlabel("source degree")
<...>
>>> ylabel("average target degree")
<...>
>>> savefig("shuffled-deg-corr-dir.png")
Average degree correlations for the different shuffled and non-shuffled directed graphs. The shuffled graph with correlations displays exactly the same correlation as the original graph.
Return the line graph of the given graph g.
Notes
Given an undirected graph G, its line graph L(G) is a graph such that
- each vertex of L(G) represents an edge of G; and
- two vertices of L(G) are adjacent if and only if their corresponding edges share a common endpoint (“are adjacent”) in G.
For a directed graph, the second criterion becomes:
- Two vertices representing directed edges from u to v and from w to x in G are connected by an edge from uv to wx in the line digraph when v = w.
References
| [line-wiki] | http://en.wikipedia.org/wiki/Line_graph |
Return the union of graphs g1 and g2, composed of all edges and vertices of g1 and g2, without overlap.
| Parameters: | g1 : Graph
g2 : Graph
props : list of tuples of PropertyMap (optional, default: [])
include : bool (optional, default: False)
|
|---|---|
| Returns: | ug : Graph
props : list of PropertyMap objects
|
Examples
>>> from numpy.random import random, seed
>>> seed(42)
>>> g = gt.triangulation(random((300,2)))[0]
>>> ug = gt.graph_union(g, g)
>>> uug = gt.graph_union(g, ug)
>>> gt.graph_draw(g, layout="arf", size=(8,8), output="graph_original.png")
<...>
>>> gt.graph_draw(ug, layout="arf", size=(8,8), output="graph_union.png")
<...>
>>> gt.graph_draw(uug, layout="arf", size=(8,8), output="graph_union2.png")
<...>
Generate a 2D or 3D triangulation graph from a given point set.
| Parameters: | points : ndarray
type : string (optional, default: ‘simple’)
periodic : bool (optional, default: False)
|
|---|---|
| Returns: | triangulation_graph : Graph
pos : PropertyMap
|
See also
Notes
A triangulation [cgal-triang] is a division of the convex hull of a point set into triangles, using only that set as triangle vertices.
In simple triangulations (type=”simple”), the insertion of a point is done by locating a face that contains the point, and splitting this face into three new faces (the order of insertion is therefore important). If the point falls outside the convex hull, the triangulation is restored by flips. Apart from the location, insertion takes a time O(1). This bound is only an amortized bound for points located outside the convex hull.
Delaunay triangulations (type=”delaunay”) have the specific empty sphere property, that is, the circumscribing sphere of each cell of such a triangulation does not contain any other vertex of the triangulation in its interior. These triangulations are uniquely defined except in degenerate cases where five points are co-spherical. Note however that the CGAL implementation computes a unique triangulation even in these cases.
References
| [cgal-triang] | http://www.cgal.org/Manual/last/doc_html/cgal_manual/Triangulation_3/Chapter_main.html |
Examples
>>> from numpy.random import seed, random
>>> seed(42)
>>> points = random((500,2))*4
>>> g, pos = gt.triangulation(points)
>>> weight = g.new_edge_property("double") # Edge weights corresponding to
... # Euclidean distances
>>> for e in g.edges():
... weight[e] = sqrt(sum((array(pos[e.source()]) -
... array(pos[e.target()]))**2))
>>> b = gt.betweenness(g, weight=weight)
>>> b[1].a *= 100
>>> gt.graph_draw(g, pos=pos, pin=True, size=(8,8), vsize=0.07, vcolor=b[0],
... eprops={"penwidth":b[1]}, output="triang.png")
<...>
>>> g, pos = gt.triangulation(points, type="delaunay")
>>> weight = g.new_edge_property("double")
>>> for e in g.edges():
... weight[e] = sqrt(sum((array(pos[e.source()]) -
... array(pos[e.target()]))**2))
>>> b = gt.betweenness(g, weight=weight)
>>> b[1].a *= 120
>>> gt.graph_draw(g, pos=pos, pin=True, size=(8,8), vsize=0.07, vcolor=b[0],
... eprops={"penwidth":b[1]}, output="triang-delaunay.png")
<...>
2D triangulation of random points:
Left: Simple triangulation. Right: Delaunay triangulation. The vertex colors and the edge thickness correspond to the weighted betweenness centrality.