Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user callback to vf2_subgraph_mono to potentially stop search before each check. #280

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions doc/vf2_sub_graph_iso.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ <h1>
typename SubGraphIsoMapCallback&gt;
bool vf2_subgraph_iso(const GraphSmall&amp; graph_small,
const GraphLarge&amp; graph_large,
SubGraphIsoMapCallback user_callback)
SubGraphIsoMapCallback user_callback,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of being a concrete function pointer, this should be a template like most other parameters. That way, users can pass in whatever they like: lambda, function object, etc.



<em class="comment">// Named parameter version</em>
Expand All @@ -59,7 +60,8 @@ <h1>
const GraphLarge&amp; graph_large,
SubGraphIsoMapCallback user_callback,
const VertexOrderSmall&amp; vertex_order_small,
const bgl_named_params&lt;Param, Tag, Rest&gt;&amp; params)
const bgl_named_params&lt;Param, Tag, Rest&gt;&amp; params,
bool(*user_step_callback)() = &vf2_trivial_step_callback)


<em class="comment">// Non-named parameter version</em>
Expand All @@ -78,7 +80,8 @@ <h1>
IndexMapLarge index_map_large,
const VertexOrderSmall&amp; vertex_order_small,
EdgeEquivalencePredicate edge_comp,
VertexEquivalencePredicate vertex_comp)
VertexEquivalencePredicate vertex_comp,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
</pre>
<p>
An isomorphism between two graphs <em>G<sub>1</sub>=(V<sub>1</sub>, E<sub>1</sub>)</em>
Expand All @@ -95,9 +98,9 @@ <h1>
<p>
This function finds all induced subgraph isomorphisms between
graphs <tt>graph_small</tt> and <tt>graph_large</tt> and outputs them to
<tt>user_callback</tt>. It continues until <tt>user_callback</tt>
<tt>user_callback</tt>. It continues until <tt>user_callback</tt> or <tt>user_step_callback</tt>
returns false or the search space has been fully explored. <tt>vf2_subgraph_iso</tt>
returns true if a graph-subgraph isomorphism exists and false otherwise.
returns true if a graph-subgraph isomorphism was found and false otherwise.
<tt>EdgeEquivalencePredicate</tt> and
<tt>VertexEquivalencePredicate</tt> predicates are used to test whether
edges and vertices are equivalent. To use property maps for equivalence,
Expand Down Expand Up @@ -202,6 +205,18 @@ <h3>Parameters</h3>
</p>
</blockquote>

<p>OUT: <tt>bool(*user_step_callback)() = &vf2_trivial_step_callback</tt></p>
<blockquote>
<p>
A function to be called at each step of the search. If it returns false,
the search is terminated. The default always returns true. Unlike
<tt>user_callback</tt> (which is only called when a match is found),
this callback is called with high frequency; it may be used, for
example, to stop the search when a time limit is exceeded or for other
external reasons.
</p>
</blockquote>

<p>IN: <tt>const VertexOrderSmall&amp; vertex_order_small</tt></p>
<blockquote>
<p>
Expand Down
47 changes: 32 additions & 15 deletions include/boost/graph/vf2_sub_graph_iso.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ template < typename Graph1, typename Graph2 > struct vf2_print_callback
const Graph2& graph2_;
};

static bool vf2_trivial_step_callback()
{
return true;
}

namespace detail
{

Expand Down Expand Up @@ -748,8 +753,8 @@ namespace detail
// and tested for feasibility to extend the mapping. If a complete
// mapping is found, the mapping is output to user_callback in the form
// of a correspondence map (graph1 to graph2). Returning false from the
// user_callback will terminate the search. Function match will return
// true if the entire search space was explored.
// user_callback or user_step_callback will terminate the search. Function
// match will return true if a match was found.
template < typename Graph1, typename Graph2, typename IndexMap1,
typename IndexMap2, typename VertexOrder1,
typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
Expand All @@ -758,7 +763,8 @@ namespace detail
SubGraphIsoMapCallback user_callback, const VertexOrder1& vertex_order1,
state< Graph1, Graph2, IndexMap1, IndexMap2, EdgeEquivalencePredicate,
VertexEquivalencePredicate, SubGraphIsoMapCallback,
problem_selection >& s)
problem_selection >& s,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
{

typename VertexOrder1::const_iterator graph1_verts_iter;
Expand All @@ -773,6 +779,10 @@ namespace detail
bool found_match = false;

recur:
if (!user_step_callback()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this callback should take in whatever data might change between each call to it. Even though in your case you'll ignore it, someone else might have a reason to examine the internals of whatever state is changing. I guess it's just that s object?

return found_match;
}

if (s.success())
{
if (!s.call_back(user_callback))
Expand Down Expand Up @@ -913,8 +923,9 @@ namespace detail
}

// Enumerates all graph sub-graph mono-/iso-morphism mappings between graphs
// graph_small and graph_large. Continues until user_callback returns true
// or the search space has been fully explored.
// graph_small and graph_large. Continues until user_callback or
// user_step_callback returns false or the search space has been fully
// explored.
template < problem_selector problem_selection, typename GraphSmall,
typename GraphLarge, typename IndexMapSmall, typename IndexMapLarge,
typename VertexOrderSmall, typename EdgeEquivalencePredicate,
Expand All @@ -924,7 +935,8 @@ namespace detail
IndexMapSmall index_map_small, IndexMapLarge index_map_large,
const VertexOrderSmall& vertex_order_small,
EdgeEquivalencePredicate edge_comp,
VertexEquivalencePredicate vertex_comp)
VertexEquivalencePredicate vertex_comp,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
{

// Graph requirements
Expand Down Expand Up @@ -1007,7 +1019,8 @@ namespace detail
edge_comp, vertex_comp);

return detail::match(
graph_small, graph_large, user_callback, vertex_order_small, s);
graph_small, graph_large, user_callback, vertex_order_small, s,
user_step_callback);
}

} // namespace detail
Expand All @@ -1028,8 +1041,8 @@ vertex_order_by_mult(const Graph& graph)
}

// Enumerates all graph sub-graph monomorphism mappings between graphs
// graph_small and graph_large. Continues until user_callback returns true or
// the search space has been fully explored.
// graph_small and graph_large. Continues until user_callback or
// user_step_callback returns false or the search space has been fully explored.
template < typename GraphSmall, typename GraphLarge, typename IndexMapSmall,
typename IndexMapLarge, typename VertexOrderSmall,
typename EdgeEquivalencePredicate, typename VertexEquivalencePredicate,
Expand All @@ -1038,23 +1051,25 @@ bool vf2_subgraph_mono(const GraphSmall& graph_small,
const GraphLarge& graph_large, SubGraphIsoMapCallback user_callback,
IndexMapSmall index_map_small, IndexMapLarge index_map_large,
const VertexOrderSmall& vertex_order_small,
EdgeEquivalencePredicate edge_comp, VertexEquivalencePredicate vertex_comp)
EdgeEquivalencePredicate edge_comp, VertexEquivalencePredicate vertex_comp,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
{
return detail::vf2_subgraph_morphism< detail::subgraph_mono >(graph_small,
graph_large, user_callback, index_map_small, index_map_large,
vertex_order_small, edge_comp, vertex_comp);
vertex_order_small, edge_comp, vertex_comp, user_step_callback);
}

// All default interface for vf2_subgraph_iso
template < typename GraphSmall, typename GraphLarge,
typename SubGraphIsoMapCallback >
bool vf2_subgraph_mono(const GraphSmall& graph_small,
const GraphLarge& graph_large, SubGraphIsoMapCallback user_callback)
const GraphLarge& graph_large, SubGraphIsoMapCallback user_callback,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
{
return vf2_subgraph_mono(graph_small, graph_large, user_callback,
get(vertex_index, graph_small), get(vertex_index, graph_large),
vertex_order_by_mult(graph_small), always_equivalent(),
always_equivalent());
always_equivalent(), user_step_callback);
}

// Named parameter interface of vf2_subgraph_iso
Expand All @@ -1064,7 +1079,8 @@ template < typename GraphSmall, typename GraphLarge, typename VertexOrderSmall,
bool vf2_subgraph_mono(const GraphSmall& graph_small,
const GraphLarge& graph_large, SubGraphIsoMapCallback user_callback,
const VertexOrderSmall& vertex_order_small,
const bgl_named_params< Param, Tag, Rest >& params)
const bgl_named_params< Param, Tag, Rest >& params,
bool(*user_step_callback)() = &vf2_trivial_step_callback)
{
return vf2_subgraph_mono(graph_small, graph_large, user_callback,
choose_const_pmap(
Expand All @@ -1075,7 +1091,8 @@ bool vf2_subgraph_mono(const GraphSmall& graph_small,
choose_param(
get_param(params, edges_equivalent_t()), always_equivalent()),
choose_param(
get_param(params, vertices_equivalent_t()), always_equivalent()));
get_param(params, vertices_equivalent_t()), always_equivalent()),
user_step_callback);
}

// Enumerates all graph sub-graph isomorphism mappings between graphs
Expand Down
40 changes: 40 additions & 0 deletions test/vf2_sub_graph_iso_test_2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ struct false_predicate
}
};

bool step_callback_always_false()
{
return false;
}

bool step_callback_max_10_steps()
{
static int n = 0;
n++;
return (n <= 10);
}

void test_empty_graph_cases()
{
typedef boost::adjacency_list< boost::vecS, boost::vecS,
Expand Down Expand Up @@ -201,9 +213,37 @@ BOOST_TEST(!got_hit);
}
}

void test_step_callback()
{
typedef boost::adjacency_list< boost::vecS, boost::vecS,
boost::bidirectionalS >
Graph;
Graph gEmpty, gLarge;
add_vertex(gLarge);

{ // isomorphism exists but search aborted
bool got_hit = false;
test_callback callback(got_hit, true);
bool found = vf2_subgraph_mono(gEmpty, gEmpty, callback,
&step_callback_always_false);
BOOST_TEST(!found);
BOOST_TEST(!got_hit);
}

{ // isomorphism exists and found within 10 steps
bool got_hit = false;
test_callback callback(got_hit, true);
bool found = vf2_subgraph_mono(gEmpty, gEmpty, callback,
&step_callback_max_10_steps);
BOOST_TEST(found);
BOOST_TEST(got_hit);
}
}

int main(int argc, char* argv[])
{
test_empty_graph_cases();
test_return_value();
test_step_callback();
return boost::report_errors();
}