# 제 2 장 분할정복법

Click here to load reader

Upload: zenia-chaney

Post on 04-Jan-2016

81 views

Category:

## Documents

0 download

DESCRIPTION

제 2 장 분할정복법. 분할정복 (Divide-and-Conquer) 법 설계 전략. 분할 (Divide): 해결하기 쉽도록 문제를 여러 개의 작은 부분으로 나눈다 . 정복 (Conquer): 나눈 작은 문제를 각각 해결한다 . 통합 (Combine): ( 필요하다면 ) 해결된 해답을 모은다 . 이러한 문제 해결 방법을 하향식 (top-down) 접근방법이라고 한다. 이분검색 (Binary Search): 재귀 알고리즘. 문제 : 크기가 n 인 정렬된 배열 S 에 x 가 있는지를 결정하라 . - PowerPoint PPT Presentation

TRANSCRIPT

• 2

2

• (Divide-and-Conquer) (Divide): .(Conquer): .(Combine): () . (top-down) .

2

• (Binary Search): : n S x .: n, S[1..n], x: locationout - x S . x S 0:x , , ! :: x , .: x .: ( )

2

• (Binary Search): index location (index low, index high) {index mid;

if (low > high) return 0; // else { mid = (low + high) / 2 // ( ) if (x == S[mid])return mid; // else if (x < S[mid])return location(low, mid-1); // elsereturn location(mid+1, high); // }}locationout = location(1, n);...

2

• locationout ? n, S, x . (recursive call) .

2

• (recursive algorithm) () (tail recursion) - (iterative algorithm) . (activation records) , (). . (constant factor) () . ML Scheme .

2

• : x S[mid] : n (= high - low + 1) while 2 , . : (1) ; (2) x 2 . () , 1 .

2

• 1: (recurrence) . n > 1 , n = 2k(k 1)

.......

2

• .: :: n = 1, W(1) = 1 = lg 1 + 1.: 2 (power) n , W(n) = lg n + 1 .: W(2n) = lg(2n) + 1 . ,

2

• 2: - y , n , .

. .

2

n

mid

n/2 - 1

1

n/2

(n-1)/2

1

(n-1)/2

• n .: : n = 1, .: n > 1, 1 < k < n k , .: n (, ),

2

• n (, ),

2

• (Mergesort): n .: n, n S[1..n]: S[1..n]: 27, 10, 12, 20, 25, 13, 15, 22

2

• :

void mergesort (int n, keytype S[]) {const int h = n / 2, m = n - h;keytype U[1..h], V[1..m];

if (n > 1) { copy S[1] through S[h] to U[1] through U[h]; copy S[h+1] through S[n] to V[1] through V[m]; mergesort(h,U); mergesort(m,V); merge(h,m,U,V,S);}}

2

• (Merge): .: (1) h, m, (2) U[1..h], V[1..m]: U V S[1..h+m]

2

• :void merge(int h, int m, const keytype U[], const keytype V[], const keytype S[]) {index i, j, k;i = 1; j = 1; k = 1;while (i
• : U[i] V[j] : 2 : h m: i = h, j = m -1 (loop) , h + m -1. , W(h,m) = h + m -1.

2

• : merge : S n: W(h,m) = W(h) + W(m) + (h + m 1) . W(h) U , W(m) V , h + m - 1 . n 2k(k 1) , . :

2 , .

2

• n 2 (power) .

. , , n = 2k , . , n = 2k .

2

• (in-place sort) . . S U V . ? mergesort S U V . merge U V . mergesort . S n, U V n . . . n , , n ( ). .

2

• (Mergesort): n .: n, n S[1..n]: S[1..n]:void mergesort2 (index low, index high) { index mid; if (low < high) { mid = (low + high) / 2; mergesort2(low, mid); mergesort2(mid+1, high); merge2(low, mid, high); }} mergesort2(1, n); ...

2

• (Merge): .: (1) low, mid, high, (2) S[low..high], S[low..mid] S[mid+1..high] .: S[1..high]

2

• : void merge2(index low, index mid, index high) {index i, j, k; keytype U[low..high]; // i = low; j = mid + 1; k = low;while (i
• (The Master Theorem)a b 1 , f(n) , n T(n) .

T(n) (asymptotic bound) .1. > 0 , 2. , . 3. > 0 , , c < 1 n , , . , .

2

• a = 9, b = 3, f(n) = n, , = 1 , . 1 , .

a = 1, b = , f(n) = 1, , . 2 , .

2

• a = 3, b = 4, f(n) = n lg n, , 0.2 , = 1 , . 3 , n , 1 c . , , n . .

a = 2, b = 2, f(n) = nlgn, , . 3 , n , 1 c . , n c . , , c . . .

2

• , k 0 k f(n) . ( ) 4 .

. .

2

• (Quicksort)1962 (C.A.R. Hoare) (Quicksort) . . (partition exchange sort) .: 15 22 13 27 12 10 20 25

2

• : n : n > 0, n S[1..n]: S[1..n]:void quicksort (index low, index high) {index pivotpoint;if (high > low) { partition(low,high,pivotpoint); quicksort(low,pivotpoint-1); quicksort(pivotpoint+1,high);}}

2

• : S .: (1) low,high, (2) low high S : low high S (pivot point), pivotpoint:void partition (index low, index high, index& pivotpoint) {index i, j;keytype pivotitem;pivotitem = S[low];//pivotitem j = low;for(i = low + 1; i
• : S[i] key : , n = high - low + 1: , T(n) = n - 1.

2

• : S[i] key : S , n: . ? () , n 0 , n-1 . ,, T(0) = 0, . T(n) = T(n - 1) + n - 1, n > 0 T(0) = 0

2

• , T(n) = T(n - 1) + n - 1 T(n - 1) = T(n - 1) + n - 2 T(n - 2) = T(n - 3) + n - 3 ... T(2) = T(1) + 1 T(1) = T(0) + 0 T(0) = 0

, . ? , .

2

• n , .: (): n = 0 ,: 0 k < n k ,: pivotpoint p

p 1 .

,

.

2

• : S[i] key : S , n: . 1 n , . , . p , p [A(p - 1) + A(n - p)], n - 1, .

2

• n ,

n n - 1 ,

(1) (2) ,

,

,

, .

,...

2

• ,

. ln n = logen,

, an 2 ln n. ,

2

• (Matrix Multiplication) : n n .: n, n n A B: A B C:void matrixmult (int n, const number A[][], const number B[][], number C[][]) { index i, j, k; for (i = 1; i
• I:: : , n : . II:: : , n : .

2

• 2 2 : : 2 2 A B (product) C,

(Strassen) :

: 8 4 , 7 18 / . ! .

2

• n n : : n 2 , 4 (submatrix) . n n A B C:

(Strassen) :

2

• : n 2 , n n .: n, n n A B: A B C:void strassen (int n, n*n_matrix A, n*n_matrix B, n*n_matrix& C) {if (n
• I: : , n : 1 . ( .)

,

. . 3 1 .

2

• II: / : , n : 1 .

3 1 .

2

• (n2) .

.

2

• n 2 , n : (exponential)

n n , n/c . c . : (nlg n)

2

2 , , , 1999 2 , , , 1999