APTED Algorithm for Tree Edit Distance

1.0.3 · maintenance · verified Thu Apr 16

APTED (All Path Tree Edit Distance) is a Python library that implements the state-of-the-art algorithm for computing the tree edit distance between two ordered, labeled trees. It is a pure Python port of the original Java implementation. The current stable version is 1.0.3, with updates primarily focusing on maintenance due to its long-standing stability.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to calculate the tree edit distance between two trees represented in bracket notation strings. It shows how to define a custom cost model for rename, insert, and delete operations, and then use the APTED class to compute the distance.

from apted import APTED
from apted.nodes import Node

# Define a custom configuration for costs (optional, unit costs are default)
class CustomConfig(object):
    def rename(self, node1, node2):
        return 0 if node1.label == node2.label else 1
    def cost_insert(self, node):
        return 1
    def cost_delete(self, node):
        return 1

# Create trees using the bracket notation string format
tree1 = Node.from_string('{a{b}{c}}')
tree2 = Node.from_string('{a{b{d}}}')

# Initialize APTED with the two trees and the custom config
apted = APTED(tree1, tree2, CustomConfig())

# Compute the tree edit distance
distance = apted.compute_edit_distance()

print(f"Tree 1: {tree1.root.to_string()}")
print(f"Tree 2: {tree2.root.to_string()}")
print(f"Edit distance: {distance}")

view raw JSON →