Edit distance between two graphs
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Edit distance, generally known for its application in string comparison, is also an increasingly important concept in the field of graph theory. The edit distance between two graphs measures the minimum number of operations needed to transform one graph into another. These operations typically include insertion, deletion, or substitution of nodes and edges.
Technical Explanation
The edit distance is a quantitative measure that reflects the similarity or dissimilarity between two graphs. Calculating the edit distance between two graphs and involves determining the minimum cost sequence of edit operations required to convert into . Common operations include:
- Node Deletion: Removing a node and its associated edges.
- Node Insertion: Adding a new node and connecting edges.
- Node Substitution: Replacing a node with another node, potentially changing its labels but preserving its edges.
- Edge Deletion: Removing an existing edge.
- Edge Insertion: Adding a new edge between nodes.
- Edge Substitution: Changing an edge by possibly altering its label or the nodes it connects.
Graph edit distance (GED) can be formally defined using cost functions that assign a non-negative cost to each operation. The objective is to find a sequence of operations yielding the minimum total cost.
Mathematical Formulation
If is a sequence of edit operations changing into , and is the cost of an operation , the GED can be expressed as:
Complexity
Computing the exact GED is an NP-hard problem because it is essentially a problem of finding optimal matchings over graphs that can't be simplified into polynomial time. Due to its complexity, heuristic and approximate algorithms are often employed, especially for large graphs.
Examples
Example 1: Simple Graphs
Consider two simple graphs:
• Graph : A - B - C • Graph : B - C - D
Calculating the edit distance involves: • Substituting A with D: 1 substitution • Edge AG ⟶ Edge DF: 1 edge substitution
Thus, the edit distance GED = 2 changes (1 node substitution, 1 edge substitution)
Example 2: Weighted Graphs
For weighted graphs, costs can be influenced by node/edge weights. Suppose nodes have values and altering them incurs different costs:
• Graph has nodes with values and weighted edges based on distances or any specific metric. • Graph has nodes with values .
Cost functions could consider these weights. For instance, replacing a node value of 5 with 1 may be costlier than substituting 5 with 4.
Comparative Table
The following table summarizes essential aspects of calculating graph edit distances:
| Aspect | Details |
| Basic Operations | Node Deletion Node Insertion Node Substitution Edge Deletion Edge Insertion Edge Substitution |
| Metric Type | Minimum-Cost Transformation |
| Complexity | NP-hard; heuristic or approximative methods often used |
| Application Areas | Network Comparison Pattern Recognition Image Analysis Biological Networks |
| Challenges | Computational Complexity Scalability Optimal Cost Determination |
Applications
- Pattern Recognition: Edit distances are pivotal where patterns are represented as graphs, such as in handwriting and fingerprint recognition systems.
- Bioinformatics: Graph models of molecular structures and biological pathways utilize edit distances for similarity assessment.
- Network Analysis: Social networks, communication networks, and other complex infrastructures can be analyzed by comparing graph structures over time using edit distances.
Additional Considerations
• Approximation Techniques: Heuristic approaches like greedy algorithms or dynamic programming may provide near-optimal solutions.
• Data-driven Approaches: Machine learning models can be trained to predict graph distances by learning from examples of graph transformations.
In conclusion, understanding edit distances between graphs helps in various disciplines by providing a framework for measuring graph similarities and addressing challenges associated with large graph data transformations. As computational capabilities advance, more efficient algorithms for GED calculation are expected to emerge.
Related reading
- Edit Distance in Python
- Edit distance recursive algorithm -- Skiena
- Edmonds-Karp Algorithm for a graph which has nodes with flow capacities
- Effect of randomness on search results
- Editing dictionary values in a foreach loop
- Effective unique on unordered elements
- Effective queries in machine learning
- Effectively sorting when your data is distributed across different microservices

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.