Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy [email protected].
-
Upload
neal-joseph -
Category
Documents
-
view
225 -
download
0
Transcript of Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy [email protected].
![Page 1: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/1.jpg)
Good afternoon!
![Page 2: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/2.jpg)
![Page 3: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/3.jpg)
![Page 5: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/5.jpg)
Example: Gift box!
![Page 6: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/6.jpg)
Example: Gift box!
![Page 7: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/7.jpg)
Example: Gift box!
![Page 8: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/8.jpg)
Value of gift box : Equation?
![Page 9: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/9.jpg)
Example: Treasure hunt!
![Page 10: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/10.jpg)
What is recursion?
• Popular in math definitions• Inductive proof
![Page 11: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/11.jpg)
Example: factorial(n)
![Page 12: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/12.jpg)
Example: factorial(n)
• Non-recursive solution
![Page 13: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/13.jpg)
How does recursion work?
• Stack memory• How much stack memory is needed?
![Page 14: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/14.jpg)
How to understand recursion?
• Method invocations & returns diagram• Example: factorial(5)
![Page 15: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/15.jpg)
How to understand recursion?
• Method invocations & returns diagram• Example: fibonacci(n)
![Page 16: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/16.jpg)
How to understand recursion?
• Tree diagram• Example: fibonacci(n)
![Page 17: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/17.jpg)
fibonacci(n) : non-recursive solution?
![Page 18: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/18.jpg)
How to analyze recursion?
• Recurrence relation & Time complexity
![Page 19: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/19.jpg)
Example: Hanoi tower
![Page 20: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/20.jpg)
Example: Fractals
Von_Koch_curve.gif
![Page 21: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/21.jpg)
Petrol cost minimization problem
![Page 22: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/22.jpg)
Similar solution: Quick-sort
![Page 23: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/23.jpg)
Quick-sort: non-recursive solution?
![Page 24: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/24.jpg)
Similar solution: Merge-sort
![Page 25: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/25.jpg)
Example: Knapsack problem
• Item weights: 40, 10, 46, 23, 22, 16, 27, 6• Instance #1: Target : 50
• Instance #2: Target: 60
• Instance #3: Target: 70
![Page 26: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/26.jpg)
How to make recursion efficient?
• Parameters• Tail recursion
![Page 27: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/27.jpg)
Example: N-Queens puzzle
![Page 28: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/28.jpg)
Example: Knight’s tour
![Page 29: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/29.jpg)
Example: Sudoku’s puzzle
![Page 30: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/30.jpg)
Example: Maximizing total conviviality
![Page 31: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/31.jpg)
Conclusions• Recursion is one of the difficult concepts to
understand, perhaps it is not that intuitive.• As per a few mathematicians & CS folks, it is
one of the most beautiful concepts!• While it is not used much in commercial
applications, it certainly puts your logical thinking skills to work!
• It is easy to remove tail recursion, but all others are lot harder to remove.
![Page 32: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu.](https://reader036.fdocuments.net/reader036/viewer/2022062519/5697bfed1a28abf838cb9102/html5/thumbnails/32.jpg)
Questions & Answers?