Upload
patience-park
View
295
Download
0
Embed Size (px)
Citation preview
이산수학 (Discrete Mathematics)
3.5 재귀 알고리즘 (Recursive Algorithms)
20062006 년 봄학기년 봄학기
문양세문양세강원대학교 컴퓨터과학과강원대학교 컴퓨터과학과
Page 2Discrete Mathematicsby Yang-Sae Moon
IntroductionIntroduction3.5 Recursive Algorithms
Recursive definitions can be used to describe algorithms as
well as functions and sets.( 재귀적 정의를 수행한 경우 , 손쉽게 알고리즘의 함수 / 집합으로 기술할 수 있다 .)
예제 1: A procedure to compute an.
procedure power(a≠0: real, nN)if n = 0 then return 1else return a · power(a, n 1)−
Page 3Discrete Mathematicsby Yang-Sae Moon
Efficiency of Recursive AlgorithmsEfficiency of Recursive Algorithms
The time complexity of a recursive algorithm may depend crit
ically 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 bn mod m, where
m≥2, n≥0, and 1≤b<m.
3.5 Recursive Algorithms
Page 4Discrete Mathematicsby Yang-Sae Moon
Modular Exponentiation Algorithm Modular Exponentiation Algorithm #1#1
Uses the fact that
• bn = b·bn 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 bn mod m.}if n=0 then return 1 elsereturn (b·mpower(b,n 1,− m)) mod m
Page 5Discrete Mathematicsby Yang-Sae Moon
Modular Exponentiation Algorithm Modular Exponentiation Algorithm #2#2
Uses the fact that b2k = bk·2 = (bk)2.
3.5 Recursive Algorithms
What is its time complexity?
procedure mpower(b,n,m) {same signature}
if n=0 then return 1else if 2|n then
return mpower(b,n/2,m)2 mod melse return (mpower(b,n 1,− m)·b) mod m
( 첫 번째 mpower() 는 한번 call 되고 , 그 값을 제곱하는
것임 )
(log n) steps
Page 6Discrete Mathematicsby Yang-Sae Moon
A Slight Variation of Algorithm #2A 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
Page 7Discrete Mathematicsby Yang-Sae Moon
Recursive Euclid’s Algorithm (Recursive Euclid’s Algorithm ( 예제 예제 4)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,bN)
if a = 0 then return b
else return gcd(b mod a, a)
Page 8Discrete Mathematicsby Yang-Sae Moon
Recursion vs. Iteration (1/3)Recursion vs. Iteration (1/3)3.5 Recursive Algorithms
Factorial – Recursion
procedure factorial(nN)if n = 1 then return nelse return n factorial(n – 1)
Factorial – Iteration
procedure factorial(nN)x := 1for i := 1 to n
x := i xreturn x
Page 9Discrete Mathematicsby Yang-Sae Moon
Recursion vs. Iteration (2/3)Recursion vs. Iteration (2/3)3.5 Recursive Algorithms
Fibonacci – Recursion
procedure fibonacci(n: nonnegative integer)if (n = 0 or n = 1) then return nelse return (factorial(n–1) + factorial(n–2))
Fibonacci – Iteration
procedure fibonacci(n: nonnegative integer)if n = 0 then return 0else
x := 0, y := 1for i := 1 to n – 1
z := x + y, x := y, y := zreturn z
Page 10Discrete Mathematicsby Yang-Sae Moon
3.5 Recursive Algorithms
재귀 (recursion) 는 프로그램을 간단히 하고 , 이해하기가
쉬운 장점이 있는 반면에 ,
각 호출이 스택을 사용하므로 , Depth 가 너무 깊어지지
않도록 조심스럽게 프로그래밍해야 함
컴퓨터에게 보다 적합한 방법은 반복 (iteration) 을 사용한
프로그래밍이나 , 적당한 범위에서 재귀를 사용하는 것이
바람직함
Recursion vs. Iteration (3/3)Recursion vs. Iteration (3/3)
Page 11Discrete Mathematicsby Yang-Sae Moon
Merge Sort (Merge Sort ( 예제 예제 8) (1/2)8) (1/2)
The merge takes The merge takes ((nn) steps, and merge-sort takes) steps, and merge-sort takes
((nn log log nn).).
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
Page 12Discrete Mathematicsby Yang-Sae Moon
The Merge RoutineThe Merge Routine
3.5 Recursive Algorithms
procedure merge(A, B: sorted lists)L = empty listi:=0, j:=0, k:=0while i<|A| j<|B| {|A| is length of A} if i=|A| then Lk := Bj; j := j + 1
else if j=|B| then Lk := Ai; i := i + 1
else if Ai < Bj then Lk := Ai; i := i + 1
else Lk := Bj; j := j + 1
k := k+1return L
Merge Sort (Merge Sort ( 예제 예제 8) (2/2)8) (2/2)
Page 13Discrete Mathematicsby Yang-Sae Moon
Homework #5Homework #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