Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion.

Slides:



Advertisements
Similar presentations
The Towers of Hanoi or Apocalypse When?.
Advertisements

C++ Programming:. Program Design Including
Analyzing Algorithms and Problems Prof. Sin-Min Lee Department of Computer Science.
2010/12/12
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
The Tower of Hanoi Ben Epstein Special Topics 2003.
: Arrange the Numbers ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11481: Arrange the Numbers 解題者:李重儀 解題日期: 2008 年 9 月 13 日 題意: 將數列 {1,2,3, …,N}
1 Q10276: Hanoi Tower Troubles Again! 星級 : ★★★ 題組: Online-judge.uva.es PROBLEM SET Volume CII 題號: Q10276: Hanoi Tower Troubles Again! 解題者:薛祖淵 解題日期: 2006.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
Chapter 10 Recursion Instructor: alkar/demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.10-2 Recursive Function recursive functionThe.
消費者物價指數反映生活成本。當消費者物價指數上升時,一般家庭需要花費更多的金錢才能維持相同的生活水準。經濟學家用物價膨脹(inflation)來描述一般物價持續上升的現象,而物價膨脹率(inflation rate)為物價水準的變動百分比。
Network Connections ★★★☆☆ 題組: Contest Archive with Online Judge 題號: Network Connections 解題者:蔡宗翰 解題日期: 2008 年 10 月 20 日 題意:給你電腦之間互相連線的狀況後,題.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
短缺,盈餘與均衡. 遊戲規則  老師想出售一些學生喜歡的小食。  老師首先講出價錢,有興趣買的請舉手。
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
: Beautiful Numbers ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11472: Beautiful Numbers 解題者:邱經達 解題日期: 2011 年 5 月 5 日 題意: 若一個 N 進位的數用到該.
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
: GCD - Extreme II ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11426: GCD - Extreme II 解題者:蔡宗翰 解題日期: 2008 年 9 月 19 日 題意: 最多 20,000 組測資,題目會給一個數字.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
: Ubiquitous Religions ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10583: Ubiquitous Religions 解題者:吳佳樺 解題日期: 2010 年 3 月 18 日 題意: 一開始給予兩個數字.
-Antidifferentiation- Chapter 6 朝陽科技大學 資訊管理系 李麗華 教授.
Introduction to Java Programming Lecture 9 Flow Control : while do-while and for loops II.
The effect of task on the information-related behaviors of individuals in a work-group environment. The effect of task on the information-related behaviors.
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
23802 兒童成長與護理 兒童成長與護理專題講座 講者:方向敏. 在了解孩子在生理、心理及 社交上的成長過程及 會面對的問題後, 更能有效地掌握 與孩子相處之道, 共同建立愉快健康的家庭。
2005/7 Linear system-1 The Linear Equation System and Eliminations.
: Problem E Antimatter Ray Clearcutting ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11008: Problem E Antimatter Ray Clearcutting 解題者:林王智瑞.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
Visual C++重點複習.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
:Problem E.Stone Game ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10165: Problem E.Stone Game 解題者:李濟宇 解題日期: 2006 年 3 月 26 日 題意: Jack 與 Jim.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
Data Structures Using C++ 2E Chapter 6 Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Data Structures Using C++ 2E Chapter 6 Recursion.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
Ceng-112 Data Structures I Chapter 6 Recursion.
Computer Science: A Structured Programming Approach Using C1 6-9 Recursion In general, programmers use two approaches to writing repetitive algorithms.
RECURSION.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 12 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Recursion Chapter 2 Objectives Upon completion you will be able to:
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.
Advanced Computer Architecture and Parallel Processing Rabie A. Ramadan http:
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.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
Discrete Mathematics Section 3.7 Applications of Number Theory 大葉大學 資訊工程系 黃鈴玲.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
資料結構 Data Structures 資料結構 2 Chapter 2 Recursion. 2 What ’ s Recursion? Definition An algorithmic technique where a function, in order to accomplish a.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion Chapter 2 Objectives Upon completion you will be able to:
CS212: Data Structures and Algorithms
Recursion (Continued)
Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
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 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Recursion Data Structures.
Presentation transcript:

Data Structures: A Pseudocode Approach with C1 Chapter 2 Objectives Upon completion you will be able to: Explain the difference between iteration and recursion Design a recursive algorithm Determine when an recursion is an appropriate solution Write simple recursive functions Recursion

Data Structures: A Pseudocode Approach with C2 How to writing repetitive algorithms Iteration Recursion Is a repetitive process in which an algorithm calls itself. 請同學區分這三個字 repetitive 、 iterative 、 recursive , 特別是看中文書的同學。

Data Structures: A Pseudocode Approach with C3 2-1 Factorial - A Case Study We begin the discussion of recursion with a case study and use it to define the concept. This section also presents an iterative and a recursive solution to the factorial algorithm. Recursive Defined Recursive Solution

Data Structures: A Pseudocode Approach with C4 Iterative The definition involves only the algorithm parameter(s) and not the algorithm itself.

Data Structures: A Pseudocode Approach with C5 Recursive A repetitive algorithm use recursion whenever the algorithm appears within the definition itself.

Data Structures: A Pseudocode Approach with C6

7 Note that Recursion is a repetitive process in which an algorithm called itself.

Data Structures: A Pseudocode Approach with C8

9

10 Which code is simpler? Which one has not a loop?

Data Structures: A Pseudocode Approach with C11 Calling a recursive algorithm

Data Structures: A Pseudocode Approach with C Designing Recursive Algorithms In this section we present an analytical approach to designing recursive algorithms. We also discuss algorithm designs that are not well suited to recursion. The Design Methodology Limitation of Recusion Design Implemenation

Data Structures: A Pseudocode Approach with C13 The Design Methodology Every recursive call either solves a part of the problem or it reduce the size of the problem.

Data Structures: A Pseudocode Approach with C14 The Design Methodology Base case The statement that “solves” the problem. Every recursive algorithm must have a base case. General case The rest of the algorithm Contains the logic needed to reduce the size of the problem.

Data Structures: A Pseudocode Approach with C15 Rules for designing a recursive algorithm 1.Determine the base case. 2.Determine the general case. 3.Combine the base case and the general cases into an algorithm.

Data Structures: A Pseudocode Approach with C16 Combine the base case and the general cases into an algorithm Each call must reduce the size of the problem and move it toward the base case. The base case, when reached, must terminate without a call to the recursive algorithms; that is, it must execute a return.

Data Structures: A Pseudocode Approach with C17 Limitations of recursion You should not use recursion if the answer to any of the following questions is no: 1.Is the algorithm or data structure naturally suited to recursion? 2.Is the recursive solution shorter and more understandable? 3.Does the recursive solution run within acceptable time and space limits?

Data Structures: A Pseudocode Approach with C18 Design implementation – reverse keyboard input

Data Structures: A Pseudocode Approach with C19 data=6 data=20 data=14 data=5 ※ 請注意 print data 在什麼時候執行

Data Structures: A Pseudocode Approach with C Recursive Examples Slide 05 Four recursive programs are developed and analyzed. Only one, the Towers of Hanoi, turns out to be a good application for recursion. Greatest Common Divisor Fiboncci Numbers Prefix to Postfix Conversion The Towers of Honoi

Data Structures: A Pseudocode Approach with C21 GCD design Greatest Common Divisor Recursive Definition

Data Structures: A Pseudocode Approach with C22 Pseudocode 歐基里德 輾轉相除法

Data Structures: A Pseudocode Approach with C23 GCD C implementation

Data Structures: A Pseudocode Approach with C24 gcd(10, 25) gcd(25, 10) gcd(10, 5) gcd(5, 0)

Data Structures: A Pseudocode Approach with C25 Another G.C.D. Recursive Definition

Data Structures: A Pseudocode Approach with C26 Fibonacci numbers 在費波那西的〈算經〉中提到一個問題: 假如一對兔子,一個月後能生產一對兔子, 而新生兔子在一個月後便具備生育能力, 也能生下一對兔子。 那麼若從一對兔子開始,一年之後將會有 多少對兔子?

Data Structures: A Pseudocode Approach with C27 Fibonacci numbers

Data Structures: A Pseudocode Approach with C28 Fibonacci numbers -- 費式數 Each number is the sum of the previous two numbers. The first few numbers in the Fibonacci series are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …

Data Structures: A Pseudocode Approach with C29 Fibonacci numbers -- 費式數

Data Structures: A Pseudocode Approach with C30 Fibonacci numbers

Data Structures: A Pseudocode Approach with C31 Fibonacci numbers – an example

Data Structures: A Pseudocode Approach with C32 0 represents.T.

Data Structures: A Pseudocode Approach with C33 (Continued)

Data Structures: A Pseudocode Approach with C34 Analysis

Data Structures: A Pseudocode Approach with C35 We omit Prefix to Postfix Conversion The Towers of Honoi

Data Structures: A Pseudocode Approach with C36 HW2 Write a recursive algorithm to calculate the combination of n objects taken k at a time. Due date : ( 甲: 、乙: )

Data Structures: A Pseudocode Approach with C Recursive Examples Slide 05 Four recursive programs are developed and analyzed. Only one, the Towers of Hanoi, turns out to be a good application for recursion. The Towers of Honoi

Data Structures: A Pseudocode Approach with C38 Towers of Hanoi Problem: Invented by French mathematician Lucas in 1880s. Original problem set in India in a holy place called Benares. There are 3 diamond needles fixed on a brass plate. One needle contains 64 pure gold disks. Largest resting on the brass plate, other disks of decreasing diameters. Called tower of Brahma.

Data Structures: A Pseudocode Approach with C39 Towers of Hanoi Problem: Priests are supposed to transfer the disks from one needle to the other such that at no time a disk of larger diameter should sit on a disk of smaller diameter. Only one disk can be moved at a time. Later setting shifted to Honoi, but the puzzle and legend remain the same. How much time would it take? Estimate…..

Data Structures: A Pseudocode Approach with C40

Data Structures: A Pseudocode Approach with C41 Towers of Hanoi Problem: Today we know that we need to have

Data Structures: A Pseudocode Approach with C42 Recursive Towers of Hanoi Design Find a pattern of moves. Case 1: move one disk from source to destination needle.

Data Structures: A Pseudocode Approach with C43 Move two disks Case 2: Move one disk to auxiliary needle. Move one disk to destination needle. Move one disk to from auxiliary to destination needle.

Data Structures: A Pseudocode Approach with C44

Data Structures: A Pseudocode Approach with C45 Move three disks Case 3: Move two disks from source to auxiliary needle. Move one disk from source to destination needle. Move two disks from auxiliary to destination needle.

Data Structures: A Pseudocode Approach with C46 (Continued) Move two disks from source to auxiliary needle. Move two disks from auxiliary to destination needle.

Data Structures: A Pseudocode Approach with C47 Algorithm Tower of Hanoi Towers (numDisks, source, dest, auxiliary) numDisks is number of disks to be moved source is the source tower dest is the destination tower auxiliary is the auxiliary tower

Data Structures: A Pseudocode Approach with C48

Data Structures: A Pseudocode Approach with C49 Generalize Tower of Hanoi General case Move n-1 disks from source to auxiliary. Base case Move one disk from source to destination. General case Move n-1 disks from auxiliary to destination.

Data Structures: A Pseudocode Approach with C50

Data Structures: A Pseudocode Approach with C51

Data Structures: A Pseudocode Approach with C52

Data Structures: A Pseudocode Approach with C53

Data Structures: A Pseudocode Approach with C54

Data Structures: A Pseudocode Approach with C55