🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

csgraph.depth_first_order()

AI & DATA SCIENCE // csgraph-depth-first-order

scipy.sparse.csgraph.depth_first_order() traverses a graph starting from a given node using depth-first search, returning the order nodes are visited in.

Syntax

scipy.sparse.csgraph.depth_first_order(csgraph, i_start, directed=True)

Deep Dive Course

Depth-first search explores as far as possible along each branch before backtracking, in contrast to breadth-first search, which explores all of a node's immediate neighbors before moving further out — depth_first_order() returns the array of node indices in the order DFS actually visited them starting from i_start, plus an array of each visited node's predecessor in the resulting traversal tree. Nodes that aren't reachable from i_start at all are simply never included in the returned visiting order.

1Understanding csgraph.depth_first_order()

Depth-first search explores as far as possible along each branch before backtracking, in contrast to breadth-first search, which explores all of a node's immediate neighbors before moving further out — depth_first_order() returns the array of node indices in the order DFS actually visited them starting from i_start, plus an array of each visited node's predecessor in the resulting traversal tree. Nodes that aren't reachable from i_start at all are simply never included in the returned visiting order.

💡

Nodes unreachable from the given starting node i_start are silently excluded from depth_first_order()'s result entirely — check the length of the returned order array against the graph's total node count if you need to detect unreached nodes explicitly.

editor.html
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order

graph = csr_matrix([[0, 1, 1], [0, 0, 0], [0, 0, 0]])
order, predecessors = depth_first_order(graph, i_start=0)
print(order)
localhost:3000

2Practical Example

Here is a real-world application of csgraph.depth_first_order() showing how it is used in production SciPy code.

editor.html
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order

graph = csr_matrix([[0, 1, 0], [0, 0, 1], [0, 0, 0]])
order, predecessors = depth_first_order(graph, i_start=0)
print(order)
print(predecessors)
localhost:3000

3Best Practices

Follow these guidelines when working with csgraph.depth_first_order():

1. Use depth_first_order() when you specifically need a depth-first traversal order, such as for topological-sort-style processing or exploring one path deeply before others

2. Check the returned order array's length against the total number of nodes to detect any nodes that weren't reachable from the given starting point

3. Use the returned predecessors array to reconstruct the actual traversal tree/path taken to reach any specific visited node

⚠️

Tip: Nodes unreachable from the given starting node i_start are silently excluded from depth_first_order()'s result entirely — check the length of the returned order array against the graph's total node count if you need to detect unreached nodes explicitly.

editor.html
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order

graph = csr_matrix([[0, 1, 1], [0, 0, 0], [0, 0, 0]])
order, predecessors = depth_first_order(graph, i_start=0)
print(order)
localhost:3000

Examples

Example 01Basic Usage
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order

graph = csr_matrix([[0, 1, 1], [0, 0, 0], [0, 0, 0]])
order, predecessors = depth_first_order(graph, i_start=0)
print(order)
Example 02Advanced Example
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order

graph = csr_matrix([[0, 1, 0], [0, 0, 1], [0, 0, 0]])
order, predecessors = depth_first_order(graph, i_start=0)
print(order)
print(predecessors)

Best Practices

  • Use depth_first_order() when you specifically need a depth-first traversal order, such as for topological-sort-style processing or exploring one path deeply before others
  • Check the returned order array's length against the total number of nodes to detect any nodes that weren't reachable from the given starting point
  • Use the returned predecessors array to reconstruct the actual traversal tree/path taken to reach any specific visited node

Interview Question

Why might depth_first_order() return an array shorter than the total number of nodes in the graph?

Hint: Think about what happens to nodes that the starting point simply can't reach.

depth_first_order() only visits, and therefore only includes in its result, nodes that are actually reachable from the given starting node by following the graph's edges — if some nodes are in a completely separate, disconnected part of the graph, or are only reachable via edges pointing the wrong direction in a directed graph, the traversal never reaches them at all. Since those unreachable nodes are never visited, they simply don't appear anywhere in the returned order array, which is exactly why the array's length can end up shorter than the graph's total node count whenever the graph isn't fully connected from that particular starting point.

Exercises

MediumPractice using csgraph.depth_first_order() in a real scenario.
View Solution
from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import depth_first_order

graph = csr_matrix([[0, 1, 1], [0, 0, 0], [0, 0, 0]])
order, predecessors = depth_first_order(graph, i_start=0)
print(order)

Frequently Asked Questions

Why might depth_first_order() return an array shorter than the total number of nodes in the graph?

depth_first_order() only visits, and therefore only includes in its result, nodes that are actually reachable from the given starting node by following the graph's edges — if some nodes are in a completely separate, disconnected part of the graph, or are only reachable via edges pointing the wrong direction in a directed graph, the traversal never reaches them at all. Since those unreachable nodes are never visited, they simply don't appear anywhere in the returned order array, which is exactly why the array's length can end up shorter than the graph's total node count whenever the graph isn't fully connected from that particular starting point.

Related Functions

csgraph-connected-componentscsgraph-dijkstrasparse-csr-matrix