igraph
directed tree
graph theory
algorithm
paths

All possible paths from one node to another in a directed tree igraph

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In a directed tree, there is at most one directed path from one node to another. That fact is the first thing to clarify, because many questions that ask for "all possible paths" are really asking about a general directed acyclic graph, not a true directed tree. If the structure really is a directed tree, the task is simpler: either one path exists or none does. In igraph, that means you usually want one shortest-path or predecessor-based query rather than an expensive path enumeration routine.

Why a Directed Tree Has at Most One Path

A tree has no cycles and no alternative routes between two vertices. Once you add edge directions, some undirected connections may become unreachable, but you still do not create multiple directed routes between the same pair of nodes unless the structure is no longer a tree.

So for a real directed tree:

  • zero paths may exist from u to v
  • one directed path may exist from u to v
  • more than one directed path means you are not dealing with a tree anymore

That distinction matters because it changes both the algorithm and the complexity discussion.

Get the Path in Python igraph

If you want the path from one vertex to another in igraph, the standard approach is to ask for a shortest path. In a tree, the unique path is also the shortest path.

python
1import igraph as ig
2
3# Directed tree: 0 -> 1, 0 -> 2, 1 -> 3, 1 -> 4
4g = ig.Graph(
5    n=5,
6    edges=[(0, 1), (0, 2), (1, 3), (1, 4)],
7    directed=True,
8)
9
10path = g.get_shortest_paths(0, to=4, output="vpath")
11print(path)

Typical output:

python
[[0, 1, 4]]

That is enough for a true directed tree because there cannot be a second valid directed route.

Detect When No Path Exists

If the direction blocks reachability, the returned path will be empty.

python
1import igraph as ig
2
3g = ig.Graph(
4    n=5,
5    edges=[(0, 1), (0, 2), (1, 3), (1, 4)],
6    directed=True,
7)
8
9path = g.get_shortest_paths(4, to=0, output="vpath")
10print(path)

Output:

python
[[]]

That means there is no directed path from 4 back to 0 in this graph.

If You Really Need All Paths, Recheck the Data Model

If your graph can have multiple valid directed paths between two nodes, then it is not a tree. It is some more general directed graph or DAG.

In that case, a depth-first search that enumerates paths is appropriate.

python
1import igraph as ig
2
3
4def all_paths(graph, source, target):
5    result = []
6
7    def dfs(node, path):
8        if node == target:
9            result.append(path.copy())
10            return
11        for neighbor in graph.successors(node):
12            if neighbor in path:
13                continue
14            path.append(neighbor)
15            dfs(neighbor, path)
16            path.pop()
17
18    dfs(source, [source])
19    return result
20
21
22g = ig.Graph(
23    n=4,
24    edges=[(0, 1), (0, 2), (1, 3), (2, 3)],
25    directed=True,
26)
27
28print(all_paths(g, 0, 3))

This prints two paths:

python
[[0, 1, 3], [0, 2, 3]]

That example is intentionally not a tree. It is the kind of structure where "all possible paths" actually makes sense.

Use the Right Question for the Structure

So the real workflow is:

  1. verify whether the graph is truly a directed tree
  2. if yes, ask for the unique path or reachability
  3. if no, use path enumeration only when you really need it

This saves a lot of wasted work. Path enumeration grows quickly in general graphs, while tree path lookup is cheap and conceptually simpler.

igraph APIs That Help

Useful igraph methods for this area include:

  • 'get_shortest_paths for retrieving the unique path in a tree'
  • 'distances for reachability or path length checks'
  • 'successors for manual DFS over outgoing edges'

For a tree-shaped problem, start with the built-in path methods before writing your own recursion.

Common Pitfalls

The biggest mistake is saying "directed tree" when the data actually contains multiple parent choices or converging routes. That changes the problem entirely.

Another mistake is enumerating all paths in a true tree. If the structure is really a tree, there is nothing to enumerate beyond zero-or-one path existence.

Developers also often forget edge direction and reason as if the graph were undirected. In a directed tree, parent-to-child reachability does not imply child-to-parent reachability.

Finally, do not use path enumeration as your first instinct in igraph when a built-in path lookup already answers the real question.

Summary

  • In a true directed tree, there is at most one directed path from one node to another.
  • In igraph, get_shortest_paths is usually the right tool for retrieving that path.
  • If multiple paths exist, the structure is not a tree anymore.
  • Only use all-path enumeration when the graph model actually permits multiple routes.
  • Clarifying the graph type is the most important step before writing the algorithm.

Course illustration
Course illustration

All Rights Reserved.