11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 1 Class 6 - Recursion on Lists r More recursive methods on lists.

Slides:



Advertisements
Similar presentations
Chapter 14 Recursion Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,, E. Reingold.
Advertisements

18/2/00SEM107 - © Kamin & Reddy Class 7 - LineList - 1 Class 7 - Line Drawings  The LineList data type r Recursive methods to construct line drawings.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
RECURSIVE FUNCTIONS. A recursive function is a function that calls itself. It will normally have two parts: 1. A basis for sufficiently small arguments.
21/3/00SEM107- Kamin & ReddyClass 15 - Recursive Sorting - 1 Class 15 - Recursive sorting methods r Processing arrays by recursion r Divide-and-conquer.
Finding the Median Algorithm : Design & Analysis [11]
Recursion. Recursive Definitions A recursive definition is one which uses the word being defined in the definition Not always useful:  for example, in.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion Introduction to Computing Science and Programming I.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Recursion Review.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
24/3/00SEM107 - © Kamin & ReddyClass 16 - Searching - 1 Class 16 - Searching r Linear search r Binary search r Binary search trees.
Recursion and Dynamic Programming. Recursive thinking… Recursion is a method where the solution to a problem depends on solutions to smaller instances.
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
Telecooperation/RBG Technische Universität Darmstadt Copyrighted material; for TUD student use only Introduction to Computer Science I Topic 8: Accumulating.
21/3/00SEM107 - © Kamin & ReddyClass 14 - Sorting - 1 Class 14 - Review: Sorting & searching r What are sorting and searching? r Simple sorting algorithms.
2/5/00SEM107 © Kamin & Reddy Review -1 Class 19 - Review r This lecture contains a selection of slides from previous lectures, giving the “high points”
Recursive Data Structures and Grammars  Themes  Recursive Description of Data Structures  Recursive Definitions of Properties of Data Structures  Recursive.
22/2/00 SEM107 © Kamin & Reddy Class 8 - Hilbert’s curve - 1 Class 8 - Recursive Pictures r Recursively-defined curves r The Hilbert curve.
Recursion on Lists Lecture 5, Programmeringsteknik del A.
CSC5101 Advanced Algorithms Analysis
LINKED LIST’S EXAMPLES Salim Malakouti. Linked List? 523 Pointer Node ValuePointer.
Winter 2006CISC121 - Prof. McLeod1 Stuff No stuff today!
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursion.
1 Artificial Intelligence CS370D Prolog programming List operations.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Introduction to Algorithm Analysis Concepts Fundamental Data Structures and Algorithms Aleks Nanevski and Margaret Reid-Miller January 15, 2003.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
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.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
CMPT 238 Data Structures More on Sorting: Merge Sort and Quicksort.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Recursion repetition without loops. Recursion  definition solving a problem in terms of a smaller version of itself  form of a recursive solution: base.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
8/2/00SEM107- © Kamin and ReddyClass 5 - Lists - 1 Class 5 - Lists r The list data type r Recursive methods on lists.
Sections 4.1 & 4.2 Recursive Definitions,
Sorting Chapter 14.
Class 2 – Recursion on Lists
MSc/ICY Software Workshop , Semester 2
CS 550 Programming Languages Jeremy Johnson
Introduction to Computing Science and Programming I
CSE 341 Lecture 5 efficiency issues; tail recursion; print
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CS 270 Math Foundations of CS Jeremy Johnson
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
PROGRAMMING IN HASKELL
CS148 Introduction to Programming II
Recursion Chapter 12.
Recursion.
ITEC324 Principle of CS III
Presentation transcript:

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 1 Class 6 - Recursion on Lists r More recursive methods on lists

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 2 Recursion on lists (review) Writing recursive methods on lists follows same principle as for integers: m To compute f(L), assume f(L’) can be calculated for lists L’ smaller than L, and use f(L’) to calculate f(L). m Some lists are small enough for f to be calculated directly

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 3 Example: joining lists Given lists L and M, create a list consisting of the elements of L followed by the elements of M.  Assume you can append the tail of L and M (i.e. append(L.tl(), M) ); how can you append L and M ?

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 4 Example: joining lists (cont.) static IntList append (IntList L, IntList M) { if (L.empty()) return M; else return IL.cons(L.hd(), append(L.tl(), M)); }

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 5 Example: addtoend Given list L and integer i, construct a list that contains the elements of L with i at the end. static IntList addtoend (IntList L, int i) { return append(L, IL.cons(i, IL.nil)); }

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 6 Example: finding maximum element Find the maximum integer in a non-empty list. m Assume you can find the maximum element of the tail (if the tail is non-empty)

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 7 Example: finding maximum element (cont.) static int max (IntList L) { if ((L.tl().empty())) return L.hd(); else { int m = max(L.tl()); return (L.hd() > m) ? L.hd(): m; }

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 8 Example: list reversal Given list L, construct the reversal of L. m Assume you can reverse the tail of L. How can you place the head of L in the reversal of the tail of L so as to get the reversal of L itself?

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 9 Example: list reversal (cont.) static IntList reverse (IntList L) { if (L.empty()) return L; else return addtoend(reverse(L.tl()), L.hd()); }

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 10 Analysis of list reversal reverse is rather slow. Consider how many method calls it makes in relation to the size of its argument. Note that addtoend(L) calls itself recursively n times, where n is the length of L. How many method calls does reverse(L) make?

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 11 Analysis of list reversal (cont.)  After recursive call to itself, it calls addtoend, on a list of length n-1. So this makes n-1 calls to addtoend.  Now consider the first recursive call. It in turn has a recursive call; after that inner recursive call, there is a call to addtoend, which entails n-2 calls to addtoend.  Continuing in this way, there is a call to addtoend that involves n-3 calls, one involving n-4 calls, etc.

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 12 Analysis of list reversal (cont.) Thus, the total number of calls to addtoend is (n-1) + (n-2) + … + 1, which equals n(n-1)/2  n 2. This is a much larger number than n itself.

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 13 Faster list reversal To avoid the quadratic cost of reversal, we can define another version, which we will call rev (just to distinguish it). The important trick is that rev uses an auxiliary (helper) method rev1 with type IntList rev1 (Intlist L, IntList M) rev1(L,M) is defined to return the reversal of L appended to M.

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 14 Faster list reversal (cont.) rev1 can be defined recursively. The key observation is this one: Placing the reversal of L onto the front of M is the same thing as placing the reversal of the tail of L onto cons(hd(L), M). For example, if we want to place the reversal of 1,2,3 onto the front of list 4,5 - obtaining 3,2,1,4,5 - we can just as well place the reversal of 2,3 onto the front of 1,4,5.

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 15 Faster list reversal (cont.) static IntList rev1 (IntList L, IntList M) { if (L.empty()) return M; else return rev1(L.tl(), IL.cons(L.hd(), M)); } This observation leads to: Note that this requires only n method calls, where n is the length of L.

11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 16 Faster list reversal (cont.) We can easily program rev once we’ve got rev1. static IntList rev (IntList L) { return rev1(L, IL.nil); }