CS300-04_Algorithm_Design

# CS300-04_Algorithm_Design - Algorithm Design Sung Yong Shin...

This preview shows pages 1–13. Sign up to view the full content.

.+ 11 Algorithm Design Sung Yong Shin CS Dept. KAIST

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
22 1. Divide and Conquer 2. Dynamic Programming 3. Greedy Search 4. Backtracking 5. Local Search P306 - 346 (Chapter 10) Aho, Hopcroft, and Ulman Outline
33 where k is a relatively small constant. Sol ( P ) = f ( Sol ( P 1), Sol ( P 2), …, Sol ( P k)) Combining the solutions of all subproblems T ( n ) = T ( n 1) + T ( n 2) + … + T ( n k) + M ( n ) , , 1 , 1 k j i P P P P j i i k i < = = = P 1 P 2 P k …... P 1. Divide and Conquer

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
44 T ( n ) = T ( n 1) + T ( n 2) + … + T ( n k) + M ( n ) In general kS S M ( n ) kS S M ( n ) Why ? (intuitive explanation) 1 k is usually small i.e., k = 2 or 3 in many cases Can you give some examples ? Well, … Merge sort Binary search Observation
55 Now, consider the sizes of subproblems. (e.g. k = 2) P = P 1 P 2 P 1 P 2 = T( n ) = T( n 1) + T( n 2) + M( n ) where n = n 1 + n 2 d T( n ) d T( n ) Why ? n1 1 n1 n n1 11 n2 n1 2 n1 12 …………………………… . ………. . ………. . d

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
66 Example : P : Given a set of n real numbers, sort them T ( n ) = T ( n 1) + T ( n 2) + M ( n ) Selection Sort Merge Sort Selection Sort (1) Choose the smallest one (2) Put it on the top (3) Repeat (1) and (2) for the remaining numbers n 1 = 1 n 2 = n - 1 Why ? M ( n ) = n - 1 1 T ( n ) = T (1) + T ( n - 1) + ( n - 1) T (1) = 0
77 n n-1 n-2 n-3 2 2 ) 1 ( ) ( 0 ) 1 ( 1 ) 2 ( ... ... 4 ) 3 ( 3 ) 2 ( 2 ) 1 ( 1 ) ( 1 n n i M M M n n M n n M n n M n n M n i - - - - - - - - = 1 1 1 1 1 1 T( n ) = T( n -1) + T(1) + M ( n ) n -1 = M ( n ) n -2 = M ( n - 1) n -3 = M ( n - 2) n -4 = M ( n - 3) 1 = M ( 2 ) 0 = M ( 1 ) T(1) = 0 T(1) = 0 T(1) = 0 T(1) = 0 T(1) = 0 T(1) = 0 0 n -1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
88 (1) Divide P into P 1 and P 2 of equal size (2) Solve P 1 and P 2 by recursively applying (1) T ( n ) = T ( n 1)+ T ( n 2)+ M ( n ) n n M n n n = = = ) ( 2 2 1 1 ) 1 ( ) 2 ( 2 ) ( = + = T n n T n T Why ? n n n n n n d = = = 1 2 ... ) 4 ( 4 ) 2 ( 2 n n / 2 n / 4 n 2 n / 4 n / 4 n / 4 …………………………………… . 1 1 1 ………………. d n d n d 2 log , 2 : Note = = = = = d i i i n M n n n d 0 2 ) 2 / ( 2 log Merge Sort
99 (1) Trading off competing cost factors Sorting Problem T ( n ) = T ( n 1) + … + T ( nk ) + M ( n ) k = # of subproblems d = depth k : smaller better, why? d : lower better, why? However, k dS (assuming that n 1 = n 2 = … = nk ) (2) Balancing the sizes of subproblems. Balancing Strategy

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1010 T(n- 1) “Tower of Hanoi” Example T(n) T(n- 1) 1 peg A peg B peg C
1111 T ( n ) = 2 T ( n -1) + 1 T (1) = 1 Basic Operation ? Example “Multiplying Long Integers” 1 2 3 4 X 1 1 1 1 Y n = # of digits 1 2 3 4 How many multiplications ? 1 2 3 4 O( n 2) 1 2 3 4 Why? 1 2 3 4 1 3 7 0 9 7 4

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
1212 Now, let n = # of bits in each integer ( X and Y ) X X = A 2 + B Y Y = C 2 + D ( H ( bits bits XY = AC 2 n + ( AD + BC ) 2 + BD How many operations?
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/07/2010 for the course CS 300 taught by Professor Shin during the Spring '10 term at Korea Advanced Institute of Science and Technology.

### Page1 / 39

CS300-04_Algorithm_Design - Algorithm Design Sung Yong Shin...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online