good morning!

33
Good morning!

Upload: ide

Post on 20-Feb-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Good morning!. Recursive Algorithms. Example: Gift box!. Example: Gift box!. Example: Gift box!. Value of gift box : Equation?. Example: Treasure hunt!. What is recursion?. Popular in math definitions Inductive proof. Example: factorial(n). Example: factorial(n). - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Good  morning!

Good morning!

Page 2: Good  morning!
Page 3: Good  morning!
Page 4: Good  morning!

Recursive Algorithms

Page 5: Good  morning!

Example: Gift box!

Page 6: Good  morning!

Example: Gift box!

Page 7: Good  morning!

Example: Gift box!

Page 8: Good  morning!

Value of gift box : Equation?

Page 9: Good  morning!

Example: Treasure hunt!

Page 10: Good  morning!

What is recursion?

• Popular in math definitions• Inductive proof

Page 11: Good  morning!

Example: factorial(n)

Page 12: Good  morning!

Example: factorial(n)

• Non-recursive solution

Page 13: Good  morning!

How does recursion work?

• Stack memory• How much stack memory is needed?

Page 14: Good  morning!

How to understand recursion?

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

Page 15: Good  morning!

How to understand recursion?

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

Page 16: Good  morning!

How to understand recursion?

• Tree diagram• Example: fibonacci(n)

Page 17: Good  morning!

fibonacci(n) : non-recursive solution?

Page 18: Good  morning!

How to analyze recursion?

• Recurrence relation & Time complexity

Page 19: Good  morning!

Example: Hanoi tower

Page 20: Good  morning!

Example: FractalsVon_Koch_curve.gif

Page 21: Good  morning!

Petrol cost minimization problem

Page 22: Good  morning!

Similar solution: Quick-sort

Page 23: Good  morning!

Quick-sort: non-recursive solution?

Page 24: Good  morning!

Similar solution: Merge-sort

Page 25: Good  morning!

Example: Knapsack problem

Page 26: Good  morning!

Example: Change for $ 100

• Available denominations: 1c, 5c, 10c, 25c, $1, $2, $5, $10, $20, $50, $100

• How many combinations are there?

Page 27: Good  morning!

How to make recursion efficient?

• Parameters• Tail recursion

Page 28: Good  morning!

Example: N-Queens puzzle

Page 29: Good  morning!

Example: Knight’s tour

Page 30: Good  morning!

Example: Sudoku’s puzzle

Page 31: Good  morning!

Example: Maximizing total conviviality

Page 32: Good  morning!

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!

Questions & Answers?