__init__.py 56.2 KB
Newer Older
1
#! /usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
5
# graph_tool -- a general graph manipulation python module
#
Tiago Peixoto's avatar
Tiago Peixoto committed
6
# Copyright (C) 2006-2013 Tiago de Paula Peixoto <tiago@skewed.de>
7
8
9
10
11
12
13
14
15
16
17
18
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
Tiago Peixoto's avatar
Tiago Peixoto committed
19
# along with this program.  If not, see <http://www.gnu.org/licenses/>.s
20

21
"""
22
``graph_tool.generation`` - Random graph generation
23
---------------------------------------------------
24
25
26
27
28
29
30
31
32
33
34
35

Summary
+++++++

.. autosummary::
   :nosignatures:

   random_graph
   random_rewire
   predecessor_tree
   line_graph
   graph_union
36
   triangulation
37
38
   lattice
   geometric_graph
39
   price_network
Tiago Peixoto's avatar
Tiago Peixoto committed
40
   complete_graph
Tiago Peixoto's avatar
Tiago Peixoto committed
41
   circular_graph
42
43
44

Contents
++++++++
45
46
"""

47
48
from __future__ import division, absolute_import, print_function

Tiago Peixoto's avatar
Tiago Peixoto committed
49
from .. dl_import import dl_import
50
dl_import("from . import libgraph_tool_generation")
51

52
from .. import Graph, GraphView, _check_prop_scalar, _prop, _limit_args, _gt_type, _get_rng, libcore
Tiago Peixoto's avatar
Tiago Peixoto committed
53
from .. stats import label_parallel_edges, label_self_loops
54
55
import inspect
import types
56
import sys, numpy, numpy.random
57

Tiago Peixoto's avatar
Tiago Peixoto committed
58
__all__ = ["random_graph", "random_rewire", "predecessor_tree", "line_graph",
59
           "graph_union", "triangulation", "lattice", "geometric_graph",
Tiago Peixoto's avatar
Tiago Peixoto committed
60
           "price_network", "complete_graph", "circular_graph"]
61

Tiago Peixoto's avatar
Tiago Peixoto committed
62

63
64
def random_graph(N, deg_sampler, directed=True,
                 parallel_edges=False, self_loops=False, block_membership=None,
65
                 block_type="int", degree_block=False,
66
                 random=True, verbose=False, **kwargs):
Tiago Peixoto's avatar
Tiago Peixoto committed
67
    r"""
68
69
70
71
72
73
74
    Generate a random graph, with a given degree distribution and (optionally)
    vertex-vertex correlation.

    The graph will be randomized via the :func:`~graph_tool.generation.random_rewire`
    function, and any remaining parameters will be passed to that function.
    Please read its documentation for all the options regarding the different
    statistical models which can be chosen.
Tiago Peixoto's avatar
Tiago Peixoto committed
75
76
77
78
79
80
81
82
83
84
85

    Parameters
    ----------
    N : int
        Number of vertices in the graph.
    deg_sampler : function
        A degree sampler function which is called without arguments, and returns
        a tuple of ints representing the in and out-degree of a given vertex (or
        a single int for undirected graphs, representing the out-degree). This
        function is called once per vertex, but may be called more times, if the
        degree sequence cannot be used to build a graph.
86

87
        Optionally, you can also pass a function which receives one or two
88
89
90
        arguments. If ``block_membership == None``, the single argument passed
        will be the index of the vertex which will receive the degree.  If
        ``block_membership != None``, the first value passed will be the vertex
91
        index, and the second will be the block value of the vertex.
92
    directed : bool (optional, default: ``True``)
Tiago Peixoto's avatar
Tiago Peixoto committed
93
        Whether the generated graph should be directed.
94
95
96
97
    parallel_edges : bool (optional, default: ``False``)
        If ``True``, parallel edges are allowed.
    self_loops : bool (optional, default: ``False``)
        If ``True``, self-loops are allowed.
98
    block_membership : list or :class:`~numpy.ndarray` or function (optional, default: ``None``)
99
        If supplied, the graph will be sampled from a stochastic blockmodel
100
101
102
        ensemble, and this parameter specifies the block membership of the
        vertices, which will be passed to the
        :func:`~graph_tool.generation.random_rewire` function.
103
104
105
106

        If the value is a list or a :class:`~numpy.ndarray`, it must have
        ``len(block_membership) == N``, and the values will define to which
        block each vertex belongs.
107
108
109
110

        If this value is a function, it will be used to sample the block
        types. It must be callable either with no arguments or with a single
        argument which will be the vertex index. In either case it must return
111
        a type compatible with the ``block_type`` parameter.
112
113
114
115

        See the documentation for the ``vertex_corr`` parameter of the
        :func:`~graph_tool.generation.random_rewire` function which specifies
        the correlation matrix.
116
    block_type : string (optional, default: ``"int"``)
117
        Value type of block labels. Valid only if ``block_membership != None``.
118
119
120
121
122
    degree_block : bool (optional, default: ``False``)
        If ``True``, the degree of each vertex will be appended to block labels
        when constructing the blockmodel, such that the resulting block type
        will be a pair :math:`(r, k)`, where :math:`r` is the original block
        label.
123
124
125
126
127
    random : bool (optional, default: ``True``)
        If ``True``, the returned graph is randomized. Otherwise a deterministic
        placement of the edges will be used.
    verbose : bool (optional, default: ``False``)
        If ``True``, verbose information is displayed.
Tiago Peixoto's avatar
Tiago Peixoto committed
128
129
130

    Returns
    -------
131
    random_graph : :class:`~graph_tool.Graph`
Tiago Peixoto's avatar
Tiago Peixoto committed
132
        The generated graph.
133
134
    blocks : :class:`~graph_tool.PropertyMap`
        A vertex property map with the block values. This is only returned if
135
        ``block_membership != None``.
Tiago Peixoto's avatar
Tiago Peixoto committed
136
137
138

    See Also
    --------
139
    random_rewire: in-place graph shuffling
Tiago Peixoto's avatar
Tiago Peixoto committed
140
141
142

    Notes
    -----
Tiago Peixoto's avatar
Tiago Peixoto committed
143
144
145
    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
146
147
    the :func:`~graph_tool.generation.random_rewire` function, with all
    remaining parameters passed to it.
Tiago Peixoto's avatar
Tiago Peixoto committed
148

149
    The complexity is :math:`O(V + E)` if parallel edges are allowed, and
150
    :math:`O(V + E \times\text{n-iter})` if parallel edges are not allowed.
151
152
153
154
155
156


    .. note ::

        If ``parallel_edges == False`` this algorithm only guarantees that the
        returned graph will be a random sample from the desired ensemble if
157
        ``n_iter`` is sufficiently large. The algorithm implements an
158
159
160
161
        efficient Markov chain based on edge swaps, with a mixing time which
        depends on the degree distribution and correlations desired. If degree
        correlations are provided, the mixing time tends to be larger.

Tiago Peixoto's avatar
Tiago Peixoto committed
162
163
    Examples
    --------
164
165
166
167
168
169
170
171

    .. testcode::
       :hide:

       from numpy.random import randint, random, seed, poisson
       from pylab import *
       seed(43)
       gt.seed_rng(42)
Tiago Peixoto's avatar
Tiago Peixoto committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

    This is a degree sampler which uses rejection sampling to sample from the
    distribution :math:`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
    :math:`P(k)\propto 1/k` (with k_max=40) and an *assortative* degree
    correlation of the form:

    .. math::

        P(i,k) \propto \frac{1}{1+|i-k|}

192
193
194
    >>> g = gt.random_graph(1000, lambda: sample_k(40), model="probabilistic",
    ...                     vertex_corr=lambda i, k: 1.0 / (1 + abs(i - k)), directed=False,
    ...                     n_iter=100)
Tiago Peixoto's avatar
Tiago Peixoto committed
195
    >>> gt.scalar_assortativity(g, "out")
196
    (0.6321636468713748, 0.01082292099309249)
Tiago Peixoto's avatar
Tiago Peixoto committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211

    The following samples an in,out-degree pair from the joint distribution:

    .. math::

        p(j,k) = \frac{1}{2}\frac{e^{-m_1}m_1^j}{j!}\frac{e^{-m_1}m_1^k}{k!} +
                 \frac{1}{2}\frac{e^{-m_2}m_2^j}{j!}\frac{e^{-m_2}m_2^k}{k!}

    with :math:`m_1 = 4` and :math:`m_2 = 20`.

    >>> def deg_sample():
    ...    if random() > 0.5:
    ...        return poisson(4), poisson(4)
    ...    else:
    ...        return poisson(20), poisson(20)
212
    ...
Tiago Peixoto's avatar
Tiago Peixoto committed
213
214
215
216
217
218
219

    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")
220
221
    >>>
    >>> clf()
222
    >>> imshow(hist[0].T, interpolation="nearest", origin="lower")
Tiago Peixoto's avatar
Tiago Peixoto committed
223
224
225
    <...>
    >>> colorbar()
    <...>
226
    >>> xlabel("in-degree")
Tiago Peixoto's avatar
Tiago Peixoto committed
227
    <...>
228
    >>> ylabel("out-degree")
Tiago Peixoto's avatar
Tiago Peixoto committed
229
    <...>
230
    >>> savefig("combined-deg-hist.pdf")
Tiago Peixoto's avatar
Tiago Peixoto committed
231

232
233
234
235
236
    .. testcode::
       :hide:

       savefig("combined-deg-hist.png")

237
    .. figure:: combined-deg-hist.*
Tiago Peixoto's avatar
Tiago Peixoto committed
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
        :align: center

        Combined degree histogram.

    A correlated directed graph can be build as follows. Consider the following
    degree correlation:

    .. math::

         P(j',k'|j,k)=\frac{e^{-k}k^{j'}}{j'!}
         \frac{e^{-(20-j)}(20-j)^{k'}}{k'!}

    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)),
256
257
258
259
    ...                     model="probabilistic",
    ...                     vertex_corr=lambda a,b: (p.pmf(a[0], b[1]) *
    ...                                              p.pmf(a[1], 20 - b[0])),
    ...                     n_iter=100)
Tiago Peixoto's avatar
Tiago Peixoto committed
260
261
262

    Lets plot the average degree correlations to check.

263
    >>> clf()
264
265
    >>> axes([0.1,0.15,0.63,0.8])
    <...>
Tiago Peixoto's avatar
Tiago Peixoto committed
266
    >>> corr = gt.avg_neighbour_corr(g, "in", "in")
267
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
268
    ...         label=r"$\left<\text{in}\right>$ vs in")
269
    <...>
Tiago Peixoto's avatar
Tiago Peixoto committed
270
    >>> corr = gt.avg_neighbour_corr(g, "in", "out")
271
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
272
    ...         label=r"$\left<\text{out}\right>$ vs in")
273
    <...>
Tiago Peixoto's avatar
Tiago Peixoto committed
274
    >>> corr = gt.avg_neighbour_corr(g, "out", "in")
275
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
276
    ...          label=r"$\left<\text{in}\right>$ vs out")
277
    <...>
Tiago Peixoto's avatar
Tiago Peixoto committed
278
    >>> corr = gt.avg_neighbour_corr(g, "out", "out")
279
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
280
    ...          label=r"$\left<\text{out}\right>$ vs out")
Tiago Peixoto's avatar
Tiago Peixoto committed
281
    <...>
282
283
284
    >>> legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
    <...>
    >>> xlabel("Source degree")
Tiago Peixoto's avatar
Tiago Peixoto committed
285
    <...>
286
    >>> ylabel("Average target degree")
Tiago Peixoto's avatar
Tiago Peixoto committed
287
    <...>
288
    >>> savefig("deg-corr-dir.pdf")
Tiago Peixoto's avatar
Tiago Peixoto committed
289

290
291
292
293
294
    .. testcode::
       :hide:

       savefig("deg-corr-dir.png")

295
    .. figure:: deg-corr-dir.*
Tiago Peixoto's avatar
Tiago Peixoto committed
296
297
298
        :align: center

        Average nearest neighbour correlations.
299
300


301
    **Stochastic blockmodels**
302
303


304
305
306
    The following example shows how a stochastic blockmodel
    [holland-stochastic-1983]_ [karrer-stochastic-2011]_ can be generated. We
    will consider a system of 10 blocks, which form communities. The connection
307
308
309
310
311
312
313
314
315
316
    probability will be given by

    >>> def corr(a, b):
    ...    if a == b:
    ...        return 0.999
    ...    else:
    ...        return 0.001

    The blockmodel can be generated as follows.

Tiago Peixoto's avatar
Tiago Peixoto committed
317
    >>> g, bm = gt.random_graph(2000, lambda: poisson(10), directed=False,
318
319
320
    ...                         model="blockmodel-traditional",
    ...                         block_membership=lambda: randint(10),
    ...                         vertex_corr=corr)
Tiago Peixoto's avatar
Tiago Peixoto committed
321
    >>> gt.graph_draw(g, vertex_fill_color=bm, edge_color="black", output="blockmodel.pdf")
322
323
    <...>

324
325
326
    .. testcode::
       :hide:

Tiago Peixoto's avatar
Tiago Peixoto committed
327
       gt.graph_draw(g, vertex_fill_color=bm, edge_color="black", output="blockmodel.png")
328

329
    .. figure:: blockmodel.*
330
331
332
333
334
335
336
337
338
339
        :align: center

        Simple blockmodel with 10 blocks.


    References
    ----------
    .. [metropolis-equations-1953]  Metropolis, N.; Rosenbluth, A.W.;
       Rosenbluth, M.N.; Teller, A.H.; Teller, E. "Equations of State
       Calculations by Fast Computing Machines". Journal of Chemical Physics 21
340
       (6): 1087-1092 (1953). :doi:`10.1063/1.1699114`
341
    .. [hastings-monte-carlo-1970] Hastings, W.K. "Monte Carlo Sampling Methods
342
       Using Markov Chains and Their Applications". Biometrika 57 (1): 97-109 (1970).
343
       :doi:`10.1093/biomet/57.1.97`
344
345
346
347
348
349
    .. [holland-stochastic-1983] Paul W. Holland, Kathryn Blackmond Laskey, and
       Samuel Leinhardt, "Stochastic blockmodels: First steps," Social Networks
       5, no. 2: 109-13 (1983) :doi:`10.1016/0378-8733(83)90021-7`
    .. [karrer-stochastic-2011] Brian Karrer and M. E. J. Newman, "Stochastic
       blockmodels and community structure in networks," Physical Review E 83,
       no. 1: 016107 (2011) :doi:`10.1103/PhysRevE.83.016107` :arxiv:`1008.3926`
Tiago Peixoto's avatar
Tiago Peixoto committed
350
    """
351

352
    g = Graph()
353

354
355
    if (type(block_membership) is types.FunctionType or
        type(block_membership) is types.LambdaType):
356
357
        btype = block_type
        bm = []
358
        if len(inspect.getargspec(block_membership)[0]) == 0:
359
            for i in range(N):
360
                bm.append(block_membership())
361
        else:
362
            for i in range(N):
363
364
365
366
                bm.append(block_membership(i))
        block_membership = bm
    elif block_membership is not None:
        btype = _gt_type(block_membership[0])
367
368

    if len(inspect.getargspec(deg_sampler)[0]) > 0:
369
370
        if block_membership is not None:
            sampler = lambda i: deg_sampler(i, block_membership[i])
371
        else:
Tiago Peixoto's avatar
Tiago Peixoto committed
372
            sampler = deg_sampler
373
374
375
376
    else:
        sampler = lambda i: deg_sampler()

    libgraph_tool_generation.gen_graph(g._Graph__graph, N, sampler,
377
                                       not parallel_edges,
378
                                       not self_loops, not directed,
379
                                       _get_rng(), verbose, True)
380
381
    g.set_directed(directed)

382
383
384
385
386
387
388
389
390
391
392
393
    if degree_block:
        if btype in ["object", "string"] or "vector" in btype:
            btype = "object"
        elif btype in ["int", "int32_t", "bool"]:
            btype = "vector<int32_t>"
        elif btype in ["long", "int64_t"]:
            btype = "vector<int64_t>"
        elif btype in ["double"]:
            btype = "vector<double>"
        elif btype in ["long double"]:
            btype = "vector<long double>"

394
    if block_membership is not None:
395
396
397
        bm = g.new_vertex_property(btype)
        if btype in ["object", "string"] or "vector" in btype:
            for v in g.vertices():
398
                if not degree_block:
399
                    bm[v] = block_membership[int(v)]
400
401
                else:
                    if g.is_directed():
402
                        bm[v] = (block_membership[int(v)], v.in_degree(),
403
404
                                 v.out_degree())
                    else:
405
                        bm[v] = (block_membership[int(v)], v.out_degree())
406
407
        else:
            try:
408
                bm.a = block_membership
409
410
411
            except ValueError:
                bm = g.new_vertex_property("object")
                for v in g.vertices():
412
                    bm[v] = block_membership[int(v)]
413
414
    else:
        bm = None
415

Tiago Peixoto's avatar
Tiago Peixoto committed
416
    if random:
417
418
419
        random_rewire(g, parallel_edges=parallel_edges,
                      self_loops=self_loops, verbose=verbose,
                      block_membership=bm, **kwargs)
420

421
422
423
424
    if bm is None:
        return g
    else:
        return g, bm
425

Tiago Peixoto's avatar
Tiago Peixoto committed
426

427
428
429
430
431
432
433
@_limit_args({"model": ["erdos", "correlated", "uncorrelated",
                        "probabilistic", "blockmodel",
                        "blockmodel-traditional"]})
def random_rewire(g, model="uncorrelated", n_iter=1, edge_sweep=True,
                  parallel_edges=False, self_loops=False, vertex_corr=None,
                  block_membership=None, alias=True, cache_probs=True,
                  persist=False, ret_fail=False, verbose=False):
434
    r"""
435

436
437
438
    Shuffle the graph in-place, following a variety of possible statistical
    models, chosen via the parameter ``model``.

439
440
441

    Parameters
    ----------
442
    g : :class:`~graph_tool.Graph`
443
        Graph to be shuffled. The graph will be modified.
444
445
446
    model : string (optional, default: ``"uncorrelated"``)
        The following statistical models can be chosen, which determine how the
        edges are rewired.
447

448
449
450
451
452
453
454
455
        ``erdos``
           The edges will be rewired entirely randomly, and the resulting graph
           will correspond to the Erdős–Rényi model.
        ``uncorrelated``
           The edges will be rewired randomly, but the degree sequence of the
           graph will remain unmodified.
        ``correlated``
           The edges will be rewired randomly, but both the degree sequence of
456
457
458
459
460
           the graph and the *vertex-vertex (in,out)-degree correlations* will
           remain exactly preserved. If the ``block_membership`` parameter is
           passed, the block variables at the endpoints of the edges will be
           preserved (instead of the degrees), in addition to the degree
           sequence.
461
462
        ``probabilistic``
           This is similar to the ``correlated`` option, but the vertex-vertex
463
           correlations are not kept unmodified, but instead are sampled from an
464
465
466
467
468
469
470
           arbitrary degree-based probabilistic model specified via the
           ``vertex_corr`` parameter.
        ``blockmodel``
          This is just like ``probabilistic``, but the values passed to the
          ``vertex_corr`` function will correspond to the block membership
          values specified by the ``block_membership`` parameter.
        ``blockmodel-traditional``
471
472
473
          This is just like ``blockmodel``, but the degree sequence *is not*
          preserved during rewiring.

474
475
476
477
478
479
480
481
482
483
484
485
486
    n_iter : int (optional, default: ``1``)
        Number of iterations. If ``edge_sweep == True``, each iteration
        corresponds to an entire "sweep" over all edges. Otherwise this
        corresponds to the total number of edges which are randomly chosen for a
        swap attempt (which may repeat).
    edge_sweep : bool (optional, default: ``True``)
        If ``True``, each iteration will perform an entire "sweep" over the
        edges, where each edge is visited once in random order, and a edge swap
        is attempted.
    parallel : bool (optional, default: ``False``)
        If ``True``, parallel edges are allowed.
    self_loops : bool (optional, default: ``False``)
        If ``True``, self-loops are allowed.
487
488
489
490
491
492
493
494
495
496
497
498
    vertex_corr : function or sequence of triples (optional, default: ``None``)

        A function which gives the vertex-vertex correlation of the edges in the
        graph. In general it should have the following signature:

        .. code::

            def vertex_corr(r, s):
                ...
                return p

        where the return value should be a scalar.
499

500
501
502
503
504
505
506
507
508
509
510
511
512
513
        Alternatively, this parameter can be a list of triples of the form
        ``(r, s, p)``, with the same meaning as the ``r``, ``s`` and ``p``
        values above. If a given ``(r, s)`` combination is not present in this
        list, the corresponding value of ``p`` is assumed to be zero. If the same
        ``(r, s)`` combination appears more than once, their ``p`` values will
        be summed together. This is useful when the correlation matrix is sparse,
        i.e. most entries are zero.

        If ``model == probabilistic`` the parameters ``r`` and ``s`` correspond
        respectively to the (in, out)-degree pair of the source vertex an edge,
        and the (in,out)-degree pair of the target of the same edge (for
        undirected graphs, both parameters are scalars instead). The value of
        ``p`` should be a number proportional to the probability of such an
        edge existing in the generated graph.
514
515

        If ``model == blockmodel`` or ``model == blockmodel-traditional``, the
516
517
518
519
        ``r`` and ``s`` values passed to the function will be the block values
        of the respective vertices, as specified via the ``block_membership``
        parameter. The value of  ``p`` should be a number proportional to the
        probability of such an edge existing in the generated graph.
520
521
522
523
524
525
526
527
528
529
530
    block_membership : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        If supplied, the graph will be rewired to conform to a blockmodel
        ensemble. The value must be a vertex property map which defines the
        block of each vertex.
    alias : bool (optional, default: ``True``)
        If ``True``, and ``model`` is any of ``probabilistic``, ``blockmodel``,
        or ``blockmodel-traditional``, the alias method will be used to sample
        the candidate edges. In the case of ``blockmodel-traditional``, if
        ``parallel_edges == True`` and ``self_loops == True`` this makes the
        sampling of the edges direct (not rejection based), so that
        ``n_iter == 1`` is enough to get an uncorrelated sample.
531
    cache_probs : bool (optional, default: ``True``)
532
        If ``True``, the probabilities returned by the ``vertex_corr`` parameter
533
534
535
536
        will be cached internally. This is crucial for good performance, since
        in this case the supplied python function is called only a few times,
        and not at every attempted edge rewire move. However, in the case were
        the different parameter combinations to the probability function is very
537
538
539
540
541
542
543
544
        large, the memory and time requirements to keep the cache may not be
        worthwhile.
    persist : bool (optional, default: ``False``)
        If ``True``, an edge swap which is rejected will be attempted again
        until it succeeds. This may improve the quality of the shuffling for
        some probabilistic models, and should be sufficiently fast for sparse
        graphs, but otherwise it may result in many repeated attempts for
        certain corner-cases in which edges are difficult to swap.
545
546
547
548
549
550
    verbose : bool (optional, default: ``False``)
        If ``True``, verbose information is displayed.


    Returns
    -------
551
552
553
    rejection_count : int
        Number of rejected edge moves (due to parallel edges or self-loops, or
        the probabilistic model used).
554
555
556
557
558
559
560

    See Also
    --------
    random_graph: random graph generation

    Notes
    -----
Tiago Peixoto's avatar
Tiago Peixoto committed
561
    This algorithm iterates through all the edges in the network and tries to
562
563
    swap its target or source with the target or source of another edge. The
    selected canditate swaps are chosen according to the ``model`` parameter.
Tiago Peixoto's avatar
Tiago Peixoto committed
564
565

    .. note::
566

567
568
569
570
571
572
573
574
        If ``parallel_edges = False``, parallel edges are not placed during
        rewiring. In this case, the returned graph will be a uncorrelated sample
        from the desired ensemble only if ``n_iter`` is sufficiently large. The
        algorithm implements an efficient Markov chain based on edge swaps, with
        a mixing time which depends on the degree distribution and correlations
        desired. If degree probabilistic correlations are provided, the mixing
        time tends to be larger.

575
576
577
578
579
580
581
        If ``model`` is either "probabilistic" or "blockmodel", the Markov chain
        still needs to be mixed, even if parallel edges and self-loops are
        allowed. In this case the Markov chain is implemented using the
        Metropolis-Hastings [metropolis-equations-1953]_
        [hastings-monte-carlo-1970]_ acceptance/rejection algorithm. It will
        eventually converge to the desired probabilities for sufficiently large
        values of ``n_iter``.
582

Tiago Peixoto's avatar
Tiago Peixoto committed
583

584
    Each edge is tentatively swapped once per iteration, so the overall
585
586
    complexity is :math:`O(V + E \times \text{n-iter})`. If ``edge_sweep ==
    False``, the complexity becomes :math:`O(V + E + \text{n-iter})`.
587

588
589
590
591
592
    Examples
    --------

    Some small graphs for visualization.

593
594
595
596
597
598
599
600
    .. testcode::
       :hide:

       from numpy.random import random, seed
       from pylab import *
       seed(43)
       gt.seed_rng(42)

601
    >>> g, pos = gt.triangulation(random((1000,2)))
602
    >>> pos = gt.arf_layout(g)
603
    >>> gt.graph_draw(g, pos=pos, output="rewire_orig.pdf", output_size=(300, 300))
604
    <...>
605
606
607
608
609
610

    .. testcode::
       :hide:

       gt.graph_draw(g, pos=pos, output="rewire_orig.png", output_size=(300, 300))

611
    >>> gt.random_rewire(g, "correlated")
Tiago Peixoto's avatar
Tiago Peixoto committed
612
    212
613
    >>> pos = gt.arf_layout(g)
614
    >>> gt.graph_draw(g, pos=pos, output="rewire_corr.pdf", output_size=(300, 300))
615
    <...>
616
617
618
619
620
621

    .. testcode::
       :hide:

       gt.graph_draw(g, pos=pos, output="rewire_corr.png", output_size=(300, 300))

622
    >>> gt.random_rewire(g)
Tiago Peixoto's avatar
Tiago Peixoto committed
623
    207
624
    >>> pos = gt.arf_layout(g)
625
    >>> gt.graph_draw(g, pos=pos, output="rewire_uncorr.pdf", output_size=(300, 300))
626
    <...>
627
628
629
630
631
632

    .. testcode::
       :hide:

       gt.graph_draw(g, pos=pos, output="rewire_uncorr.png", output_size=(300, 300))

633
    >>> gt.random_rewire(g, "erdos")
Tiago Peixoto's avatar
Tiago Peixoto committed
634
    21
635
    >>> pos = gt.arf_layout(g)
636
    >>> gt.graph_draw(g, pos=pos, output="rewire_erdos.pdf", output_size=(300, 300))
637
    <...>
638

639
640
641
642
643
    .. testcode::
       :hide:

       gt.graph_draw(g, pos=pos, output="rewire_erdos.png", output_size=(300, 300))

644
    Some `ridiculograms <http://www.youtube.com/watch?v=YS-asmU3p_4>`_ :
645

646
647
648
649
    .. image:: rewire_orig.*
    .. image:: rewire_corr.*
    .. image:: rewire_uncorr.*
    .. image:: rewire_erdos.*
650

651
652
    **From left to right**: Original graph; Shuffled graph, with degree correlations;
    Shuffled graph, without degree correlations; Shuffled graph, with random degrees.
653

654
    We can try with larger graphs to get better statistics, as follows.
655

656
657
    >>> figure()
    <...>
658
659
660
    >>> g = gt.random_graph(30000, lambda: sample_k(20), model="probabilistic",
    ...                     vertex_corr=lambda i, j: exp(abs(i-j)), directed=False,
    ...                     n_iter=100)
661
    >>> corr = gt.avg_neighbour_corr(g, "out", "out")
662
663
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", label="Original")
    <...>
664
    >>> gt.random_rewire(g, "correlated")
Tiago Peixoto's avatar
Tiago Peixoto committed
665
    212
666
    >>> corr = gt.avg_neighbour_corr(g, "out", "out")
667
668
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="*", label="Correlated")
    <...>
669
    >>> gt.random_rewire(g)
Tiago Peixoto's avatar
Tiago Peixoto committed
670
    120
671
    >>> corr = gt.avg_neighbour_corr(g, "out", "out")
672
673
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", label="Uncorrelated")
    <...>
674
    >>> gt.random_rewire(g, "erdos")
Tiago Peixoto's avatar
Tiago Peixoto committed
675
    20
676
    >>> corr = gt.avg_neighbour_corr(g, "out", "out")
677
678
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-", label=r"Erd\H{o}s")
    <...>
679
680
681
682
683
684
    >>> xlabel("$k$")
    <...>
    >>> ylabel(r"$\left<k_{nn}\right>$")
    <...>
    >>> legend(loc="best")
    <...>
685
    >>> savefig("shuffled-stats.pdf")
686

687
688
689
690
691
692
    .. testcode::
       :hide:

       savefig("shuffled-stats.png")


693
    .. figure:: shuffled-stats.*
694
695
696
697
698
699
700
701
702
703
704
        :align: center

        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
    :func:`~graph_tool.generation.random_graph` for more details.

    >>> p = scipy.stats.poisson
    >>> g = gt.random_graph(20000, lambda: (sample_k(19), sample_k(19)),
705
706
707
    ...                     model="probabilistic",
    ...                     vertex_corr=lambda a, b: (p.pmf(a[0], b[1]) * p.pmf(a[1], 20 - b[0])),
    ...                     n_iter=100)
708
    >>> figure()
709
710
711
    <...>
    >>> axes([0.1,0.15,0.6,0.8])
    <...>
712
    >>> corr = gt.avg_neighbour_corr(g, "in", "out")
713
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
714
    ...          label=r"$\left<\text{o}\right>$ vs i")
715
    <...>
716
    >>> corr = gt.avg_neighbour_corr(g, "out", "in")
717
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
718
    ...          label=r"$\left<\text{i}\right>$ vs o")
719
    <...>
720
    >>> gt.random_rewire(g, "correlated")
Tiago Peixoto's avatar
Tiago Peixoto committed
721
    4130
722
    >>> corr = gt.avg_neighbour_corr(g, "in", "out")
723
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
724
    ...          label=r"$\left<\text{o}\right>$ vs i, corr.")
725
    <...>
726
    >>> corr = gt.avg_neighbour_corr(g, "out", "in")
727
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
728
    ...          label=r"$\left<\text{i}\right>$ vs o, corr.")
729
    <...>
730
    >>> gt.random_rewire(g, "uncorrelated")
Tiago Peixoto's avatar
Tiago Peixoto committed
731
    194
732
    >>> corr = gt.avg_neighbour_corr(g, "in", "out")
733
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
734
    ...          label=r"$\left<\text{o}\right>$ vs i, uncorr.")
735
    <...>
736
    >>> corr = gt.avg_neighbour_corr(g, "out", "in")
737
    >>> errorbar(corr[2][:-1], corr[0], yerr=corr[1], fmt="o-",
738
    ...          label=r"$\left<\text{i}\right>$ vs o, uncorr.")
739
    <...>
740
741
742
    >>> legend(bbox_to_anchor=(1.01, 0.5), loc="center left", borderaxespad=0.)
    <...>
    >>> xlabel("Source degree")
743
    <...>
744
    >>> ylabel("Average target degree")
745
    <...>
746
    >>> savefig("shuffled-deg-corr-dir.pdf")
747

748
749
750
751
752
    .. testcode::
       :hide:

       savefig("shuffled-deg-corr-dir.png")

753
    .. figure:: shuffled-deg-corr-dir.*
754
755
756
757
758
759
        :align: center

        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.

760
761
762
763
764
    References
    ----------
    .. [metropolis-equations-1953]  Metropolis, N.; Rosenbluth, A.W.;
       Rosenbluth, M.N.; Teller, A.H.; Teller, E. "Equations of State
       Calculations by Fast Computing Machines". Journal of Chemical Physics 21
765
       (6): 1087-1092 (1953). :doi:`10.1063/1.1699114`
766
    .. [hastings-monte-carlo-1970] Hastings, W.K. "Monte Carlo Sampling Methods
767
       Using Markov Chains and Their Applications". Biometrika 57 (1): 97-109 (1970).
768
       :doi:`10.1093/biomet/57.1.97`
769
770
771
772
773
774
    .. [holland-stochastic-1983] Paul W. Holland, Kathryn Blackmond Laskey, and
       Samuel Leinhardt, "Stochastic blockmodels: First steps," Social Networks
       5, no. 2: 109-13 (1983) :doi:`10.1016/0378-8733(83)90021-7`
    .. [karrer-stochastic-2011] Brian Karrer and M. E. J. Newman, "Stochastic
       blockmodels and community structure in networks," Physical Review E 83,
       no. 1: 016107 (2011) :doi:`10.1103/PhysRevE.83.016107` :arxiv:`1008.3926`
775
776

    """
Tiago Peixoto's avatar
Tiago Peixoto committed
777
778
779
780
781
782
783
784
785
786
787
788
789
    if not parallel_edges:
        p = label_parallel_edges(g)
        if p.a.max() != 0:
            raise ValueError("Parallel edge detected. Can't rewire " +
                             "graph without parallel edges if it " +
                             "already contains parallel edges!")
    if not self_loops:
        l = label_self_loops(g)
        if l.a.max() != 0:
            raise ValueError("Self-loop detected. Can't rewire graph " +
                             "without self-loops if it already contains" +
                             " self-loops!")

790
791
    if (vertex_corr is not None and not g.is_directed()) and "blockmodel" not in model:
        corr = lambda i, j: vertex_corr(i[1], j[1])
792
    else:
793
        corr = vertex_corr
794

795
    if model not in ["probabilistic", "blockmodel", "blockmodel-traditional"]:
796
        g = GraphView(g, reversed=False)
797

798
799
800
801
    if model == "blockmodel" and alias and edge_sweep:
        edge_sweep = False
        n_iter *= g.num_edges()

802
803
804
805
    traditional = False
    if model == "blockmodel-traditional":
        model = "blockmodel"
        traditional = True
806

807
    pcount = libgraph_tool_generation.random_rewire(g._Graph__graph, model,
808
809
                                                    n_iter, not edge_sweep,
                                                    self_loops, parallel_edges,
810
811
                                                    alias, traditional, persist,
                                                    corr, _prop("v", g, block_membership),
812
                                                    cache_probs,
813
                                                    _get_rng(), verbose)
814
    return pcount
Tiago Peixoto's avatar
Tiago Peixoto committed
815

Tiago Peixoto's avatar
Tiago Peixoto committed
816

Tiago Peixoto's avatar
Tiago Peixoto committed
817
def predecessor_tree(g, pred_map):
Tiago Peixoto's avatar
Tiago Peixoto committed
818
    """Return a graph from a list of predecessors given by the ``pred_map`` vertex property."""
Tiago Peixoto's avatar
Tiago Peixoto committed
819
820
821
822
823
824
825

    _check_prop_scalar(pred_map, "pred_map")
    pg = Graph()
    libgraph_tool_generation.predecessor_graph(g._Graph__graph,
                                               pg._Graph__graph,
                                               _prop("v", g, pred_map))
    return pg
826

Tiago Peixoto's avatar
Tiago Peixoto committed
827

828
def line_graph(g):
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
    """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.

845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871

    Examples
    --------

    >>> g = gt.collection.data["lesmis"]
    >>> lg, vmap = gt.line_graph(g)
    >>> gt.graph_draw(g, pos=g.vp["pos"], output="lesmis.pdf")
    <...>
    >>> pos = gt.graph_draw(lg, output="lesmis-lg.pdf")

    .. testcode::
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], output="lesmis.png")
       pos = gt.graph_draw(lg, pos=pos, output="lesmis-lg.png")


    .. figure:: lesmis.png
       :align: left

       Coappearances of characters in Victor Hugo's novel "Les Miserables".

    .. figure:: lesmis-lg.png
       :align: right

       Line graph of the coappearance network on the left.

872
873
874
875
    References
    ----------
    .. [line-wiki] http://en.wikipedia.org/wiki/Line_graph
    """
876
877
878
879
880
881
882
883
    lg = Graph(directed=g.is_directed())

    vertex_map = lg.new_vertex_property("int64_t")

    libgraph_tool_generation.line_graph(g._Graph__graph,
                                        lg._Graph__graph,
                                        _prop("v", lg, vertex_map))
    return lg, vertex_map
Tiago Peixoto's avatar
Tiago Peixoto committed
884

Tiago Peixoto's avatar
Tiago Peixoto committed
885

886
def graph_union(g1, g2, intersection=None, props=None, include=False):
887
888
889
890
891
892
893
894
895
    """Return the union of graphs g1 and g2, composed of all edges and vertices
    of g1 and g2, without overlap.

    Parameters
    ----------
    g1 : :class:`~graph_tool.Graph`
       First graph in the union.
    g2 : :class:`~graph_tool.Graph`
       Second graph in the union.
896
    intersection : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
897
       Vertex property map owned by `g1` which maps each of its vertices
898
899
900
       to vertex indexes belonging to `g2`. Negative values mean no mapping
       exists, and thus both vertices in `g1` and `g2` will be present in the
       union graph.
901
    props : list of tuples of :class:`~graph_tool.PropertyMap` (optional, default: ``[]``)
902
       Each element in this list must be a tuple of two PropertyMap objects. The
903
904
905
       first element must be a property of `g1`, and the second of `g2`. If either
       value is ``None``, an empty map is created. The values of the property
       maps are propagated into the union graph, and returned.
906
    include : bool (optional, default: ``False``)
907
908
909
910
911
912
913
914
915
916
       If true, graph `g2` is inserted into `g1` which is modified. If false, a
       new graph is created, and both graphs remain unmodified.

    Returns
    -------
    ug : :class:`~graph_tool.Graph`
        The union graph
    props : list of :class:`~graph_tool.PropertyMap` objects
        List of propagated properties.  This is only returned if `props` is not
        empty.
917
918
919
920

    Examples
    --------

921
922
923
924
925
926
927
928
    .. testcode::
       :hide:

       from numpy.random import random, seed
       from pylab import *
       seed(42)
       gt.seed_rng(42)

929
930
931
    >>> g = gt.triangulation(random((300,2)))[0]
    >>> ug = gt.graph_union(g, g)
    >>> uug = gt.graph_union(g, ug)
932
    >>> pos = gt.sfdp_layout(g)
933
    >>> gt.graph_draw(g, pos=pos, output_size=(300,300), output="graph_original.pdf")
934
    <...>
935
936
937
938
939
940
941

    .. testcode::
       :hide:

       gt.graph_draw(g, pos=pos, output_size=(300,300), output="graph_original.png")

    >>> pos = gt.sfdp_layout(ug)
942
    >>> gt.graph_draw(ug, pos=pos, output_size=(300,300), output="graph_union.pdf")
943
    <...>
944
945
946
947
948
949
950

    .. testcode::
       :hide:

       gt.graph_draw(ug, pos=pos, output_size=(300,300), output="graph_union.png")

    >>> pos = gt.sfdp_layout(uug)
951
    >>> gt.graph_draw(uug, pos=pos, output_size=(300,300), output="graph_union2.pdf")
952
953
    <...>

954
955
956
957
958
959
    .. testcode::
       :hide:

       gt.graph_draw(uug, pos=pos, output_size=(300,300), output="graph_union2.png")


960
961
962
    .. image:: graph_original.*
    .. image:: graph_union.*
    .. image:: graph_union2.*
963

964
    """
Tiago Peixoto's avatar
Tiago Peixoto committed
965
966
    if props == None:
        props = []
Tiago Peixoto's avatar
Tiago Peixoto committed
967
    if not include:
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
        g1 = GraphView(g1, skip_properties=True)
        p1s = []
        for i, (p1, p2) in enumerate(props):
            if p1 is None:
                continue
            if p1.key_type() == "v":
                g1.vp[str(i)] = p1
            elif p1.key_type() == "e":
                g1.ep[str(i)] = p1

        g1 = Graph(g1, prune=True)

        for i, (p1, p2) in enumerate(props):
            if p1 is None:
                continue
            if str(i) in g1.vp:
                props[i] = (g1.vp[str(i)], p2)
                del g1.vp[str(i)]
            else:
                props[i] = (g1.ep[str(i)], p2)
                del g1.ep[str(i)]
    else:
        emask, emask_flip = g1.get_edge_filter()
        emask_flipped = False
        if emask is not None and not emask_flip:
            emask.a = not emask.a
            emask_flipped = True
            g1.set_edge_filter(emask, True)

        vmask, vmask_flip = g1.get_vertex_filter()
        vmask_flipped = False
        if vmask is not None and not vmask_flip:
            vmask.a = not vmask.a