Advanced maths

Exact treewidth calculator with a tree decomposition

Treewidth measures how a graph can be assembled from overlapping vertex bags arranged as a tree.

Your numbers

Vertices are indexed 0 through n−1.
Rows from,to,weight; absent weight means one. Leave empty for 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

Treewidth measures how a graph can be assembled from overlapping vertex bags arranged as a tree. This calculator uses exact subset elimination on small graphs, constructs a decomposition and verifies edge coverage and the running-intersection condition. Its width is certified by a completed optimum search rather than a heuristic fill order.

THE FORMULAtreewidth(G)=min_elimination_order max(number of remaining filled neighbors).

A worked example

A four-cycle has treewidth two. Eliminating one vertex joins its two neighbors, and two bags of size three can cover the cycle with a connected overlap. The largest bag size minus one is two, while no width-one forest decomposition can represent the cycle.

Good to know

At most 12 vertices, simple undirected graph. Exact exponential dynamic programming. Empty-edge nonempty graphs have width zero. The decomposition is not unique; a minimum-fill heuristic alone is not treated as a minimum-treewidth certificate.

Elimination creates fill among remaining neighbors

Eliminating a vertex connects all its remaining neighbors before that vertex is removed. The number of those neighbors contributes to the elimination width. Fill edges are computational relationships induced by elimination, not new original graph edges, and their later effect must be retained when subsequent vertices are considered.

For a fixed eliminated subset, the remaining torso connections can be determined through paths whose internal vertices were eliminated. This lets the subset dynamic program compare complete remaining choices without treating every permutation prefix as unrelated. The completed recurrence minimizes the largest width encountered over the rest of the order.

Two size-three bags decompose a four-cycleBag: {0,1,3}Bag: {1,2,3}Shared vertices: {1,3}Maximum bag size 3 → width 2
A four-cycle has treewidth two. Eliminating one vertex joins its two neighbors, and two bags of size three can cover the cycle with a connected overlap. The largest bag size minus one is two, while no width-one forest decomposition can represent the cycle.

The Maximal clique calculator from graph edge list covers the nearby task. A clique list is not a tree decomposition; running intersection and elimination fill determine treewidth.

Bags must satisfy more than edge coverage

Each eliminated vertex and its then-remaining neighbors form a bag. The decomposition connects bags so every original edge lies together somewhere and all bags containing a particular vertex form a connected subtree. Edge coverage alone is insufficient: disconnected appearances of one vertex would violate the running-intersection property even if every edge is listed.

The calculator verifies those structural conditions after reconstructing the optimal order. It also reports parent bag indices and the elimination order, making the certificate inspectable. Several valid minimum-width decompositions can exist, so different bag arrangements are not automatically a disagreement when they share the certified width.

Exact width and heuristic upper bounds are different

A heuristic such as minimum fill can quickly produce an elimination order and an upper bound. That does not prove no better order exists. This page uses a completed bounded exact recurrence before reporting an exact width, and its method description avoids borrowing an optimum claim from a heuristic reference implementation.

Forests and cycles supply clear baseline cases: nontrivial forests have width one, and a cycle has width two. Independent small-graph tests enumerate elimination orders or compare known families. The vertex cap is deliberate because exact treewidth is computationally demanding; no claim of practical scalability to arbitrary real-world networks is made.

Understand what makes a valid tree decomposition

A tree decomposition assigns vertex bags to nodes of a tree. Every graph vertex must occur in a bag, every original edge must have both endpoints together in some bag, and the bags containing any given vertex must form a connected subtree. That last running-intersection condition prevents unrelated pieces of one variable from being separated in a dynamic program.

The width is the largest bag size minus one, and treewidth is the minimum possible width over all valid decompositions. It measures how close a graph is to a tree for certain structural algorithms; it is not the number of edges, the maximum degree or a geometric drawing width.

Verify the four-cycle certificate manually

For edges 0–1,1–2,2–3,3–0, bags {0,1,3} and {1,2,3} joined by one tree edge cover every original edge. Vertices 1 and 3 occur in both adjacent bags, while 0 and 2 occur once, so running intersection holds. Largest bag size is three, giving width two.

The cycle cannot have treewidth one because graphs of treewidth at most one are forests. Hence this width-two certificate is optimal. A path on the same four vertices has treewidth one and can use consecutive two-vertex bags. A single isolated vertex has width zero, showing why the minus-one convention matters.

Connect elimination fill to the displayed bags

Eliminating a vertex joins its remaining neighbors into a clique before removing it. Its elimination bag contains that vertex and those neighbors. The largest such neighbor count is the width of the elimination ordering. Fill edges represent dependencies introduced by elimination, not new original graph edges that must be entered by the user.

This calculator uses exact subset dynamic programming to select the best ordering for at most 12 vertices. A greedy minimum-fill ordering, such as the comparison method in the linked graph-library documentation, supplies an upper bound but does not generally prove the exact minimum. The page’s exact-search claim comes from the completed bounded optimization, not from treating that heuristic as exact.

Use treewidth to assess structure, not universal runtime

Many graph algorithms perform work exponential in bag size but roughly linear in the number of bags once a decomposition is supplied. Low treewidth can therefore help, although the exact cost depends on the problem and state representation. A width-two graph does not imply that every computation on it takes the same amount of time.

Different optimal decompositions can have different bag counts and shapes. Compare their widths and validity rather than expecting identical bag labels from another solver. For disconnected input, component decompositions can be linked without increasing the maximum width. Empty-edge graphs with at least one vertex consequently have width zero rather than requiring a fictitious edge.

A few questions, answered

Is a minimum-fill order exact treewidth?

Not necessarily. Minimum fill is a heuristic; this page completes bounded exact elimination DP.

What validates the bags?

Every original edge is covered and bags containing any one vertex form a connected subtree.

Are fill edges part of my original graph?

No. They are introduced during elimination to track remaining dependencies. Bags still certify coverage of the original graph.

Does a minimum-fill heuristic prove exact treewidth?

No. It normally supplies an upper bound. This small-graph calculator completes an exact elimination optimization before labeling its result exact.

Find your calculator

Start typing to explore the library.