From f743a273c95f9f2c714108d09f553b773952384a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Szabolcs=20Horv=C3=A1t?= Date: Sun, 29 Nov 2026 23:48:59 +0000 Subject: [PATCH] fix: correct result and mappings of VF2 functions with callback Graph.isomorphic_vf2() and Graph.subisomorphic_vf2() always returned False when a callback was given, because the callback variants of the C functions have no result argument and res was never set. They also passed the output mapping vectors to the C functions, which use them only as search workspace, so the returned mappings were meaningless (all -1 after a complete search, or empty). The result is now True if the callback was called at least once. The mapping vectors are no longer passed to the C core; instead the callback copies each match into them, so the mappings returned are those of the last match passed to the callback, i.e. the one at which the callback stopped the search, or the last one found. Assisted-by: Claude Opus 5.5 --- CHANGELOG.md | 6 ++++ src/_igraph/graphobject.c | 45 ++++++++++++++++++++++--- tests/test_isomorphism.py | 69 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce2624727..f95b05086 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,12 @@ ### Fixed +- `Graph.isomorphic_vf2()` and `Graph.subisomorphic_vf2()` always returned + `False` when a `callback` was given, even when matches were found, and the + mappings returned with `return_mapping_12` or `return_mapping_21` were + meaningless. The result is now `True` if at least one match was found, and + the mappings are those of the last match passed to the callback. + - Errors raised while converting a boolean argument to a boolean (e.g. when passing a NumPy array with more than one element) are now propagated correctly instead of being treated as `True` and left pending. diff --git a/src/_igraph/graphobject.c b/src/_igraph/graphobject.c index 478d62508..8963240d4 100644 --- a/src/_igraph/graphobject.c +++ b/src/_igraph/graphobject.c @@ -10567,6 +10567,10 @@ typedef struct { PyObject* callback_fn; PyObject* graph1; PyObject* graph2; + /* Used only by igraphmodule_i_Graph_isomorphic_vf2_callback_fn(): */ + igraph_bool_t found; /* set to true when a match is found */ + igraph_vector_int_t* map12; /* if not NULL, receives the last match */ + igraph_vector_int_t* map21; /* if not NULL, receives the last match */ } igraphmodule_i_Graph_isomorphic_vf2_callback_data_t; igraph_error_t igraphmodule_i_Graph_isomorphic_vf2_callback_fn( @@ -10578,6 +10582,17 @@ igraph_error_t igraphmodule_i_Graph_isomorphic_vf2_callback_fn( PyObject *map12_o, *map21_o; PyObject *result_o; + /* Record the match before calling the user's callback, so that the caller + * can report the match at which the callback stopped the search. map12 and + * map21 are owned by the C core and are only valid during this call. */ + data->found = true; + if (data->map12 && igraph_vector_int_update(data->map12, map12) != IGRAPH_SUCCESS) { + return IGRAPH_ENOMEM; + } + if (data->map21 && igraph_vector_int_update(data->map21, map21) != IGRAPH_SUCCESS) { + return IGRAPH_ENOMEM; + } + map12_o = igraphmodule_vector_int_t_to_PyList(map12); if (map12_o == NULL) { /* Error in conversion, return an error code */ @@ -10759,12 +10774,18 @@ PyObject *igraphmodule_Graph_isomorphic_vf2(igraphmodule_GraphObject * self, edge_compat_fn == Py_None ? 0 : igraphmodule_i_Graph_isomorphic_vf2_edge_compat_fn, &callback_data); } else { + /* map12 and map21 are workspace of the C function, not outputs, so we + * do not pass ours. The callback copies each match into them instead. */ + callback_data.found = false; + callback_data.map12 = map12; + callback_data.map21 = map21; retval = igraph_get_isomorphisms_vf2_callback(&self->g, &other->g, - color1, color2, edge_color1, edge_color2, map12, map21, + color1, color2, edge_color1, edge_color2, NULL, NULL, igraphmodule_i_Graph_isomorphic_vf2_callback_fn, node_compat_fn == Py_None ? 0 : igraphmodule_i_Graph_isomorphic_vf2_node_compat_fn, edge_compat_fn == Py_None ? 0 : igraphmodule_i_Graph_isomorphic_vf2_edge_compat_fn, &callback_data); + res = callback_data.found; } if (color1) { igraph_vector_int_destroy(color1); free(color1); } @@ -11093,12 +11114,18 @@ PyObject *igraphmodule_Graph_subisomorphic_vf2(igraphmodule_GraphObject * self, edge_compat_fn == Py_None ? 0 : igraphmodule_i_Graph_isomorphic_vf2_edge_compat_fn, &callback_data); } else { + /* map12 and map21 are workspace of the C function, not outputs, so we + * do not pass ours. The callback copies each match into them instead. */ + callback_data.found = false; + callback_data.map12 = map12; + callback_data.map21 = map21; retval = igraph_get_subisomorphisms_vf2_callback(&self->g, &other->g, - color1, color2, edge_color1, edge_color2, map12, map21, + color1, color2, edge_color1, edge_color2, NULL, NULL, igraphmodule_i_Graph_isomorphic_vf2_callback_fn, node_compat_fn == Py_None ? 0 : igraphmodule_i_Graph_isomorphic_vf2_node_compat_fn, edge_compat_fn == Py_None ? 0 : igraphmodule_i_Graph_isomorphic_vf2_edge_compat_fn, &callback_data); + res = callback_data.found; } if (color1) { igraph_vector_int_destroy(color1); free(color1); } @@ -18105,7 +18132,12 @@ struct PyMethodDef igraphmodule_Graph_methods[] = { " the first graph, the second graph, a mapping from the nodes of the\n" " first graph to the second, and a mapping from the nodes of the second\n" " graph to the first. The function must return C{True} if the search\n" - " should continue or C{False} otherwise.\n" + " should continue or C{False} otherwise. When a callback is given, the\n" + " boolean result is C{True} if at least one isomorphism was found, and\n" + " the returned mappings (if requested) are those of the last\n" + " isomorphism passed to the callback, i.e. the one at which the callback\n" + " stopped the search by returning C{False}, or the last one found if it\n" + " never did. If no isomorphism was found, the mappings are empty lists.\n" "@param node_compat_fn: a function that receives the two graphs and two\n" " node indices (one from the first graph, one from the second graph) and\n" " returns C{True} if the nodes given by the two indices are compatible\n" @@ -18230,7 +18262,12 @@ struct PyMethodDef igraphmodule_Graph_methods[] = { " the first graph, the second graph, a mapping from the nodes of the\n" " first graph to the second, and a mapping from the nodes of the second\n" " graph to the first. The function must return C{True} if the search\n" - " should continue or C{False} otherwise.\n" + " should continue or C{False} otherwise. When a callback is given, the\n" + " boolean result is C{True} if at least one subisomorphism was found, and\n" + " the returned mappings (if requested) are those of the last\n" + " subisomorphism passed to the callback, i.e. the one at which the callback\n" + " stopped the search by returning C{False}, or the last one found if it\n" + " never did. If no subisomorphism was found, the mappings are empty lists.\n" "@param node_compat_fn: a function that receives the two graphs and two\n" " node indices (one from the first graph, one from the second graph) and\n" " returns C{True} if the nodes given by the two indices are compatible\n" diff --git a/tests/test_isomorphism.py b/tests/test_isomorphism.py index 7984e9b34..df420b475 100644 --- a/tests/test_isomorphism.py +++ b/tests/test_isomorphism.py @@ -150,6 +150,75 @@ def callback(g1, g2, map1, map2): expected_maps = [[0, 1, 2, 3], [0, 2, 1, 3], [3, 1, 2, 0], [3, 2, 1, 0]] self.assertTrue(sorted(maps) == expected_maps) + def testIsomorphicCallbackResult(self): + g = Graph.Ring(5) + + # The result is True if the callback was called at least once + self.assertTrue(g.isomorphic_vf2(g, callback=lambda *args: True)) + self.assertFalse( + g.isomorphic_vf2(Graph.Star(5), callback=lambda *args: True) + ) + + # The mappings are those at which the callback stopped the search + def stop_at_0_to_3(g1, g2, map12, map21): + return map12[0] != 3 + + result = g.isomorphic_vf2( + g, callback=stop_at_0_to_3, return_mapping_12=True, return_mapping_21=True + ) + self.assertTrue(result[0]) + self.assertEqual(result[1][0], 3) + self.assertEqual([result[1][i] for i in result[2]], list(range(5))) + + # ... or the last ones found if the callback never stopped the search + maps = [] + + def collect(g1, g2, map12, map21): + maps.append((map12, map21)) + return True + + result = g.isomorphic_vf2( + g, callback=collect, return_mapping_12=True, return_mapping_21=True + ) + self.assertEqual(len(maps), 10) + self.assertEqual(result, (True,) + maps[-1]) + + # Empty mappings if there is no isomorphism + self.assertEqual( + g.isomorphic_vf2( + Graph.Star(5), + callback=collect, + return_mapping_12=True, + return_mapping_21=True, + ), + (False, [], []), + ) + + def testSubisomorphicCallbackResult(self): + g = Graph.Ring(6) + path = Graph(3, [(0, 1), (1, 2)]) + + self.assertTrue(g.subisomorphic_vf2(path, callback=lambda *args: True)) + + def stop_at_234(g1, g2, map12, map21): + return map21 != [2, 3, 4] + + result = g.subisomorphic_vf2( + path, callback=stop_at_234, return_mapping_12=True, return_mapping_21=True + ) + self.assertEqual(result, (True, [-1, -1, 0, 1, 2, -1], [2, 3, 4])) + + # Pattern larger than the graph: no subisomorphism, empty mappings + self.assertEqual( + path.subisomorphic_vf2( + g, + callback=lambda *args: True, + return_mapping_12=True, + return_mapping_21=True, + ), + (False, [], []), + ) + def testCountIsomorphisms(self): g = Graph.Full(4) self.assertTrue(g.count_automorphisms_vf2() == 24)