Commit 169265f7 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Include maximum number of subgraphs in subgraph_isomorphism()

parent 6f43a01e
......@@ -64,7 +64,8 @@ struct get_subgraphs
void operator()(const Graph1& g1, const Graph2* g2,
VertexLabel vertex_label1, boost::any vertex_label2,
EdgeLabel edge_label1, boost::any edge_label2,
vector<vector<pair<size_t,size_t> > >& F) const
vector<vector<pair<size_t,size_t> > >& F, size_t max_n)
const
{
typedef PropLabelling<Graph1,Graph2,VertexLabel,VertexLabel>
vlabelling_t;
......@@ -75,7 +76,7 @@ struct get_subgraphs
(g1, *g2, vlabelling_t(g1, *g2, vertex_label1,
any_cast<VertexLabel>(vertex_label2)),
elabelling_t(g1, *g2, edge_label1,
any_cast<EdgeLabel>(edge_label2)), F);
any_cast<EdgeLabel>(edge_label2)), F, max_n);
}
};
......@@ -147,7 +148,8 @@ typedef mpl::vector<ConstantPropertyMap<bool,GraphInterface::edge_t> > edge_prop
void subgraph_isomorphism(GraphInterface& gi1, GraphInterface& gi2,
boost::any vertex_label1, boost::any vertex_label2,
boost::any edge_label1, boost::any edge_label2,
python::list vmapping, python::list emapping)
python::list vmapping, python::list emapping,
size_t max_n)
{
if (gi1.GetDirected() != gi2.GetDirected())
return;
......@@ -171,7 +173,7 @@ void subgraph_isomorphism(GraphInterface& gi1, GraphInterface& gi2,
run_action<graph_tool::detail::always_directed>()
(gi1, bind<void>(get_subgraphs(),
_1, _2, _3, vertex_label2, _4, edge_label2,
ref(F)),
ref(F), max_n),
directed_graph_view_pointers(), vertex_props_t(),
edge_props_t())
(gi2.GetGraphView(), vertex_label1, edge_label1);
......@@ -181,7 +183,7 @@ void subgraph_isomorphism(GraphInterface& gi1, GraphInterface& gi2,
run_action<graph_tool::detail::never_directed>()
(gi1, bind<void>(get_subgraphs(),
_1, _2, _3, vertex_label2, _4, edge_label2,
ref(F)),
ref(F), max_n),
undirected_graph_view_pointers(), vertex_props_t(),
edge_props_t())
(gi2.GetGraphView(), vertex_label1, edge_label1);
......
......@@ -192,7 +192,8 @@ bool refine_check(const Graph1& g1, const Graph2& g2, matrix_t& M, size_t count,
template <class Graph1, class Graph2, class EdgeLabelling, class Mapping>
void find_mappings(const Graph1& g1, const Graph2& g2, matrix_t M0,
vector<Mapping>& FF, EdgeLabelling edge_labelling)
vector<Mapping>& FF, EdgeLabelling edge_labelling,
size_t max_n)
{
size_t i = 0;
for (i=0; i < num_vertices(g1); ++i)
......@@ -214,7 +215,7 @@ void find_mappings(const Graph1& g1, const Graph2& g2, matrix_t M0,
tr1::unordered_set<size_t> already_mapped;
// perform depth-first search of combination space
while (!Mstack.empty())
while (!Mstack.empty() && (max_n == 0 || FF.size() < max_n))
{
const matrix_t& M = get<0>(Mstack.back());
size_t& i = get<1>(Mstack.back());
......@@ -290,7 +291,8 @@ template <class Graph1, class Graph2, class VertexLabelling,
class EdgeLabelling, class Mapping>
void subgraph_isomorphism(const Graph1& g1, const Graph2& g2,
VertexLabelling vertex_labelling,
EdgeLabelling edge_labelling, vector<Mapping>& F)
EdgeLabelling edge_labelling, vector<Mapping>& F,
size_t max_n)
{
detail::matrix_t M0(num_vertices(g1));
......@@ -314,7 +316,7 @@ void subgraph_isomorphism(const Graph1& g1, const Graph2& g2,
}
if (abort)
return;
detail::find_mappings(g1, g2, M0, F, edge_labelling);
detail::find_mappings(g1, g2, M0, F, edge_labelling, max_n);
}
} // namespace boost
......
......@@ -35,7 +35,8 @@ bool is_planar(GraphInterface& gi, boost::any embed_map, boost::any kur_map);
void subgraph_isomorphism(GraphInterface& gi1, GraphInterface& gi2,
boost::any vertex_label1, boost::any vertex_label2,
boost::any edge_label1, boost::any edge_label2,
python::list vmapping, python::list emapping);
python::list vmapping, python::list emapping,
size_t n_max);
void export_components();
void export_dists();
......
......@@ -88,9 +88,10 @@ def isomorphism(g1, g2, isomap=False):
return iso
def subgraph_isomorphism(sub, g):
def subgraph_isomorphism(sub, g, max_n=0):
r"""
Obtain all subgraph isomorphisms of `sub` in `g`.
Obtain all subgraph isomorphisms of `sub` in `g` (or at most `max_n`
subgraphs, if `max_n > 0`).
It returns two lists, containing the vertex and edge property maps for `sub`
with the isomorphism mappings. The value of the properties are the
......@@ -155,7 +156,7 @@ def subgraph_isomorphism(sub, g):
_prop("v", g, vlabels[1]),
_prop("e", sub, elabels[0]),
_prop("e", g, elabels[1]),
vmaps, emaps)
vmaps, emaps, max_n)
for i in xrange(len(vmaps)):
vmaps[i] = PropertyMap(vmaps[i], sub, "v")
emaps[i] = PropertyMap(emaps[i], sub, "e")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment