graph_subgraph_isomorphism.cc 12.9 KB
Newer Older
1 2
// graph-tool -- a general graph modification and manipulation thingy
//
Tiago Peixoto's avatar
Tiago Peixoto committed
3
// Copyright (C) 2006-2013 Tiago de Paula Peixoto <tiago@skewed.de>
4 5 6 7 8 9 10 11 12 13 14 15 16 17
//
// 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/>.

18 19
#include <graph_python_interface.hh>

20 21 22
#include "graph.hh"
#include "graph_filtering.hh"

23 24
#include "random.hh"

25 26
#include <graph_subgraph_isomorphism.hh>

27 28 29
#include "tr1_include.hh"
#include TR1_HEADER(unordered_map)

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
using namespace graph_tool;
using namespace boost;

template <class Graph1, class Graph2, class Label1, class Label2>
class PropLabelling
{
public:
    PropLabelling(const Graph1& g1, const Graph2& g2,
                  Label1 label1, Label2 label2)
        : _g1(g1), _g2(g2), _label1(label1), _label2(label2) {}

    typedef typename property_traits<Label1>::value_type value_type1;
    typedef typename property_traits<Label2>::value_type value_type2;

    bool operator()(typename graph_traits<Graph1>::vertex_descriptor v1,
45
                    typename graph_traits<Graph2>::vertex_descriptor v2) const
46 47 48 49 50 51 52 53
    {
        if (in_degreeS()(v2, _g2) < in_degreeS()(v1, _g1) ||
            out_degree(v2, _g2) < out_degree(v1, _g1))
            return false;
        return _label1[v1] == _label2[v2];
    }

    bool operator()(typename graph_traits<Graph1>::edge_descriptor e1,
54
                    typename graph_traits<Graph2>::edge_descriptor e2) const
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    {
        return _label1[e1] == _label2[e2];
    }

private:
    const Graph1& _g1;
    const Graph2& _g2;
    Label1 _label1;
    Label2 _label2;
};

struct get_subgraphs
{
    template <class Graph1, class Graph2, class VertexLabel,
              class EdgeLabel>
70
    void operator()(const Graph1& sub, const Graph2* g,
71 72
                    VertexLabel vertex_label1, boost::any vertex_label2,
                    EdgeLabel edge_label1, boost::any edge_label2,
73
                    vector<vector<pair<size_t, size_t> > >& F,
74
                    vector<size_t>& vlist, pair<reference_wrapper<rng_t>,size_t> sn) const
75 76 77 78 79
    {
        typedef PropLabelling<Graph1,Graph2,VertexLabel,VertexLabel>
            vlabelling_t;
        typedef PropLabelling<Graph1,Graph2,EdgeLabel,EdgeLabel>
            elabelling_t;
80
        rng_t& rng = sn.first;
81 82 83 84 85 86 87 88 89 90 91
        size_t max_n = sn.second;

        vlist.resize(num_vertices(*g));
        int i, N = num_vertices(*g);
        for (i = 0; i < N; ++i)
            vlist[i] = i;
        for (i = 0; i < N - 1; ++i)
        {
            tr1::uniform_int<> random(i, N - 1);
            swap(vlist[i], vlist[random(rng)]);
        }
92 93

        subgraph_isomorphism
94
            (sub, *g, vlabelling_t(sub, *g, vertex_label1,
95
                                   any_cast<VertexLabel>(vertex_label2)),
96 97
             elabelling_t(sub, *g, edge_label1,
                          any_cast<EdgeLabel>(edge_label2)), F, vlist, max_n);
98 99 100 101 102 103 104
    }
};

struct get_mapping
{
    template <class Graph1, class Graph2, class EdgeLabel, class VertexMap,
              class EdgeMap, class EdgeIndexMap>
105
    void operator()(const Graph1& sub, const Graph2* g, EdgeLabel edge_label1,
106 107
                    boost::any edge_label2, vector<pair<size_t, size_t> >& F,
                    VertexMap vmapping, EdgeMap emapping,
108
                    EdgeIndexMap edge_index2, vector<size_t>& vlist) const
109
    {
110 111
        typedef typename graph_traits<Graph1>::edge_descriptor edge1_t;
        typedef typename graph_traits<Graph2>::edge_descriptor edge2_t;
112 113
        typedef PropLabelling<Graph1,Graph2,EdgeLabel,EdgeLabel>
            elabelling_t;
114
        elabelling_t edge_labelling(sub, *g, edge_label1,
115 116
                                    any_cast<EdgeLabel>(edge_label2));
        int i, N = F.size();
117
        #pragma omp parallel for default(shared) private(i) schedule(static) if (N > 100)
118 119
        for (i = 0; i < N; ++i)
        {
120
            if (vertex(i, sub) == graph_traits<Graph1>::null_vertex())
121
                continue;
122
            vmapping[vertex(F[i].first, sub)] = vertex(vlist[F[i].second], *g);
123
            typename graph_traits<Graph1>::out_edge_iterator e, e_end;
124 125
            for (tie(e, e_end) = out_edges(vertex(i, sub), sub); e != e_end;
                 ++e)
126 127 128
            {
                bool found = false;
                typename graph_traits<Graph2>::out_edge_iterator e2, e2_end;
129 130
                for (tie(e2, e2_end) =
                         out_edges(vertex(vlist[F[i].second], *g), *g);
131 132
                     e2 != e2_end; ++e2)
                {
133
                    if (target(edge2_t(*e2), *g) ==
134
                        vertex(vlist[F[target(*e, sub)].second], *g) &&
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
                        edge_labelling(*e, *e2))
                    {
                        emapping[*e] = edge_index2[*e2];
                        found = true;
                    }
                }
                if (!found)
                    throw GraphException("edge not found... "
                                         "can't be isomorphism!!! "
                                         "This is a bug.");
            }
        }
    }
};

struct directed_graph_view_pointers:
    mpl::transform<graph_tool::detail::always_directed,
                   mpl::quote1<add_pointer> >::type {};

struct undirected_graph_view_pointers:
    mpl::transform<graph_tool::detail::never_directed,
                   mpl::quote1<add_pointer> >::type {};

158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
struct vertex_label_mapping
{
    template <class Graph, class VertexMap, class VertexLabel>
    void operator()(const Graph& g, VertexMap vmap, VertexLabel vlabel,
                    boost::any& vlist) const
    {
        typedef typename property_traits<VertexMap>::value_type value_t;
        tr1::unordered_map<value_t, int32_t> values;
        if (!vlist.empty())
            values = boost::any_cast<tr1::unordered_map<value_t, int32_t> >(vlist);

        typename graph_traits<Graph>::vertex_iterator v, v_end;
        for (tie(v, v_end) = vertices(g); v != v_end; ++v)
        {
            const value_t& val = vmap[*v];
            if (values.find(val) == values.end())
                values[val] = values.size();
            vlabel[*v] = values[val];
        }
        vlist = values;
    }
};

struct edge_label_mapping
{
    template <class Graph, class EdgeMap, class EdgeLabel>
    void operator()(const Graph& g, EdgeMap emap, EdgeLabel elabel,
                    boost::any& vlist) const
    {
        typedef typename property_traits<EdgeMap>::value_type value_t;
        tr1::unordered_map<value_t, int32_t> values;
        if (!vlist.empty())
            values = boost::any_cast<tr1::unordered_map<value_t, int32_t> >(vlist);

        typename graph_traits<Graph>::edge_iterator e, e_end;
        for (tie(e, e_end) = edges(g); e != e_end; ++e)
        {
            const value_t& val = emap[*e];
            if (values.find(val) == values.end())
                values[val] = values.size();
            elabel[*e] = values[val];
        }
        vlist = values;
    }
};
203 204 205 206 207 208 209 210 211

// typedef mpl::push_back<vertex_properties,
//                        ConstantPropertyMap<bool,GraphInterface::vertex_t> >
//     ::type vertex_props_t;

// typedef mpl::push_back<edge_properties,
//                        ConstantPropertyMap<bool,GraphInterface::edge_t> >
//     ::type edge_props_t;

212
typedef property_map_type::apply<int32_t, GraphInterface::vertex_index_map_t>::type vlabel_t;
213 214
typedef mpl::vector<vlabel_t, ConstantPropertyMap<bool,GraphInterface::vertex_t> > vertex_props_t;

215
typedef property_map_type::apply<int32_t, GraphInterface::edge_index_map_t>::type elabel_t;
216
typedef mpl::vector<elabel_t, ConstantPropertyMap<bool,GraphInterface::edge_t> > edge_props_t;
217 218 219 220

void subgraph_isomorphism(GraphInterface& gi1, GraphInterface& gi2,
                          boost::any vertex_label1, boost::any vertex_label2,
                          boost::any edge_label1, boost::any edge_label2,
221
                          python::list vmapping, python::list emapping,
222
                          size_t max_n, rng_t& rng)
223 224 225 226
{
    if (gi1.GetDirected() != gi2.GetDirected())
        return;

227 228 229 230 231
    if (belongs<vertex_props_t>()(vertex_label1))
    {
        vertex_label2 = any_cast<vlabel_t>(vertex_label2).get_unchecked(num_vertices(gi2.GetGraph()));
    }
    else
232
    {
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        if (vertex_label1.empty())
        {
            vertex_label1 = vertex_label2 =
                ConstantPropertyMap<bool,GraphInterface::vertex_t>(true);
        }
        else
        {
            boost::any vlist;
            vlabel_t vlabel1(gi1.GetVertexIndex());
            run_action<>()(gi1, bind<void>(vertex_label_mapping(),
                                           _1, _2, vlabel1, ref(vlist)),
                           vertex_properties())
                (vertex_label1);
            vertex_label1 = vlabel1;

            vlabel_t vlabel2(gi2.GetVertexIndex());
            run_action<>()(gi2, bind<void>(vertex_label_mapping(),
                                           _1, _2, vlabel2, ref(vlist)),
                           vertex_properties())
                (vertex_label2);
            vertex_label2 = vlabel2.get_unchecked(num_vertices(gi2.GetGraph()));
        }
255 256
    }

257
    if (belongs<edge_props_t>()(edge_label1))
258
    {
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
        edge_label2 = any_cast<elabel_t>(edge_label2).get_unchecked(gi2.GetGraph().get_last_index());
    }
    else
    {
        if (edge_label1.empty())
        {
            edge_label1 = edge_label2 =
                ConstantPropertyMap<bool,GraphInterface::edge_t>(true);
        }
        else
        {
            boost::any vlist;
            elabel_t elabel1(gi1.GetEdgeIndex());
            run_action<>()(gi1, bind<void>(edge_label_mapping(),
                                           _1, _2, elabel1, ref(vlist)),
                           edge_properties())
                (edge_label1);
            edge_label1 = elabel1;

            elabel_t elabel2(gi2.GetEdgeIndex());
            run_action<>()(gi2, bind<void>(edge_label_mapping(),
                                           _1, _2, elabel2, ref(vlist)),
                           edge_properties())
                (edge_label2);
            edge_label2 = elabel2.get_unchecked(num_edges(gi2.GetGraph()));
        }
285 286 287
    }

    vector<vector<pair<size_t,size_t> > > F;
288
    vector<size_t> vlist;
289 290 291 292 293 294

    if (gi1.GetDirected())
    {
        run_action<graph_tool::detail::always_directed>()
            (gi1, bind<void>(get_subgraphs(),
                             _1, _2, _3, vertex_label2, _4, edge_label2,
295
                             ref(F), ref(vlist), make_pair(ref(rng), max_n)),
296 297 298 299 300 301 302 303 304
             directed_graph_view_pointers(), vertex_props_t(),
             edge_props_t())
            (gi2.GetGraphView(), vertex_label1,  edge_label1);
    }
    else
    {
        run_action<graph_tool::detail::never_directed>()
            (gi1, bind<void>(get_subgraphs(),
                             _1, _2, _3, vertex_label2, _4, edge_label2,
305
                             ref(F), ref(vlist), make_pair(ref(rng), max_n)),
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
             undirected_graph_view_pointers(), vertex_props_t(),
             edge_props_t())
            (gi2.GetGraphView(), vertex_label1, edge_label1);
    }

    for (size_t i = 0; i < F.size(); ++i)
    {
        typedef property_map_type
            ::apply<int64_t, GraphInterface::vertex_index_map_t>::type
            vmap_t;
        typedef property_map_type
            ::apply<int64_t, GraphInterface::edge_index_map_t>::type
            emap_t;
        vmap_t::unchecked_t vm(gi1.GetVertexIndex(), gi1.GetNumberOfVertices());
        emap_t::unchecked_t ep(gi1.GetEdgeIndex(), gi1.GetMaxEdgeIndex()+1);
        if (gi1.GetDirected())
        {
            run_action<graph_tool::detail::always_directed>()
                (gi1, bind<void>(get_mapping(),
                                 _1, _2, _3, edge_label2,
                                 ref(F[i]), ref(vm), ref(ep),
327
                                 gi2.GetEdgeIndex(), ref(vlist)),
328 329 330 331 332 333 334 335 336
                 directed_graph_view_pointers(), edge_props_t())
                (gi2.GetGraphView(), edge_label1);
        }
        else
        {
            run_action<graph_tool::detail::never_directed>()
                (gi1, bind<void>(get_mapping(),
                                 _1, _2, _3, edge_label2,
                                 ref(F[i]), ref(vm), ref(ep),
337
                                 gi2.GetEdgeIndex(), ref(vlist)),
338 339 340 341 342 343 344
                 undirected_graph_view_pointers(), edge_props_t())
                (gi2.GetGraphView(), edge_label1);
        }
        vmapping.append(PythonPropertyMap<vmap_t>(vm.get_checked()));
        emapping.append(PythonPropertyMap<emap_t>(ep.get_checked()));
    }
}