130 likes | 397 Views
이산수학 (Discrete Mathematics) 재귀 알고리즘 (Recursive Algorithms). 2014 년 봄학기 강원대학교 컴퓨터과학전공 문양세. Introduction. Recursive Algorithms. Recursive definitions can be used to describe algorithms as well as functions and sets. ( 재귀적 정의를 수행한 경우 , 손쉽게 알고리즘의 함수 / 집합으로 기술할 수 있다 .)
E N D
이산수학(Discrete Mathematics) 재귀 알고리즘 (Recursive Algorithms) 2014년 봄학기 강원대학교 컴퓨터과학전공 문양세
Introduction Recursive Algorithms • Recursive definitions can be used to describe algorithms as well as functions and sets.(재귀적 정의를 수행한 경우, 손쉽게 알고리즘의 함수/집합으로 기술할 수 있다.) • 예제: A procedure to compute an. procedurepower(a≠0: real, nN) ifn = 0 then return 1elsereturna · power(a, n−1)
Efficiency of Recursive Algorithms Recursive Algorithms • The time complexity of a recursive algorithm may depend critically on the number of recursive calls it makes.(재귀 호출에서의 시간 복잡도는 재귀 호출 횟수에 크게 의존적이다.) • 예제: 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 bnmodm, wherem≥2, n≥0, and 1≤b<m.
Modular Exponentiation Algorithm #1 Recursive Algorithms • Uses the fact that • bn= b·bn−1 and that • x·ymod m = x·(ymodm) mod m. procedure mpower(b≥1,n≥0,m>b N) {Returns bnmod m.}ifn=0 then return 1;elsereturn (b·mpower(b,n−1,m)) modm; Note this algorithm takes (n) steps!
Modular Exponentiation Algorithm #2 Recursive Algorithms • Uses the fact that • b2k = bk·2 = (bk)2, • x·ymod m = x·(ymodm) mod m, and • x·xmod m = (x mod m)2mod m. procedurempower(b,n,m) {same signature} if n=0 then return 1else if 2|nthenreturnmpower(b,n/2,m)2modmelse return (mpower(b,n−1,m)·b) modm (첫 번째 mpower()는 한번 call되고, 그 값을 제곱하는 것임) (log n) steps What is its time complexity?
A Slight Variation of Algorithm #2 Recursive Algorithms Nearly identical but takes (n) time instead! procedurempower(b,n,m) {same signature} if n=0 then return 1else if 2|nthenreturn (mpower(b,n/2,m)·mpower(b,n/2,m)) modmelse return (mpower(b,n−1,m)·b) modm The number of recursive calls made is critical.
Recursive Euclid’s Algorithm (예제) Recursive Algorithms proceduregcd(a,bN)ifa = 0 thenreturn belse returngcd(bmoda, a) 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를 차지한다.)
Recursion vs. Iteration (1/3) Recursive Algorithms Factorial – Recursion procedurefactorial(nN)ifn = 1 thenreturn 1else returnn factorial(n – 1) Factorial – Iteration procedurefactorial(nN)x := 1 fori := 1 to n x := i xreturnx
Recursion vs. Iteration (2/3) Recursive Algorithms Fibonacci – Recursion procedurefibonacci(n: nonnegative integer)if (n = 0 or n = 1) thenreturn nelse return (fibonacci(n–1) + fibonacci(n–2)) Fibonacci – Iteration procedurefibonacci(n: nonnegative integer)ifn = 0 thenreturn 0 else x := 0, y := 1 fori := 1 to n – 1 z := x + y, x := y, y := zreturn z
Recursion vs. Iteration (3/3) Recursive Algorithms 재귀(recursion)는 프로그램을 간단히 하고, 이해하기가 쉬운 장점이 있는 반면에, 각 호출이 스택을 사용하므로, depth가 너무 깊어지지 않도록 조심스럽게 프로그래밍해야 함 컴퓨터에게 보다 적합한 방법은 반복(iteration)을 사용한 프로그래밍이나, 적당한 범위에서 재귀를 사용하는 것이 바람직함
Merge Sort (예제) (1/2) Recursive Algorithms procedure sort(L = 1,…, n)ifn>1 thenm := n/2 {this is rough ½-way point}L := merge(sort(1,…, m), sort(m+1,…, n))return L The merge takes (n) steps, and merge-sort takes(n log n).
Merge Sort (예제) (2/2) – skip Recursive Algorithms The Merge Routine proceduremerge(A, B: sorted lists)L = empty listi:=0, j:=0, k:=0whilei<|A| j<|B| {|A| is length of A} ifi=|A| thenLk := Bj; j := j + 1else if j=|B| thenLk := Ai; i := i + 1else if Ai < Bjthen Lk := Ai; i := i + 1elseLk := Bj; j := j + 1k := k+1returnL
Homework #5 Recursive Algorithms