1

How can I implement the transpose/swap/twiddle/exchange distance alone using dynamic programming. I must stress that I do not want to check for the other operations (ie copy, delete, insert, kill etc) just transpose/swap.

I wish to apply Levenstein's algorithm just for swap distance. How would the code look like?

Vlad Otrocol
  • 2,952
  • 7
  • 33
  • 55

2 Answers2

1

I'm not sure that Levenstein's algorithm can be used in this case. Without insert or delete operation, distance is good defined only between strings with same length and same characters. Examples of strings that isn't possible to transform to same string with only transpositions:

AB, ABC
AAB, ABB

With that, algorithm can be to find all possible permutations of positions of characters not on same places in both strings and look for one that can be represent with minimum number of transpositions or swaps.

Ante
  • 5,350
  • 6
  • 23
  • 46
1

An efficient application of dynamic programming usually requires that the task decompose into several instances of the same task for a shorter input. In case of the Levenstein distance, this boils down to prefixes of the two strings and the number of edits required to get from one to the other. I don't see how such a decomposition can be achieved in your case. At least I don't see one that would result in a polynomial time algorithm.

Also, it is not quite clear what operations you are talking about. Depending on the context, a swap or exchange can mean either the same thing as transposition or a replacement of a letter with an arbitrary other letter, e.g. test->text. If by "transpose/swap/twiddle/exchange" you try to say just "transpose", than you should have a look at Counting the adjacent swaps required to convert one permutation into another. If not, please clarify the question.

Community
  • 1
  • 1
Qnan
  • 3,714
  • 18
  • 15
  • I only used all names I found on the same operation in different places. CLRS calls it twiddle or exchange. Thank you for the answer. – Vlad Otrocol Sep 04 '12 at 00:21