150 likes | 316 Views
MIS 215 Module 4 – Recursion. Where are we?. MIS215. Basic Algorithms. Introduction. List Structures. Advanced structures. Search Techniques. Intro to Java, Course. Sorting Techniques. Java lang. basics. Linked Lists. Hashtables. Binary Search. Graphs, Trees . Stacks, Queues.
E N D
Where are we? MIS215 Basic Algorithms Introduction List Structures Advanced structures Search Techniques Intro to Java, Course Sorting Techniques Java lang. basics Linked Lists Hashtables Binary Search Graphs, Trees Stacks, Queues Arrays Bubblesort Fast Sorting algos (quicksort, mergesort) Newbie Programmers Designers Developers Professionals
Today’s 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
Recursion: What is it? • It is a problem solving technique • divide and conquer • It is a programming technique • let a function call itself
1 2 3 Towers of Hanoi:A Classical Example
Towers of Hanoi:Recursive 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.
An Example with Two Disks:Trace 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) Trivial recursive call Move (0,2,1,3) 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
Designing Recursive Algorithms • Find the key step • Find a stopping rule • Outline your algorithm • Check termination • Draw a recursion tree
Examples • Factorial • Fibonacci Sequence • Tower’s of Hanoi • Linear Search in LinkList? • Binary Search in Array? • Any problem that can be defined in terms of a smaller version of itself!
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?
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
Recursively define factorial fact(n) • Base case: • Recursion:
Recursively define gcd(x, y) • Base case: • Recursion:
Recursively define find method in a linked list • find(Node n, int target) • Base case: • Recursion:
Recursively define binary search • find(int lb, int ub, int target) • Base case: • Recursion: