I’m looking at the Double Metaphone algorithm. I just want to calculate the efficiency class that the algorithm falls under. Here it is in Python. This is the most readable code I could find.
I’m not great an analyzing algorithms’ times, but I know that the while loop in this algorithm should take the most time. In this case, the program looks at each character in a string from left to right:
while pos <= last :
I would assume that this will take n steps, where n is the length of the string.
However, the algorithm has a LOT of ‘if’ and ‘elif’ statements inside of this loop; I don’t know if they would significantly affect the time. Could anyone step me through this? I’d like to work out the time efficiency with summations.
To attempt to answer my own question, I think the best efficiency of it would be a string of length 1, where it has to enter as few ‘if’ statements as possible. On the other hand, the worst case scenario is probably just an extremely long string.
Thank you!
Without looking at every condition, this looks linear to me. A simple rule with some exceptions below is that if/then statements don’t affect the asymptotic runtime of the algorithm unless they do some sort of looping or recursion. Everything I saw looks like constant time operations.
Here’s how I would approach it if I really needed to be sure. If it’s not straightforwardly dependent on the condition statement, you need to look for a few things
breakstatements and other things that alter the flow of control – this might lessen asymptotic timewhileorforstatement, this might make the algorithm take more time. Here, this would mean eitherposorlastget modified.This isn’t a comprehensive list, but it might aid you.