so embarked on coding project try create code mathematically create way depict how similar 2 strings are. on research found plenty of examples online me create code desired. have error 1 found in running of creating, calling, exponential loops. it's not infinite loop, runs , solves problems, longer strings pass method exponentially longer method runs for. code here below
public static int levenshteindistance(this string source, string target) { console.writeline("start of method"); if (source.length == 0) { return target.length; } if (target.length == 0) { return source.length; } int distance = 0; console.writeline("distance creation"); if (source[source.length - 1] == target[target.length - 1]) { distance = 0; } else { distance = 1; } console.writeline("recursive methodcall"); return math.min(math.min(levenshteindistance(source.substring(0, source.length - 1), target) + 1, levenshteindistance(source, target.substring(0, target.length - 1))) + 1, levenshteindistance(source.substring(0, source.length - 1), target.substring(0, target.length - 1)) + distance); }
so, smaller strings method runs fine however, when start pass in things addresses or long names takes long time. long in fact disbanded method entirely , wrote 1 (i'll provide if wants it's not important question) serves purpose in interest of solving problems , learning, tried figure out why 1 taking long when coded recursively. stepped through code pen , paper, in debug mode , when recursive call here
return math.min(math.min(levenshteindistance(source.substring(0, source.length - 1), target) + 1, levenshteindistance(source, target.substring(0, target.length - 1))) + 1, levenshteindistance(source.substring(0, source.length - 1), target.substring(0, target.length - 1)) + distance); }
and can work out happening solidly these parts
return math.min(***math.min(levenshteindistance(source.substring(0, source.length - 1), target) + 1, levenshteindistance(source, target.substring(0, target.length - 1))) + 1,*** levenshteindistance(source.substring(0, source.length - 1), target.substring(0, target.length - 1)) + distance); }
i tried bolden , italicize part mean, it's part '***' around it. getting part understand next part, third levenshteindistance call , it's first iteration start lose focus on recursion , how works , confusion starts. part
levenshteindistance(source.substring(0, source.length - 1), target.substring(0, target.length - 1)) + distance); }
the code gather once reaches call starts first levenshteindistance call on again , reiterating on calls performed. i'm being confused on. why call first part of recursive call again , second , causing time complete code lengthen exponentially?
note: time may not exponentially in literal sense, times running short string comparison sub-second , once strings got little longer jumper sub-second -> ~15 seconds -> 2:30 mins -> 35 minutes
2nd note: tagged infinite loop exponential loop doesn't exist , close it.
because it's recursive, not single recursion (like use treeview), puppy passes return result of 3 recursive calls!
that's why seeing exponential timing increases longer strings.
Comments
Post a Comment