My company's cat-herding application tracks a convoy of cats. Periodically, it needs to compare previousOrder
to currentOrder
(each is an ArrayList<Cat>
) and notify the cat-wranglers of any changes.
Each cat is unique and can only appear once in each list (or not at all). Most of the time, the previousOrder
and currentOrder
lists have the same contents, in the same order, but any of the following can happen (from more frequent to less frequent):
- The order of cats is scrambled completely
- Cats individually move up or down in the list
- New cats join, at a specific point in the convoy
- Cats leave the convoy
This appears like an edit distance problem to me. Ideally, I am looking for an algorithm that determines the steps required to make previousOrder
match currentOrder
:
- MOVE
Fluffy
to position12
- INSERT
Snuggles
at position37
- DELETE
Mr. Chubbs
- etc.
The algorithm should also recognize scenario #1, in which case the new order is communicated in its entirety.
What's the best approach for this?
(This post and that post pose similar questions, but they are both dealing with sorted lists. Mine are ordered, but unsorted.)
EDIT
The Levenshtein algorithm is a great suggestion, but I'm concerned about the time/space requirement of creating a matrix. My main goal is to determine and communicate the changes as quickly as possible. Something that is faster than finding the additions and sending message along the lines of "Here are the new cats, and here is the current order."