Download presentation
Presentation is loading. Please wait.
Published byAbigayle MargaretMargaret Chambers Modified over 8 years ago
1
An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms
2
Objectives After finishing this chapter, you should be able to: Provide a brief definition of the following terms: –Recursion, recursive method, iterative process, iteration, overhead, linear recursion, exponential recursion, infinite recursion, conditional recursion, base condition, and base case Describe what is meant by recursion in an algorithm and how to implement recursion in Alice An Introduction to Programming Using Alice 2.2, Second Edition 2
3
Objectives (cont’d.) Describe how recursion differs from iteration, including the cost of recursion, and how to tell when one might be more appropriate to use than the other Describe the difference between infinite recursion and conditional recursion Create a recursive method in Alice Convert an iterative Alice method into a linear recursive method An Introduction to Programming Using Alice 2.2, Second Edition 3
4
4 What Is Recursion? Recursion –Routine that has the ability to call itself –Important concept in computer programming –Not a simple concept
5
An Introduction to Programming Using Alice 2.2, Second Edition 5 Recursive Algorithms Recursive method –Method that calls itself Sierpinski gasket algorithm –Splits the triangle into four smaller triangles, then calls itself for three of the four smaller triangles
6
An Introduction to Programming Using Alice 2.2, Second Edition6
7
7
8
Recursive Algorithms (cont’d.) Algorithm for drawing a Sierpinski gasket An Introduction to Programming Using Alice 2.2, Second Edition 8
9
9
10
10 Recursive Algorithms (cont’d.) Sierpinski gasket algorithm –Demonstrates the power of recursion in drawing complex structures Algorithm –Set of instructions describing how to complete a process Recursion –Can describe a very sophisticated process with a simple and efficient set of instructions
11
Recursion Compared to Iteration Iterative process –Uses a loop to repeat a set of instructions The cost of recursion –Overhead The processor time and storage space in a computer’s memory needed to manage a method as it runs An Introduction to Programming Using Alice 2.2, Second Edition 11
12
An Introduction to Programming Using Alice 2.2, Second Edition12
13
An Introduction to Programming Using Alice 2.2, Second Edition 13 Recursion Compared to Iteration (cont’d.) Linear recursion and exponential recursion –Linear recursion Occurs when a method calls itself only once each time through the method –Exponential recursion Occurs when a method calls itself more than once in each pass through the method –Recursive Sierpinski gasket algorithm Example of exponential recursion because it calls itself three times
14
An Introduction to Programming Using Alice 2.2, Second Edition14
15
An Introduction to Programming Using Alice 2.2, Second Edition 15 Infinite Recursion and Conditional Recursion Infinite recursion –An algorithm that calls itself repeatedly without stopping –Occurs when an algorithm does not contain any instructions about when to stop the recursion –Continues until: All available memory is used, or It triggers some time-out mechanism in the operating system Conditional recursion –Recursion that is not infinite
16
Infinite Recursion and Conditional Recursion (cont’d.) Base condition (base case) –Condition that stops the recursion Properly structured recursive algorithm –Should always contain a Boolean expression in a selection sequence In conditional exponentially recursive algorithms: –The recursive step breaks the problem into smaller parts, then calls the algorithm itself for each of those parts, continuing to do so until the base case is reached An Introduction to Programming Using Alice 2.2, Second Edition 16
17
Tutorial 7A - Creating a Recursive Method You will be building a short method for a very specific task Specifications do not call for you to build an airport What’s an aircraft? Get more information There are no internal specifications for the method, only functional specifications An Introduction to Programming Using Alice 2.2, Second Edition 17
18
Tutorial 7A - Creating a Recursive Method (cont’d.) Setting the scene –Start Alice, and open a new world with a grass background –Click the ADD OBJECTS button to enter Scene Editor mode –Find the Airport object class in the Buildings folder within the Local Gallery –Click the Airport tile, but pause for moment before adding it to the world –Click the Add instance to world button to add an airport to the world An Introduction to Programming Using Alice 2.2, Second Edition 18
19
An Introduction to Programming Using Alice 2.2, Second Edition19
20
An Introduction to Programming Using Alice 2.2, Second Edition 20 Tutorial 7A - Creating a Recursive Method (cont’d.) Setting the scene (cont’d.) –Note that a tile for the airport appears in the Object tree –Using the blue camera control arrows, move, tilt, and pan the camera so that the view of the airport looks more like Figure 7-9, but without the red plane –Add a biplane to the world, and position it near the right end of the runway –Now that the scene is ready, click the DONE button to exit Scene Editor mode –Save the world with the name airport setup
21
An Introduction to Programming Using Alice 2.2, Second Edition21
22
An Introduction to Programming Using Alice 2.2, Second Edition22
23
An Introduction to Programming Using Alice 2.2, Second Edition 23 Tutorial 7A - Creating a Recursive Method (cont’d.) Coding the recursion
24
An Introduction to Programming Using Alice 2.2, Second Edition 24 Tutorial 7A - Creating a Recursive Method (cont’d.) To create the method: –Select the biplane tile in the Object tree and the methods tab in the Details area –Click the create new method button, type taxi as the method name, and then click the OK button –Click the create new parameter button, type the name target, make sure that object is selected as the data type, and then click the OK button –Drag an IF/ELSE tile from the bottom of the Editor area and drop it in the new taxi method –Select true
25
An Introduction to Programming Using Alice 2.2, Second Edition25
26
An Introduction to Programming Using Alice 2.2, Second Edition 26 Tutorial 7A - Creating a Recursive Method (cont’d.) To create the method (cont’d.): –Select world in the Object tree and the functions tab in the Details area –Drag a copy of the a > b tile and drop it into the IF/ELSE instruction tile –Select 1 as the value for a and 1 as the value for b –Select biplane in the Object tree –Drag and drop a copy of the distance to function in place of the first number 1 in the condition in the IF/ELSE tile –Select expressions, then target
27
An Introduction to Programming Using Alice 2.2, Second Edition 27 Tutorial 7A - Creating a Recursive Method (cont’d.) Add the instructions that belong inside the IF/ELSE structure –Select the methods tab in the Details area –Drag and drop a copy of the biplane turn to face instruction tile into the IF/ELSE tile in the Editor area –Select expressions, and then target –Click more and change the style to abruptly –Drag a copy of the biplane move tile from the methods tab –Drop it into the new method just below the biplane turn to face instruction
28
An Introduction to Programming Using Alice 2.2, Second Edition 28 Tutorial 7A - Creating a Recursive Method (cont’d.) Add the instructions that belong inside the IF/ELSE structure (cont’d.) –Select forward as the direction and 1 meter as the amount, and then click more and change the style to abruptly –Drag a copy of the taxi target tile from the methods tab in the Details area and drop it into the new method –Select expressions and then target –Click the Yes, I understand what I am doing. button
29
An Introduction to Programming Using Alice 2.2, Second Edition29
30
An Introduction to Programming Using Alice 2.2, Second Edition 30 Tutorial 7A - Creating a Recursive Method (cont’d.) Add the instructions that belong inside the IF/ELSE structure (cont’d.) –Drag a Do together tile from the bottom of the Editor area and drop it into the method between the biplane move forward instruction and the biplane.taxi instruction –Drag the biplane turn to face and biplane move instruction tiles into the Do together tile
31
An Introduction to Programming Using Alice 2.2, Second Edition31
32
An Introduction to Programming Using Alice 2.2, Second Edition 32 Tutorial 7A - Creating a Recursive Method (cont’d.) To test the method: –Select world.my first method in the Editor area –Drag a copy of the taxi target tile from the methods tab in the Details area and drop it into world.my first method in place of Do Nothing –Select airport, then runwayandParking, and then garage02 –Drag and drop a copy of the biplane.turn to face tile in the methods area on the Details tab into world.my first method below the biplane.taxi instruction –Save the world with the name recursive taxi
33
Tutorial 7B - Converting an Existing Iterative Method to Recursion Start Alice and open the sail to object world from Tutorial 5C Save the world with the name recursive sail to Select the sailboat in the Object tree and the methods tab in the Details area, if necessary Click the edit button next to the sail to method tile An Introduction to Programming Using Alice 2.2, Second Edition 33
34
An Introduction to Programming Using Alice 2.2, Second Edition34
35
An Introduction to Programming Using Alice 2.2, Second Edition 35 Pseudocode for the existing iterative method Tutorial 7B - Converting an Existing Iterative Method to Recursion (cont’d.)
36
Drag an IF/ELSE instruction tile from the bottom of the Editor area and drop it in the method Select true Drag the Boolean condition tile that starts with the word not from the WHILE tile and drop it into the IF/ELSE tile in place of true Drag the Do together tile with the sailboat turn to face target and sailboat move forward 2 meters instructions from the WHILE tile An Introduction to Programming Using Alice 2.2, Second Edition 36
37
An Introduction to Programming Using Alice 2.2, Second Edition37
38
Tutorial 7B - Converting an Existing Iterative Method to Recursion (cont’d.) An Introduction to Programming Using Alice 2.2, Second Edition 38 Drop it into the IF/ELSE tile Drag a copy of the sail to target method tile from the methods tab in the Details area Drop it in the IF/ELSE instruction below the Do together tile but above ELSE Select expressions, and then target
39
An Introduction to Programming Using Alice 2.2, Second Edition39
40
Tutorial 7B - Converting an Existing Iterative Method to Recursion (cont’d.) Click the Yes, I understand what I am doing. button Either right-click the While tile and select delete or drag the tile to the trash can An Introduction to Programming Using Alice 2.2, Second Edition 40
41
An Introduction to Programming Using Alice 2.2, Second Edition 41 Summary In computer programming, an algorithm that calls itself is said to be recursive Iterative process –One that uses a loop to repeat a set of instructions, so iteration is just another name for looping Overhead –Processor time and storage space in a computer’s memory needed to manage a method as it runs Linear recursion –Occurs when a method calls itself only once each time through the method
42
Summary (cont’d.) Exponential recursion –Occurs when a method calls itself more than once in each pass through the method Infinite recursion –The algorithm calls itself repeatedly without stopping Base case –The condition that stops the recursion An Introduction to Programming Using Alice 2.2, Second Edition 42
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.