Advanced maths

Graph edit distance calculator for two small graphs

Compare two small unlabeled simple graphs by minimum edit cost under an explicit correspondence model.

Your numbers

Each row: from,to; optional third weight. Empty allowed for an edgeless graph.
Rows separated by semicolons; values by commas.

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

Compare two small unlabeled simple graphs by minimum edit cost under an explicit correspondence model. The solver considers partial injective vertex maps, charges unmatched vertices and differing edges, and returns a minimum-cost mapping. Its edge-charging convention remains visible because graph-edit definitions can differ in how incident edges are treated.

THE FORMULAmin_correspondence cV·vertex_edits+cE·edge_edits.

A worked example

A three-vertex path becomes a triangle by inserting its missing endpoint edge at cost one. With all vertex and edge edit costs one, no zero-cost isomorphism exists, so graph edit distance is one.

Good to know

Graphs≤7 vertices each, exact partial injective correspondences under500,000 states. Incident-edge deletion/insertion is charged separately from vertex edits; no attributes.

Define what an edit costs

The vertex cost applies to each unmatched vertex deletion or insertion. The edge cost applies separately to each deleted or inserted edge, including edges incident to an unmatched vertex. This convention prevents a deleted high-degree vertex from removing all its incident edges for free unless the edge cost itself is zero.

Other graph-edit formulations can package incident-edge removal into a vertex operation or include attribute-substitution costs. Their numeric distances need not match this page. Compare the operation definitions before comparing results; the term graph edit distance is not enough to identify a unique cost model.

One inserted edge turns a path into a triangle012Solid: original edgesDashed: insert edgeUnit edit cost = 1
A three-vertex path becomes a triangle by inserting its missing endpoint edge at cost one. With all vertex and edge edit costs one, no zero-cost isomorphism exists, so graph edit distance is one. Diagram is schematic; labels specify the numerical values.

The Coarsest equitable graph partition calculator refines one graph by equal neighbor-count signatures. It does not find an edit correspondence or certify that two graphs are isomorphic.

Let correspondences ignore vertex names

Input labels are zero-based identifiers within each graph. A matched vertex in the first graph can correspond to any unused vertex in the second. The map is injective on matched vertices, so two different first-graph vertices cannot be merged into one second-graph vertex.

A minus-one mapping value marks a deleted vertex. Second-graph vertices not used by the map are insertions. This is an unlabeled topology comparison: vertex names do not themselves incur a substitution cost, and optional first-graph edge weights are not interpreted as attributes. Directed or attributed graphs need a different model.

Work through the path-to-triangle example

A three-vertex path has two edges, while a triangle has three. Match corresponding vertices and insert the missing edge between the path endpoints. With unit vertex and edge costs, this produces total cost one and no vertex edits.

Cost zero would require an isomorphism, which is impossible because their edge counts differ. The one-edit witness therefore meets a lower bound of one. A different correspondence can rename which triangle edge is considered inserted, but the minimum cost remains the same for these unlabeled graphs.

Understand how unmatched edges are counted

An original edge survives without charge only when both endpoints are matched and the corresponding second-graph edge exists. Otherwise it is deleted. A second-graph edge is inserted unless both its matched preimages exist and form an original edge. The two scans make vertex deletion and insertion effects explicit.

This does not charge a substitution merely because an undirected edge’s endpoints are listed in reverse order. Edges are unordered pairs. Duplicates and loops are rejected rather than treated as additional attributes or parallel links. Both declared vertex counts must include isolated vertices, since matching or editing them can affect total cost.

Read the exact partial-map search

For each first-graph vertex, the solver branches between deletion and mapping to an unused second-graph vertex. At a complete correspondence it evaluates all vertex and edge costs. The best result is retained only after the full supported search finishes, giving a minimum for the stated model.

Both graphs are limited to at most seven vertices and the search has a five-hundred-thousand-state cap. A cap error means optimality is unproven, not that no correspondence exists. The problem always permits the delete-all/insert-all correspondence at finite nonnegative costs, but that simple feasible bound is not necessarily minimum.

Interpret zero and ties carefully

With positive vertex and edge costs, a zero distance corresponds to a cost-free topology-preserving full correspondence. If an edit cost is set to zero, additional nonisomorphic graphs can also have zero cost. Therefore a zero result is not always an isomorphism certificate unless the selected costs support that interpretation.

Several minimum maps can exist because of graph symmetries or equivalent edit choices. The returned map is one witness, not an alignment uniquely determined by the data. If actual entity identities matter, a topology-only optimal correspondence may be inappropriate even though its combinatorial cost is correct.

Use the distance as a model-specific comparison

The cost depends on your chosen units and ratio of vertex to edge penalties. Increasing vertex cost can favor preserving vertices even when more edges must change. Changing penalties changes the objective; it is not simply rescaling every solution unless all costs are multiplied together by the same factor.

The calculator does not normalize distances by graph size or assign a probability that the graphs have the same source. Choose such interpretations separately and validate them against the application. A small edit distance means structural similarity under this explicit operation model, not semantic equivalence of the represented systems.

A few questions, answered

Are vertex labels forced to match?

No. It is an unlabeled topology comparison using partial injective correspondences.

Are edges incident to a deleted vertex free?

No. This page charges their deletion separately using the edge-edit cost.

Does zero always prove isomorphism?

Only under a cost convention that makes every actual edit positive. Zero-cost edits can collapse that interpretation.

Why can another library return a different distance?

Its edit operations, attribute costs or incident-edge convention may differ. Align the definitions before comparing numbers.

Find your calculator

Start typing to explore the library.