Recursion in Computer Science

recursion n.w
1 / 20
Embed
Share

Dive into the world of recursion in computer science with this comprehensive guide. Learn the difference between recursion and iteration, explore infinite recursion and termination conditions, understand how to apply recursion to solve problems like factorial calculations, and discover the use of helper functions in recursive algorithms.

  • Recursion
  • Computer Science
  • Algorithms
  • Programming
  • Helper functions

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Recursion CMSC 201

  2. Recursion Vs. Iteration Recursion and iteration are both methods of solving problems. An iterative iterative solution to a problem is anything involving loops. Everything we ve done so far has been iterative. Anything that can be done iteratively can be done Anything that can be done iteratively can be done recursively. recursively.

  3. Recursion Recursion is when a function calls itself. The simplest example: def foo(): foo() This is an example of infinite recursion. infinite recursion.

  4. Recursion Infinite recursion: def foo(): foo() Infinite loop: while True: pass

  5. Recursion We can get loop like behavior by passing arguments! def foo(i): print(i) foo(i-1) What does this do? How might we get it to terminate?

  6. Recursion def foo(i): if(i > 0): print(i) foo(i-1) return Is equivalent to: while a > 0: print(a) a = a + 1

  7. Recursion def foo(i): if(i > 0): print(i) foo(i-1) Termination condition, known as the base case return Recursive call, which should always take us closer to the base case. Is equivalent to: while a > 0: print(a) a = a + 1

  8. Recursion We use recursion when we have a problem that can be written in terms of a smaller version of the original problem. Take factorial, for example. N! = N * (N-1)! We can rewrite factorial in terms of something easy (multiplication), and a smaller factorial. One more thing! For this to work, we need to know that 1! = 1

  9. Factorial def factorial(num): if(num == 1): return 1 return num * factorial(num-1)

  10. Helper Functions Sometimes in recursion we have to pass information as arguments. When we do this, we use a helper function to keep the user from sending incorrect arguments (this will make more sense in the next slide).

  11. Helper Functions def myMax(myList): return _myMax(myList, myList[0]) def _myMax(myList, arg1): if(len(myList) == 0): return arg1 return _myMax(myList[1:], max(arg1, myList[0]))

  12. Hints For these exercises, remember: Figure out the base case Figure out a way of restating the original problem in terms of something simple, and a smaller version of the original problem. When writing your recursive call, you re allowed to assume your function works!

  13. Exercise Right a recursive function that takes a list as an argument and reverses it. def reverse(myList): #Your code here

  14. Exercise Write a recursive function that takes a list as an argument and reverses it. def reverse(myList): if(len(myList) == 1): return myList return [myList[-1]] + reverse(myList[:-1])

  15. Exercise Write a recursive function that takes two sorted lists, listA and listB, and merges them (so that they re still sorted.) So merge([1, 3, 5], [2, 4, 6, 7]) should return [1, 2, 3, 4, 5, 6 7] def merge(): #your code here

  16. Exercise def merge(listA, listB): if(len(listA) == 0): return listB if(len(listB) == 0): return listA if(listA[0] > listB[0]): return [listB[0]] + merge(listA, listB[1:]) else: return [listA[0]] + merge(listA[1:], listB)

  17. Towers Of Hanoi This one is a bit more complicated! This is the towers of Hanoi puzzle: The goal is to move the entire stack over to the peg on the far right.

  18. Towers Of Hanoi The Rules: No disk may be on top of a smaller disk Every disk must go from one peg to another (no storing it o the table)

  19. Towers Of Hanoi We want to write a function, called move, that moves the entire stack, down to whatever depth we specify, to whatever peg we wish. def hanoi(depthOfDisk, movingFrom, movingTo, otherPeg): #your code The variable otherPeg is just to keep track of the free peg we re not using. Also, you can assume the function move(from, to) moves the disk at the top of peg from to peg to

  20. Towers Of Hanoi def hanoi(depthOfDisk, movingFrom, movingTo, otherPeg): if(depthOfDisk == 1): move(movingFrom, movingTo) else: hanoi(depthOfDisk 1, movingFrom, otherPeg, movingTo) move(movingFrom, movingTo) hanoi(depthOfDisk 1, otherPeg, movingTo, movingFrom)

More Related Content