With respect to string manipulation and permutation, think of the string as simply a 'set' of chars. this window gets decreasing at each level. At each level, you are running a piece of code, here you are running a for loop n-i times at each level. Think about the recursion as simply a number of levels. I have given some static CallCnt variables and the table to understand each line execution in detail.Įxperts, feel free to edit my answer or comment if any of my details are not clear or incorrect, I am happy correct them.ĭownload the sample code and other samples from here Total 6 times (Factorial times) we enter into if to display the permutations. If statement's for loop repeats 'n' times to display chars from the example "ABC" i.e. So from point 2 to 4.2 total calls are 5 for each loop and total is 15 calls + main entry call = 16.Įach time loopCnt is 3 then if condition gets executed.įrom the diagram we can see loop count becoming 3 total 6 times i.e. Under each loop we are recursively calling with LpCnt + 1.Ĥ.1 When index is 1 then 2 recursive calls.Ĥ.2 When index is 2 then 1 recursive calls.In the else part in for loop we are repeating from 0 to 2 making 1 call each time.So calling with Index 0 and that is first call. Permutations method called from Main for first time.ResultString += "\nNo of Swap Calls : " + noOfSwapCalls ResultString += "\nNo of Char Display Calls : " + noOfCharDispla圜alls ResultString += "\nNo of For Loop Calls : " + noOfForLoopCalls ResultString += "\nNo of General Case Calls : " + noOfRecursiveCaseCalls ResultString += "\nNo of Base Case Calls : " + noOfBaseCaseCalls ResultString += "\nNo of Function Calls : " + noOfFunctionCalls string resultString = Permute("ABCDE".ToCharArray(), 0) string resultString = Permute("ABCD".ToCharArray(), 0) String resultString = Permute("ABC".ToCharArray(), 0) string resultString = Permute("AB".ToCharArray(), 0) string resultString = Permute("A".ToCharArray(), 0) Public static void StringPermutationsTest() Static void Swap(ref char Char1, ref char Char2) Permute(elementsList, (currentIndex + 1)) Swap(ref elementsList, ref elementsList) Static string Permute(char elementsList, int currentIndex)įor (int lpIndex = currentIndex lpIndex < elementsList.Length lpIndex++) I have written the sample in C# but easy to understand for most of the programmers. Also planning to explain the running time without focusing on Recursive Reconciliation. Though it is little old question and already answered thought of adding my inputs to help new visitors. Perhaps this visualization of the call graph with a slightly smaller string makes it more obvious how it works: For an explanation of the code I'll refer you to quasiverse's excellent answer. You have to run through the code by "hand" (using whatever tools you want - debuggers, paper, logging function calls and variables at certain points) until you understand it.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |