Stacks & Recursion.

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 20 Recursion.
Advertisements

Factorial Recursion stack Binary Search Towers of Hanoi
Recursion. Binary search example postponed to end of lecture.
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 CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Recursion Gordon College CPS212
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
Recursion CS Goals Discuss recursion as another form of repetition Do the following tasks, given a recursive routine Determine whether the routine.
1 C++ Plus Data Structures Nell Dale Chapter 7 Programming with Recursion Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
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.
Chapter 2 Recursion: The Mirrors CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
ICS220 – Data Structures and Algorithms Dr. Ken Cosh Week 5.
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:
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Chapter 7 Programming with Recursion. What Is Recursion? Recursive call A method call in which the method being called is the same as the one.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
5.3 EVALUATION OF POSTFIX EXPRESSION For example, consider the evaluation of the following postfix expression using stacks: abc+d*f/- where, a=6, b=3,
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.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Data Structures I (CPCS-204) Week # 5: Recursion Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
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.
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.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
Recursion CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Recursion Function calling itself
Recursion Powerful Tool
Programming with Recursion
Recursion.
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion Salim Arfaoui.
Recursion CENG 707.
RECURSION.
Recursion DRILL: Please take out your notes on Recursion
Stacks & Recursion.
Data Structures and Algorithms
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Programming with Recursion
Jordi Cortadella Department of Computer Science
Recursion Output Input
Stacks & Recursion.
CSE 1342 Programming Concepts
Recursion Data Structures.
Stacks & Recursion.
Chapter 12 Supplement: Recursion with Java 1.5
Recursion Chapter 18.
Chapter 17 Recursion.
Recursion Definition: A method that calls itself. Examples
Chapter 17 Recursion.
Chapter 18 Recursion.
Yan Shi CS/SE 2630 Lecture Notes
Chapter 19: Recursion.
Chapter 3 :Recursion © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Recursion.
ITEC324 Principle of CS III
Advanced Analysis of Algorithms
Presentation transcript:

Stacks & Recursion

Stack Linear data structure Can only be accessed from one end Very useful when data has to stored and then retrieved in the reverse order. Called LAST IN FIRST OUT (LIFO)

LIFO : Last In First Out Stack 10 6 9

Stacks Stack 10 6 9 LIFO : Last In First Out

Operations By Adam Drozdek

RECURSIVE STRUCTURE Sierpinski_Triangle from www.wikipedia.org

Recursive Definition An object is defined in terms of smaller version of itself A problem is decomposed into simpler subproblems of the same kind Recursion is the process in which repetition in a “self-similar” manner Generally used to define functions or sequences of numbers Has two parts Base Case / Anchor Case / Ground Case Define the basic building blocks Rules / General Case Define how objects / elements can be built from basic elements or already constructed objects Serves two purposes Generating new elements Testing whether an element belongs to a set

Will test if a number is member of series Examples Factorials n! = 1 if n=0 n! = n(n-1)! if n> 0 This definition will evaluate the factorial of a number The set of natural numbers 0  N If n  N then (n+1)  N There are no other natural numbers This definition will generate natural numbers Will test if a number is member of series Will generate numbers

Activation Records/Stack Frame Recursive Function Recursion means having the characteristic of coming up again and again A function that invokes itself by making a call to itself is called a recursive function Direct Recursion: When a function calls itself Indirect Recursion: When A calls B and B calls A Activation Records/Stack Frame The state of each function is characterized by an activation record. It is the private pool of information for a function. It has the following information: Parameters and local variables: of the function Dynamic link: Link to the calling function’s activation record Return Address: Caller function’s return address Runtime Stack Runtime stack is a stack whose each element is the activation records of the different functions being called during the running of the program

Example int factorial(n) void main() { { int fact = 1; if (n==0) return fact; else { fact = n*factorial(n-1); return fact; } void main() { int answer = factorial(3); cout << answer; } Factorial(0) Factorial(1) Factorial(2) Factorial(3) Main() fact = 1 fact = 1 * factorial(0) fact = 1*1=1 answer = factorial (3) fact = 3 * factorial(2) fact = 2 * factorial(1) fact = 2 * factorial(1) answer = factorial (3) fact = 3 * factorial(2) fact = 2 * 1=2 fact = 3 * factorial(2) answer = factorial (3) fact = 3 * 2 = 6 answer = factorial (3) answer = 6

Towers of Hanoi Watch animation with 4 disks!!! Pictures and animation from: www.wikipedia.org void Hanoi(int N,char src,char dest,char intermediate) { if (N>0) { //move top n-1 disks from ‘src’ to ‘intermediate’ //using ‘dest’as intermediate location Hanoi(N-1,src,intermediate,dest); //move the bottom disk from ‘src’ to ‘dest’ Move(src,dest); //now move the rest of N-1 disks from ‘intermediate’ //to ‘dest’ using ‘src’ as intermediate location Hanoi(N-1,intermediate,dest,src); }

How it is working??? Lets trace for Hanoi(3,’A’,’C’,’B’) Sequence of moves: Move(A,B) Move(AC) Move(B,C) Move(B,A) Move(A,C) Hanoi(0,A,C,B) Move(A,B) Hanoi(0,C,B,A) Hanoi(1,A,B,C) Move(A,C) Hanoi(1,B,C,A) See animation!!! Hanoi(2,A,C,B) Move(A,B) Hanoi(2,B,C,A) Hanoi(0,B,A,C) Move(B,C) Hanoi(0,A,C,B) Hanoi(3,A,B,C) Hanoi(0,B,C,A) Move(B,A) Hanoi(0,C,A,B) Hanoi(1,B,A,C) Move(B,C) Hanoi(1,A,C,B) Hanoi(0,A,B,C) Move(A,C) Hanoi(0,B,C,A)

Tail Recursion Use of only one recursive call at the very end of the function implementation. Useful in logic programming languages like prolog. Non-Tail Recursion When the recursive call is NOT tail recursion

More examples, which you can try… Fibonacci Series Linear search Binary search Reverse items of an array