Lecture - 8 On Stacks, Recursion. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci.

Slides:



Advertisements
Similar presentations
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Advertisements

Introduction to Algorithms
Starting Out with Java: From Control Structures through Objects
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Kavita Math231 Recursion and Iteration. Kavita Math231 We use Recursion when we have to perform a complex task that can be broken into the several subtasks.
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.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Apan Qasem Texas State University Spring 2011.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 14: Recursion Starting Out with C++ Early Objects
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
CHAPTER 61 STACK, QUEUES, RECURSION. Introduction when one wants to restrict insertion and deletion so that they can take place only at the beginning.
13.2 Recursive Definitions Objective 1) Provide the recursive definition for sequences; 2) Identify the type of a sequence from a recursive definition.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Fall 2015 COMP 2300 Discrete Structures for Computation Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
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.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Lecture#16 Discrete Mathematics. Recursion Now, 1 is an odd positive integer by the definition base. With k = 1, = 3, so 3 is an odd positive integer.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
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.
Upon completion, the world will end…
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
1 Recursion. 2 A process by which a function calls itself repeatedly  Either directly. X calls X  Or cyclically in a chain. X calls Y, and Y calls X.
CMPF144 FUNDAMENTALS OF COMPUTING THEORY Module 9: The Tower of Hanoi.
1 CS1120: Recursion. 2 What is Recursion? A method is said to be recursive if the method definition contains a call to itself A recursive method is a.
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Tower of Hanoi Tower of Hanoi is a mathematical puzzle invented by a French Mathematician Edouard Lucas in The game starts by having few discs stacked.
Recursively Defined Sequences Lecture 40 Section 8.1 Wed, Apr 11, 2007.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Lecture - 6(Stacks) On Data structures. Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline What is a Stack? Array implementation of stacks Operations.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 15 Recursion.
Recursion: The Mirrors
Chapter 15 Recursion.
STACKS.
Chapter 8: Recursion Java Software Solutions
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.
Chapter 14: Recursion Starting Out with C++ Early Objects
CS1120: Recursion.
Chapter 8: Recursion Java Software Solutions
Chapter 8: Recursion Java Software Solutions
CSC 143 Recursion.
Dr. Sampath Jayarathna Cal Poly Pomona
Presentation transcript:

Lecture - 8 On Stacks, Recursion

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Lecture Outline Quick sort Algorithm Recursion –Calculate n factorial –Fibonacci Sequence –TOWERS OF HANOI

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Quick sort Algorithm This algorithm sorts an array A with N elements 1. [Initialize] TOP=NULL. 2.[Push boundary values of A onto stack when A has 2 or more elements] If N>1, then TOP:=TOP+1, LOWER[1]:=1 and UPPER[1]:=N. 3. Repeat Step 4 to 7 while TOP!= NULL. 4. [Pop sub list from stack] Set BEG:=LOWER[TOP], END:=UPPER[TOP] TOP:=TOP Call QUICK(A,N,BEG,END,LOC). [Procedure 6.5] 6.[Push left sub list onto stacks when it has 2 or more elements] If BEG<LOC-1 then: TOP:=TOP+1, LOWER[TOP]:=BEG, UPPER[TOP]=LOC-1 [End of If structure]. 7. [Push right sub list onto stacks when it has 2 or more elements] If LOC+1 < END then: TOP:=TOP+1, LOWER[TOP]:= LOC+1, UPPER[TOP]:= END [End of If structure] [End of Step 3 loop]. 8. Exit

Prepared by, Jesmin Akhter, Lecturer, IIT,JU [1] [2] [3] [4] [5] [6] [7] [8] [9] First sublist second sublist TOP:=NULL 9>1 ? Yes TOP:=TOP+1=1, LOWER[1]:=1, UPPER[1]:=9 Repeat TOP=!NULL. Pop sub list from STACKs Set BEG:=LOWER[TOP]=1, END:=UPPER[TOP] =9 TOP:=TOP-1.=0 Call QUICK(A,N,1,9,LOC). LOC=5 [Push left sub list onto stacks when it has 2 or more elements] If BEG(1)<LOC-1(4) Yes [Left sublist’s ending position=loc-1] TOP:=TOP+1=1, LOWER[TOP]:=1, UPPER[TOP]=LOC-1=5-1=4 [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(6) < END(9) ? yes [Right sublist’s starting position=loc+1] TOP:=TOP+1=1+1=2, LOWER[TOP]:= LOC+1=6, UPPER[TOP]:= END=9 LOC=5 LOWER UPPER 1 LOWER 9 UPPER LOWER UPPER LOWER UPPER

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Repeat TOP=!NULL. Pop sub list from STACKs Set BEG:=LOWER[TOP]=6, END:=UPPER[TOP] =9 TOP:=TOP-1.=2-1=1 Call QUICK(A,N,6,9,LOC). LOC=6 [Push left sub list onto stacks when it has 2 or more elements] If BEG(6)<LOC-1(5) ? No [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(7) < END(9) ? yes TOP:=TOP+1=1+1=2, LOWER[TOP]:= LOC+1=7, UPPER[TOP]:= END=9 6 1 LOWER 9 4 UPPER 1 LOWER 4 UPPER 7 1 LOWER 9 4 UPPER [1] [2] [3] [4] [5] [6] [7] [8] [9] First sublist second sublist LOC=6

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Repeat TOP=!NULL? No Pop sub list from STACKs Set BEG:=LOWER[TOP]=7, END:=UPPER[TOP] =9 TOP:=TOP-1.=2-1=1 Call QUICK(A,N,7,9,LOC). LOC=7 [Push left sub list onto stacks when it has 2 or more elements] If BEG(7)<LOC-1(6) ? No [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(8) < END(9) ? yes TOP:=TOP+1=1+1=2, LOWER[TOP]:= LOC+1=8, UPPER[TOP]:= END=9 7 1 LOWER 9 4 UPPER 1 LOWER 4 UPPER 8 1 LOWER 9 4 UPPER TOP=1 TOP=2 LOC= [1] [2] [3] [4] [5] [6] [7] [8] [9] First sublist second sublist

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Repeat TOP=!NULL? No Pop sub list from STACKs Set BEG:=LOWER[TOP]=8, END:=UPPER[TOP] =9 TOP:=TOP-1.=2-1=1 Call QUICK(A,N,8,9,LOC). LOC=8 [Push left sub list onto stacks when it has 2 or more elements] If BEG(8)<LOC-1(7) ? No [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(9) < END(9) ? No 8 1 LOWER 9 4 UPPER 1 LOWER 4 UPPER LOC= [1] [2] [3] [4] [5] [6] [7] [8] [9] First sublist second sublist

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Repeat TOP=!NULL? No Pop sub list from STACKs Set BEG:=LOWER[TOP]=1, END:=UPPER[TOP] =4 TOP:=TOP-1.=1-1=0 Call QUICK(A,N,1,4,LOC). LOC=4 [Push left sub list onto stacks when it has 2 or more elements] If BEG(1)<LOC-1(3) ? Yes TOP:=TOP+1=1, LOWER[TOP]:=1, UPPER[TOP]=LOC-1=4-1=3 [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(5) < END(4) ? No 1 LOWER 4 UPPER LOWER UPPER 1 LOWER 3 UPPER [1] [2] [3] [4] [5] [6] [7] [8] [9] First sublist second sublist LOC=4

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Repeat TOP=!NULL? No Pop sub list from STACKs Set BEG:=LOWER[TOP]=1, END:=UPPER[TOP] =3 TOP:=TOP-1.=1-1=0 Call QUICK(A,N,1,3,LOC). LOC=1 [Push left sub list onto stacks when it has 2 or more elements] If BEG(1)<LOC-1(0) ? No [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(2) < END(3) ? Yes TOP:=TOP+1=0+1=1, LOWER[TOP]:= LOC+1=2, UPPER[TOP]:= END=3 1 LOWER 3 UPPER 2 LOWER 3 UPPER LOWER UPPER [1] [2] [3] [4] [5] [6] [7] [8] [9] First sublist second sublist LOC=1

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Repeat TOP=!NULL? No Repeat TOP=!NULL? Yes Pop sub list from STACKs Set BEG:=LOWER[TOP]=2, END:=UPPER[TOP] =3 TOP:=TOP-1.=1-1=0 Call QUICK(A,N,2,3,LOC). LOC=3 [Push left sub list onto stacks when it has 2 or more elements] If BEG(2)<LOC-1(2) ? No [Push right sub list onto stacks when it has 2 or more elements] If LOC+1(4) < END(3) ? No 2 LOWER 3 UPPER LOWER UPPER [1] [2] [3] [4] [5] [6] [7] [8] [9] LOC=

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Recursion A function is said to be recursively defined, if a function containing either a Call statement to itself or a Call statement to a second function that may eventually result in a Call statement back to the original function. A recursive function must have the following properties: 1. There must be certain criteria, called base criteria for which the function does not call itself. 2. Each time the function does call itself (directly or indirectly), the argument of the function must be closer to a base value

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Example 1: Factorial function: In general, we can express the factorial function as follows:n! = n * (n-1)! The factorial function is only defined for positive integers. if n<=1, then n! = 1 if n>1, then n! = n * (n-1)!

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Calculate n factorial Procedure 6.7A: FACTORIAL(FACT, N) This procedure calculates N! and return the value in the variable FACT. 1.If N=0, then: Set FACT :=1, and Return 2.Set FACT = 1. 3.Repeat for K=1 to N Set FACT : = K*FACT 4.Return. Procedure 6.7B: FACTORIAL(N) This procedure calculates N! and return the value in the variable FACT. 1.If N=0, then: FACTORIAL(FACT, N) Return 1 1. If N=0, then: Set FACT :=1, and Return Else 2. FACT: = N* FACTORIAL(FACT, N-1) Return N* FACTORIAL(N-1) 3.Return.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU

Example 2 Fibonacci Sequence : 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,………………… Definition 6.2 (a)If n=0 or n=1, then Fn =n (b)If n>1, then Fn =Fn-2 +Fn-1. Procedure 6.8 : FIBONACCI(FIB, N) This procedure calculates Fn and returns the value in the first parameter FIB. 1.If N=0 or N=1, then : set FIB :=N, and return. 2.Call FIBONACCI(FIBA, N-2). 3.Call FIBONACCI(FIBB, N-1). 4.Set FIB : =FIBA +FIBB. 5.Return.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU

TOWERS OF HANOI

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Disks of different sizes (call the number of disks "n") are placed on the left hand post, arranged by size with the smallest on top. You are to transfer all the disks to the right hand post in the fewest possible moves, without ever placing a larger disk on a smaller one. TOWERS OF HANOI The object is to move all the disks over to another pole. But you cannot place a larger disk onto a smaller disk.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU How many moves will it take to transfer n disks from the left post to the right post? Let's look for a pattern in the number of steps it takes to move just one, two, or three disks. We'll number the disks starting with disk 1 on the bottom.1 disk: 1 move Move 1: move disk 1 to post C TOWERS OF HANOI

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Move 1: move disk 2 to post B Move 2: move disk 1 to post C Move 3: move disk 2 to post C 2 disks: 3 moves

Prepared by, Jesmin Akhter, Lecturer, IIT,JU 3 disks: 7 moves Move 1: move disk 3 to post C Move 2: move disk 2 to post B Move 3: move disk 3 to post B Move 4: move disk 1 to post C Move 5: move disk 3 to post A Move 6: move disk 2 to post C Move 7: move disk 3 to post C

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Its solution touches on two important topics : A. recursive functions and stacks B. recurrence relations B. Recurrence relations Let TN be the minimum number of moves needed to solve the puzzle with N disks. From the previous section T1 = 1, T2 = 3 and T3 = 7 A trained mathematician would also note that T0 = 0. Now let us try to derive a general formula. Thus we can define the quantity TN as T 0 = 0 T N = 2T N for N > 0 We may compute T 1 = 2T = 1, T 2 = 2T 1 + 1= 3, T 3 = 2T = 7 T 4 = 2T = 15 and so on sequentially. TOWERS OF HANOI

Prepared by, Jesmin Akhter, Lecturer, IIT,JU A. Recursive pattern From the moves necessary to transfer one, two, and three disks, we can find a recursive pattern - a pattern that uses information from one step to find the next step - for moving n disks from post A to post C: First, transfer n-1 disks from post A to post B. The number of moves will be the same as those needed to transfer n-1 disks from post A to post C. Call this number M moves. [As you can see above, with three disks it takes 3 moves to transfer two disks (n-1) from post A to post C.] Next, transfer disk 1 to post C [1 move]. Finally, transfer the remaining n-1 disks from post B to post C. [Again, the number of moves will be the same as those needed to transfer n-1 disks from post A to post C, or M moves.] TOWERS OF HANOI for 1 disk it takes 1 move to transfer 1 disk from post A to post C; for 2 disks, it will take 3 moves: 2M + 1 = 2(1) + 1 = 3 for 3 disks, it will take 7 moves: 2M + 1 = 2(3) + 1 = 7 for 4 disks, it will take 15 moves: 2M + 1 = 2(7) + 1 = 15 for 5 disks, it will take 31 moves: 2M + 1 = 2(15) + 1 = 31 for 6 disks... ?

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Procedure6.9 TOWER(N, BEG, AUX, END) This procedure gives a recursive solution to the Towers of Hanoi problem for N disks. 1.If N=1, then: (a) Write : BEG END (b) Return. [End of If structure] 2.[Move N-1 disks from peg BEG to peg AUX] call TOWER(N-1, BEG, END,AUX) 3.Write : BEG END 4.[Move N-1 disks from peg AUX to peg END] call TOWER(N-1, AUX, BEG, END) 5.Return.

Prepared by, Jesmin Akhter, Lecturer, IIT,JU Applications The Tower of Hanoi is frequently used in psychological research on problem solving. There also exists a variant of this task called Tower of London for neuropsychological diagnosis and treatment of executive functions.problem solvingTower of London The Tower of Hanoi is also used as Backup rotation scheme when performing computer data Backups where multiple tapes/media are involved.Backup rotation schemeBackups As mentioned above, the Tower of Hanoi is popular for teaching recursive algorithms to beginning programming students. A pictorial version of this puzzle is programmed into the emacs editor, accessed by typing M-x hanoi. There is also a sample algorithm written in Prolog.emacsProlog The Tower of Hanoi is also used as a test by neuropsychologists trying to evaluate frontal lobe (The frontal lobes are considered our emotional control center and home to our personality. )deficits.frontal lobe