33
Good morning!

Good morning!. Recursive Algorithms Dr. Jeyakesavan Veerasamy [email protected]

Embed Size (px)

Citation preview

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

Good morning!

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

Recursive Algorithms

Dr. Jeyakesavan [email protected]

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

Example: Gift box!

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

Example: Gift box!

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

Example: Gift box!

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

Value of gift box : Equation?

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

Example: Treasure hunt!

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

What is recursion?

• Popular in math definitions• Inductive proof

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

Example: factorial(n)

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

Example: factorial(n)

• Non-recursive solution

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

How does recursion work?

• Stack memory• How much stack memory is needed?

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

How to understand recursion?

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

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

How to understand recursion?

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

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

How to understand recursion?

• Tree diagram• Example: fibonacci(n)

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

fibonacci(n) : non-recursive solution?

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

How to analyze recursion?

• Recurrence relation & Time complexity

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

Example: Hanoi tower

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

Example: Fractals

Von_Koch_curve.gif

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

Petrol cost minimization problem

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

Similar solution: Quick-sort

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

Quick-sort: non-recursive solution?

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

Similar solution: Merge-sort

Page 25: Good morning!. 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 morning!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu
Page 27: Good morning!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

How to make recursion efficient?

• Parameters• Tail recursion

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

Example: N-Queens puzzle

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

Example: Knight’s tour

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

Example: Sudoku’s puzzle

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

Example: Maximizing total conviviality

Page 32: Good morning!. 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 33: Good morning!. Recursive Algorithms Dr. Jeyakesavan Veerasamy jeyv@utdallas.edu

Questions & Answers?