# How to limit length of paths in a graph？

**URL:** <https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527>\
**Category:** Usage\
**Tags:** Python\
**Created:** [4 April 2023 10:35 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527 "2023-04-04T10:35:24Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![forestbat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/forestbat/32/902_2.png) [@forestbat](https://igraph.discourse.group/u/forestbat)\
**Post date:** [4 April 2023 10:35 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/1 "2023-04-04T10:35:24Z")

</div>

I have a graph which looks like this：  
`[(0,2),(1,2),(3,1)]`  
Now I want to limit lengths of paths to 2 from node 2, such as this:  
`[(0,2),(1,2)]`  
So what’s the fastest method?

---

<div class="post-metadata">

**Author:** ![dan\_suthers](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/dan_suthers/32/254_2.png) [@dan\_suthers](https://igraph.discourse.group/u/dan_suthers)\
**Post date:** [5 April 2023 10:28 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/2 "2023-04-05T10:28:41Z")

</div>

Limit lengths when doing what operation?  
all\_simple\_paths has a cutoff parameter; set it to 2.  
(I would not worry about whether it is “the fastest” method for most applications; generally the underlying implementations in igraph are well tuned.)

---

<div class="post-metadata">

**Author:** ![forestbat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/forestbat/32/902_2.png) [@forestbat](https://igraph.discourse.group/u/forestbat)\
**Post date:** [5 April 2023 15:08 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/3 "2023-04-05T15:08:06Z")

</div>

> Limit lengths when doing what operation?

I want to find some nodes which have limited skips from start in my work codes，so I hope I can get a subgraph which longest path is 2 (or other pointed length)，is there a method to change a graph to the form which I want？if not，I will use all\_simple\_paths to complete this.

---

<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:** [5 April 2023 16:02 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/4 "2023-04-05T16:02:32Z")

</div>

I do not understand what you are trying to do. A much clearer, and more detailed explanation would be needed before this can be answered. Include a concrete example.

---

<div class="post-metadata">

**Author:** ![dan\_suthers](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/dan_suthers/32/254_2.png) [@dan\_suthers](https://igraph.discourse.group/u/dan_suthers)\
**Post date:** [5 April 2023 23:10 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/5 "2023-04-05T23:10:49Z")

</div>

If you are searching from a specific node (or a set of specific nodes one at a time) you can use igraph::ego, with order = 2, and also set mindist to eliminate the nodes 1 step away.

---

<div class="post-metadata">

**Author:** ![forestbat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/forestbat/32/902_2.png) [@forestbat](https://igraph.discourse.group/u/forestbat)\
**Post date:** [6 April 2023 13:27 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/6 "2023-04-06T13:27:25Z")

</div>

Imagine you have a map which contains many train stations and railways，and someone is asking you：“I want to go to stations which are five stations away from here，please give me these stations.”  
This is what I want to do.

---

<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:** [6 April 2023 13:38 UTC](https://igraph.discourse.group/t/how-to-limit-length-of-paths-in-a-graph/1527/7 "2023-04-06T13:38:34Z")

</div>

Dan’s suggestion of `ego()` accomplishes that.
