/
Recursion How do we achieve the Recursion How do we achieve the

Recursion How do we achieve the - PowerPoint Presentation

myesha-ticknor
myesha-ticknor . @myesha-ticknor
Follow
344 views
Uploaded On 2019-12-13

Recursion How do we achieve the - PPT Presentation

Recursion How do we achieve the complexity promised Need a new tool recursive algorithms An algorithm which calls itself Example recursive computation the Fibonacci sequence The nth term is defined as the sum of the two previous ones ID: 770199

recursive return post hanoi return recursive hanoi post number pow pos algorithm search str towers exponentiation sourcepost num disk

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Recursion How do we achieve the" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Recursion How do we achieve the complexity promised?Need a new tool: recursive algorithmsAn algorithm which calls itselfExample recursive computation: the Fibonacci sequenceThe n’th term is defined as the sum of the two previous onesThe first is 1, the second is 1Formally:  

Recursion In C++, a recursive solution to compute the n’th Fibonacci number:int fib (int n){ if (n == 1 || n == 0){ return 0; } return fib(n) + fib(n-1);}

Recursion Example: Given a string and a character, count the number of times that character is contained in the stringint countOccurrences(string str, string search, int pos){ if (str.length() <= pos) return 0; if (str[pos] == search) return 1+countOccurrences( str , search, pos + 1); else return countOccurrences ( str , search, pos + 1); }

Recursive Running time For a recursive function F: where ; And Write — go until  

Recursive exponentiation Goal: compute for Algorithm pow(x,n):If (n == 0) return 1;If (n == 1) return x;Else:If (n % 2 == 0)Return pow(x, n/2) * pow(x, n/2)ElseReturn pow(x, (n-1)/2) * pow(x, (n-1)/2) * x 

Exponentiation runtime “Unroll” First steps: After steps: Stops when so goes until the first integer such that N on-recursive expression is:  

Fast exponentiation Reduce the number of “parallel” recursions:Algorithm fastPow(x, n):If (n == 0) return 1;If (n == 1) return x;Else:If (n % 2 == 0)return fastPow(x*x, n/2); Elsereturn x * fastPow(x*x, (n-1)/2);

Fast Exponentiation New recursive runtime: Unrolling, we get: We can compute using only multiplications! Unfortunately larger numbers take longer to multiply together, so the “true” runtime is a bit more complicated To express in binary takes bitsBinary multiplication over bits takes where is a constant ( by standard high-school methods)Better: Schönhage–Strassen algorithm (‘71): Best known: Fürer’s algorithm (‘07): Total time (with “slow” multiplication):  

Example: Binary SeaRch Can be defined recursively!How?

Towers of Hanoi (Very) simple game:Given three posts, one with a “pyramid” of disksTask: move the pyramid to another post while obeying the following rules:Cannot move more than one at onceCannot put a disk on top of one which is smaller

Towers of Hanoi

Towers of Hanoi How can we come up with a general solution? Recursively!Idea: Call starting post A, middle post B, and goal post CRecursive idea: If we can move the first n-1 disks to post B, using C as the temporary post, we can put the base on C.Then we can move the next n-2 disks from B back to A using C as the temp storage (because the largest disk is there, we won’t break the size rule)Repeat, moving the next top to whichever post is still empty

Towers of Hanoi Recursive algorithmFunction hanoi(num, sourcePost, destPost, tempPost):If num > 0:hanoi(num - 1, sourcePost, tempPost, destPost)// Take remaining disk from sourcePost and put on destPosthanoi(num – 1, tempPost, destPost, sourcePost)

Towers of Hanoi Number of disk moves:Total: Legend: based on a monastery which has been playing the game with 64 disksWould take about 10^19 moves (number of insects on the earth)