# Edit distance between two partitions

I have two partitions of $[1 \ldots n]$ and am looking for the edit distance between them.

By this, I want to find the minimal number of single transitions of a node into a different group that are necessary to go from partition A to partition B.

For example the distance from {0 1} {2 3} {4} into {0} {1} {2 3 4} would be two

After searching I came across this paper, but a) I am not sure if they are taking into account the ordering of the groups (something I don't care about) in their distance b) I am not sure how it works and c) There are no references.

Any help appreciated

• What would you consider the distance to be between {0 1 2 3} and {0 1} {2 3} ? Would it be 2 ? Secondly, I don't see why "graphs" come into the picture at all. It sounds like you have two partitions of [n] and want to compute a distance between them. – Suresh Venkat May 14 '11 at 5:18
• Yes, it would be two. Indeed these are set partitions on the nodes of a graph (i.e. a graph partition). This is likely not important to the solution, but this is the problem I am trying to solve, hence why I mentioned it. – zenna May 14 '11 at 11:36
• If the graph is irrelevant, please remove all references to "graphs" and "nodes" from your question; it does not help, it distracts. – Jukka Suomela May 14 '11 at 20:57
• Can't the edit distance be defined in terms of the distance on the partition lattice? – Tegiri Nenashi May 17 '11 at 17:26
• @Tegiri - It is indeed the geodesic distance on the lattice of partititons. Unfortunately computing that lattice for any set of cardinality much greater than 10 is intractable. – zenna Aug 31 '11 at 10:16

This problem can be transformed into the assignment problem, also known as maximum weighted bipartite matching problem.

Note first that the edit distance equals the number of elements which need to change from one set to another. This equals the total number of elements minus the number of elements which do not need to change. So finding the minimum number of elements which do not change is equivalent to finding the maximum number of vertices that do not change.

Let $A = \{ A_1, A_2, ..., A_k \}$ and $B = \{ B_1, B_2, ..., B_l \}$ be partitions of $[1, 2, ..., n]$. Also, without loss of generality, let $k \ge l$ (allowed because $edit(A, B) = edit(B, A)$). Then let $B_{l+1}$, $B_{l+2}$, ..., $B_k$ all be the empty set. Then the maximum number of vertices that do not change is:

$\max_f \sum_{i=1}^k |A_i \cap B_{f(i)} |$

where $f$ is a permutation of $[1, 2, ..., k]$.

This is exactly the assignment problem where the vertices are $A_1$, ..., $A_k$, $B_1$, ..., $B_k$ and the edges are pairs $(A_i, B_j)$ with weight $|A_i \cap B_j|$. This can be solved in $O(|V|^2 \log |V| + |V||E|)$ time.

• Could you name the algorithm, which gives this time complexity please? – D-503 May 16 '11 at 3:01
• I believe @bbejot is referring to the successive shortest path algorithm (with subroutine Dijkstra's implemented using fibonacci heaps). – Wei Jul 19 '19 at 16:02
• It took me a long time to parse this because I'm not a math person, but thank you. I spent a long time searching and this was the only thing I could find that showed how to convert the partition distance problem to the assignment problem -- or to any algorithm that I could call from some a Python library. (The hard part for me has been figuring out how to use scipy.optimize.linear_sum_assignment and then to set up the matrices based on these instructions.) – Sigfried Nov 12 '19 at 11:51
• I needed to make the weights negative. Otherwise scipy.optimize.linear_sum_assignment gives me 0 for everything. – Sigfried Nov 12 '19 at 18:00
• In case anyone is still looking, I believe the Hungarian algorithm is the best in terms of complexity for the bipartite matching problem. It's what scipy.optimize.linear_sum_assignment uses. en.wikipedia.org/wiki/Hungarian_algorithm – gdavtor Aug 4 at 19:27

Look at this paper's PDF

http://www.ploscompbiol.org/article/info:doi/10.1371/journal.pcbi.0030160

The definition of edit distance in there is exactly what you need I think. The 'reference' partition would be (an arbitrary) one of your two partitions, the other would simply be the other one. Also contains relevant citations.

Best, Rob

• Thanks Rob. However, unless I am missing something, this is an edit distance defined in terms of split-merge moves. These are well studied and as the paper points out, the variation of information is a information theoretic measure of this. I am interested however, in single element move transitions. – zenna Aug 31 '11 at 12:58

Cranky Sunday morning idea that might or might not be correct:

Wlog, let $P_1$ be the partition with more sets, $P_2$ the other. First, assign pairwise different names $n_1(S) \in \Sigma$ to your sets $P_1$. Then, find a best naming $n_2(S)$ for the sets $P_2$ by the following rules:

• $n_2(S) := n_1(S')$ for $S \in P_2$ with $S \cap S'$ maximal amongst all $S' \in P_1$; pick the one creating the least conflicts if multiple choices are possible.
• If now $n_2(S) = n_2(S')$ for some $S \neq S'$, assign the one that shares less elements with $S'', n_1(S'') = n_2(S)$, the name of the set in $P_1$ it shares the second most elements with, i.e. have it compete for that set's name.
• If the former rule can not be applied, check for both sets wether they can compete for the name of other sets they share less elements with (they might still have more elements from some $S'' \in P_1$ than the sets that got assigned its name!). If so, assign that name to the one of $S, S'$ that shares more elements with the respective set whose name they can compete for; the other keeps the formerly conflicting name.
• Iterate this procedure until all conflicts are resolved. Since $P_1$ does not have less sets than $P_2$, there are enough names.

Now, you can consider the bit strings of your elements wrt either partition, i.e. $w_1 = n_1(1) \cdot \dots \cdot n_1(n)$ and $w_2 = n_2(1) \cdot \dots \cdot n_2(n)$ (with $n_j(i) = n_j(S), i \in S \in P_j$). Then, the desired quantity is $d_H(w_1, w_2)$, i.e. the Hamming distance between the bit strings.