CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion.

Slides:



Advertisements
Similar presentations
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Advertisements

Types of Recursive Methods
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Computer Science II Recursion Professor: Evan Korth New York University.
CSE1303 Part A Data Structures and Algorithms Lecture A3 – Basic Data Structures (Stacks)
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.
CSE1303 Part A Data Structures and Algorithms Lecture A7 – Nodes and Linked Structures.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A15 – Hash Tables: Collision Resolution.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
CSE1303 Part A Data Structures and Algorithms Lecture A13 – Binary Search Trees (Information Retrieval)
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.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A8 – Linked Stacks and Linked Queues.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A9 – Linked Lists.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A10 – Elementary Algorithms (Revision)
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A11 – Recursion.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
FIT FIT1002 Computer Programming Unit 20 Recursion.
1 CSE1301 Computer Programming Lecture 27 Recursion (Part 1)
CSE1303 Part A Data Structures and Algorithms Lecture A9 – Linked Lists.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A5 – Basic Data Structures – Continued (Lists)
 2000 Prentice Hall, Inc. All rights reserved. Chapter 5 – Recursive Funtions From Deitel’s “C” Book 5.13Recursion 5.14Example Using Recursion: The Fibonacci.
Data Structures Using C++1 Chapter 11 Binary Trees.
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions , Semester 2, Recursion.
Recursion Examples Fundamentals of CS Case 1: Code /* Recursion: Case 1 */ #include void count (int index); main () { count (0); getchar(); } void count.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 13 Pointers and Linked Lists.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Ceng-112 Data Structures I Chapter 6 Recursion.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
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.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Basic Data Structures (Stacks). 2 Basic Data Structures Stacks Queues Lists.
Pei Zheng, Michigan State University 1 Chapter 8 Recursion.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
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.
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Maitrayee Mukerji. Factorial For any positive integer n, its factorial is n! is: n! = 1 * 2 * 3 * 4* ….* (n-1) * n 0! = 1 1 ! = 1 2! = 1 * 2 = 2 5! =
Program Development and Design Using C++, Third Edition
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.
递归算法的效率分析. When a function is called... A transfer of control occurs from the calling block to the code of the function --It is necessary that there be.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A3 – Basic Data Structures (Stacks)
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Pointers and Linked Lists
Introduction to Recursion
Pointers and Linked Lists
Introduction to Recursion
5.13 Recursion Recursive functions Functions that call themselves
Linked Lists.
Recursion Chapter 10.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Chapter 16-2 Linked Structures
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Recursion Data Structures.
Stack Frames and Functions
ITEC324 Principle of CS III
Presentation transcript:

CSE1303 Part A Data Structures and Algorithms Lecture A11 – Recursion

2 Overview Unary Recursion Binary Recursion Examples Features Stacks Disadvantages Advantages

3 What is Recursion - Recall A procedure defined in terms of itself Components: –Base case –Recursive definition –Convergence to base case

4 double power(double x, int n) { int i double tmp = 1; if (n > 0) { for (i = 0; i < n; i++) { tmp *= x; } return tmp; }

5 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; }

6 Unary Recursion Functions calls itself once (at most) Usual format: function RecursiveFunction ( ) { if ( base case ) then return base value else return RecursiveFunction ( ) }

7 Unary Recursion /* Computes the factorial */ int factorial ( int n ) { if ( n <= 1 ) { return 1; } else { return n*factorial(n-1); } function Factorial ( n ) { if ( n is less than or equal to 1) then return 1 else return n  Factorial ( n - 1 ) }

8 Binary Recursion Defined in terms of two or more calls to itself. For example – Fibonacci A series of numbers which –begins with 0 and 1 –every subsequent number is the sum of the previous two numbers 0, 1, 1, 2, 3, 5, 8, 13, 21,...

9 Binary Recursion /* Compute the n-th Fibonacci number */ long fib ( long n ) { if ( n <= 1 ) return n ; else return fib( n - 2 ) + fib( n - 1 ); } function Fibonacci ( n ) { if ( n is less than or equal to 1 ) then return n else return Fibonacci ( n - 2 ) + Fibonacci ( n - 1 ) }

10 Copy List HeadPtr struct LinkedListRec { int count; Node* headPtr; }; typedef struct LinkedListRec List;

11 #include “linkList.h” Node* copyNodes(Node* oldNodePtr); void copyList(List* newListPtr, List* oldListPtr) { if (listEmpty(oldListPtr)) { initializeList(newListPtr); } else { newListPtr->headPtr = copyNodes(oldListPtr->headPtr); newListPtr->count = oldListPtr->count; }

12 Node* copyNodes(Node* oldNodePtr) { Node* newNodePtr = NULL: if (oldNodePtr != NULL) { newNodePtr = makeNode(oldNodePtr->value); newNodePtr->nextPtr = copyNodes(oldNodePtr->nextPtr); } return newNodePtr; }

13 Copy Nodes OldNodePtr NewNodePtr

14 Copy Nodes OldNodePtr NewNodePtr NewNodePtr->next is set to the Result of calling copy nodes on The remainder of the list

15 Copy Nodes OldNodePtr NewNodePtr

16 Copy Nodes OldNodePtr NewNodePtr NULL

17 Copy Nodes NewNodePtr OldNodePtr

18 Copy Nodes NewNodePtr OldNodePtr

19 Recursion Process Every recursive process consists of: 1. A base case. 2. A general method that reduces to the base case.

20 Types of Recursion Direct. Indirect. Linear. n-ary (Unary, Binary, Ternary, …)

21 Stacks Every recursive function can be implemented using a stack and iteration. Every iterative function which uses a stack can be implemented using recursion.

22 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1

23 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 1

24 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 1 x = 2, n = 1 tmp = 1

25 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 1 x = 2, n = 1 tmp = 1 x = 2, n = 0 tmp = 1

26 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 x = 2, n = 1 tmp = 1 tmp = 1*1*2 = 2

27 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 1 x = 2, n = 2 tmp = 2*2 = 4

28 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x = 2, n = 5 tmp = 4*4*2 = 32

29 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x n tmp Third call x n tmp Second call x n tmp First call x n tmp Fourth call Runtime stack

30 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x n tmp Return to Third call x n tmp Second call x n tmp First call Runtime stack

31 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } x n tmp Return to Second call x n tmp First call Runtime stack

32 double power(double x, int n) { double tmp = 1; if (n > 0) { tmp = power(x, n/2); if (n % 2 == 0) { tmp = tmp*tmp; } else { tmp = tmp*tmp*x; } return tmp; } Return to First call x n tmp Runtime stack

33 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 5, x = Runtime stack x n tmp

34 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 5 Stack n = 5, x = Runtime stack x n tmp

35 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 2 5 Stack n = 2, x = Runtime stack x n tmp

36 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 1, x = Runtime stack x n tmp

37 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 0, x = 2 tmp = Runtime stack x n tmp

38 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 2 5 Stack n = 0, x = 2 tmp = Runtime stack x n tmp

39 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } 5 Stack n = 0, x = 2 tmp = Runtime stack x n tmp

40 module power(x, n) { create a Stack initialize a Stack loop{ if (n == 0) then {exit loop} push n onto Stack n = n/2 } tmp = 1 loop { if (Stack is empty) then {return tmp} pop n off Stack if (n is even) {tmp = tmp*tmp} else {tmp = tmp*tmp*x} } Stack n = 0, x = 2 tmp = Runtime stack x n tmp

41 double power(double x, int n) { double tmp = 1; Stack theStack; initializeStack(&theStack); while (n != 0) { push(&theStack, n); n /= 2; } while (!stackEmpty(&theStack)) { n = pop(&theStack); if (n % 2 == 0) { tmp = tmp*tmp;} else { tmp = tmp*tmp*x;} } return tmp; }

42Disadvantages May run slower. –Compilers –Inefficient Code May use more space.Advantages More natural. Easier to prove correct. Easier to analysis. More flexible.

43 Free List – Non Recursive headPtr /* Delete the entire list */ void FreeList(Node* headPtr){ Node* nodePtr; while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; } 0x2000 0x4c680x258a 0x2000 Runtime stack headPtr nodePtr

44 Free List – Non Recursive nodePtr 0x2000 0x258a0x4c68 headPtr /* Delete the entire list */ void FreeList(Node* headPtr){ Node* nodePtr; while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; } 0x258a 0x2000 Runtime stack headPtr nodePtr

45 Free List – Non Recursive nodePtr 0x258a0x4c68 headPtr /* Delete the entire list */ void FreeList(Node* headPtr){ Node* nodePtr; while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; } 0x4c68 0x258a Runtime stack headPtr nodePtr

46 Free List – Non Recursive nodePtr NULL 0x4c68 headPtr /* Delete the entire list */ void FreeList(Node* headPtr){ Node* nodePtr; while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; } NULL 0x4c68 Runtime stack headPtr nodePtr

47 Free List – Non Recursive nodePtr NULL Has local variables on the stack. This is performing two assignments and one comparison per iteration. headPtr /* Delete the entire list */ void FreeList(Node* headPtr){ Node* nodePtr; while (headPtr != NULL) { nodePtr=headPtr->next; free(headPtr); headPtr=nodePtr; } NULL Runtime stack headPtr nodePtr

48 Free List /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } 0x258a0x4c68 0x2000 Runtime stack 0x2000 headPtr

49 Free List 0x258a0x4c68 0x2000 0x258a 0x2000 headPtr /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } Runtime stack

50 Free List 0x258a0x4c68 0x2000 0x4c68 0x258a 0x2000 headPtr /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } Runtime stack

51 Free List 0x258a0x4c68 0x2000 NULL 0x4c68 0x258a 0x2000 headPtr /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } Runtime stack

52 Free List /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } 0x258a0x4c68 0x2000 0x4c68 0x258a 0x2000 headPtr Runtime stack

53 Free List 0x258a 0x2000 0x258a 0x2000 headPtr /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } Runtime stack

54 Free List 0x2000 headPtr /* Delete the entire list */ void FreeList(Node* headPtr) { if (headPtr==NULL) return; FreeList(headPtr->next); free(headPtr); } Has no local variables on the stack. This is performing one assignment and one comparison per function call. Runtime stack

55 Revision Recursion Revision: Reading Kruse Standish 3, 14 Langsam 3 Preparation Next lecture: Binary Trees Read Chapter 9 in Kruse et al.