Massachusetts Institute of Technology Department of Mechanical Engineering

Similar documents
Solving problems by recursion

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Test Bank Ver. 5.0: Data Abstraction and Problem Solving with C++: Walls and Mirrors, 5 th edition, Frank M. Carrano

2: Complexity and Recursion

05-15/17 Discussion Notes

CmpSci 187: Programming with Data Structures Spring 2015

Chapter 15: Recursion

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

Data Abstraction & Problem Solving with C++: Walls and Mirrors 6th Edition Carrano, Henry Test Bank


Chapter 6 Recursion. The Concept of Recursion

Reading 8 : Recursion

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

UNIT 5A Recursion: Basics. Recursion

Linear Data Structure

CS200: Recursion and induction (recap from cs161)

CSE 214 Computer Science II Recursion

Recursion Chapter 3.5

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

Assembly Language Manual for the STACK Computer

COMP2611: Computer Organization MIPS function and recursion

Chapter 2A Instructions: Language of the Computer

12/30/2013 S. NALINI,AP/CSE

CSE548, AMS542: Analysis of Algorithms, Fall 2012 Date: October 16. In-Class Midterm. ( 11:35 AM 12:50 PM : 75 Minutes )

Goals: Define computational problem instance of a computational problem algorithm pseudocode Briefly discuss recursive procedure big-o notation

To illustrate what is intended the following are three write ups by students. Diagonalization

WYSE Academic Challenge 2002 Computer Science Test (Sectional) SOLUTION

We cover recursion in 150. Why do it again in 151?

Inference rule for Induction

Resources matter. Orders of Growth of Processes. R(n)= (n 2 ) Orders of growth of processes. Partial trace for (ifact 4) Partial trace for (fact 4)

CS 11 C track: lecture 8

UNIT 5A Recursion: Basics. Recursion

RECURSION. Problem Solving with Computers-II 6

Introduction to Computers & Programming

Recursion (Part 2) 1

Clicker Question What will this print? def blah(x): return x+1 def main(): z = blah(3) print(z) main() A) 3 B) 4 C) 5 D) It causes an error

Biostatistics 615/815 Statistical Computing

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Recursive Algorithms II

Introduction to Computer Programming for Non-Majors

A function that invokes itself is said to

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

Solutions to Problem 1 of Homework 3 (10 (+6) Points)

1.7 Recursion. Department of CSE

COSC2430: Programming and Data Structures Sparse Matrix Multiplication: Search and Sorting Algorithms

Programming II (CS300)

Wednesday, February 4, Chapter 4

CS302 Data Structures using C++

WJEC MATHEMATICS INTERMEDIATE NUMBER STANDARD FORM

CIT Week13 Lecture

Recursion. Thinking Recursively. Tracing the Recursive Definition of List. CMPT 126: Lecture 10. Recursion

Biostatistics 615/815 - Lecture 2 Introduction to C++ Programming

Chapter 2. Instructions:

Data Structures And Algorithms

Names and Functions. Chapter 2

Run-time Environments

Run-time Environments

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

CSC 148 Lecture 3. Dynamic Typing, Scoping, and Namespaces. Recursion

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

Virtual Machine Where we are at: Part I: Stack Arithmetic. Motivation. Compilation models. direct compilation:... 2-tier compilation:

Recursive Definitions

Programming II (CS300)

Programming Paradigms

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

EXERCISES SOFTWARE DEVELOPMENT I. 10 Recursion, Binary (Search) Trees Towers of Hanoi // Tree Traversal 2018W

CSE 230 Intermediate Programming in C and C++ Recursion

Algorithmic Methods Tricks of the Trade

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Functions in MIPS. Functions in MIPS 1

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

def F a c t o r i a l ( n ) : i f n == 1 : return 1 else : return n F a c t o r i a l ( n 1) def main ( ) : print ( F a c t o r i a l ( 4 ) )

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

CSE 546, Fall, 2016 Homework 1

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

GE PROBLEM SOVING AND PYTHON PROGRAMMING. Question Bank UNIT 1 - ALGORITHMIC PROBLEM SOLVING

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Recursion. Chapter 7

Analysis of Algorithm. Chapter 2

Recursion Solution. Counting Things. Searching an Array. Organizing Data. Backtracking. Defining Languages

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Programming II (CS300)

Odds and Ends. Think about doing research Programming grading. Assignment 3 due Tuesday

Algorithm. Building blocks of algorithm

Python Programming: An Introduction to Computer Science

Bisection method. we can implement the bisection method using: a loop to iterate until f(c) is close to zero a function handle to the function f

LIFO : Last In First Out

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Introduction to Computer Programming for Non-Majors

recursive algorithms 1

ECE331: Hardware Organization and Design

Recursion. notes Chapter 8

CMPSCI 187: Programming With Data Structures. Lecture #15: Thinking Recursively David Mix Barrington 10 October 2012

Transcription:

Massachusetts Institute of Technology Department of Mechanical Engineering 2.003J/1.053J Dynamics & Control I Fall 2007 Homework 5 Solution Problem 5.1 : Calculating the factorial of non-negative integer with recursion n! is associated with ( n 1 )! as below: n! = n ( n 1 ) L 2 1 = n ( n 1 )! Therefore, the function of calculating n! is achieved by calling itself for ( n 1 )! and multiplying n. m-file is shown as below: function o=fctrlrc(n) Problem 5.1 : Calculating the factorial of non-negative integer with recursion if n==0 o=1; fctrlrc(0)=1 else o=n*fctrlrc(n-1); Use recursion end The output of 125! will be displayed as below. >> fctrlrc(125) Calculate 125! ans = 1.8827e+209

Problem 5.2 : Developing the Algorithm for the Solution of the Tower of Hanoi i) Now, you have 2 different size disks, the small and the big one, in the tower of Hanoi. First, top disk (the small one) on the starting post can be moved to either intermediate post or ending post. Since ending post should be reserved for the big disk, the small one is moved to intermediate post (from 1 to 2). Next, remaining disk (the big one) on the staring post should be stacked on the ending post since there is no disk on this post. (from 1 to 3). Finally, the disk placed on the intermediate post is also stacked on the ending post. 1: from 1 to 2 2: from 1 to 3 3: from 2 to 3 ii) Assume that you have N different size disks for the tower of Hanoi. They are located on the starting post. Now, we define function MoveTower(N,sp,ep,ip) to move whole N disks from the starting post (sp) to ending post (ep) with using intermediate disk (ip). This function can be rewritten with MoveTower associated with (N-1) disks movement. To move whole disks from one to another, at least the biggest disk should move from sp to ep. To achieve this task, (N-1) disks on top of the biggest should move from sp to ip at first. After moving the biggest disk, (N-1) disk on the intermediate post (ip) should be stacked to the ending post (ep). This procedure for recursive algorithm is also described in below figures. 1 3 [sp ep] 2 sp ip ep MoveTower(N-1,sp,ip,ep) 4 sp ip ep MoveTower(N-1,ip,ep,sp) sp ip ep sp ip ep In addition to general case, special case N=0 should be considered. Since MoveTower(0,sp,ep,ip) means that there is no disk to move, nothing will be

done. Therefore, following description is for the recursive algorithm for the solution of the tower of Hanoi with N different size disks 1. If N is smaller than 1, just return (in the special case of MoveTower(0,sp,ep,ip)) 2. Output Å MoveTower(N-1,sp,ip,ep) (move N-1 disks from sp to ip) 3. Output Å [sp ep] (move the biggest disk from sp to ep) 4. Output Å MoveTower(N-1,ip,ep,sp) (move N-1 disks from ip to ep) Problem 5.3 : Solving the Tower of Hanoi with recursive algorithm By using your own algorithm, the function to solve the tower of Hanoi is shown as below. Please, see the comments for each line. function o=hanoi(n) Problem 5.2 Solve the tower of Hanoi with n different disks with recursion algorithm Input argument n: number of disks Output argument o: solution matrix The first column: post number which disk moves from The second column: post number which disk moves to Number all the posts sp=1; starting post are numbered to '1' ep=3; ending post are numbered to '3' ip=2; intermediate post are numbered to '2' Calculate Hanoitower wirh reccursion Move all the disks on the leftmost side post to the rightmost side post o=movetower(n,sp,ep,ip);

function o=movetower(n,sp,ep,ip) The solution of whole n disk tower movement one post to the other post It can be also expressed with whole (n-1) disk tower movent n: number of disk in the start post a: start post number b: end post number c: immediate post number Define output matrix o=[]; if there is no disk, simply return without movement o is still empty if n<=0, return; end Move whole disks except for the bottom one from starting post to temporary post o=[o ; MoveTower(n-1,sp,ip,ep)]; Move the remaining disk on starting post to ending post o=[o ; sp ep]; Move whole disks on the temporary post to ending post o=[o ; MoveTower(n-1,ip,ep,sp)]; The solution of the tower of Hanoi with 5 different size disks is represented as below: >> hanoi(5) ans =

3 1 3 1 Problem 5.4 : (Optional) Comparison between solving problem with and without recursion. Two m-code works same in terms of the output of calculating the factorials. However, they are not quite different in the view of computer and programming performance. First, the program with the recursive algorithm is very concise, and easy to understand. Recursive algorithm requires so many function calls, and it needs stack memories to keep current position of program counter and some variables for each function call. In addition, function call needs much more

CPU time than normal operations. Eventually, running the code with recursion gives the value too late where large number of recursion is necessary. In case of programming without recursion, program itself can be a little complicated, but it runs faster than recursive version. However, it is sometimes much more useful to solve the problem recursively than sequentially like the tower of Hanoi.