# Question about how to get dfs() working ?

**URL:** https://igraph.discourse.group/t/question-about-how-to-get-dfs-working/1372
**Category:** Usage
**Tags:** R, C
**Created:** [28 September 2022 09:17 UTC](https://igraph.discourse.group/t/question-about-how-to-get-dfs-working/1372 "2022-09-28T09:17:29Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![KeesP](https://avatars.discourse-cdn.com/v4/letter/k/2bfe46/32.png) [@KeesP](https://igraph.discourse.group/u/KeesP)
#### Post date: [28 September 2022 09:17 UTC](https://igraph.discourse.group/t/question-about-how-to-get-dfs-working/1372/1 "2022-09-28T09:17:29Z")

</div>

E.g. I wonder if it is possible to compute the “layer” of all vertices of a directed tree using igraph::dfs(). Without using other functions that implicitly traverse the graph such as shortst\_paths(), distances(), degree(), layout\_with\_sugiyama().

The layer of a vertex is (recursively) defined as

- equal to zero if there are no outgoing edges and
- otherwise the largest layer among the successors, plus 1.

It is straightforward to code this manually, but the native dfs() is obviously much faster.

---

<div class="post-metadata">

### Author: ![krlmlr](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/krlmlr/32/905_2.png) [@krlmlr](https://igraph.discourse.group/u/krlmlr)
#### Post date: [8 April 2023 02:59 UTC](https://igraph.discourse.group/t/question-about-how-to-get-dfs-working/1372/2 "2023-04-08T02:59:37Z")

</div>

Thanks. [?dfs](https://r.igraph.org/reference/dfs.html#ref-examples) has an example for the `out.callback` argument, is that helpful?

---

<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: [8 April 2023 11:12 UTC](https://igraph.discourse.group/t/question-about-how-to-get-dfs-working/1372/3 "2023-04-08T11:12:14Z")

</div>

You seem to be asking to compute the Strahler stream order. [While this is very straightforward when programming igraph in C](https://github.com/szhorvat/IGraphM/blob/8de23ab6b2dcab448ba13bffe4c3a864c54927e7/IGraphM/LibraryResources/Source/IG.h#L3473), I failed when I tried to do it in R a while ago.

> <https://github.com/igraph/rigraph/issues/522>
>
> \*\*Describe the bug\*\*
> 
> The \`bfs()\` and \`dfs()\` functions produce a 'father' vec…tor that contains \`NA\` for the root. This is in fact not a vector, but an \`igraph.vs\`, where it is reasonable to expect that all values are proper vertex IDs and not \`NA\`. As a result, there will be problems when trying to use this result.
> 
> This is closely related to #186. Perhaps the same fix should be applied as there. While this will be a breaking change, the \`father\` results appears to be unusable at the moment, as the \`NA\`s are basically guaranteed to trigger an error (unless I'm missing something about how to work with \`NA\`).
> 
> \*\*To reproduce\*\*
> 
> \`\`\`
> g \<- sample\_gnm(10,20)
> res \<- bfs(g,1,father=T)$father
> \`\`\`
> 
> \`\`\`
> \> str(res)
> Error in simple\_vs\_index(x, ii, na\_ok) : Unknown vertex selected
> \> res\[1\]
> Error in simple\_vs\_index(x, args\[\[1\]\]$expr) : Unknown vertex selected
> \`\`\`
> 
> I am not sure how to even test whether a certain element or \`NA\`, which is what originally prevented me from being able to come up with a nice solution for the Strahler number computation \[here\](https://stackoverflow.com/q/70257375/695132).
> 
> \*\*Version information\*\*
> 
> 1.3.0, but also earlier.

@krlmlr, I suggest you try to do this before settling on a solution for the above issue.

Basically you need to iterate through vertices according to `order.out`, then whenever the parent `p` of a vertex `v` doesn’t have a higher stream order, set its stream order of `p` to that of the `v` plus one.

Note that it is _not_ necessary to use the callback function.
