An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms.

Slides:



Advertisements
Similar presentations
Space Man Sam: Grammar Mistakes By Aleis Murphy Duke University, Under the direction of Professor Susan Rodger July 2010.
Advertisements

Getting Started With Alice By Ruthie Tucker under the direction of Prof. Susan Rodger Duke University, July
PIIT Computer Science Summer Camp - Alice July 10, 2012 Brenda Parker Computer Science Department MTSU.
Coloring Randomly: Random Selection in Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University July 2008.
Lets Play Catch! Keeping Score in Alice By Francine Wolfe Duke University Professor Susan Rodger May 2010.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming Third Edition.
Repetition Structures
Lights Camera Action! Part 3: BDE Events By Deborah Nelson under the direction of Professor Susan Rodger Duke University July 2008.
Exploring Events. Try this Start Alice and create a blank world using the grass template. Add an instance of a BlueBallerina. Add an instance of a PinkBallerina.
Copyright 2011 Wanda Dann, Steve Cooper, Don Slater Alice Workshop Implementation Algorithm  Code World.my first method Control blocks Statements (methods,
Alice: A Free 3D Animation World for Teaching Programming Barbara Ericson Georgia Institute of Technology Oct 2005.
Calvin and Hobbes Teach Properties and Functions Created by Daniel MacDonald under the direction of Professor Susan Rodger Duke University June 2013.
Princess & Dragon Part 4: Breathing Fire—Adding Effects to Alice By Elizabeth Liang under the direction of Professor Susan Rodger Duke University June.
Checking for Collisions Ellen Yuan Under the direction of Professor Susan Rodger at Duke University June 2014.
Distributing (Fun + Learning): The Distributive Property By: Peggy Li Under the direction of Professor Susan Rodger Duke University, June
Variables and Functions Chapter Variables Named storage location in computer’s memory Programs may need to store data when running o Stored in.
by Chris Brown under Prof. Susan Rodger Duke University June 2012
Branching and Looping Examples, cont’d. Remember the generic triple jump world…
Making a Boat Racing Game in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2010.
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June
Class-level Methods Chapter 6 part 1. Classes and Objects Classes o In Alice, classes are predefined as 3D models Objects o An object is an instance of.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Alice 2.0 Introductory Concepts and Techniques Project 1 Exploring Alice and Object-Oriented Programming.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Poses Tutorial Alex Boldt Under the direction of Professor Susan Rodger Duke University July 2015.
Changing Color, Using Text Objects, and Random Selection in Alice By Jenna Hayes Under the direction of Professor Susan Rodger Duke University, July 2008.
Teaching a character to walk in more than one world: Parameters and Inheritance. By Lana Dyck under the direction of Professor Susan Rodger Duke University.
A Simple Quiz: Ask User Functions. By Lana Dyck under the direction of Professor Susan Rodger Duke University June 2009, added Part 2 July 2011.
An Introduction to Alice (Short Version) – Extras! Yossra Hamid Under the Supervision of Professor Susan Rodger Duke University, June 2014 This is a continuation.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
Skater World: Part Two By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 1,
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops Susan Rodger Duke University July 2011.
Alice 3.0 A Guided Tour Mike R-D YRDSB. Agenda General Walkthrough of Alice 3.0 Loops, Variables, Threads Methods / Properties Arrays Misc.
The Essentials of Alice Mrs. Jayne Slease SBMS CTE Computer Science and Animation Credit to Duke Students under the direction of Professor Susan Rodger.
Repetition Structures Chapter 5 Part The While Instruction  Combines Loop and the condition that is also used in If/else statements  The loop.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
Simple Collision Detection By David Yan Under the direction of Professor Susan Rodger and Chari Distler Duke University, June 2015.
Creating a 3D Interactive Story Prof. Susan Rodger Duke University July 19, 2007.
Changing Camera Views! Part 1: Set Point of View to By Bella Onwumbiko under the direction of Professor Susan Rodger Duke University July 2013 Updates.
Learning to Program: Part 1 Scene Setup and Starting Animation by Ruthie Tucker and Jenna Hayes Under the direction of Professor Susan Rodger Duke University,
Skater World: Part Three By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 2009.
Skater World: Part Four By Deborah Nelson Duke University Under the direction of Professor Susan Rodger June 1, 2009.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
BDE tutorial By Deborah Nelson Duke University Under the direction of Professor Susan Rodger July 13, 2008.
Alice: A Free 3D Animation World for Teaching Programming.
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.
The Essentials of Alice By Jenna Hayes under the direction of Professor Susan Rodger Duke University May 2009.
Creating a Simple Game in Scratch Barb Ericson Georgia Tech May 2009.
An Introduction to Programming with C++1 An Introduction to Control Structures Tutorial 1.
Jonathon Kuo Under the Direction of Dr. Susan Rodger
A Simple Quiz for Alice 3.2:
An Introduction to Alice (Short Version)
Changing Camera Views! Part 1: Set Point of View to By Bella Onwumbiko
The Alice Interface.
Bunny Eat Broccoli Repetition – Simple loops and Conditional loops
Changing Color, Using Text Objects, and Random Selection in Alice
Let's Race! Typing on the Home Row
A Simple Quiz for Alice 3.2:
Chapter 2: Programming in Alice
Making Objects Move in Unison: Using Lists
Checking for Collisions: Using Functions in Alice
An Introduction to Programming Using Alice 2.2, Second Edition
Getting Started With Alice
Professor Susan Rodger
Professor Susan Rodger
Making Objects Move in Unison: Using Lists
Restricting Events Creating Conditional Events in Alice By Henry Qin
Presentation transcript:

An Introduction to Programming Using Alice 2.2, Second Edition Chapter 7 Recursive Algorithms

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

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 What Is Recursion? Recursion –Routine that has the ability to call itself –Important concept in computer programming –Not a simple concept

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

An Introduction to Programming Using Alice 2.2, Second Edition6

7

Recursive Algorithms (cont’d.) Algorithm for drawing a Sierpinski gasket An Introduction to Programming Using Alice 2.2, Second Edition 8

9

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

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

An Introduction to Programming Using Alice 2.2, Second Edition12

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

An Introduction to Programming Using Alice 2.2, Second Edition14

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

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

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

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

An Introduction to Programming Using Alice 2.2, Second Edition19

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

An Introduction to Programming Using Alice 2.2, Second Edition21

An Introduction to Programming Using Alice 2.2, Second Edition22

An Introduction to Programming Using Alice 2.2, Second Edition 23 Tutorial 7A - Creating a Recursive Method (cont’d.) Coding the recursion

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

An Introduction to Programming Using Alice 2.2, Second Edition25

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

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

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

An Introduction to Programming Using Alice 2.2, Second Edition29

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

An Introduction to Programming Using Alice 2.2, Second Edition31

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

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

An Introduction to Programming Using Alice 2.2, Second Edition34

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.)

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

An Introduction to Programming Using Alice 2.2, Second Edition37

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

An Introduction to Programming Using Alice 2.2, Second Edition39

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

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

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