How this calculation works
Find the smallest possible maximum red degree in a complete contraction sequence of a small graph, with an exact partition witness.
tww(G)=min_sequences max_steps max_bags redDegree(bag); a bag pair is red exactly when its original cross-adjacencies contain both 0 and 1.A worked example
For the path 1–2–3–4, merge {1,2}, then {3,4}, then the remaining bags. The maximum red degree is 1, so twin-width is 1.
Good to know
Simple undirected graphs with 1–8 vertices. Exact memoized search over partitions of original vertices; black/absent/red relations recomputed from original adjacencies, subset-partition minimax recurrence. At most 1,000,000 states. Return optimal merge sequence, every red-degree profile and reconstruction check. A cap yields unresolved, never an exact best-found value.
Reference: Bonnet et al., Twin-width VI: the lens of contraction sequences (opens in a new tab) ↗
Enter the graph before contracting
Supply a symmetric zero-diagonal adjacency matrix with entries zero or one. Row labels identify the original vertices throughout the witness, so a later bag such as {1,2} still refers to the same two original rows. Isolated vertices are permitted; self-loops and weighted edges belong to different models.
The calculator accepts one through eight vertices because it searches all relevant partitions rather than following a single promising merge. A graph drawing can help transcription, but geometric distance and crossing edges have no role. Check the matrix against the drawing before interpreting a contraction certificate.
Compare this with the Exact graph rank width calculator with a decomposition tree. Minimize the largest red degree during successive vertex contractions. Existing rank-width optimizes GF(2) cut ranks in a binary decomposition tree; it does not construct trigraph states or evaluate contraction width.
What a red edge records
Two current bags have a black relation when every original vertex in one is adjacent to every original vertex in the other. They have no relation when all these cross pairs are nonedges. A red relation records the remaining mixed case: both adjacency and nonadjacency occur across the bags.
Red is therefore a record of lost uniformity, rather than an edge deleted from the original graph. The red degree of a bag counts its red neighbors, with each neighboring bag counted once. A large bag can have red degree zero, while a much smaller bag can accumulate several red neighbors.
Why the maximum is minimized
Every complete sequence starts with singleton bags and finishes with one bag, merging exactly two bags at each step. Its width is the largest red degree seen at any intermediate step. Twin-width minimizes that worst value over every permitted sequence, so a locally attractive merge need not be part of an optimal solution.
The partition search recomputes relations directly from the original matrix. For a partition P, its optimum continuation is the minimum, over possible next merges, of the larger of the present maximum red degree and the continuation value. Memoization identifies sequences reaching the same partition, without assuming their future choices are different mathematical states.
Check the four-vertex path
In the default path, merging vertices 1 and 2 creates a red relation between {1,2} and {3}, because only vertex 2 meets vertex 3. The relation to {4} remains absent. Merge 3 with 4 next: the two resulting bags have mixed cross-adjacency and hence one red relation.
The last merge leaves one bag with red degree zero. This provides an upper bound of one. A width-zero sequence would require an initial twin pair with identical adjacency outside the pair, but the four-vertex path has no such pair. The positive lower bound and the explicit sequence together certify the exact value one.
Read profiles alongside the diagram
The illustration shows the path and a contraction with one red relation. Its purpose is to distinguish a mixed bag relation from an ordinary edge. The returned sequence provides the full evidence for your own input: inspect bag memberships, relation colors and the red-degree profile after each merge.
The largest number anywhere in those profiles must equal the reported sequence width. Reconstructing each bag as a set of original labels provides a second check: bags must remain disjoint and cover the vertex set. A final single bag is essential; stopping after a convenient intermediate partition does not certify twin-width.
Exactness and the search cap
A completed minimax search proves the optimum within the stated input model. The one-million-state cap is a resource boundary. If reached, a promising partial sequence does not establish the minimum, and the result must remain unresolved. Increasing the cap is a different computation, not a rounding adjustment.
Complete and edgeless graphs have value zero, as do several less obvious families. Disconnected input is legal, and relabeling vertices cannot change the answer. These facts are useful checks when experimenting, but they do not replace exhaustive minimization on a graph whose structure does not supply an independent proof.
Use the certificate for comparisons
Twin-width measures how uniformly a graph can be compressed under vertex merging. It does not count the fewest edge deletions, minimize a tree decomposition, or find a minimum coloring. Two graphs with the same vertex and edge counts can have different contraction behavior because the placement of neighborhoods matters.
When comparing graphs, retain the original adjacency matrices and optimal sequences together. A reported value without a sequence is harder to audit, while a sequence alone supplies only an upper bound unless optimality is justified. The partition recurrence bridges these two roles by producing a witness and proving that no narrower continuation was overlooked. In particular, two merge orders can reach the same partition with identical future possibilities.
A few questions, answered
Can I merge nonadjacent vertices?
Yes. Any two current bags may merge; adjacency does not restrict candidate contractions.
Does red degree count original edges?
No. It counts neighboring bags with mixed original cross-adjacency.
What does unresolved mean?
The search cap was reached, so the calculator has not certified the optimal width.
Why is a complete graph width zero?
Every pair of bags has uniformly present cross-adjacency throughout every contraction.