Recursion Data Structure Submitted By:- Dheeraj Kataria.

Slides:



Advertisements
Similar presentations
Types of Recursive Methods
Advertisements

More on Recursive Methods KFUPM- ICS Data Structures.
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
More on Recursive Recursion vs. Iteration Why Recursion?
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
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.
Prof. S.M. Lee Department of Computer Science. Answer:
Data Structures Using C++ 2E Chapter 6 Recursion.
Data Structures Using C++ 2E Chapter 6 Recursion.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Prof. S.M. Lee Department of Computer Science.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
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.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 17: Recursion.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
1 Recursion Recursive method –Calls itself (directly or indirectly) through another method –Method knows how to solve only a base case –Method divides.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
递归算法的效率分析. 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.
Recursion Chapter 2 Objectives Upon completion you will be able to:
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
Recursion.
Recursion The programs discussed so far have been structured as functions that invoke one another in a disciplined manner For some problems it is useful.
Recursion Topic 5.
Recursion what is it? how to build recursive algorithms
Chapter 10 Recursion Instructor: Yuksel / Demirer.
Data Structures and Algorithms
Abdulmotaleb El Saddik University of Ottawa
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
More on Recursive Recursion vs. Iteration Why Recursion?
Java 4/4/2017 Recursion.
More on Recursive Methods
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Data Structures Using Java
Using a Stack Chapter 6 introduces the stack data type.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Recursion Copyright (c) Pearson All rights reserved.
Applied Algorithms (Lecture 17) Recursion Fall-23
Recursion Chapter 11.
Using a Stack Chapter 6 introduces the stack data type.
Recursion Data Structures.
Unit 3 Test: Friday.
Stack Frames and Functions
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Using a Stack Chapter 6 introduces the stack data type.
Using a Stack Chapter 6 introduces the stack data type.
The structure of programming
Thinking procedurally
Chapter 5 Recursion.
ITEC324 Principle of CS III
Types of Recursive Methods
Presentation transcript:

Recursion Data Structure Submitted By:- Dheeraj Kataria

Recursion Nested Recursion A more complicated case of recursion is found in definitions in which a function is not only defined in terms of itself but it is also used as one of the parameters. Example: h(1)=h(2+h(2))=h(14)=14 h(2)=h(2+h(4))=h(12)=12 h(3)=h(2+h(6))=h(2+6)=h(8)=8 h(4)=h(2+h(8))=h(2+8)=h(10)=10

Recursion Nested Recursion The Ackermann function This function is interesting because of its remarkably rapid growth. It grows so fast that it is guaranteed not to have a representation by a formula that uses arithmetical operations such as addition, multiplication, and exponentiation.

Recursion Nested Recursion The Ackermann function A(0,0)=0+1=1,A(0,1)=2,A(1,0)=A(0,0)=1, A(1,1)=A(0,A(1,0))=A(0,1)=3 A(1,2)=A(0,A(1,1))=A(0,2)=4 A(2,1)=A(1,A(2,0))=A(1,A(1,0))=A(1,1)=5 A(3,m)=A(2,A(3,m-1))=A(2,A(2,A(3,m-2)))=…=2m+3-3

Recursion Excessive Recursion Logical simplicity and readability are used as an argument supporting the use of recursion. The price for using recursion is slowing down execution time and storing on the run-time stack more things than required in a non-recursive approach. Example: The Fibonacci numbers void Fibonacci(int n) { If (n<2) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); }

Recursion Excessive Recursion Many repeated computations

Recursion Excessive Recursion

Recursion Excessive Recursion void IterativeFib(int n) { if (n < 2) return n; else int i = 2, tmp, current = 1, last = 0; for ( ; i<=n; ++i) tmp= current; current += last; last = tmp; } return current;

Recursion Excessive Recursion

Recursion Excessive Recursion We can also solve this problem by using a formula discovered by A. De Moivre. Can be neglected when n is large The characteristic formula is :- The value of is approximately -0.618034

Recursion Backtracking Suppose you have to make a series of decisions, among various choices, where You don’t have enough information to know what to choose Each decision leads to a new set of choices Some sequence of choices (possibly more than one) may be a solution to your problem Backtracking is a methodical way of trying out various sequences of decisions, until you find one that “works”

Recursion Backtracking Backtracking allows us to systematically try all available avenues from a certain point after some of them lead to nowhere. Using backtracking, we can always return to a position which offers other possibilities for successfully solving the problem.

Recursion Backtracking The Eight Queens Problem Place 8 queens on an 8 by 8 chess board so that no two of them are on the same row, column, or diagonal

Recursion Backtracking The Eight Queens Problem

Recursion Backtracking The Eight Queens Problem Pseudo code of the backtracking algorithm PutQueen(row) for every position col on the same row if position col is available { place the next queen in position col; if (row < 8) PutQueen(row+1); else success; remove the queen from position col; /* backtrack */ }

Recursion Backtracking The Eight Queens Problem Natural Implementation 1 1 1 1 1 1 1 1 Q Q 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Q 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Initialization The first queen The second queen

Recursion Backtracking The Eight Queens Problem Natural Implementation Q Q Q Q Q Q Q Q 1 1 1 1 Q Q 1 1 1 1 1 1 1 Q 1 1 1 Q 1 1 1 1 1 1 1 1 1 1 1 The third queen The fourth queen The 5th & 6th queen Have to backtrack now! This would be queen now.

Recursion Backtracking The Eight Queens Problem Natural Implementation The setting and resetting part would be the most time-consuming part of this implementation. However, if we focus solely on the queens, we can consider the chessboard from their perspective. For the queens, the board is not divided into squares, but into rows, columns, and diagonals.

Recursion Backtracking The Eight Queens Problem Simplified data structure A 4 by 4 chessboard Row-column = constant for each diagonal

Recursion Backtracking The Eight Queens Problem

Recursion Backtracking The Eight Queens Problem

Recursion Backtracking The Eight Queens Problem

Recursion Backtracking The Eight Queens Problem

Recursion Backtracking The Eight Queens Problem

Why Recursion? Usually recursive algorithms have less code, therefore algorithms can be easier to write and understand - e.g. Towers of Hanoi. However, avoid using excessively recursive algorithms even if the code is simple. Sometimes recursion provides a much simpler solution. Obtaining the same result using iteration requires complicated coding - e.g. Quicksort, Towers of Hanoi, etc.

Why Recursion? Recursive methods provide a very natural mechanism for processing recursive data structures. A recursive data structure is a data structure that is defined recursively – e.g. Linked-list, Tree. Functional programming languages such as Clean, FP, Haskell, Miranda, and SML do not have explicit loop constructs. In these languages looping is achieved by recursion.

Why Recursion? Recursion is a powerful problem-solving technique that often produces very clean solutions to even the most complex problems. Recursive solutions can be easier to understand and to describe than iterative solutions.

Why Recursion? By using recursion, you can often write simple, short implementations of your solution. However, just because an algorithm can be implemented in a recursive manner doesn’t mean that it should be implemented in a recursive manner.

Limitations of Recursion Recursive solutions may involve extensive overhead because they use calls. When a call is made, it takes time to build a stackframe and push it onto the system stack. Conversely, when a return is executed, the stackframe must be popped from the stack and the local variables reset to their previous values – this also takes time.

Limitations of Recursion In general, recursive algorithms run slower than their iterative counterparts. Also, every time we make a call, we must use some of the memory resources to make room for the stackframe.

Limitations of Recursion Therefore, if the recursion is deep, say, factorial(1000), we may run out of memory. Because of this, it is usually best to develop iterative algorithms when we are working with large numbers.

Main disadvantage of programming recursively The main disadvantage of programming recursively is that, while it makes it easier to write simple and elegant programs, it also makes it easier to write inefficient ones. when we use recursion to solve problems we are interested exclusively with correctness, and not at all with efficiency. Consequently, our simple, elegant recursive algorithms may be inherently inefficient.

Thank You!!