Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs.

Slides:



Advertisements
Similar presentations
Lecture 6 Sept 11, 2008 Goals for the day: Linked list and project # 1 list class in STL (section 3.3) stack – implementation and applications.
Advertisements

Introduction to Recursion and Recursive Algorithms
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Linked Lists. 2 Merge Sorted Lists Write an algorithm that merges two sorted linked lists The function should return a pointer to a single combined list.
Tree Data Structures &Binary Search Tree 1. Trees Data Structures Tree  Nodes  Each node can have 0 or more children  A node can have at most one parent.
ADA: 4. Divide/Conquer1 Objective o look at several divide and conquer examples (merge sort, binary search), and 3 approaches for calculating their.
Recursion.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
Computer Science II Recursion Professor: Evan Korth New York University.
Lecture 2 Feb 3, 2009 goals: continue recursion more examples of recursive programs.
CS 315 March 24 Goals: Heap (Chapter 6) priority queue definition of a heap Algorithms for Insert DeleteMin percolate-down Build-heap.
Lecture 2 Aug goals: Introduction to recursion examples of recursive programs.
Algorithm Design Techniques: Induction Chapter 5 (Except Sections 5.6 and 5.7)
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.
Lecture 12 Oct 15 Recursion – more examples Chapter 8 problems and solutions Cell arrays, Chapter 10.
CS 206 Introduction to Computer Science II 10 / 01 / 2008 Instructor: Michael Eckmann.
Algorithm Design Techniques: Induction Chapter 5 (Except Section 5.6)
CS 206 Introduction to Computer Science II 10 / 14 / 2009 Instructor: Michael Eckmann.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Simple Recursion. COMP104 Lecture 35 / Slide 2 Recursion: Example 0 * What does the following program do? #include using namespace std; int fac(int n){
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Recursion CITS1001.
Induction and recursion
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Review Binary Tree Binary Tree Representation Array Representation Link List Representation Operations on Binary Trees Traversing Binary Trees Pre-Order.
CS 312: Algorithm Analysis Lecture #3: Algorithms for Modular Arithmetic, Modular Exponentiation This work is licensed under a Creative Commons Attribution-Share.
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
1 Decrease-and-Conquer Approach Lecture 06 ITS033 – Programming & Algorithms Asst. Prof. Dr. Bunyarit Uyyanonvara IT Program, Image and Vision Computing.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Ceng-112 Data Structures I Chapter 6 Recursion.
Chapter 1 Introduction. Goals Why the choice of algorithms is so critical when dealing with large inputs Basic mathematical background Review of Recursion.
Week 5 - Monday.  What did we talk about last time?  Linked list implementations  Stacks  Queues.
Whole Numbers Section 3.3 Multiplication and Division of Whole Numbers
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
Analysis of Algorithms
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 TCSS 143, Autumn 2004 Lecture Notes Recursion Koffman/Wolfgang Ch. 7, pp ,
CS162 - Topic #11 Lecture: Recursion –Problem solving with recursion –Work through examples to get used to the recursive process Programming Project –Any.
Fast Exponentiation (3/31) What is the most efficient way to compute (mod 32591)? We will need an efficient algorithm in order to do “RSA cryptography”,
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Lecture 2 Jan Goals: Introduction to recursion Examples of recursive programs Reading: Chapter 1 of the text.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Merge Sort: Taught By Example CO1406: Algorithms and Data Structures Module Lecturer: Dr. Nearchos Paspallis Week 10 Lab - Practice with Merge sort and.
Lecture 6 Feb 8, 2012 Goals: Linked list (Chapter 3) list class in STL (section 3.3) implementing with linked lists.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
Basic Design Techniques iteration versus recursion divide-and-conquer an example: merge sort.
23 February Recursion and Logarithms CSE 2011 Winter 2011.
Lecture 13 Oct 15, 2012 cell array (review) Ch 4 recursion (Ch 12)
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
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.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
MA/CSSE 473 Day 05 Factors and Primes Recursive division algorithm.
Decrease-and-Conquer Approach
CMSC 341 Lecture 13 Leftist Heaps
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Applied Algorithms (Lecture 17) Recursion Fall-23
CS201: Data Structures and Discrete Mathematics I
Rosen 5th ed., §§ ~18 slides, ~1 lecture
CS148 Introduction to Programming II
Discrete Mathematics and its Applications
Discrete Mathematics and its Applications
Presentation transcript:

Lecture 2 Aug 28 goals: Introduction to recursion examples of recursive programs

Recursion Recursive program A function that calls itself. e.g. f(x)= 2 * f(x – 1) Recursive definition defining a concept in terms of itself. e.g. an arithmetic expression is either a number, or is of the form X op Y where X and Y are arithmetic expressions and op is an operator. Recursive data structure a structure whose substructure is of the same type as the structure

Recursion Recursive data structure a structure whose substructure is of the same type as the structure e.g. a binary tree is either a single node or a node with two children both of which are also binary trees.

Recursion - rules Rule 1: Provide exit from recursion. (focus on base cases – some times, more than one base case is needed.) Rule 2: Make sure that the successive recursive calls progress towards the base case. Rule 3: Assume that recursive calls work. Rule 4: Compound interest rule: Avoid redundant calls.

Recursion – simple examples 1)Compute n! n! = 1 x 2 … x n = (n – 1)! x n 2) Compute … + n f(n) = n + f(n – 1) 3) Compute the n-th Fibonacci number f(n) = f(n – 1) + f(n – 2)

Example: (from Chapter 2 of text) Given x and n, we want to compute x n. Obvious iterative solution: int exp(int x, int n) { int temp = x; for (int j= 1; j < n; ++j) temp*= x; return temp; } The number of multiplications performed is n – 1. We will now see that a recursive algorithm will provide a much faster solution. Faster algorithm is crucial for RSA encryption algorithm.

Idea behind the algorithm Rule of exponents: x n = (x 2 ) n/2 if n is even x * (x 2 ) (n-1)/2 if n is odd base case n = 1  return x even n  call exp(x*x, n/2) and return the result. odd n  call exp(x*x, (n – 1)/2) and multiply the result of call by x and return the output.

Code for recursive exponent algorithm int exp (int x, int n) { if (n == 1) return x; else if (n%2 == 0) return exp(x*x, n/2); else return x* exp(x*x, (n – 1)/2); } Fact: When called to compute exp(x,100000), the iterative algorithm performs multiplications while the recursive algorithm will performs 21 multiplications.

Recursive algorithm for modular exponentiation In cryptography (RSA algorithm), the following problem arises. Given x, n and m, compute x n (mod m). In addition to the recursive algorithm we described above, we need one more idea to make this computation feasible. In practice, x, n and m are extremely large numbers, with hundreds of digits in it.

Modular exponentiation algorithm int exp (int x, int n, int m) { if (n == 1) return x % m; else if (n%2 == 0) return exp((x*x) % m, n/2); else return (x* exp((x*x) % m, (n – 1)/2)) % m; }

An example involving 1-d array Given two segments of sorted arrays A and B, output the result of merging them as a single sorted array. A B merge(A, 2, 3, B, 9, 13) should return merge(A, 2, 2, B, 9,10) should return the array merge(A, 3, 4, B, 9, 8) should return 5 7 since the second array is empty. (high index < low index means the array is empty.)

What are the base cases?

one array segment is empty. In this case, what is the output?

What are the base cases? one array segment is empty. In this case, what is the output? The other array segment, so we just have to copy the segment to the output.

What are the base cases? one array segment is empty. In this case, what is the output? The other array segment, so we just have to copy the segment to the output. what if both segments are not empty? We need to make recursive call.

Before we proceed, we need to make a change to the prototype of the function merge. Why? We need to add two more parameters - the name of the array and the starting index in the output array at which we want to write.

AB merge(A, 2, 3, B, 9, 13, C, 3) should return C

merge(A, low1,high1, B, low2, high2, C, low) A B merge(A, 9, 13, B, 2, 3, C, 0) Case 1: A[low1] < B[low2] Example: C

merge(A, low1,high1, B, low2, high2, C,low) A B merge(A, 9, 13, B, 2, 3, C, 0) Case 1: A[low1] < B[low2] Example: Step1: move A[low1] to C[low]. Now a recursive call to merge will get the rest of the job done. What call?

merge(A, low1,high1, B, low2, high2) A B merge(A, 9, 13, B, 2, 3, C, 0) Case 1: A[low1] < B[low2] Example: Step1: move A[low1] to Out[0]. Now a recursive call to merge will get the rest of the job done. What call? merge(A, low1+1, high1, B, low2,high2, C, low + 1)

Case 2: A[low1] > B[low2]. Move B[low2] to C[low] and make a recursive call to merge(A,low1, high1, B, low2+1, high2, C, low+1) The complete code is shown below:

void merge(int A[], int low1, int high1, int B[], int low2,int high2, int C[], int low) { if (high1 < low1) copy(B, low2, high2, C,low); else if (high2 < low2) copy(A, low1, high1, C,low); else if (A[low1] < B[low2]) {C[low] = A[low1]; merge(A,low1+1, high1, B, low2, high2, C,low + 1);} else {C[low] = B[low2]; merge(A,low1, high1, B, low2+1, high2, C,low + 1); } void copy(int A[], int s, int t, int B[], int s1) { for (int j= s; j<= t; ++j) B[s1 +j - s] = A[j]; }

Last example: recursion and a linked list. Suppose we want to insert a key x as a last item of a list. a k g c x = ‘b’ a k g c b

void insertLast(int k) { // insert k as the last item of the list Node* tmp = new Node(k); if (head == 0) head = tmp; else if (head->next == 0) { head->next = tmp;} else {List rest; rest.head = head->next; rest.insertLast(k); }