Advanced maths

Graph tree depth calculator with an elimination forest

Find the minimum height of an elimination forest and return every vertex’s parent and level.

Your numbers

Vertices are labeled 0 through n−1, including isolated vertices.
One edge per line: u,v. No loops or duplicate pairs. Blank means no edges.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

Find the minimum height of an elimination forest and return every vertex’s parent and level. Each original edge must connect an ancestor to a descendant in that forest.

THE FORMULAtd(∅)=0; td(G)=max_C td(C) for disconnected G; td(G)=1+min_v td(G−v) for connected G.

A worked example

The path 0–1–2–3–4 has tree depth 3. Choose 2 as root; its children can be 0 and 3, with 1 below 0 and 4 below 3. Every path edge has comparable endpoints. Depth 2 would only allow star components, so it cannot contain this five-vertex path.

Good to know

Simple undirected graphs with 1–12 vertices. Exact memoized subset recursion, at most 4095 nonempty states; component extraction adds polynomial work. Height counts vertices, not edges. Returns one optimum, not every forest.

What the forest measures

Tree depth measures how many nested vertex removals are needed when disconnected pieces may be processed in parallel. A root represents the first removed vertex of a connected piece. Its child subtrees describe the pieces that remain, and the longest chain records the deepest dependency.

An elimination forest need not be a subgraph of the entered graph. A forest parent may be nonadjacent to its child in the original graph. The necessary condition instead concerns closure: every original edge must join two vertices lying on the same ancestor chain, possibly several levels apart.

A path and its depth three elimination forestOriginal path01234Elimination forest: height 301234Remove 2, then solve both sides
Scroll across to see the full diagram.
The path 0–1–2–3–4 has tree depth 3. Choose 2 as root; its children can be 0 and 3, with 1 below 0 and 4 below 3. Every path edge has comparable endpoints. Depth 2 would only allow star components, so it cannot contain this five-vertex path.

Compare this with the Exact treewidth calculator with a tree decomposition. Minimize rooted elimination-forest height,which differs from tree-decomposition bag width.

Enter a complete vertex set

Set the vertex count before listing undirected edges. Labels start at zero and stop one below that count. An isolated vertex still exists when its label never appears in an edge row, so increasing the count can add components without changing the edge list.

Enter two integer endpoints per row, using commas or whitespace. Direction, weights, loops and parallel copies are outside this model. A reversed duplicate such as 1,0 after 0,1 is rejected. Correct the offending row and calculate again; the graph is rebuilt from the corrected input.

Follow the five vertex example

For the default path, remove vertex 2 first. Two edges remain as separate components: 0–1 and 3–4. Each component needs a root and one child, so both have height two. Placing those forests under 2 gives total height three, despite the path having five vertices.

A height two forest can contain only star-shaped connected subgraphs in its closure. The default path is not a star, because it has no vertex adjacent to the other four. Therefore height two cannot work. This supplies a short independent lower-bound argument beside the returned height three construction.

Why the recursion is exact

A connected graph must sit inside one rooted tree of any elimination forest. Trying every possible root covers every first removal. After a root is removed, the connected components can be solved independently, and only the largest remaining height contributes to the new root’s height.

The program memoizes vertex subsets, so repeated residual graphs share their answer. For disconnected subsets it takes a maximum rather than a sum. This distinction matters: two disjoint long paths can use equally deep trees simultaneously, whereas adding their depths would incorrectly charge for processing them in sequence.

Read parents and levels together

The parent array is indexed by original vertex label. A parent of −1 marks a root, while level one marks that root’s height position. Trace parent pointers to inspect ancestry. For each entered edge, one endpoint must occur on the other endpoint’s parent chain.

An optimum value alone gives no readable explanation of the removals. The witness supplies that explanation, and its largest level must equal the reported depth. Different root choices can attain the same value. Relabeling vertices may change the chosen certificate without changing the mathematical optimum.

Compare related graph parameters

Tree width controls the sizes of overlapping bags in a tree decomposition. Tree depth controls ancestor chains in a rooted forest. Even a path, whose tree width stays small, needs increasing tree depth as its length grows. Use the treewidth calculator when bag size is the intended question.

A spanning tree is also a different object: its edges must come from the graph and it aims to connect all vertices without cycles. An elimination forest is a structural certificate about ancestry. Treating the returned parent links as selected communication links would give the wrong operational interpretation.

Bounds and safe interpretation

The implementation accepts at most twelve vertices because its exact method explores a subset state space. There are at most 4095 nonempty subsets, with component checks and root trials inside those states. This is a deliberately small exact calculator rather than an approximation intended for large networks.

The supported input is always nonempty. A single isolated vertex has depth one, and any number of mutually isolated entered vertices also has depth one. All connected components are retained in the certificate. The reference explains the parameter and recurrence; this implementation uses its own bounded subset search.

A few questions, answered

Does height count edges or vertices?

Vertices. A single vertex has depth 1.

Must forest edges be original graph edges?

No. Every original edge must instead have ancestor-related endpoints.

How are disconnected graphs handled?

Their tree depths combine by maximum, with one elimination tree for each component.

Is the returned forest unique?

No. Several root choices and parent structures can attain the same minimum height.

Find your calculator

Start typing to explore the library.