__init__.py 63.3 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
19
20
#
# 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
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

21
"""
22
23
``graph_tool.topology`` - Assessing graph topology
--------------------------------------------------
24
25
26
27
28
29
30

Summary
+++++++

.. autosummary::
   :nosignatures:

31
   shortest_distance
Tiago Peixoto's avatar
Tiago Peixoto committed
32
   shortest_path
Tiago Peixoto's avatar
Tiago Peixoto committed
33
   pseudo_diameter
34
   similarity
35
   isomorphism
36
37
   subgraph_isomorphism
   mark_subgraph
38
39
   max_cardinality_matching
   max_independent_vertex_set
40
   min_spanning_tree
41
   random_spanning_tree
42
43
44
   dominator_tree
   topological_sort
   transitive_closure
Tiago Peixoto's avatar
Tiago Peixoto committed
45
   tsp_tour
46
   sequential_vertex_coloring
47
48
   label_components
   label_biconnected_components
49
   label_largest_component
50
   label_out_component
Tiago Peixoto's avatar
Tiago Peixoto committed
51
   kcore_decomposition
52
   is_bipartite
Tiago Peixoto's avatar
Tiago Peixoto committed
53
   is_DAG
54
   is_planar
55
   make_maximal_planar
Tiago Peixoto's avatar
Tiago Peixoto committed
56
   edge_reciprocity
57
58
59

Contents
++++++++
60

61
62
"""

63
64
from __future__ import division, absolute_import, print_function

Tiago Peixoto's avatar
Tiago Peixoto committed
65
from .. dl_import import dl_import
66
dl_import("from . import libgraph_tool_topology")
67

68
from .. import _prop, Vector_int32_t, _check_prop_writable, \
69
     _check_prop_scalar, _check_prop_vector, Graph, PropertyMap, GraphView,\
Tiago Peixoto's avatar
Tiago Peixoto committed
70
     libcore, _get_rng, _degree
71
import random, sys, numpy
72
__all__ = ["isomorphism", "subgraph_isomorphism", "mark_subgraph",
73
           "max_cardinality_matching", "max_independent_vertex_set",
74
           "min_spanning_tree", "random_spanning_tree", "dominator_tree",
Tiago Peixoto's avatar
Tiago Peixoto committed
75
           "topological_sort", "transitive_closure", "tsp_tour",
76
77
           "sequential_vertex_coloring", "label_components",
           "label_largest_component", "label_biconnected_components",
Tiago Peixoto's avatar
Tiago Peixoto committed
78
79
80
           "label_out_component", "kcore_decomposition", "shortest_distance",
           "shortest_path", "pseudo_diameter", "is_bipartite", "is_DAG",
           "is_planar", "make_maximal_planar", "similarity", "edge_reciprocity"]
81
82
83
84
85
86
87
88
89
90


def similarity(g1, g2, label1=None, label2=None, norm=True):
    r"""Return the adjacency similarity between the two graphs.

    Parameters
    ----------
    g1 : :class:`~graph_tool.Graph`
        First graph to be compared.
    g2 : :class:`~graph_tool.Graph`
Tiago Peixoto's avatar
Tiago Peixoto committed
91
        Second graph to be compared.
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    label1 : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Vertex labels for the first graph to be used in comparison. If not
        supplied, the vertex indexes are used.
    label2 : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Vertex labels for the second graph to be used in comparison. If not
        supplied, the vertex indexes are used.
    norm : bool (optional, default: ``True``)
        If ``True``, the returned value is normalized by the total number of
        edges.

    Returns
    -------
    similarity : float
        Adjacency similarity value.

    Notes
    -----
    The adjacency similarity is the sum of equal entries in the adjacency
    matrix, given a vertex ordering determined by the vertex labels. In other
    words it counts the number of edges which have the same source and target
    labels in both graphs.

    The algorithm runs with complexity :math:`O(E_1 + V_1 + E_2 + V_2)`.

    Examples
    --------
118
119
120
121
122
123
124
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

125
126
127
128
129
130
    >>> g = gt.random_graph(100, lambda: (3,3))
    >>> u = g.copy()
    >>> gt.similarity(u, g)
    1.0
    >>> gt.random_rewire(u);
    >>> gt.similarity(u, g)
Tiago Peixoto's avatar
Tiago Peixoto committed
131
    0.05
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
    """

    if label1 is None:
        label1 = g1.vertex_index
    if label2 is None:
        label2 = g2.vertex_index
    if label1.value_type() != label2.value_type():
        raise ValueError("label property maps must be of the same type")
    s = libgraph_tool_topology.\
           similarity(g1._Graph__graph, g2._Graph__graph,
                      _prop("v", g1, label1), _prop("v", g1, label2))
    if not g1.is_directed() or not g2.is_directed():
        s /= 2
    if norm:
        s /= float(max(g1.num_edges(), g2.num_edges()))
    return s
148

Tiago Peixoto's avatar
Tiago Peixoto committed
149

150
def isomorphism(g1, g2, isomap=False):
151
152
153
154
155
156
157
    r"""Check whether two graphs are isomorphic.

    If `isomap` is True, a vertex :class:`~graph_tool.PropertyMap` with the
    isomorphism mapping is returned as well.

    Examples
    --------
158
159
160
161
162
163
164
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

165
166
167
168
169
170
171
172
173
    >>> g = gt.random_graph(100, lambda: (3,3))
    >>> g2 = gt.Graph(g)
    >>> gt.isomorphism(g, g2)
    True
    >>> g.add_edge(g.vertex(0), g.vertex(1))
    <...>
    >>> gt.isomorphism(g, g2)
    False

174
    """
175
176
    imap = g1.new_vertex_property("int32_t")
    iso = libgraph_tool_topology.\
177
           check_isomorphism(g1._Graph__graph, g2._Graph__graph,
Tiago Peixoto's avatar
Tiago Peixoto committed
178
                             _prop("v", g1, imap))
179
180
181
182
183
    if isomap:
        return iso, imap
    else:
        return iso

Tiago Peixoto's avatar
Tiago Peixoto committed
184

185
def subgraph_isomorphism(sub, g, max_n=0, random=False):
186
    r"""
187
188
    Obtain all subgraph isomorphisms of `sub` in `g` (or at most `max_n`
    subgraphs, if `max_n > 0`).
189

190

Tiago Peixoto's avatar
Tiago Peixoto committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    Parameters
    ----------
    sub : :class:`~graph_tool.Graph`
        Subgraph for which to be searched.
    g : :class:`~graph_tool.Graph`
        Graph in which the search is performed.
    max_n : int (optional, default: 0)
        Maximum number of matches to find. If `max_n == 0`, all matches are
        found.
    random : bool (optional, default: False)
        If `True`, the vertices of `g` are indexed in random order before
        the search.

    Returns
    -------
    vertex_maps : list of :class:`~graph_tool.PropertyMap` objects
        List containing vertex property map objects which indicate different
        isomorphism mappings. The property maps vertices in `sub` to the
        corresponding vertex index in `g`.
    edge_maps : list of :class:`~graph_tool.PropertyMap` objects
        List containing edge property map objects which indicate different
        isomorphism mappings. The property maps edges in `sub` to the
        corresponding edge index in `g`.

    Notes
    -----
    The algorithm used is described in [ullmann-algorithm-1976]_. It has a
    worse-case complexity of :math:`O(N_g^{N_{sub}})`, but for random graphs it
    typically has a complexity of :math:`O(N_g^\gamma)` with :math:`\gamma`
    depending sub-linearly on the size of `sub`.
221
222
223

    Examples
    --------
224
225
226
227
228
229
230
231
232
233
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(44)
       gt.seed_rng(44)

    >>> from numpy.random import poisson
    >>> g = gt.random_graph(30, lambda: (poisson(6.0), poisson(6.0)))
    >>> sub = gt.random_graph(10, lambda: (poisson(1.9), poisson(1.9)))
234
    >>> vm, em = gt.subgraph_isomorphism(sub, g)
235
    >>> print(len(vm))
Tiago Peixoto's avatar
Tiago Peixoto committed
236
    5632
237
    >>> for i in range(len(vm)):
238
239
240
241
242
243
244
245
246
247
    ...   g.set_vertex_filter(None)
    ...   g.set_edge_filter(None)
    ...   vmask, emask = gt.mark_subgraph(g, sub, vm[i], em[i])
    ...   g.set_vertex_filter(vmask)
    ...   g.set_edge_filter(emask)
    ...   assert(gt.isomorphism(g, sub))
    >>> g.set_vertex_filter(None)
    >>> g.set_edge_filter(None)
    >>> ewidth = g.copy_property(emask, value_type="double")
    >>> ewidth.a += 0.5
Tiago Peixoto's avatar
Tiago Peixoto committed
248
249
250
    >>> ewidth.a *= 2
    >>> gt.graph_draw(g, vertex_fill_color=vmask, edge_color=emask,
    ...               edge_pen_width=ewidth, output_size=(200, 200),
251
    ...               output="subgraph-iso-embed.pdf")
252
    <...>
Tiago Peixoto's avatar
Tiago Peixoto committed
253
    >>> gt.graph_draw(sub, output_size=(200, 200), output="subgraph-iso.pdf")
254
255
    <...>

Tiago Peixoto's avatar
Tiago Peixoto committed
256
257
    .. image:: subgraph-iso.*
    .. image:: subgraph-iso-embed.*
258

259

Tiago Peixoto's avatar
Tiago Peixoto committed
260
    **Left:** Subgraph searched, **Right:** One isomorphic subgraph found in main graph.
261
262
263

    References
    ----------
264
    .. [ullmann-algorithm-1976] Ullmann, J. R., "An algorithm for subgraph
265
       isomorphism", Journal of the ACM 23 (1): 31-42, 1976, :doi:`10.1145/321921.321925`
266
    .. [subgraph-isormophism-wikipedia] http://en.wikipedia.org/wiki/Subgraph_isomorphism_problem
267
268

    """
269
270
    if sub.num_vertices() == 0:
        raise ValueError("Cannot search for an empty subgraph.")
271
272
    # vertex and edge labels disabled for the time being, until GCC is capable
    # of compiling all the variants using reasonable amounts of memory
Tiago Peixoto's avatar
Tiago Peixoto committed
273
274
    vlabels=(None, None)
    elabels=(None, None)
275
276
    vmaps = []
    emaps = []
277
    if random:
278
        rng = _get_rng()
279
    else:
280
        rng = libcore.rng_t()
281
282
283
284
285
286
    libgraph_tool_topology.\
           subgraph_isomorphism(sub._Graph__graph, g._Graph__graph,
                                _prop("v", sub, vlabels[0]),
                                _prop("v", g, vlabels[1]),
                                _prop("e", sub, elabels[0]),
                                _prop("e", g, elabels[1]),
287
                                vmaps, emaps, max_n, rng)
288
    for i in range(len(vmaps)):
289
290
291
292
        vmaps[i] = PropertyMap(vmaps[i], sub, "v")
        emaps[i] = PropertyMap(emaps[i], sub, "e")
    return vmaps, emaps

Tiago Peixoto's avatar
Tiago Peixoto committed
293

294
295
296
297
298
299
300
301
302
303
def mark_subgraph(g, sub, vmap, emap, vmask=None, emask=None):
    r"""
    Mark a given subgraph `sub` on the graph `g`.

    The mapping must be provided by the `vmap` and `emap` parameters,
    which map vertices/edges of `sub` to indexes of the corresponding
    vertices/edges in `g`.

    This returns a vertex and an edge property map, with value type 'bool',
    indicating whether or not a vertex/edge in `g` corresponds to the subgraph
304
    `sub`.
305
    """
306
    if vmask is None:
307
        vmask = g.new_vertex_property("bool")
308
    if emask is None:
309
310
311
312
313
314
315
316
317
318
319
320
321
322
        emask = g.new_edge_property("bool")

    vmask.a = False
    emask.a = False

    for v in sub.vertices():
        w = g.vertex(vmap[v])
        vmask[w] = True
        for ew in w.out_edges():
            for ev in v.out_edges():
                if emap[ev] == g.edge_index[ew]:
                    emask[ew] = True
                    break
    return vmask, emask
323

Tiago Peixoto's avatar
Tiago Peixoto committed
324

325
def min_spanning_tree(g, weights=None, root=None, tree_map=None):
326
327
328
329
330
331
332
    """
    Return the minimum spanning tree of a given graph.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
333
    weights : :class:`~graph_tool.PropertyMap` (optional, default: `None`)
334
335
        The edge weights. If provided, the minimum spanning tree will minimize
        the edge weights.
336
    root : :class:`~graph_tool.Vertex` (optional, default: `None`)
337
        Root of the minimum spanning tree. If this is provided, Prim's algorithm
338
        is used. Otherwise, Kruskal's algorithm is used.
339
    tree_map : :class:`~graph_tool.PropertyMap` (optional, default: `None`)
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
        If provided, the edge tree map will be written in this property map.

    Returns
    -------
    tree_map : :class:`~graph_tool.PropertyMap`
        Edge property map with mark the tree edges: 1 for tree edge, 0
        otherwise.

    Notes
    -----
    The algorithm runs with :math:`O(E\log E)` complexity, or :math:`O(E\log V)`
    if `root` is specified.

    Examples
    --------
355
356
357
358
359
360
361
362
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

    >>> from numpy.random import random
363
364
365
    >>> g, pos = gt.triangulation(random((400, 2)) * 10, type="delaunay")
    >>> weight = g.new_edge_property("double")
    >>> for e in g.edges():
Tiago Peixoto's avatar
Tiago Peixoto committed
366
    ...    weight[e] = linalg.norm(pos[e.target()].a - pos[e.source()].a)
367
    >>> tree = gt.min_spanning_tree(g, weights=weight)
368
    >>> gt.graph_draw(g, pos=pos, output="triang_orig.pdf")
369
370
    <...>
    >>> g.set_edge_filter(tree)
371
    >>> gt.graph_draw(g, pos=pos, output="triang_min_span_tree.pdf")
372
373
374
    <...>


375
    .. image:: triang_orig.*
Tiago Peixoto's avatar
Tiago Peixoto committed
376
        :width: 400px
377
    .. image:: triang_min_span_tree.*
Tiago Peixoto's avatar
Tiago Peixoto committed
378
        :width: 400px
379
380

    *Left:* Original graph, *Right:* The minimum spanning tree.
381
382
383
384
385

    References
    ----------
    .. [kruskal-shortest-1956] J. B. Kruskal.  "On the shortest spanning subtree
       of a graph and the traveling salesman problem",  In Proceedings of the
Tiago Peixoto's avatar
Tiago Peixoto committed
386
387
       American Mathematical Society, volume 7, pages 48-50, 1956.
       :doi:`10.1090/S0002-9939-1956-0078686-7`
388
389
390
391
392
    .. [prim-shortest-1957] R. Prim.  "Shortest connection networks and some
       generalizations",  Bell System Technical Journal, 36:1389-1401, 1957.
    .. [boost-mst] http://www.boost.org/libs/graph/doc/graph_theory_review.html#sec:minimum-spanning-tree
    .. [mst-wiki] http://en.wikipedia.org/wiki/Minimum_spanning_tree
    """
393
    if tree_map is None:
394
395
396
397
        tree_map = g.new_edge_property("bool")
    if tree_map.value_type() != "bool":
        raise ValueError("edge property 'tree_map' must be of value type bool.")

398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
    try:
        g.stash_filter(directed=True)
        g.set_directed(False)
        if root is None:
            libgraph_tool_topology.\
                   get_kruskal_spanning_tree(g._Graph__graph,
                                             _prop("e", g, weights),
                                             _prop("e", g, tree_map))
        else:
            libgraph_tool_topology.\
                   get_prim_spanning_tree(g._Graph__graph, int(root),
                                          _prop("e", g, weights),
                                          _prop("e", g, tree_map))
    finally:
        g.pop_filter(directed=True)
413
    return tree_map
414

Tiago Peixoto's avatar
Tiago Peixoto committed
415

416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
def random_spanning_tree(g, weights=None, root=None, tree_map=None):
    """
    Return a random spanning tree of a given graph, which can be directed or
    undirected.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    weights : :class:`~graph_tool.PropertyMap` (optional, default: `None`)
        The edge weights. If provided, the probability of a particular spanning
        tree being selected is the product of its edge weights.
    root : :class:`~graph_tool.Vertex` (optional, default: `None`)
        Root of the spanning tree. If not provided, it will be selected randomly.
    tree_map : :class:`~graph_tool.PropertyMap` (optional, default: `None`)
        If provided, the edge tree map will be written in this property map.

    Returns
    -------
    tree_map : :class:`~graph_tool.PropertyMap`
        Edge property map with mark the tree edges: 1 for tree edge, 0
        otherwise.

    Notes
    -----
    The typical running time for random graphs is :math:`O(N\log N)`.

    Examples
    --------
445
446
447
448
449
450
451
452
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

    >>> from numpy.random import random
453
454
455
456
457
458
459
460
    >>> g, pos = gt.triangulation(random((400, 2)) * 10, type="delaunay")
    >>> weight = g.new_edge_property("double")
    >>> for e in g.edges():
    ...    weight[e] = linalg.norm(pos[e.target()].a - pos[e.source()].a)
    >>> tree = gt.random_spanning_tree(g, weights=weight)
    >>> gt.graph_draw(g, pos=pos, output="rtriang_orig.pdf")
    <...>
    >>> g.set_edge_filter(tree)
Tiago Peixoto's avatar
Tiago Peixoto committed
461
    >>> gt.graph_draw(g, pos=pos, output="triang_random_span_tree.pdf")
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
    <...>


    .. image:: rtriang_orig.*
        :width: 400px
    .. image:: triang_random_span_tree.*
        :width: 400px

    *Left:* Original graph, *Right:* A random spanning tree.

    References
    ----------

    .. [wilson-generating-1996] David Bruce Wilson, "Generating random spanning
       trees more quickly than the cover time", Proceedings of the twenty-eighth
       annual ACM symposium on Theory of computing, Pages 296-303, ACM New York,
       1996, :doi:`10.1145/237814.237880`
    .. [boost-rst] http://www.boost.org/libs/graph/doc/random_spanning_tree.html
    """
    if tree_map is None:
        tree_map = g.new_edge_property("bool")
    if tree_map.value_type() != "bool":
        raise ValueError("edge property 'tree_map' must be of value type bool.")

    if root is None:
        root = g.vertex(numpy.random.randint(0, g.num_vertices()),
                        use_index=False)

    # we need to restrict ourselves to the in-component of root
    l = label_out_component(GraphView(g, reversed=True), root)
    g = GraphView(g, vfilt=l)

    libgraph_tool_topology.\
        random_spanning_tree(g._Graph__graph, int(root),
                             _prop("e", g, weights),
497
                             _prop("e", g, tree_map), _get_rng())
498
499
500
    return tree_map


Tiago Peixoto's avatar
Tiago Peixoto committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
def dominator_tree(g, root, dom_map=None):
    """Return a vertex property map the dominator vertices for each vertex.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    root : :class:`~graph_tool.Vertex`
        The root vertex.
    dom_map : :class:`~graph_tool.PropertyMap` (optional, default: None)
        If provided, the dominator map will be written in this property map.

    Returns
    -------
    dom_map : :class:`~graph_tool.PropertyMap`
        The dominator map. It contains for each vertex, the index of its
        dominator vertex.

    Notes
    -----
    A vertex u dominates a vertex v, if every path of directed graph from the
    entry to v must go through u.

    The algorithm runs with :math:`O((V+E)\log (V+E))` complexity.

    Examples
    --------
528
529
530
531
532
533
534
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

Tiago Peixoto's avatar
Tiago Peixoto committed
535
536
537
    >>> g = gt.random_graph(100, lambda: (2, 2))
    >>> tree = gt.min_spanning_tree(g)
    >>> g.set_edge_filter(tree)
538
    >>> root = [v for v in g.vertices() if v.in_degree() == 0]
Tiago Peixoto's avatar
Tiago Peixoto committed
539
    >>> dom = gt.dominator_tree(g, root[0])
540
    >>> print(dom.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
541
542
    [ 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
      0  0  0 78  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
543
      0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
Tiago Peixoto's avatar
Tiago Peixoto committed
544
      0  0  0 15  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0]
Tiago Peixoto's avatar
Tiago Peixoto committed
545
546
547

    References
    ----------
548
    .. [dominator-bgl] http://www.boost.org/libs/graph/doc/lengauer_tarjan_dominator.htm
Tiago Peixoto's avatar
Tiago Peixoto committed
549
550

    """
551
    if dom_map is None:
Tiago Peixoto's avatar
Tiago Peixoto committed
552
553
554
        dom_map = g.new_vertex_property("int32_t")
    if dom_map.value_type() != "int32_t":
        raise ValueError("vertex property 'dom_map' must be of value type" +
555
556
                         " int32_t.")
    if not g.is_directed():
Tiago Peixoto's avatar
Tiago Peixoto committed
557
        raise ValueError("dominator tree requires a directed graph.")
558
    libgraph_tool_topology.\
Tiago Peixoto's avatar
Tiago Peixoto committed
559
560
561
               dominator_tree(g._Graph__graph, int(root),
                              _prop("v", g, dom_map))
    return dom_map
562

Tiago Peixoto's avatar
Tiago Peixoto committed
563

564
def topological_sort(g):
Tiago Peixoto's avatar
Tiago Peixoto committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
    """
    Return the topological sort of the given graph. It is returned as an array
    of vertex indexes, in the sort order.

    Notes
    -----
    The topological sort algorithm creates a linear ordering of the vertices
    such that if edge (u,v) appears in the graph, then v comes before u in the
    ordering. The graph must be a directed acyclic graph (DAG).

    The time complexity is :math:`O(V + E)`.

    Examples
    --------
579
580
581
582
583
584
585
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

Tiago Peixoto's avatar
Tiago Peixoto committed
586
587
588
589
    >>> g = gt.random_graph(30, lambda: (3, 3))
    >>> tree = gt.min_spanning_tree(g)
    >>> g.set_edge_filter(tree)
    >>> sort = gt.topological_sort(g)
590
    >>> print(sort)
Tiago Peixoto's avatar
Tiago Peixoto committed
591
592
    [17  1 20  5  6  8 28  0  3  9 11 24 29  2 22  4  7 14 19 26 23 10 12 13 15
     16 18 21 25 27]
Tiago Peixoto's avatar
Tiago Peixoto committed
593
594
595

    References
    ----------
596
    .. [topological-boost] http://www.boost.org/libs/graph/doc/topological_sort.html
Tiago Peixoto's avatar
Tiago Peixoto committed
597
598
599
600
    .. [topological-wiki] http://en.wikipedia.org/wiki/Topological_sorting

    """

601
    topological_order = Vector_int32_t()
Tiago Peixoto's avatar
Tiago Peixoto committed
602
603
604
605
606
    is_DAG = libgraph_tool_topology.\
        topological_sort(g._Graph__graph, topological_order)
    if not is_DAG:
        raise ValueError("Graph is not a directed acylic graph (DAG).");
    return topological_order.a.copy()
607

Tiago Peixoto's avatar
Tiago Peixoto committed
608

609
def transitive_closure(g):
Tiago Peixoto's avatar
Tiago Peixoto committed
610
611
612
613
614
615
616
617
618
619
620
621
622
    """Return the transitive closure graph of g.

    Notes
    -----
    The transitive closure of a graph G = (V,E) is a graph G* = (V,E*) such that
    E* contains an edge (u,v) if and only if G contains a path (of at least one
    edge) from u to v. The transitive_closure() function transforms the input
    graph g into the transitive closure graph tc.

    The time complexity (worst-case) is :math:`O(VE)`.

    Examples
    --------
623
624
625
626
627
628
629
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

Tiago Peixoto's avatar
Tiago Peixoto committed
630
631
632
633
634
    >>> g = gt.random_graph(30, lambda: (3, 3))
    >>> tc = gt.transitive_closure(g)

    References
    ----------
635
    .. [transitive-boost] http://www.boost.org/libs/graph/doc/transitive_closure.html
Tiago Peixoto's avatar
Tiago Peixoto committed
636
637
638
639
    .. [transitive-wiki] http://en.wikipedia.org/wiki/Transitive_closure

    """

640
641
642
643
644
645
646
    if not g.is_directed():
        raise ValueError("graph must be directed for transitive closure.")
    tg = Graph()
    libgraph_tool_topology.transitive_closure(g._Graph__graph,
                                              tg._Graph__graph)
    return tg

Tiago Peixoto's avatar
Tiago Peixoto committed
647

648
def label_components(g, vprop=None, directed=None, attractors=False):
649
    """
650
    Label the components to which each vertex in the graph belongs. If the
651
652
    graph is directed, it finds the strongly connected components.

653
654
655
    A property map with the component labels is returned, together with an
    histogram of component labels.

656
657
    Parameters
    ----------
658
    g : :class:`~graph_tool.Graph`
659
        Graph to be used.
660
    vprop : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
661
662
        Vertex property to store the component labels. If none is supplied, one
        is created.
663
    directed : bool (optional, default: ``None``)
664
665
        Treat graph as directed or not, independently of its actual
        directionality.
666
667
668
669
    attractors : bool (optional, default: ``False``)
        If ``True``, and the graph is directed, an additional array with Boolean
        values is returned, specifying if the strongly connected components are
        attractors or not.
670
671
672

    Returns
    -------
673
    comp : :class:`~graph_tool.PropertyMap`
674
        Vertex property map with component labels.
675
676
    hist : :class:`~numpy.ndarray`
        Histogram of component labels.
677
678
679
680
    is_attractor : :class:`~numpy.ndarray`
        A Boolean array specifying if the strongly connected components are
        attractors or not. This returned only if ``attractors == True``, and the
        graph is directed.
681
682
683
684
685
686

    Notes
    -----
    The components are arbitrarily labeled from 0 to N-1, where N is the total
    number of components.

687
    The algorithm runs in :math:`O(V + E)` time.
688
689
690

    Examples
    --------
691
692
693
694
695
696
    .. testcode::
       :hide:

       numpy.random.seed(43)
       gt.seed_rng(43)

697
698
    >>> g = gt.random_graph(100, lambda: (poisson(2), poisson(2)))
    >>> comp, hist, is_attractor = gt.label_components(g, attractors=True)
699
    >>> print(comp.a)
700
701
702
703
    [12 12 12 12 12 12 12 12 13 12 12 12 12 12 11 12  5 14 12 12  7 15 12 12 12
     12 12 12 12 12 12 12 12 12 16 12  2  0 12 17 12 12  1 12 12 10 12 18 12 21
     12 12 12  6  9 12 12 22 12 12 12 12  3 12  8 23 24 12 12 12 25 12 12 12 12
     12 27 28 12 12 26 12 20 12 12 12 12 12 12 12 12 29 30 19 12  4 12 31 12 12]
704
    >>> print(hist)
705
706
707
708
709
710
    [ 1  1  1  1  1  1  1  1  1  1  1  1 69  1  1  1  1  1  1  1  1  1  1  1  1
      1  1  1  1  1  1  1]
    >>> print(is_attractor)
    [ True False False  True  True False  True  True  True False False  True
     False False False False False False False False False False False False
     False False False False False False False False]
711
712
    """

713
    if vprop is None:
714
715
716
717
718
        vprop = g.new_vertex_property("int32_t")

    _check_prop_writable(vprop, name="vprop")
    _check_prop_scalar(vprop, name="vprop")

719
720
    if directed is not None:
        g = GraphView(g, directed=directed)
721

722
723
    hist = libgraph_tool_topology.\
               label_components(g._Graph__graph, _prop("v", g, vprop))
724
725
726
727
728
729
730
731
732

    if attractors and g.is_directed() and directed != False:
        is_attractor = numpy.ones(len(hist), dtype="bool")
        libgraph_tool_topology.\
               label_attractors(g._Graph__graph, _prop("v", g, vprop),
                                is_attractor)
        return vprop, hist, is_attractor
    else:
        return vprop, hist
733
734
735
736


def label_largest_component(g, directed=None):
    """
737
738
    Label the largest component in the graph. If the graph is directed, then the
    largest strongly connected component is labelled.
739
740
741
742
743
744
745
746
747
748
749
750
751
752

    A property map with a boolean label is returned.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    directed : bool (optional, default:None)
        Treat graph as directed or not, independently of its actual
        directionality.

    Returns
    -------
    comp : :class:`~graph_tool.PropertyMap`
753
         Boolean vertex property map which labels the largest component.
754
755
756
757
758
759
760

    Notes
    -----
    The algorithm runs in :math:`O(V + E)` time.

    Examples
    --------
761
762
763
764
765
766
767
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

768
769
    >>> g = gt.random_graph(100, lambda: poisson(1), directed=False)
    >>> l = gt.label_largest_component(g)
770
    >>> print(l.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
771
772
773
    [0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0]
774
    >>> u = gt.GraphView(g, vfilt=l)   # extract the largest component as a graph
775
    >>> print(u.num_vertices())
Tiago Peixoto's avatar
Tiago Peixoto committed
776
    10
777
778
779
780
    """

    label = g.new_vertex_property("bool")
    c, h = label_components(g, directed=directed)
781
782
783
784
785
    vfilt, inv = g.get_vertex_filter()
    if vfilt is None:
        label.a = c.a == h.argmax()
    else:
        label.a = (c.a == h.argmax()) & (vfilt.a ^ inv)
786
    return label
787

Tiago Peixoto's avatar
Tiago Peixoto committed
788

789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
def label_out_component(g, root):
    """
    Label the out-component (or simply the component for undirected graphs) of a
    root vertex.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    root : :class:`~graph_tool.Vertex`
        The root vertex.

    Returns
    -------
    comp : :class:`~graph_tool.PropertyMap`
         Boolean vertex property map which labels the out-component.

    Notes
    -----
    The algorithm runs in :math:`O(V + E)` time.

    Examples
    --------
812
813
814
815
816
817
818
819
820
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

    >>> g = gt.random_graph(100, lambda: poisson(2.2), directed=False)
    >>> l = gt.label_out_component(g, g.vertex(2))
821
    >>> print(l.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
822
823
824
    [1 1 1 1 0 1 0 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 0 0 1 1 0 0
     1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 0 0 1 0 1 1 1 1 1
     1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 0]
825
826
827

    The in-component can be obtained by reversing the graph.

Tiago Peixoto's avatar
Tiago Peixoto committed
828
    >>> l = gt.label_out_component(gt.GraphView(g, reversed=True, directed=True),
829
    ...                            g.vertex(1))
830
    >>> print(l.a)
831
    [0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Tiago Peixoto's avatar
Tiago Peixoto committed
832
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
833
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
834
835
836
837
838
839
840
841
842
    """

    label = g.new_vertex_property("bool")
    libgraph_tool_topology.\
             label_out_component(g._Graph__graph, int(root),
                                 _prop("v", g, label))
    return label


843
def label_biconnected_components(g, eprop=None, vprop=None):
844
845
846
847
    """
    Label the edges of biconnected components, and the vertices which are
    articulation points.

848
849
850
851
    An edge property map with the component labels is returned, together a
    boolean vertex map marking the articulation points, and an histogram of
    component labels.

852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.

    eprop : :class:`~graph_tool.PropertyMap` (optional, default: None)
        Edge property to label the biconnected components.

    vprop : :class:`~graph_tool.PropertyMap` (optional, default: None)
        Vertex property to mark the articulation points. If none is supplied,
        one is created.


    Returns
    -------
    bicomp : :class:`~graph_tool.PropertyMap`
        Edge property map with the biconnected component labels.
    articulation : :class:`~graph_tool.PropertyMap`
        Boolean vertex property map which has value 1 for each vertex which is
        an articulation point, and zero otherwise.
    nc : int
        Number of biconnected components.

    Notes
    -----

    A connected graph is biconnected if the removal of any single vertex (and
    all edges incident on that vertex) can not disconnect the graph. More
    generally, the biconnected components of a graph are the maximal subsets of
    vertices such that the removal of a vertex from a particular component will
    not disconnect the component. Unlike connected components, vertices may
    belong to multiple biconnected components: those vertices that belong to
    more than one biconnected component are called "articulation points" or,
    equivalently, "cut vertices". Articulation points are vertices whose removal
    would increase the number of connected components in the graph. Thus, a
    graph without articulation points is biconnected. Vertices can be present in
    multiple biconnected components, but each edge can only be contained in a
    single biconnected component.

    The algorithm runs in :math:`O(V + E)` time.

    Examples
    --------
895
896
897
898
899
900
901
    .. testcode::
       :hide:

       import numpy.random
       numpy.random.seed(42)
       gt.seed_rng(42)

902
    >>> g = gt.random_graph(100, lambda: 2, directed=False)
903
    >>> comp, art, hist = gt.label_biconnected_components(g)
904
    >>> print(comp.a)
Tiago Peixoto's avatar
Tiago Peixoto committed
905
906
907
    [1 1 0 1 0 1 1 2 1 1 3 3 0 2 2 1 1 3 2 1 0 1 1 1 1 3 1 2 1 3 4 3 1 1 4 0 0
     0 1 1 1 1 2 1 1 2 2 2 2 0 1 0 1 1 1 1 2 2 1 1 1 1 1 0 1 1 0 0 1 0 1 4 1 2
     1 1 1 1 0 1 2 1 1 1 1 1 1 1 1 1 4 1 1 1 1 3 1 3 1 3]
908
    >>> print(art.a)
909
910
911
    [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
     0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
912
    >>> print(hist)
Tiago Peixoto's avatar
Tiago Peixoto committed
913
    [14 59 14  9  4]
914
    """
915

916
    if vprop is None:
917
        vprop = g.new_vertex_property("bool")
918
    if eprop is None:
919
920
921
922
923
924
925
        eprop = g.new_edge_property("int32_t")

    _check_prop_writable(vprop, name="vprop")
    _check_prop_scalar(vprop, name="vprop")
    _check_prop_writable(eprop, name="eprop")
    _check_prop_scalar(eprop, name="eprop")

926
927
    g = GraphView(g, directed=False)
    hist = libgraph_tool_topology.\
928
929
             label_biconnected_components(g._Graph__graph, _prop("e", g, eprop),
                                          _prop("v", g, vprop))
930
    return eprop, vprop, hist
931

Tiago Peixoto's avatar
Tiago Peixoto committed
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
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
def kcore_decomposition(g, deg="out", vprop=None):
    """
    Perform a k-core decomposition of the given graph.

    Parameters
    ----------
    g : :class:`~graph_tool.Graph`
        Graph to be used.
    deg : string
        Degree to be used for the decomposition. It can be either "in", "out" or
        "total", for in-, out-, or total degree of the vertices.
    vprop : :class:`~graph_tool.PropertyMap` (optional, default: ``None``)
        Vertex property to store the decomposition. If ``None`` is supplied,
        one is created.

    Returns
    -------
    kval : :class:`~graph_tool.PropertyMap`
        Vertex property map with the k-core decomposition, i.e. a given vertex v
        belongs to the ``kval[v]``-core.

    Notes
    -----

    The k-core is a maximal set of vertices such that its induced subgraph only
    contains vertices with degree larger than or equal to k.

    This algorithm is described in [batagelk-algorithm]_ and runs in :math:`O(V + E)`
    time.

    Examples
    --------

    >>> g = gt.collection.data["netscience"]
    >>> g = gt.GraphView(g, vfilt=gt.label_largest_component(g))
    >>> kcore = gt.kcore_decomposition(g)
    >>> gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=kcore, vertex_text=kcore, output="netsci-kcore.pdf")
    <...>

    .. testcode::
       :hide:

       gt.graph_draw(g, pos=g.vp["pos"], vertex_fill_color=kcore, vertex_text=kcore, output="netsci-kcore.png")

    .. figure:: netsci-kcore.*
        :align: center

        K-core decomposition of a network of network scientists.

    References
    ----------
    .. [k-core] http://en.wikipedia.org/wiki/Degeneracy_%28graph_theory%29
    .. [batagelk-algorithm] V. Batagelj, M. Zaversnik, "An O(m) Algorithm for
       Cores Decomposition of Networks", 2003, :arxiv:`cs/0310049`

    """

    if vprop is None:
        vprop = g.new_vertex_property("int32_t")

    _check_prop_writable(vprop, name="vprop")
    _check_prop_scalar(vprop, name="vprop")
    if deg not in ["in", "out", "total"]:
        raise ValueError("invalid degree: " + str(deg))

    if g.is_directed():
        if deg == "out":
            g = GraphView(g, reversed=True)
        if deg == "total":