이산수학 (Discrete Mathematics) 3.5 재귀 알고리즘 (Recursive Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.

Slides:



Advertisements
Similar presentations
22C:19 Discrete Structures Induction and Recursion Spring 2014 Sukumar Ghosh.
Advertisements

22C:19 Discrete Structures Induction and Recursion Fall 2014 Sukumar Ghosh.
이산수학(Discrete Mathematics)
Induction and Recursion. Odd Powers Are Odd Fact: If m is odd and n is odd, then nm is odd. Proposition: for an odd number m, m k is odd for all non-negative.
Based on Rosen, Discrete Mathematics & Its Applications, 5e Prepared by (c) Michael P. Frank Modified by (c) Haluk Bingöl 1/18 Module.
Discrete Structures & Algorithms More on Methods of Proof / Mathematical Induction EECE 320 — UBC.
Recursion Rosen 5 th ed., § Recursion Sometimes, defining an object explicitly might be difficult.Sometimes, defining an object explicitly might.
CSE115/ENGR160 Discrete Mathematics 04/05/11 Ming-Hsuan Yang UC Merced 1.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Csci1300 Introduction to Programming Recursion Dan Feng.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
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.
Section Section Summary Recursive Algorithms Proving Recursive Algorithms Correct Recursion and Iteration (not yet included in overheads) Merge.
Module #1 - Logic 1 Based on Rosen, Discrete Mathematics & Its Applications. Prepared by (c) , Michael P. Frank. Modified By Mingwu Chen Induction.
Fibonacci numbers Fibonacci numbers:Fibonacci numbers 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,... where each number is the sum of the preceding two. Recursive.
22C:19 Discrete Math Induction and Recursion Fall 2011 Sukumar Ghosh.
Induction and recursion
Discrete Mathematics Chapter 4 Induction and Recursion 大葉大學 資訊工程系 黃鈴玲 (Lingling Huang)
Recursion. Basic problem solving technique is to divide a problem into smaller subproblems These subproblems may also be divided into smaller subproblems.
2015 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) 수열과 합 (Sequences and Summations)
Discrete Mathematics and Its Applications Sixth Edition By Kenneth Rosen Chapter 4 Induction and Recursion 歐亞書局.
2015 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) 행렬 (Matrices)
Chapter 4: Induction and Recursion
Chapter 3 (Part 3): Mathematical Reasoning, Induction & Recursion  Recursive Algorithms (3.5)  Program Correctness (3.6)
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 4 (Part 3): Mathematical Reasoning, Induction.
Recursion.  A recursive function contains a call to itself Example: the factorial n!=n*(n-1)! for n>1 n!=1 for n=1 int factorial (int n) { if (n == 0)
강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) 재귀 호출 (Recursion)
INDUCTION AND RECURSION. PRINCIPLE OF MATHEMATICAL INDUCTION To prove that P(n) is true for all positive integers n, where P(n) is a propositional function,
Module #14: Recursion Rosen 5 th ed., §§ In this class, we will study recursion, one of the most important topics in computer science. In the last.
Chapter 12 Recursion, Complexity, and Searching and Sorting
2015 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) 귀납과 재귀 (Induction and Recursion)
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Inductive Proofs. Mathematical Induction A powerful, rigorous technique for proving that a predicate P(n) is true for every natural number n, no matter.
CSE 311: Foundations of Computing Fall 2014 Lecture 12: Primes, GCD.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
이산수학 (Discrete Mathematics) 7.3 관계의 표현 (Representing Relations) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.
이산수학 (Discrete Mathematics) 2.5 정수와 알고리즘 (Integers and Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Recursive Algorithm (4.4) An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input.
2012 년 봄학기 강원대학교 컴퓨터과학전공 문양세 이산수학 (Discrete Mathematics) 수학적 귀납법 (Mathematical Induction)
Given a node v of a doubly linked list, we can easily insert a new node z immediately after v. Specifically, let w the be node following v. We execute.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Section Recursion  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Mathematical Induction And Recursion Discrete Math Team KS MATEMATIKA DISKRIT (DISCRETE MATHEMATICS ) 1.
Chapter 4: Induction and Recursion
Recursive Algorithms Section 5.4.
Chapter 4 (Part 3): Mathematical Reasoning, Induction & Recursion
CSE15 Discrete Mathematics 04/12/17
Introduction to Recursion
CS2210:0001Discrete Structures Induction and Recursion
Induction and Recursion
Recursively Defined Functions
Introduction to Computer Science - Alice
Quiz 6 The way I expected it..
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Applied Algorithms (Lecture 17) Recursion Fall-23
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Data Structures Review Session
Applied Discrete Mathematics Week 9: Integer Properties
Chapter 4 Induction and Recursion
Rosen 5th ed., §§ ~18 slides, ~1 lecture
Main() { int fact; fact = Factorial(4); } main fact.
Math/CSE 1019: Discrete Mathematics for Computer Science Fall 2011
Discrete Mathematics and its Applications
Induction and recursion
Discrete Mathematics and its Applications
Recursion.
Presentation transcript:

이산수학 (Discrete Mathematics) 3.5 재귀 알고리즘 (Recursive Algorithms) 2006 년 봄학기 문양세 강원대학교 컴퓨터과학과

Discrete Mathematics by Yang-Sae Moon Page 2 Introduction 3.5 Recursive Algorithms Recursive definitions can be used to describe algorithms as well as functions and sets. ( 재귀적 정의를 수행한 경우, 손쉽게 알고리즘의 함수 / 집합으로 기술할 수 있다.) 예제 1: A procedure to compute a n. procedure power(a≠0: real, n  N) if n = 0 then return 1 else return a · power(a, n−1)

Discrete Mathematics by Yang-Sae Moon Page 3 Efficiency of Recursive Algorithms The time complexity of a recursive algorithm may depend critically on the number of recursive calls it makes. ( 재귀 호출에서의 시간 복잡도는 재귀 호출 횟수에 크게 의존적이다.) 예제 2: Modular exponentiation to a power n can take log(n) time if done right, but linear time if done slightly differently. ( 잘하면 O(log(n)) 이나, 조금만 잘못하면 O(n) 이 된다.) Task: Compute b n mod m, where m≥2, n≥0, and 1≤b<m. 3.5 Recursive Algorithms

Discrete Mathematics by Yang-Sae Moon Page 4 Modular Exponentiation Algorithm #1 Uses the fact that b n = b·b n−1 and that x·y mod m = x·(y mod m) mod m. 3.5 Recursive Algorithms Note this algorithm takes  (n) steps! procedure mpower(b≥1,n≥0,m>b  N) {Returns b n mod m.} if n=0 then return 1 else return (b·mpower(b,n−1,m)) mod m

Discrete Mathematics by Yang-Sae Moon Page 5 Modular Exponentiation Algorithm #2 Uses the fact that b 2k = b k·2 = (b k ) Recursive Algorithms What is its time complexity? procedure mpower(b,n,m) {same signature} if n=0 then return 1 else if 2|n then return mpower(b,n/2,m) 2 mod m else return (mpower(b,n−1,m)·b) mod m ( 첫 번째 mpower() 는 한번 call 되고, 그 값을 제곱하는 것임 )  (log n) steps

Discrete Mathematics by Yang-Sae Moon Page 6 A Slight Variation of Algorithm #2 Nearly identical but takes  (n) time instead! 3.5 Recursive Algorithms The number of recursive calls made is critical. procedure mpower(b,n,m) {same signature} if n=0 then return 1 else if 2|n then return (mpower(b,n/2,m)·mpower(b,n/2,m)) mod m else return (mpower(b,n−1,m)·b) mod m

Discrete Mathematics by Yang-Sae Moon Page 7 Recursive Euclid’s Algorithm ( 예제 4) Note recursive algorithms are often simpler to code than iterative ones… (Recursion 이 코드를 보다 간단하게 하지만 …) However, they can consume more stack space, if your compiler is not smart enough. ( 일반적으로, recursion 은 보다 많은 stack space 를 차지한다.) 3.5 Recursive Algorithms procedure gcd(a,b  N) if a = 0 then return b else return gcd(b mod a, a)

Discrete Mathematics by Yang-Sae Moon Page 8 Recursion vs. Iteration (1/3) 3.5 Recursive Algorithms Factorial – Recursion procedure factorial(n  N) if n = 1 then return n else return n  factorial(n – 1) Factorial – Iteration procedure factorial(n  N) x := 1 for i := 1 to n x := i  x return x

Discrete Mathematics by Yang-Sae Moon Page 9 Recursion vs. Iteration (2/3) 3.5 Recursive Algorithms Fibonacci – Recursion procedure fibonacci(n: nonnegative integer) if (n = 0 or n = 1) then return n else return (factorial(n–1) + factorial(n–2)) Fibonacci – Iteration procedure fibonacci(n: nonnegative integer) if n = 0 then return 0 else x := 0, y := 1 for i := 1 to n – 1 z := x + y, x := y, y := z return z

Discrete Mathematics by Yang-Sae Moon Page Recursive Algorithms 재귀 (recursion) 는 프로그램을 간단히 하고, 이해하기가 쉬 운 장점이 있는 반면에, 각 호출이 스택을 사용하므로, Depth 가 너무 깊어지지 않도 록 조심스럽게 프로그래밍해야 함 컴퓨터에게 보다 적합한 방법은 반복 (iteration) 을 사용한 프 로그래밍이나, 적당한 범위에서 재귀를 사용하는 것이 바람 직함 Recursion vs. Iteration (3/3)

Discrete Mathematics by Yang-Sae Moon Page 11 Merge Sort ( 예제 8) (1/2) The merge takes (n) steps, and merge-sort takes (n log n). The merge takes  (n) steps, and merge-sort takes  (n log n). 3.5 Recursive Algorithms procedure sort(L = 1,…, n ) if n>1 then m :=  n/2  {this is rough ½-way point} L := merge(sort( 1,…, m ), sort( m+1,…, n )) return L

Discrete Mathematics by Yang-Sae Moon Page 12 The Merge Routine 3.5 Recursive Algorithms procedure merge(A, B: sorted lists) L = empty list i:=0, j:=0, k:=0 while i<|A|  j<|B| {|A| is length of A} if i=|A| then L k := B j ; j := j + 1 else if j=|B| then L k := A i ; i := i + 1 else if A i < B j then L k := A i ; i := i + 1 else L k := B j ; j := j + 1 k := k+1 return L Merge Sort ( 예제 8) (2/2)

Discrete Mathematics by Yang-Sae Moon Page 13 Homework #5 $3.1 의 연습문제 : 1, 3, 32 A hint for 32: prove it by cases (n = 2k and n = 2k + 1) $3.2 의 연습문제 : 2(c,d), 9(d,f), 18(b,d) $3.3 의 연습문제 : 3, 8, 15 $3.4 의 연습문제 : 2(a,c), 8(b) $3.5 의 연습문제 : 2, 4 Due Date: 3.5 Recursive Algorithms