__init__.py 5.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#! /usr/bin/env python
# graph_tool.py -- a general graph manipulation python module
#
# Copyright (C) 2007 Tiago de Paula Peixoto <tiago@forked.de>
#
# 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/>.

19
"""
20
21
``graph_tool.topology`` - Topology related functions
----------------------------------------------------
22
23
"""

Tiago Peixoto's avatar
Tiago Peixoto committed
24
from .. dl_import import dl_import
25
dl_import("import libgraph_tool_topology")
26

27
28
from .. core import _prop, Vector_int32_t, _check_prop_writable, \
     _check_prop_scalar, Graph
29
import random, sys
30
31
32
__all__ = ["isomorphism", "min_spanning_tree", "denominator_tree",
           "topological_sort", "transitive_closure", "label_components",
           "label_biconnected_components"]
33
34
35
36

def isomorphism(g1, g2, isomap=None):
    if isomap == None:
        isomap = g1.new_vertex_property("int32_t")
37
    return libgraph_tool_topology.\
38
39
           check_isomorphism(g1._Graph__graph,g2._Graph__graph,
                             _prop("v", g1, isomap))
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

def min_spanning_tree(g, weights=None, root=None, tree_map=None):
    if tree_map == 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.")

    g.stash_filter(directed=True)
    g.set_directed(False)
    if root == 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))
    g.pop_filter(directed=True)
    return tree_map
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167

def denominator_tree(g, root, pred_map=None):
    if pred_map == None:
        pred_map = g.new_vertex_property("int32_t")
    if pred_map.value_type() != "int32_t":
        raise ValueError("vertex property 'pred_map' must be of value type" +
                         " int32_t.")
    if not g.is_directed():
        raise ValueError("denominator tree requires a directed graph.")
    libgraph_tool_topology.\
               denominator_tree(g._Graph__graph, int(root),
                                _prop("v", g, pred_map))
    return pred_map

def topological_sort(g):
    topological_order = Vector_int32_t()
    libgraph_tool_topology.\
               topological_sort(g._Graph__graph, topological_order)
    return topological_order
<<<<<<< HEAD
=======

def transitive_closure(g):
    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

def label_components(g, vprop=None, directed=None):
    """
    Labels the components to which each vertex in the graph belongs. If the
    graph is directed, it finds the strongly connected components.

    Parameters
    ----------
    g : Graph
        Graph to be used.

    vprop : PropertyMap (optional, default: None)
        Vertex property to store the component labels. If none is supplied, one
        is created.

    directed : bool (optional, default:None)
        Treat graph as directed or not, independently of its actual
        directionality.

    Returns
    -------
    comp : PropertyMap
        Vertex property map with component labels.

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

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

    Examples
    --------
    >>> g = gt.random_graph(100, lambda: (1, 1), seed=42)
    >>> comp = gt.label_components(g)
    >>> print comp.get_array()
    [0 1 2 3 4 0 3 3 4 4 2 3 4 0 3 3 3 3 0 3 2 1 3 0 0 2 2 3 3 3 0 1 2 3 2 3 0
     1 0 5 5 1 4 2 2 1 0 3 3 3 3 3 3 0 0 3 4 2 3 2 5 5 0 2 1 0 3 2 0 3 3 0 4 3
     2 6 2 2 1 3 1 1 0 3 0 1 3 0 3 0 2 0 2 2 0 6 1 1 0 2]
    """

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

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

    if directed != None:
        g.stash_filter(directed=True)
        g.set_directed(directed)

    libgraph_tool_topology.\
          label_components(g._Graph__graph, _prop("v", g, vprop))

    if directed != None:
        g.pop_filter(directed=True)
    return vprop

def label_biconnected_components(g, eprop=None, vprop=None):

    if vprop == None:
        vprop = g.new_vertex_property("bool")
    if eprop == None:
        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")

    g.stash_filter(directed=True)
    g.set_directed(False)
    nc = libgraph_tool_topology.\
          label_biconnected_components(g._Graph__graph, _prop("e", g, eprop),
                                       _prop("v", g, vprop))
    g.pop_filter(directed=True)
    return eprop, vprop, nc
>>>>>>> 3b8ab1b... merge transitive closure