Discrete Mathematics

이산수학(Discrete Mathematics)
분할과 정복
(Divide and Conquer)
강원대학교 컴퓨터과학전공 문양세
Divide & Conquer Recurrence Relations
6.3 Divide and Conquer
Main points so far:
Many types of problems are solvable by reducing a
problem of size n into some number a of independent subproblems, each of size n/b, where a1 and b>1.
(많은 경우에 있어서, 크기 n의 문제를 a개의 크기 n/b의 작은 문제로 바꾸어
처리할 수 있다.)
The time complexity to solve such problems is given by a
recurrence relation: T(n) = a·T(n/b) + g(n)
(이런 경우의 시간 복잡도는 T(n)의 점화 관계로 나타낼 수 있다.)
Time to break problem
up into sub-problems
Time for each sub-problem
Page 2
Discrete Mathematics
by Yang-Sae Moon
Divide & Conquer Examples
6.3 Divide and Conquer
Binary search(예제 1): Break list into 1 sub-problem
(smaller list) (so a=1) of size n/2 (so b=2).
• So T(n) = T(n/2)+c
(g(n)=c constant)
Merge sort(예제 3): Break list of length n into 2 sub-lists
(a=2), each of size n/2 (so b=2), then merge them,
in g(n) = (n) time.
• So T(n) = 2T(n/2) + cn (roughly, for some c)
Page 3
Discrete Mathematics
by Yang-Sae Moon
Fast Multiplication Example (1/3)
6.3 Divide and Conquer
The ordinary grade-school algorithm takes (n2) steps to
multiply two n-digit numbers.
(학교에서 배운 방법에 따르면, n자리인 두 수의 곱은 (n2) 스텝이 필요하다.)
• This seems like too much work!
So, let’s find an faster multiplication algorithm!
To find the product cd of two 2n-digit base-b numbers,
c=(c2n-1c2n-2…c0)b and d=(d2n-1d2n-2…d0)b,
first, we break c and d in half:
c=bnC1+C0, d=bnD1+D0,
and then... (see next slide)
Page 4
Discrete Mathematics
by Yang-Sae Moon
Fast Multiplication Example (2/3)
6.3 Divide and Conquer
cd  (b nC1  C0 )(b n D1  D0 )
 b 2 n C1 D1  b n (C1 D0  C0 D1 )  C0 D0
Zero
 b 2 n C1 D1  C0 D0 
b n (C1 D0  C0 D1  (C1 D1  C1 D1 )  (C0 D0  C0 D0 ))
 (b 2 n  b n )C1 D1  (b n  1)C0 D0 
b n (C1 D0  C1 D1  C0 D0  C0 D1 )
 (b 2 n  b n )C1 D1  (b n  1)C0 D0 
b n (C1  C0 )( D0  D1 )
(Factor last polynomial)
Three multiplications, each with n-digit numbers
Page 5
Discrete Mathematics
by Yang-Sae Moon
Fast Multiplication Example (3/3)
6.3 Divide and Conquer
Notice that the time complexity T(n) of the fast
multiplication algorithm obeys the recurrence:
T(2n)=3T(n)+(n)
Time to do the needed adds & subtracts
of n-digit and 2n-digit numbers
i.e.,
T(n)=3T(n/2)+(n)
So a=3, b=2. ( The order will be reduced …)
Page 6
Discrete Mathematics
by Yang-Sae Moon
The Master Theorem
6.3 Divide and Conquer
Consider a function f(n) that, for all n=bk for all kZ+,
satisfies the recurrence relation:
(n=bk 일 때, 다음 점화 관계가 성립하면)
f(n) = af(n/b) + cnd
with a≥1, integer b>1, real c>0, d≥0. Then:
if a  b d
 O(nd )

f (n)   O(nd log n) if a  b d

log b a
d
O(
n
)
if
a

b

Proof of the theorem is …. omitted.
Page 7
Discrete Mathematics
by Yang-Sae Moon
Master Theorem Examples (1/3)
6.3 Divide and Conquer
Recall that complexity of fast multiplication was:
T(n)=3T(n/2)+(n)
Thus, a=3, b=2, d=1. So a > bd, so case 3 of the master
theorem applies, so:
T (n)  O(nlogb a )  O(nlog 2 3 )
which is O(n1.58…), so the new algorithm is strictly faster
than ordinary (n2) multiply!
Page 8
Discrete Mathematics
by Yang-Sae Moon
Master Theorem Examples (2/3)
6.3 Divide and Conquer
예제 7(Binary Search):
이진 탐색의 복잡도는 얼마인가(비교 수를 추정하라)?
•
이진 탐색의 점화 관계: T(n) = T(n/2)+c (n 이 짝수라 가정)
•
매스터 정리로 보면, a = 1, b = 2, d = 0으로서,
a = 1 = bd인 두 번째 경우에 해당한다.
•
결국, 다음과 같은 과정에 의해 O(logn)이 된다.
T (n)  O(nd log n)  O(n0 log n)  O(log n)
Page 9
Discrete Mathematics
by Yang-Sae Moon
Master Theorem Examples (3/3)
6.3 Divide and Conquer
예제 9(Merge Sort):
합병 정렬의 복잡도는 얼마인가(비교 수를 추정하라)?
•
이진 탐색의 점화 관계: T(n) = 2T(n/2)+cn (n 이 짝수라 가정)
•
매스터 정리로 보면, a = 2, b = 2, d = 1로서,
a = 2 = bd인 두 번째 경우에 해당한다.
•
결국, 다음과 같은 과정에 의해 O(nlogn)이 된다.
T (n)  O(nd log n)  O(n1 log n)  O(n log n)
Page 10
Discrete Mathematics
by Yang-Sae Moon
Homework #7
6.3 Divide and Conquer
$5.1의 연습문제: 4, 12
$6.1의 연습문제: 2(b, d), 10
$6.3의 연습문제: 4, 13
Due Date:
Page 11
Discrete Mathematics
by Yang-Sae Moon