1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
CSE Lecture 3 – Algorithms I
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Dec 7, What we’ll do today Practice writing recursive specifications and functions Given a recursive problem definition Determine a proper specification.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Proving the Correctness of Algorithms Algorithm Design and Analysis Week 2
CS102 Algorithms and Programming II1 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive.
Recursion. Binary search example postponed to end of lecture.
Recursion Chapter 11 Chapter 11.
Lecture 12 Oct 13, 2008 Some simple recursive programs recursive problem solving, connection to induction Some examples involving recursion Announcements.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Recursion. Recursive Solutions Recursion breaks a problem into smaller identical problems – mirror images so to speak. By continuing to do this, eventually.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Fundamental in Computer Science Recursive algorithms 1.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Building Java Programs Chapter 13 Searching reading: 13.3.
CompSci 105 SS 2005 Principles of Computer Science Lecture 4 Lecturer: Santokh Singh.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
COSC 2006 Data Structures I Recursion II
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(
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 2: Recursion: The Mirrors.
Computer Science Searching & Sorting.
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
MS 101: Algorithms Instructor Neelima Gupta
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
COMP Recursion, Searching, and Selection Yi Hong June 12, 2015.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Week 6 - Monday.  What did we talk about last time?  Exam 1!  Before that:  Recursion.
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
1 Lecture 18: Selection Sort, Quicksort & Merge Sort Lecturer: Santokh Singh CompSci 105 SS 2005 Principles of Computer Science.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Selection Sort main( ) { int a[ ] = { 17, 6, 13,12, 2 } ; int i, j, t ; for ( i = 0 ; i
Basic Design Techniques iteration versus recursion divide-and-conquer an example: merge sort.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
Ramamurthy Recursion: The Mirrors B. Ramamurthy CS114A,B.
1 CS1110 Lecture 16, 26 Oct 2010 While-loops Reading for next time: Ch (arrays) Prelim 2: Tu Nov 9 th, 7:30-9pm. Last name A-Lewis: Olin 155 Last.
CS 367 Introduction to Data Structures Lecture 11.
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
CORRECTNESS ISSUES AND LOOP INVARIANTS Lecture 8 CS2110 – Fall 2014.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Data Abstraction and Problem Solving with JAVA Walls and Mirrors Frank M. Carrano and Janet J. Prichard © 2001 Addison Wesley Data Abstraction and Problem.
Lecturer: Santokh Singh
Recursion CENG 707.
Recursion CENG 707.
Data Structures in Java with JUnit ©Rick Mercer
Binary Search one reason that we care about sorting is that it is much faster to search a sorted list compared to sorting an unsorted list the classic.
CS 3343: Analysis of Algorithms
Searching: linear & binary
Linear Search Binary Search Tree
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Data Structures & Programming
Presentation transcript:

1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great complexity” - Textbook, p. 49 CompSci 105 SS 2005 Principles of Computer Science Lecture 7: Searching via Recursion Lecturer: Santokh Singh Assignment 1 due Today!!! Date:

2 public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Textbook, p n = 0 A: fact(n-1)= ? return= ? n= 1 A: fact(n-1)= ? return= ? n = 3 A: fact(n-1)= ? return= ? n= 2 A: fact(n-1)= ? return= ? A A A A

3 Proving recursive algorithms 1.Prove each base cases works 2.Prove each recursive case works* 3.Prove all recursive calls meet a base case public static int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact( n-1 ); } Proof by induction Textbook, p

4 Invariants public static int fact( int n ) { if ( n == 0 ) { return 1; } else { // Invariant: return n * fact( n-1 ); } Textbook, p. 56

5 Reversing a String Textbook, p. 59ff god “ ” If string is empty do nothing Otherwise, Output last character Reverse substring of first (n-1) characters

6 //General Description: Converts a decimal number to // Binary representation. //Precondition: Input is a positive decimal // number. //Postcondition: Returns Binary representation // of input. // private static String binVal(long decVal){ String retString = ""; if(decVal / 2 > 0) retString = binVal(decVal / 2); retString += (decVal % 2); return retString; } Recursive Decimal to Binary Conversion by student “Charlie” during his CS 105 here

7 Loop invariants Is there something that we want to be true every time the while test is executed? // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; } Textbook, p. 10

8 sum contains the sum of the first j items j is in the range 0..n-1 Textbook, p. 10 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; } Loop Invariants:

9 sum contains the sum of the first j items j is in the range 0..n-1 Textbook, p. 10 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j < n) { sum += item[j]; j++; } Loop Invariants: 31 item: j: n: 2 01 sum:

10 sum contains the sum of the first j items j is in the range 0..n Textbook, p. 10 // Computes the sum of // the first n items. int sum = 0; int j = 0; while (j <= n) { sum += item[j]; j++; } Loop Invariants: an error

11 Searching in a sorted array Binary search Searching in an unsorted array Finding the smallest item Finding the k th smallest item

12 Divide and Conquer Search Phone Directory Search 1st Half of Phone Directory Search 2nd Half of Phone Directory Textbook, p. 50

13 Binary Search Search Phone Directory Search 1st Half of Phone Directory Search 2nd Half of Phone Directory Textbook, pp. 77ff

14 bSearch( anArray, value) if (anArray is of size 1) Is anArray’s item = to value? else Find midpoint of anArray Which half of anArray has value? bSearch( halfOfAnArray, value) Binary Search Algorithm

15 bSearch( anArray, value) if (anArray is of size 1) Is anArray’s item = to value? else Find midpoint of anArray Which half of anArray has value? bSearch( halfOfAnArray, value) Binary Search Algorithm Textbook, pp. 78

anArray: value: Textbook, pp

anArray: first: last: value: Textbook, pp

anArray: first: last: mid: value: Textbook, pp

19 bSearch( anArray, value, first, last ) if (last == first) Is anArray’s item = to value? else mid = (first + last) / 2 if (value is in first half ) bSearch( anArray,value,first,mid) else bSearch( anArray,value,mid+1,last) Textbook, pp. 78

20 Searching in a sorted array Binary search Searching in an unsorted array Finding the smallest item Finding the k th smallest item

21 Unsorted array: Find Smallest Find smallest Find smallest in First half Find smallest in Second half Textbook, pp. 76

22 Unsorted array: Find k th Smallest Find k th smallest Find k th smallest in First half Find k th smallest in Second half Textbook, pp. 81ff

23 Please remember to write YOUR name and login/ID on your assignment 1 answers’ booklet.