Advanced maths

Graceful graph labeling calculator with a difference certificate

Search for a graceful labeling: distinct vertex labels chosen from 0 through the edge count whose edge differences are exactly 1 through that count.

Your numbers

Labels are 0 through n−1; include isolated vertices.
One row: u v. No loops or duplicate unordered 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

Search for a graceful labeling: distinct vertex labels chosen from 0 through the edge count whose edge differences are exactly 1 through that count.

THE FORMULAFor m edges, require injective f: V→{0,…, m} and {|f(u)−f(v)|: uv∈E}={1,…, m}.

A worked example

For the four-edge star, label its center 0 and its leaves 1,2,3,4. The four edge differences are 1,2,3,4, so this is a graceful labeling.

Good to know

1–9 vertices; at most 12 distinct undirected edges. Exact backtracking with a two-million-state cap; a cap error does not prove nonexistence.

What makes a labeling graceful

A graceful labeling assigns different integers to vertices so every edge receives a different positive endpoint difference. With m edges, the allowed vertex labels are zero through m and the edge differences must use each integer from one through m exactly once. Both conditions are essential to the definition.

Vertex labels need not use every allowed integer when the graph has fewer than m plus one vertices. Edge differences, however, cannot skip a number. A drawing with distinct vertex labels alone is insufficient, and a collection of distinct edge sums would be a different labeling problem rather than a graceful witness.

A four-edge star is graceful01234Allowed values: 0…4No vertex value repeatsDifferences: 1,2,3,4
Scroll across to see the full diagram.
Center label 0 and leaf labels 1–4 produce differences 1–4 exactly once.

Compare this with the Graph isomorphism calculator for two adjacency matrices. Find an injective vertex labeling in0…m whose absolute edge differences are exactly1…m, rather than relabel adjacency to test isomorphism.

Entering the graph rather than proposed labels

Supply the vertex count and one unordered endpoint pair per edge row. Labels in this input identify vertices, not the integer values that the search will assign. The output label array uses the same vertex order, letting you translate the found integer assignment back onto your original drawing.

The graph is simple and undirected. Loops would have difference zero and are rejected, while duplicate unordered edges would repeat a difference and are also rejected. Include isolated vertices in the declared count: they still require distinct vertex labels even though they contribute no edge differences to the graceful condition.

The default star gives a direct witness

The example contains a center joined to four leaves, so m equals four and the allowed integers are zero through four. Assign zero to the center and one, two, three and four to its leaves. Each edge then has exactly its leaf's label as its absolute difference.

The difference list is therefore one, two, three and four without repetition. Five distinct integers are assigned to five vertices, and every required edge value appears once. This example makes verification especially transparent, but a general graph may require backtracking because choosing one endpoint value can constrain several incident differences simultaneously.

Backtracking over injective assignments

The implementation labels vertices in decreasing degree order, which exposes more edge constraints early. At each step it tries an unused allowed integer, evaluates edges to already labeled neighbors and immediately rejects repeated differences. A completed assignment is accepted only when all m required edge differences have been accounted for.

Replacing every assigned value f(v) by m−f(v) preserves each absolute difference. The search uses this reflection symmetry to reduce equivalent branches at its first vertex. This is a safe reduction of duplicate work, rather than a change to the mathematical problem or a guarantee that the displayed witness is unique.

How to verify the output yourself

Read the output array as one assigned integer per input vertex. Check that every value lies between zero and m and that no value repeats. Next, compute the absolute endpoint difference for every original edge. Sorting those m values must produce the consecutive list one through m.

A positive result supplies this directly checkable certificate. Its order can differ from a textbook example without changing validity. Many graphs admit more than one graceful labeling, and reflections already generate paired witnesses in most nontrivial cases. The calculator returns one successful assignment rather than enumerating all assignments or counting their symmetry classes.

Interpreting a negative result carefully

If there are more than m plus one vertices, injectivity is impossible immediately because there are not enough allowed labels. Otherwise a completed exhaustive search can establish that this particular finite input has no graceful labeling. Neither conclusion should be generalized to every graph of the same vertex or edge count.

A resource-cap error has a separate meaning: the bounded search did not finish and has not certified existence or nonexistence. In particular, an unfinished search on a tree is not a counterexample to any general graph-labeling conjecture. The page reports the searched instance and its outcome, not a theorem about an entire family.

Scope and useful structural checks

At most nine vertices and twelve edges are accepted to keep exact search bounded. Edge weights, directed arcs, prescribed vertex values and relaxed label ranges are not supported. Adding an edge changes m and therefore changes both the allowed vertex range and the full target difference set; it is not a simple monotone modification.

As small checks, a single edge accepts endpoint labels zero and one. A graph with two isolated vertices and no edges cannot be graceful under this injective convention because only label zero is available. Relabeling input vertex identities should preserve the existence result, even though the returned assignment is rearranged or another valid witness is found.

A few questions, answered

Must all integers 0 through m appear on vertices?

No. Vertex values must be distinct within that range; every edge difference 1 through m must appear.

Does a search-cap error mean not graceful?

No. It means the bounded computation did not certify a final existence result.

Are the input vertex numbers already assigned graceful labels?

No. They identify vertices; the output array supplies the searched integer assignment.

Can isolated vertices affect existence?

Yes. They still need distinct labels from the same finite range, despite having no incident differences.

Find your calculator

Start typing to explore the library.