MIS 215 Module 4 - PowerPoint PPT Presentation

About This Presentation
Title:

MIS 215 Module 4

Description:

A programming style where a method directly or indirectly calls itself. ... A Classical Example. 1. 2. 3. ISOM. Towers of Hanoi: Recursive Function ... – PowerPoint PPT presentation

Number of Views:48
Avg rating:3.0/5.0
Slides: 16
Provided by: WrightStat2
Category:
Tags: mis | module

less

Transcript and Presenter's Notes

Title: MIS 215 Module 4


1
MIS 215 Module 4 Recursion
2
Where are we?
MIS215
Basic Algorithms
Introduction
List Structures
Advanced structures
Search Techniques
Intro to Java, Course
Sorting Techniques
Java lang. basics
Hashtables
Linked Lists
Binary Search
Graphs, Trees
Stacks, Queues
Arrays
Bubblesort
Fast Sorting algos (quicksort, mergesort)
Newbie
Programmers
Developers
Professionals
Designers
3
Todays buzzwords
  • Recursion
  • A data structure where items can be added to the
    top and removed from the top
  • A LIFO (Last In, First Out) Structure
  • Recursion
  • A programming style where a method directly or
    indirectly calls itself.
  • Typically leads to simpler and more elegant
    looking implementations, although not necessarily
    more efficient
  • Recursive structures
  • A data structure including a reference to itself
  • Base Case/Condition
  • The case that forms the basis from which other
    cases can be built

4
Recursion What is it?
  • It is a problem solving technique
  • divide and conquer
  • It is a programming technique
  • let a function call itself

5

Towers of HanoiA Classical Example
6

Towers of HanoiRecursive Function
int Move(int count, int start, int finish, int
temp) Pre There are at least count disks on
the tower start. The top disk (if any) on each of
towers temp and finish is larger than any of the
top count disks on tower start. Post The top
count disks on start have been moved to finish
temp has been returned to its starting position.
7
An Example with Two DisksTrace of the Function
Move (2,1,3,2)
Outer Call
Move (1,1,2,3)
First recursive call
Trivial recursive call
Move (0,1,3,2)
First instruction printed
Move disk 1 from 1 to 2
Trivial recursive Call
Move (0,3,2,1)
End of first recursive call
Second instruction period
Move disk 2 from 1 to 3
Second recursive call
Move (1,2,3,1)
Move (0,2,1,3)
Trivial recursive call
Third instruction printed
Move disk 1 from 2 to 3
Trivial recursive call
Move (0,3,1,2)
End of second recursive call
End of outer Call
8

Designing Recursive Algorithms
  • Find the key step
  • Find a stopping rule
  • Outline your algorithm
  • Check termination
  • Draw a recursion tree

9
Examples
  • Factorial
  • Fibonacci Sequence
  • Towers of Hanoi
  • Linear Search in LinkList?
  • Binary Search in Array?
  • Any problem that can be defined in terms of a
    smaller version of itself!

10
Building A recursion
  • Say, I want you to write a recursion for finding
    power(x, n) i.e, find the nth power of x (xn)
  • What is x0?
  • Can you write x2 in terms of x?
  • Can you write x3 in terms of x2?
  • Can you write x25 in terms of x24?
  • So, can you write xn in terms of xn-1?

11
So, to find recursion
  • Find the base case
  • Try out a few small examples from the base case
    building on top of another
  • Now try to generalize

12
Recursively define factorial fact(n)
  • Base case
  • Recursion

13
Recursively define gcd(x, y)
  • Base case
  • Recursion

14
Recursively define find method in a linked list
  • find(Node n, int target)
  • Base case
  • Recursion

15
Recursively define binary search
  • find(int lb, int ub, int target)
  • Base case
  • Recursion
Write a Comment
User Comments (0)
About PowerShow.com