Ceng-112 Data Structures I2007 1 Chapter 6 Recursion.

Slides:



Advertisements
Similar presentations
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
Advertisements

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)
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
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.
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
Chapter 19 Recursion.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Recursion A recursive function is a function that calls itself either directly or indirectly through another function. The problems that can be solved.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
When confronted with a new problem there are two questions you should ask: 1. Is this problem like, or a special case of, a problem that I already know.
Data Structures Using C++ 2E Chapter 6 Recursion.
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.
1 Chapter 1 RECURSION. 2 Chapter 1  Subprogram implementation  Recursion  Designing Recursive Algorithms  Towers of Hanoi  Backtracking  Eight Queens.
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter Nature of Recursion t Problems that lend themselves to a recursive solution have the following characteristics: –One or more.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Recursion. Basic problem solving technique is to divide a problem into smaller sub problems These sub problems may also be divided into smaller sub problems.
© 2006 Pearson Addison-Wesley. All rights reserved 3-1 Chapter 3 Recursion: The Mirrors.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
RECURSION.
Data Structures and Abstractions with Java, 4e Frank Carrano
Programming Principles II Lecture Notes 5 Recursion Andreas Savva.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
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, ]
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
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.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
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.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 2 Objectives Upon completion you will be able to:
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
1 Dr. Chow-Sing LinRecursion - CH 10 Problem Solving and Program Design in C Chapter 9 Recursion Chow-Sing Lin.
Chapter 9 Recursion. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe recursive function is –a.
CS212: Data Structures and Algorithms
Chapter 19: Recursion.
Topic 6 Recursion.
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Recursion DRILL: Please take out your notes on Recursion
Chapter 17 Recursion.
Recursion Chapter 12.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Algorithm Analysis (for Divide-and-Conquer problems)
Chapter 17 Recursion.
Recursion Data Structures.
Chapter 17 Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Ceng-112 Data Structures I Chapter 6 Recursion

Ceng-112 Data Structures I Recursion Recursion is a repetitive process in which an algorithm calls itself.

Ceng-112 Data Structures I Recursion defined Decompose the problem from the top to the bottom. Then, solve the problem from bottom to the top. The statement that solves the problem is known as the base case. The rest of the algorithm is known as the general case.

Ceng-112 Data Structures I Figure 6-1 Factorial (4) = 4 x 3 x 2 x 1 = 24 Iterative algorithm definition.

Ceng-112 Data Structures I Figure 6-2 Recursive algorithm definition. Factorial (4) = 4 x Factorial(3) = 4 x 3 x Factorial(2) =4 x 3 x 2 x Factorial(1) = 24

Ceng-112 Data Structures I Figure 6-3 Base case is Factorial(0) = 1. General case is n*Factorial(n-1).

Ceng-112 Data Structures I How Recursion Works When a program calls a subrutine, the current module suspends processing and the called subroutine takes over the control of the program. When the subroutine completes its processing and returs to the module that called it. The value of the parameters must be the same before and after a call.

Ceng-112 Data Structures I Figure 6-4

Ceng-112 Data Structures I Designing Recursive Algorithm 1.First, determine the base case. 2.Then, determine the general case. 3.Combine the base case and general case into an algorithm. Note: Recursion works best when the algorithm uses a data structure that naturally supports recursion!

Ceng-112 Data Structures I Usage of Recursion Is the algorithm or data structure naturally suited to recursion? Is the recursive solution shorter and more understandable? Does the recursive solution run in acceptable time and space limits?

Ceng-112 Data Structures I Figure 6-5

Ceng-112 Data Structures I Recursive Power Algorithm algorithm power ( val base, val exp ) This algorithm computes the value of a number, base, raised to the power of an exponent, exp. Pre base is the number to be raised exp is the exponent Post value of base**exp returned 1.if (exp equal 0) 1.return(1) 2.else 1.return (base * power(base, exp-1) end power

Ceng-112 Data Structures I Figure 6-6

Ceng-112 Data Structures I Figure 6-7 Reverse a Linked List

Ceng-112 Data Structures I Figure 6-8

Ceng-112 Data Structures I Fibonacci Numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... We can generalize it: Given: Fib(0)=0 Fib(1)=1 Then: Fib(n)=Fib(n-1)+Fib(n-2)...

Ceng-112 Data Structures I Fibonacci Numbers program Fibonacci This program prints out a Fibonacci series. 1.print ( This program prints a Fibonacci Series) 2.print (How many numbers do you want?) 3.read (seriesSize) 4.if (seriesSize < 2) 1.seriesSize = 2 5.print (First seriesSize Fibonacci numbers are) 6.looper = 0 7.loop (looper < seriesSize) 1.nextFib= fib(looper) 2.print(nextFib) 3.looper = looper + 1 end Recursive function!

Ceng-112 Data Structures I Fibonacci Numbers algorithm fib(val num ) Calculates the nth Fibonacci number. Pre num identified the original of the Fibonacci number. Post returns the nth Fibonacci number. 1.if (num is 0 OR num is 1) 1.“Base case” return(num) 2.return(fib(num-1) + fib(num-2)) end fib

Ceng-112 Data Structures I The Towers of Hanoi Only one disk could be moved at a time. A larger disk must never be stacked above a smaller one. One and only one auxillary needle could be used for the intermediate storage of disks.

Ceng-112 Data Structures I Figure 6-11 The Towers of Hanoi Case 1

Ceng-112 Data Structures I Figure 6-11 The Towers of Hanoi Move one disk to auxiliary needle. Move one disk to destination needle. Move one disk from auxiliary to destination needle.

Ceng-112 Data Structures I Move two disks from source to auxiliary needle. (Step-3) Move one disk from source to destination needle. (Step-4) Move two disks from auxiliary to destination needle. (Step-7)

Ceng-112 Data Structures I The Towers of Hanoi 1.Move n-1 disks from source to auxiliary. General Case 2.Move one disk from source to destination. Base Case 3.Move n-1 disks form auxiliary to destination. General Case Call Towers( n-1, source, auxiliary, destination) Move one disk from source to destination Call Towers(n-1, auxilary, destination, source).

Ceng-112 Data Structures I The Towers of Hanoi algorithm towers (val disks, val source, val dest, val auxiliary, step ) Recursively move one disk from source to destination. Pre: The tower consists of integer disks Source, destination and auxilary towers given. Post: Steps for moves printed

Ceng-112 Data Structures I The Towers of Hanoi 1 print(“Towers :”, disks, source, dest, auxiliary) 2 if (disks =1) 1 print(“Step”, step, “Move from”, source, “to”, dest) 2 step = step else 1 towers(disks – 1, source, auxiliary, dest, step) 2 print(“Step1”, step, “Move from”, source, “to”, dest) 3 step = step towers (disks – 1, auxiliary, dest, source, step) 4 return end towers

Ceng-112 Data Structures I print(“Towers :”, disks, source, dest, auxiliary) 2 if (disks = 1) 1 print(“Step”, step, “Move from”, source, “to”, dest) 2 step = step else 1 towers(disks – 1, source, auxiliary, dest, step) 2 print(“Step”, step, “Move from”, source, “to”, dest) 3 step = step towers (disks – 1, auxiliary, dest, source, step) 4 return end towers Towers (3, A, C, B) Towers (2, A, B, C) Towers (1, A, C, B) Step 1 Move from A to C Step 2 Move from A to B Towers(1, C, B, A) Step 3 Move from C to B Step 4 Move from A to C Towers(2, B, C, A) Towers(1, B, A, C) Step 5 Move from B to A Step 6 Move from B to C Towers(1, A, C, B) Step 7 Move from A to C

Ceng-112 Data Structures I Exercise #1 Write the recursive program, that calculates and returns the length of a linked list.

Ceng-112 Data Structures I typedef struct student{ int studentId; char studentName[20]; struct student *nextPtr; }STD; insertStudent(STD *headNode); deleteStudent(STD *headNode); displayList(STD *headNode); int countList(STD *tempNode); int main(void){ //define a head node that initialize the list char choice = 0; STD *headNode = (STD *)malloc(sizeof(STD)); headNode->nextPtr = NULL; for(;;){ printf("Yapmak istediginiz islemi giriniz..\n"); printf("1.Ogrenci Ekleme\n"); printf("2.Ogrenci Cikarma\n"); printf("3.Tum Listeyi Goruntuleme\n"); printf("4.Listedeki kayıt sayısı \n"); printf("5.Exit\n"); Exercise #1

Ceng-112 Data Structures I scanf("%c",&choice); switch(choice){ case '1': insertStudent(headNode); break; case '2': deleteStudent(headNode); break; case '3': displayList(headNode); break; case '4': { printf("\n node count of the list = %d \n", countList(headNode)); break;} case '5': exit(0); } Exercise #1 int countList(STD *tempNode) { if (tempNode->nextPtr == NULL) return 0; else return(1+countList(tempNode->nextPtr)); }

Ceng-112 Data Structures I Exercise #2 Implement the Russian peasant algorithm to multiply two integer values by using recursive structure. Here are the multiplication rules: Double the number in the first operand, and halve the number in the second operand. If the number in the second operand is odd, divide it by two and drop the remainder. If the number in the second operand is even, cross out that entire row. Keep doubling, halving, and crossing out until the number in the second operand is 1. Add up the remaining numbers in the first operand. The total is the product of your original numbers. Example: 16 x 27 = ? The product: =432

Ceng-112 Data Structures I #include int russian(int op1, int op2); int main(void){ int op1, op2; printf("\n operand 1 i giriniz:"); scanf("%d", &op1); printf("\n operand 2 i giriniz:"); scanf("%d", &op2); printf("\n %d * %d = %d \n", op1, op2, russian(op1, op2)); } Exercise #2 int russian(int op1, int op2) { if (op2 = = 1) { printf("\n %d", op1); return(op1);} if ((op2 % 2) = = 0) return(russian((op1*2), (op2/2))); else return(op1 + russian((op1*2), (op2/2))); }

Ceng-112 Data Structures I Exercise #3 (Quiz ?!!) Write a recursive procedure that has as arguments an array of characters and two bounds on array indices. The procedure should reverse the order of those entries in the array whose indices are between the two bounds. For example, if the array is; A[1] = “A” A[2]= “B” A[3]= “C”A[4]= “D” A[5]= “E” and the bounds are 2 and 5. Then after the procedure is run the array elements should be: A[1] = “A” A[2]= “E” A[3]= “D”A[4]= “C” A[5]= “B”

Ceng-112 Data Structures I Exercise #3 convert (int first, int end, char A[]) {char temp; if (end > first) {temp = A[end]; A[end]= A[first]; A[first] = temp; first ++; last -- ; convert(firts, end, A); }