CMPT 225 Recursion-part 3. Recursive Searching Linear Search Binary Search Find an element in an array, return its position (index) if found, or -1 if.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Recursion.  Understand how the Fibonacci series is generated  Recursive Algorithms  Write simple recursive algorithms  Analyze simple recursive algorithms.
Recursion –a programming strategy for solving large problems –Think “divide and conquer” –Solve large problem by splitting into smaller problems of same.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion.
Recursive Functions The Fibonacci function shown previously is recursive, that is, it calls itself Each call to a recursive method results in a separate.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Recursion. Binary search example postponed to end of lecture.
Unit 181 Recursion Definition Recursive Methods Example 1 How does Recursion work? Example 2 Problems with Recursion Infinite Recursion Exercises.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Analysis of Recursive Algorithms
Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.
Recursion!. Can a method call another method? YES.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Recursion.  Identify recursive algorithms  Write simple recursive algorithms  Understand recursive function calling  With reference to the call stack.
Algorithm Cost Algorithm Complexity. Algorithm Cost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Introduction to Recursion by Dr. Bun Yue Professor of Computer Science 2013
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Chapter 4: Induction and Recursion
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Stephen P. Carl - CS 2421 Recursion Reading : Chapter 4.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
CSC 211 Data Structures Lecture 13
 O(1) – constant time  The time is independent of n  O(log n) – logarithmic time  Usually the log is to the base 2  O(n) – linear time  O(n*logn)
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
CMPT 225 Recursion. Objectives Understand how the Fibonacci series is generated Recursive Algorithms  Write simple recursive algorithms  Analyze simple.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Recursion Powerful Tool
Chapter 4: Induction and Recursion
RECURSION.
Computer Science 4 Mr. Gerb
Towers of Hanoi Move n (4) disks from pole A to pole C
More Computational Theory
Induction and Recursion
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
CS 3343: Analysis of Algorithms
Algorithm design and Analysis
Recursion Recursion is a math and programming tool
Module 1-10: Recursion.
Java Programming: Chapter 9: Recursion Second Edition
Lecture 11 Algorithm Design
At the end of this session, learner will be able to:
Presentation transcript:

CMPT 225 Recursion-part 3

Recursive Searching Linear Search Binary Search Find an element in an array, return its position (index) if found, or -1 if not found.

Linear Search Algorithm (Java) public int linSearch(int[] arr, int target) { for (int i=0; i<arr.size; i++) { if (target == arr[i]) { return i; } } //for return -1; //target not found }

Recursive Binary search int recLinSearch (int[] arr, int low, int x) To search the entire array for 2 index=recLinSearch(arr,0,2); low Compare x to arr[low] search this part low+1 recLinSearch(arr, low+1, x)

Recursive Linear Search Algorithm public int recLinSearch(int[] arr,int low,int x) { if (low >= arr.length) { // reach the end return -1; } else if (x == arr[low]){ return low; } else return recLinSearch(arr, low + 1, x); } Base case  Found the target or  Reached the end of the array Recursive case  Call linear search on array from the next item to the end

How many comparison does the recLinSearch perform in worst case? Suppose T(n) is the number of comparison where n is the size of the elements in the array. We have: T(0)=0 T(n)=1+T(n-1) By expanding the T(n) n times we have T(n)=1+[1+T(n-2)]=2+T(n-2)=…=k+T(n-k)=…=n+T(0)=n=O(n) Thus, the recLinSearch is not more efficient than the non- recursive linear search.

Recursive linear search int recBinSearch (int[] arr, int lower, int upper, int x) To search the whole array for 2 index=recBinSearch(arr,0, arr.length, 2); if x>arr[mid] recBinSearch(arr, mid+1, upper, x) low Compare x to arr[mid] upper mid recBinSearch(arr, low, mid-1, x) if x<arr[mid]

Recursive Binary Search Algorithm public int binSearch( int[] arr, int lower, int upper, int x) { int mid = (lower + upper) / 2; if (lower > upper) {// empty interval return - 1; // base case } else if(arr[mid] == x){ return mid; // second base case } else if(arr[mid] < x){ return binSearch(arr, mid + 1, upper, x); } else { // arr[mid] > target return binSearch(arr, lower, mid - 1, x); }

How many comparison does the recBinSearch perform in worst case? Suppose T(n) is the number of comparison where n is the size of the elements in the array, and assume n = 2 k (e.g. if n = 128, k = 7) We have: T(1)=1 T(n)=1+T(n/2) By expanding the T(n) n times we have T(n)=1+[1+T(n/2)]=2+T(n/2)=…=k+T(n/ 2 k )=k+T(1)=k+1=O(log 2 n ) Because n = 2 k, k = log 2 n

Binary Search vs Linear Search N Linear N Binary log 2 (N) , , , ,000, ,000,000 24

Processing linked list recursively. It’s possible and sometimes desirable to process linked list recursively. Eg. Displaying the elements in the list recursively. writeList(Node cur): displays the elements from cur to the end of the list … cur Print cur Node Print the rest writeList(cur.getNext()) cur.getNext()

private void writeList(Node cur){ if (cur==null) return; System.out.println(cur.getItem()); writeList(cur.getNext()); }

Writing a list backward. A non-recursive solution. private void wirteListBackward1(){ Object temp[]= new Object[size]; Node cur=head; for(int i=0; i<size; i++, cur=cur.getNext()) temp[i]=cur.getItem(); for(int i=size-1; i>=0; i--) System.out.println(temp[i]); } Needs an extra array of the same size as list.  The space complexity is O(n) –where n is the size of the array

Writing a list backward-a new solution. private void wirteListBackward2(){ for(int i=size-1; i>=0; i--){ //returns the reference to the ith element in the //list. Node cur=get(i); System.out.println(cur.getItem()); } } Does not need extra space.  The space complexity is O(1) However, it’s slow  The get(i) method requires i operations.  Therefore, the total number of operations: (n-1)+(n-2)+…+1=O(n 2 )

Writing a list backward-a recursive solution. writeListBackwardRec(Node cur): displays the elements from cur to the end of the list in a reverse order … cur Print cur Node Print this part backward writeListBackwardRec(cur.getNext()) cur.getNext()

private void writeListBackwardRec(Node cur){ if (cur == null) return; writeListBackwardRec(cur.getNext()); System.out.println(cur.getItem()); } The time complexity is O(n), therefore it’s and optimal solution in terms of time. What is the space complexity? Is it O(1)? No!!!. It requires a stack of size O(n) when it’s executed.

public void displayBackward(){ writeListBackwardRec(head); //Z } private void writeListBackwardRec(Node cur){ if (cur == null) return; writeListBackwardRec(cur.getNext()); //X System.out.println(cur.getItem()); //Y } cur Cur:1 Ret:Z Cur:6 Ret:X Cur:7 Ret:X Cur:12 Ret:X Cur:nul Ret:x cur Call Stack

The optimal solution Reverse the list. Restore the list to original order Print the reversed list. O(n): time, O(1):space Total O(n): time, O(1):space

Recursion Disadvantage 1 Recursive algorithms have more overhead than similar iterative algorithms  Because of the repeated method calls (storing and removing data from call stack)  This may also cause a “stack overflow” when the call stack gets full It is often useful to derive a solution using recursion and implement it iteratively  Sometimes this can be quite challenging! (Especially, when computation continues after the recursive call -> we often need to remember value of some local variable -> stacks can be often used to store that information.)

Recursion Disadvantage 2 Some recursive algorithms are inherently inefficient An example of this is the recursive Fibonacci algorithm which repeats the same calculation again and again  Look at the number of times fib(2) is called Even if the solution was determined using recursion such algorithms should be implemented iteratively To make recursive algorithm efficient:  Generic method (used in AI): store all results in some data structure, and before making the recursive call, check whether the problem has been solved.  Make iterative version.

Function Analysis for call fib(5) fib(5) fib(4) fib(3) fib(2) fib(1)fib(0)fib(2) fib(1)fib(0) fib(1) fib(2) fib(1)fib(0) fib(1) public static int fib(int n) if (n == 0 || n == 1) return n else return fib(n-1) + fib(n-2)

Iterative Fib solution int itrFib(int n){ int F0=0, F1=1, F2=0; for(int i=0; i<n; i++){ F2=F0+F1; F0=F1; F1=F2; } return F2; }

Generic Fib Solution int genericFib(int n){ if(n==0 || n==1) return n; if(globalFib[n]>=0) return globalFib[n]; globalFib[n]=genericFib(n-1)+genericFib(n-2); return globalFib[n]; } The globalFib is a global array that is initially set to -1.

Analyzing Recursive Functions Recursive functions can be tricky to analyze It is useful to trace through the sequence of recursive calls This can be done using a recursion tree  As shown for the Fibonacci function Recursion trees can also be used to determine the running time (in number of operations) of algorithms  Annotate the tree to indicate how much work is performed at each level of the tree  Determine how many levels of the tree there are

Recursion and Induction Recursion is similar to mathematical induction as recursion solves a problem by  Specifying a solution for the base case and  Using the recursive case to derive solutions of any size from the solutions to smaller problems Induction proves a property by  Proving it is true for a base case (which is often true by definition) and  Proving that it is true for some number, n, if it is true for all numbers less than n

Recursive Factorial Algorithm public int fact (int x){ // Should check for negative values of x if (x == 0){ return 1; } else return n * fact(n – 1); } Prove, using induction, that the algorithm returns the values:  fact(0) = 0! = 1  fact(n) = n! = n * (n – 1) * (n – 2) * … * 1 if n > 0

Proof by Induction of fact Method Basis: Show that the property is true for n = 0, i.e. that fact(0) returns 1  This is true by definition as fact(0) is the base case of the algorithm and returns 1 Now establish that the property is true for an arbitrary k implies that it is true for k + 1 Inductive hypothesis: Assume that the property is true for n = k, that is assume that  fact(k) = k * (k – 1) * (k – 2) * … * 2 * 1

Proof by Induction of fact Method Inductive conclusion: Show that the property is true for n = k + 1, i.e., that fact (k + 1) returns ((k + 1) * k * (k – 1) * (k – 2) * … * 2 * 1 By definition of the function fact(k + 1) returns ((k + 1) * fact(k) – the recursive case And by the inductive hypothesis fact(k) returns kk * (k – 1) * (k – 2) * … * 2 * 1 Therefore fact(k + 1) must return ((k + 1) * k * (k – 1) * (k – 2) * … * 2 * 1 Which completes the inductive proof