Hello,

I want to implement an adjancency search in a graph. That is, I want for a node all the nodes that can be reached using at most `depth`

edges.

I have this implementation so far, but I'm not sure if it is optimal. I'm not even sure if some tail recursive optimizations will be done.

```
struct Graph {
adjacencies: Vec<Vec<usize>>,
}
impl Graph {
fn new(adjacencies: Vec<Vec<usize>>) -> Graph {
Graph { adjacencies }
}
// Returns all the nodes adjacent that are reachable using at most `depth`
// edges starting from `node`.
fn adjacent_to(&self, node: usize, depth: u32) -> Vec<usize> {
if node >= self.adjacencies.len() {
panic!("invalid node");
}
if depth == 0 {
vec![node]
} else {
let mut adjacents = Vec::new();
self.adjacent_to_recursion(node, depth, &mut adjacents);
adjacents.sort();
adjacents.dedup();
adjacents
}
}
// Helper function. Is it tail recursive?
fn adjacent_to_recursion(&self, node: usize, depth: u32, adjacents: &mut Vec<usize>) {
adjacents.extend(self.adjacencies[node].iter());
adjacents.push(node);
if depth > 1 {
self.adjacencies[node]
.iter()
.for_each(|n| self.adjacent_to_recursion(*n, depth - 1, adjacents))
}
}
}
fn main() {
let graph = Graph::new(vec![
vec![1, 2],
vec![2, 3],
vec![4],
vec![0, 1, 2],
vec![0, 3],
]);
assert_eq!(graph.adjacent_to(2, 0), &[2]);
assert_eq!(graph.adjacent_to(2, 1), &[2, 4]);
assert_eq!(graph.adjacent_to(2, 2), &[0, 2, 3, 4]);
assert_eq!(graph.adjacent_to(2, 3), &[0, 1, 2, 3, 4]);
}
```

Any comments or reccomendations regarding the implementation are welcome.

Thanks!