All Products
Search
Document Center

Platform For AI:Tree depth

Last Updated:Apr 01, 2026

The Tree Depth component calculates the depth of each node in a tree structure — specifically, the number of edges on the path from the root node to the farthest leaf node. It takes a directed edge table as input and outputs the depth, root, and tree membership for every node in the graph.

Tree Depth is a graph algorithm component. It computes structural depth in a directed graph (such as an organizational hierarchy or dependency tree), not the max_depth hyperparameter used in decision tree models.

How it works

  1. Read the input edge table, where each row represents a directed edge from a start node to an end node.

  2. Traverse from each root node (a node with no incoming edges) to assign depth values.

  3. Write the depth and root assignment for every node to the output table.

If the graph contains multiple disconnected trees, each tree is processed independently. Nodes in different trees have different root values in the output.

Configure the component

Method 1: Configure on the pipeline canvas

Add the Tree Depth component to your pipeline in Machine Learning Designer. The following tables describe the parameters.

Fields setting tab

ParameterDescription
Edge Table: Start vertex columnThe column in the edge table that contains the start node of each edge.
Edge Table: End vertex columnThe column in the edge table that contains the end node of each edge.

Tuning tab

ParameterDescription
WorkersThe number of parallel workers. Higher values increase parallelism but also increase inter-worker communication overhead. Leave blank to let the system decide.
Memory size per workerThe maximum memory allocated to each worker, in MB. Default: 4096. If a worker exceeds this limit, an OutOfMemory error is reported. Increase this value when processing dense subgraphs or large adjacency lists.
Data split size (MB)The size of each data partition, in MB. Default: 64. Decrease this value to create more, smaller partitions for better load balancing on uneven graphs.

Method 2: Configure using PAI commands

Run Tree Depth using the SQL Script component. For setup instructions, see Scenario 4: Execute PAI commands within the SQL script component.

PAI -name TreeDepth
    -project algo_public
    -DinputEdgeTableName=TreeDepth_func_test_edge
    -DfromVertexCol=flow_out_id
    -DtoVertexCol=flow_in_id
    -DoutputTableName=TreeDepth_func_test_result;

Parameters

ParameterRequiredDefaultDescription
inputEdgeTableNameYesThe name of the input edge table.
inputEdgeTablePartitionsNoFull tableThe partitions to read from the input edge table.
fromVertexColYesThe column containing the start node of each edge.
toVertexColYesThe column containing the end node of each edge.
outputTableNameYesThe name of the output table.
outputTablePartitionsNoThe partitions to write to in the output table.
lifecycleNoThe lifecycle of the output table.
workerNumNoSystem defaultThe number of parallel workers.
workerMemNo4096The maximum memory per worker, in MB. Increase this when processing dense subgraphs or large adjacency lists.
splitSizeNo64The data partition size, in MB. Decrease this for better load balancing on uneven graphs.

Example

This example builds a small graph with two disconnected trees and runs Tree Depth to compute node depths.

Step 1: Create the input edge table

Add a SQL Script component to the canvas. Clear the Use Script Mode and Whether the system adds a create table statement checkboxes, then enter the following SQL:

drop table if exists TreeDepth_func_test_edge;
create table TreeDepth_func_test_edge as
select * from
(
    select '0' as flow_out_id, '1' as flow_in_id
    union all
    select '0' as flow_out_id, '2' as flow_in_id
    union all
    select '1' as flow_out_id, '3' as flow_in_id
    union all
    select '1' as flow_out_id, '4' as flow_in_id
    union all
    select '2' as flow_out_id, '4' as flow_in_id
    union all
    select '2' as flow_out_id, '5' as flow_in_id
    union all
    select '4' as flow_out_id, '6' as flow_in_id
    union all
    select 'a' as flow_out_id, 'b' as flow_in_id
    union all
    select 'a' as flow_out_id, 'c' as flow_in_id
    union all
    select 'c' as flow_out_id, 'd' as flow_in_id
    union all
    select 'c' as flow_out_id, 'e' as flow_in_id
)tmp;
drop table if exists TreeDepth_func_test_result;
create table TreeDepth_func_test_result
(
  node string,
  root string,
  depth bigint
);

The SQL creates a graph with two disconnected trees:

Data structure
图结构

Step 2: Run Tree Depth

Add a second SQL Script component, clear both checkboxes, enter the following PAI command, and connect it to the first component:

drop table if exists ${o1};
PAI -name TreeDepth
    -project algo_public
    -DinputEdgeTableName=TreeDepth_func_test_edge
    -DfromVertexCol=flow_out_id
    -DtoVertexCol=flow_in_id
    -DoutputTableName=${o1};

Step 3: Run the pipeline

Click image in the upper-left corner of the canvas to run the pipeline.

Step 4: View results

Right-click the SQL Script component from Step 2 and choose View Data > SQL Script Output.

noderootdepth
aa0
ba1
ca1
da2
ea2
000
101
201
302
402
502
603

Output columns:

  • node: The node identifier.

  • root: The root node of the tree this node belongs to. All nodes in the same tree share the same root value. When the graph contains multiple disconnected trees, each tree has a distinct root value. In this example, nodes 06 belong to the tree rooted at 0, and nodes ae belong to the tree rooted at a.

  • depth: The number of edges from the root node to this node. Root nodes have depth 0; their direct children have depth 1; and so on.