good afternoon!. recursive algorithms dr. jeyakesavan veerasamy [email protected]

32
Good afternoon!

Upload: neal-joseph

Post on 18-Jan-2016

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Good afternoon!

Page 2: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu
Page 3: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu
Page 4: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Recursive Algorithms

Dr. Jeyakesavan [email protected]

Page 5: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Gift box!

Page 6: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Gift box!

Page 7: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Gift box!

Page 8: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Value of gift box : Equation?

Page 9: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Treasure hunt!

Page 10: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

What is recursion?

• Popular in math definitions• Inductive proof

Page 11: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: factorial(n)

Page 12: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: factorial(n)

• Non-recursive solution

Page 13: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

How does recursion work?

• Stack memory• How much stack memory is needed?

Page 14: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

How to understand recursion?

• Method invocations & returns diagram• Example: factorial(5)

Page 15: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

How to understand recursion?

• Method invocations & returns diagram• Example: fibonacci(n)

Page 16: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

How to understand recursion?

• Tree diagram• Example: fibonacci(n)

Page 17: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

fibonacci(n) : non-recursive solution?

Page 18: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

How to analyze recursion?

• Recurrence relation & Time complexity

Page 19: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Hanoi tower

Page 20: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Fractals

Von_Koch_curve.gif

Page 21: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Petrol cost minimization problem

Page 22: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Similar solution: Quick-sort

Page 23: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Quick-sort: non-recursive solution?

Page 24: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Similar solution: Merge-sort

Page 25: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

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

How to make recursion efficient?

• Parameters• Tail recursion

Page 27: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: N-Queens puzzle

Page 28: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Knight’s tour

Page 29: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Sudoku’s puzzle

Page 30: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Example: Maximizing total conviviality

Page 31: Good afternoon!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

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

Questions & Answers?