# Stop subgraph isomorphism after X matches

**URL:** https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185
**Category:** Development
**Tags:** Python
**Created:** [16 April 2020 15:15 UTC](https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185 "2020-04-16T15:15:11Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![steve](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/steve/32/193_2.png) [@steve](https://igraph.discourse.group/u/steve)
#### Post date: [16 April 2020 15:15 UTC](https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185/1 "2020-04-16T15:15:12Z")

</div>

As far as I know, the python interface does not have a way to stop the algorithm (get\_subisomorphisms\_vf2) after finding a certain number of matches. Could this be added as a new option? It would be a useful as isomorphism can have combinatorial explosion of results. I’d be willing to work on it.

---

<div class="post-metadata">

### Author: ![tamas](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/tamas/32/1261_2.png) [@tamas](https://igraph.discourse.group/u/tamas)
#### Post date: [17 April 2020 14:13 UTC](https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185/2 "2020-04-17T14:13:33Z")

</div>

You are right, the Python interface does not support stopping the search yet; it is supported in the C interface via `igraph_subisomorphic_function_vf2()`. In the Python interface, I would prefer not to create another method for this but simply to extend the argument list of `get_subisomorphisms_vf2()` with an extra `callback` argument. If the callback is present, we need to prepare a special callback in the C layer that simply invokes the Python callback and casts its return value to a Boolean. This special C callback can then be passed to `igraph_subisomorphic_function_vf2()`.

This would all have to be done in the C code that binds igraph to Python (somewhere in `src/graphobject.c` in the source code of the Python interface). If you are willing to work on it, let me know and I’ll be happy to help along the way and/or review your PR when it’s ready. Thanks in advance!

---

<div class="post-metadata">

### Author: ![steve](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/steve/32/193_2.png) [@steve](https://igraph.discourse.group/u/steve)
#### Post date: [18 April 2020 01:23 UTC](https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185/3 "2020-04-18T01:23:07Z")

</div>

Sounds good, I’ll let you know if I need help, thanks.

---

<div class="post-metadata">

### Author: ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)
#### Post date: [18 April 2020 14:19 UTC](https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185/4 "2020-04-18T14:19:57Z")

</div>

@steve I am not sufficiently familiar with the Python interface of igraph, or with Python’s C interface to comment much on how this can be implemented, but I wanted to point out that there are already a few other functions that have such a callback argument, notably [the motif finding ones](https://igraph.org/python/doc/igraph.GraphBase-class.html#motifs_randesu). You may want to look at the implementation of those for inspiration.

---

<div class="post-metadata">

### Author: ![steve](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/steve/32/193_2.png) [@steve](https://igraph.discourse.group/u/steve)
#### Post date: [23 May 2020 04:24 UTC](https://igraph.discourse.group/t/stop-subgraph-isomorphism-after-x-matches/185/5 "2020-05-23T04:24:30Z")

</div>

Just in case someone looks at this in the future, we determined this functionality could already be achieved through a callback in the function subisomorphic\_vf2.
