# all\_simple\_paths returning fewer paths than expected

**URL:** <https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722>\
**Category:** Usage\
**Tags:** Python\
**Created:** [15 December 2023 22:02 UTC](https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722 "2023-12-15T22:02:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayasare](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/rayasare/32/1027_2.png) [@rayasare](https://igraph.discourse.group/u/rayasare)\
**Post date:** [15 December 2023 22:02 UTC](https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722/1 "2023-12-15T22:02:44Z")

</div>

I have a [60-node graph](https://www.dropbox.com/scl/fi/0faqodxttp2sdm50ekwvz/graph60.gml?rlkey=nsjsf9kxqqymrrzhqp64caqtr&dl=0) with one connected component, in which there is a path between every pair of nodes. I am trying to enumerate all simple paths between nodes. I expect the longest simple path to be of length 60 after iterating through all nodes and using `get_all_simple_paths` with a `cutoff=-1`, but instead the longest simple path is only of length 27. Might I be doing something wrong here / thinking about this incorrectly?

**To reproduce**

```auto
from tqdm import tqdm
from igraph import Graph 
import itertools

G = Graph.Load('graph60.gml', format='gml')
sp_dicts = {}
sp_list = []
end_num = 60

for i, j in tqdm(itertools.combinations(range(end_num), 2)):
    simple_path_lists = G.get_all_simple_paths(i, to=j, mode="OUT", cutoff=-1)

    sp_dicts[f'{i}_{j}'] = [simple_path for simple_path in simple_path_lists]

    for path_list in simple_path_lists:
        sp_list.append(sorted(path_list)) 

# Convert lists to tuples and then to a set to get unique tuples
unique_tuples = set(tuple(lst) for lst in sp_list)

# Convert unique tuples back to lists
unique_lists = [sorted(list(tpl)) for tpl in unique_tuples]
n_mer_list = sorted(unique_lists, key=lambda x: (len(x), x))
print(f'length of longest path: {len(n_mer_list[-1])}')

```

**Version information**  
‘0.11.3’ of `python-igraph` from pip

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [16 December 2023 11:27 UTC](https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722/2 "2023-12-16T11:27:24Z")

</div>

Why do you expect to find a path that visits every vertex in this graph? This is called a Hamiltonian path, and it is not guaranteed to exist.

---

<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 December 2023 13:19 UTC](https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722/3 "2023-12-18T13:19:10Z")

</div>

It is easy to see that no such path exists: there are several vertices with zero in-degree.

---

<div class="post-metadata">

**Author:** ![rayasare](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/rayasare/32/1027_2.png) [@rayasare](https://igraph.discourse.group/u/rayasare)\
**Post date:** [18 December 2023 21:04 UTC](https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722/4 "2023-12-18T21:04:22Z")

</div>

Thanks for the responses – it helped me realize that I made a mistake when generating the graph. The [corrected graph](https://www.dropbox.com/scl/fi/3efnfeo0yhiej5eypjen5/graph60_undirected.gml?rlkey=08qhqepkfukgutmm5i4tqn38i&dl=0) should be undirected. I believe that a path should exist that visits every vertex since the graph is undirected and since it has only one connected component.

As a follow-up: the docs mention that `get_all_simple_paths` may run out of memory for exponentially many paths between two vertices. I noticed that on my [newly generated, undirected graph](https://www.dropbox.com/scl/fi/3efnfeo0yhiej5eypjen5/graph60_undirected.gml?rlkey=08qhqepkfukgutmm5i4tqn38i&dl=0), this same calculation appears to be suffering from this limitation. Do you have any suggestions to speed up this calculation of simple paths between nodes?

I have tried reducing the `cutoff` parameter of `get_all_simple_paths` to half of the node length, because I can intuit the paths that I need from only this truncation, but the calculation is still very slow. I suspect that part of the slowdown comes from creating the new, undirected graph with the full adjacency matrix, whereas the other (accidentally) directed graph was created with only the upper triangular adjacency matrix and was definitely sparser. Any suggestions are welcomed.

---

<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:** [20 December 2023 18:34 UTC](https://igraph.discourse.group/t/all-simple-paths-returning-fewer-paths-than-expected/1722/5 "2023-12-20T18:34:24Z")

</div>

> [@rayasare](#):
>
> Do you have any suggestions to speed up this calculation of simple paths between nodes?

This function is already very efficient, and it is unlikely that it could be sped up significantly.

There is a feature request to add a version which does not store results, but instead calls a function with each path that is found. This won’t speed up the calculation, in fact it will slow it down. But it will eliminate the need to store all paths at the same time. You can instead process them one by one as they are found. If this will help your use case, add your vote here:

> <https://github.com/igraph/igraph/issues/2357>
>
> \*\*What is the feature or improvement you would like to see?\*\*
> 
> \`igraph\_get\_all…\_simple\_paths()\` produces an exponentially large number of results. Among such functions, it is unique in not having a callback version, so results can be filtered using arbitrary criteria before storing them.
> 
> This is a proposal to add a callback version.
> 
> This would be better done with iterators, but we are a long way off from building a consistent iterator interface.
> 
> \*\*Use cases for the feature\*\*
> 
> Reducing the result size based on arbitrary criteria.
> 
> \*\*References\*\*
> 
> - #2355 
> - #2356 
> - User request: https://igraph.discourse.group/t/how-to-break-up-all-simple-paths-calculation/1570?u=szhorvat

If you know C, a pull request is very welcome. This is not a difficult task.

You can add your vote to the following two feature requests if they help your use case. These would make it possible to filter paths by length without needing a (slow) callback.

> <https://github.com/igraph/igraph/issues/2355>
>
> \*\*What is the feature or improvement you would like to see?\*\*
> 
> \`igraph\_get\_all…\_simple\_paths()\` has a \`cutoff\` argument specifying an upper bound on path length.
> 
> There should also be a lower bound.
> 
> \*\*Use cases for the feature\*\*
> 
> While this does not affect the performance of calculation (short paths need to be computed in order to extend them to longer ones), pre-filtering results by length is practically useful for reducing the size of the output.
> 
> Having both an upper and a lower bound makes it convenient to find paths of a specific length.
> 
> \*\*References\*\*
> 
> - Mathematica has this as a built-in: https://reference.wolfram.com/language/ref/FindPath.html
> - User request: https://igraph.discourse.group/t/how-to-break-up-all-simple-paths-calculation/1570?u=szhorvat

> <https://github.com/igraph/igraph/issues/2356>
>
> \*\*What is the feature or improvement you would like to see?\*\*
> 
> \`igraph\_get\_all…\_simple\_paths()\` has an integer \`cutoff\` argument, specifying the maximum unweighted path length. #2355 proposes adding a minimum length as well.
> 
> This should be extended to the weighted case.
> 
> \*\*Use cases for the feature\*\*
> 
> Many, if not most network datasets are weighted.
> 
> In spatially embedded networks, for example road networks, the relevant measure is typically not the number of hops, but the physical length of paths.
> 
> \*\*References\*\*
> 
> - Mathematica has this as a built-in: https://reference.wolfram.com/language/ref/FindPath.html
