design and analysis of algorithms
play

Design and Analysis of Algorithms - PowerPoint PPT Presentation

Design and Analysis of Algorithms


  1. คําเตือน Design and Analysis of Algorithms เนื้อหาอันรวมถึงขอความ ตัวเลข สัญลักษณ รูปภาพ และ คําบรรยาย อาจมีขอผิดพลาดแฝงอยู ผูจัดทําจะไมรับผิด ผศ . ดร . สมชาย ประสิทธิ์จูตระกูล ชอบตอความเสียหายทั้งทางดานผลการเรียน สุขภาพกาย ภาควิชาวิศวกรรมคอมพิวเตอร และสุขภาพจิตใดๆ อันเนื่องมาจากการใชสื่อการเรียนนี้ จุฬาลงกรณมหาวิทยาลัย 2542 Outline Algorithm Design Techniques • General idea • Examples – Mergesort, Quicksort Divide-and-Conquer – Selection – Exponentiation – Matrix Multiplication – Closest point • Conclusion

  2. General Idea General Idea • Divide problem instance into subinstances of the D&Q ( P ) same kind { i f ( P i s t r i vi al ) r et ur n Sol ve( P ) • For each subinstance (conquer) Di vi de P i nt o P 1 , P 2 , … , P k – solve it directly if it is trivial f or ( i = 1 t o k ) – solve it recursively otherwise S i = D&Q ( P i ) • Combine subinstance solutions to give the S = Com bi ne( S 1 , S 2 , … , S k ) solution for the bigger problem instance r et ur n S } Mergesort Mergesort : Analysis • T ( n ) = 2 T ( n /2) + Θ ( n ) • Master method : a = 2, b = 2, f ( n ) = Θ ( n ) Mergesort Mergesort 2 T ( n /2) • c = log b a = log 2 2 = 1 T ( n ) = 2 T ( n /2) + Θ ( n ) • n c = n 1 , f ( n ) = Θ ( n c ) = Θ ( n ) • T ( n ) = Θ ( n c log n ) = Θ ( n log n ) Θ ( n ) Merge

  3. Mergesort : Quiz Quicksort • What if we do not divide the array in half ? – 40-60 Θ ( n ) Partition – 20-80 k elements T ( n ) = T ( k ) + T ( n - k ) + Θ ( n ) – 1 0-90 Quicksort Quicksort T ( k ) + T ( n - k ) – 1 -99 – random Quicksort : Worst-Case Analysis Quicksort : Best-Case Analysis • T ( n ) = T ( k ) + T ( n - k ) + Θ ( n ) • T ( n ) = T ( k ) + T ( n - k ) + Θ ( n ) • Worst-case when k = 1 in every step • Base-case when k = n /2 in every step = + − + Θ = + + Θ T ( n ) T ( 1 ) T ( n 1 ) ( n ) ( ) ( / 2 ) ( / 2 ) ( ) T n T n T n n = − + Θ = + Θ T ( n 1 ) ( n ) 2 T ( n / 2 ) ( n ) n ( ) = Θ = Θ ( i ) n log n � = i 1 n � � = Θ i � � � = � i 1 � ( ) = Θ 2 n

  4. Quicksort : Partition Quicksort : Partition Par t i t i on( A, p, r ) Par t i t i on( A, p, r ) { { c = A[ p] p p p p r r r r ... ... ... ... A A x x x x y x x x x x x x x x A x A y p j r ... A x x x x x x x A[ p. . j ] > A[ j +1. . r ] r et ur n j r et ur n j } } Quicksort : Partition Quicksort : Partition Par t i t i on( A, p, r ) Par t i t i on( A, p, r ) { { i j i j c = A[ p] c = A[ p] p p p p r r r r i = p- 1; j = r +1 i = p- 1; j = r +1 ... ... ... ... A A A A whi l e ( i < j ) { } r et ur n j r et ur n j } }

  5. Quicksort : Partition Quicksort : Partition Par t i t i on( A, p, r ) Par t i t i on( A, p, r ) { { i j i j c = A[ p] c = A[ p] p p p p r r r r i = p- 1; j = r +1 i = p- 1; j = r +1 ... ... ... ... A A A A whi l e ( i < j ) { whi l e ( i < j ) { r epeat j = j - 1 r epeat j = j - 1 unt i l ( A[ j ] ≤ c ) unt i l ( A[ j ] ≤ c ) < c > c r epeat i = i +1 r epeat i = i +1 unt i l ( A[ i ] ≥ c ) unt i l ( A[ i ] ≥ c ) i f i < j exchange A[ i ] , A[ j ] } } r et ur n j r et ur n j } } Quicksort : Partition Quicksort : Partition Par t i t i on( A, p, r ) Par t i t i on( A, p, r ) { { i j j i c = A[ p] c = A[ p] p p p p r r r r i = p- 1; j = r +1 i = p- 1; j = r +1 ... ... ... ... A A A A whi l e ( i < j ) { whi l e ( i < j ) { r epeat j = j - 1 r epeat j = j - 1 unt i l ( A[ j ] ≤ c ) unt i l ( A[ j ] ≤ c ) ≤ c ≥ c ≤ c ≥ c r epeat i = i +1 r epeat i = i +1 unt i l ( A[ i ] ≥ c ) unt i l ( A[ i ] ≥ c ) i f i < j i f i < j exchange A[ i ] , A[ j ] exchange A[ i ] , A[ j ] } } r et ur n j r et ur n j } }

  6. Quicksort : Partition Quicksort : Partition p p p p r r r r ... ... ... ... A A x x x x A A x x x x y x x x x x x x x x y x x x x x x x x x j p r p r j ... ... A x x x x x x x A x x x x x x x Quicksort : Partition Quicksort : Partition p p p p r r r r ... ... ... ... A x x x x A A A x x x x y x x x x x x x x x y x x x x x x x x x p j r p j r ... ... A x x x x x x x A x x x x x x x

  7. Quicksort : Partition Quicksort : Partition p p p p r r r r ... ... ... ... A A x x x x A A x x x x y x x x x x x x x x y x x x x x x x x x p j r p j r ... ... A x x x x x x x A x x x x x x x Quicksort : Partition Quicksort : Partition p p p p r r r r ... ... ... ... A x x x x A A A x x x x y x x x x x x x x x y x x x x x x x x x j j p r p r ... ... A x x x x x x x A x x x x x x x

  8. Randomized Partition Randomized Quicksort : Analysis • T ( n ) = T ( k ) + T ( n - k ) + Θ ( n ) Random i zed- Par t i t i on( A, p, r ) − n 1 1 ( ) � � = + − + + − + Θ { T ( n ) T ( 1 ) T ( n 1 ) T ( j ) T ( n j ) ( n ) � � � � � n i = r andom ( p, r ) = j 1 � � exchange A[ p] , A[ i ] − n 1 1 ( ) par t i t i on( A, p, r ) = � + − + Θ T ( j ) T ( n j ) ( n ) } n = j 1 − − n 1 n 1 1 � � = + − + Θ T ( j ) T ( n j ) ( n ) � � � � � � n = = j 1 j 1 � � − n 1 2 = � + Θ T ( j ) ( n ) n = j 1 Randomized Quicksort : Analysis Randomized Quicksort : Analysis − n 1 ′ 2 = + − + = � + Θ nT ( n ) ( n 1 ) T ( n 1 ) c n T ( n ) T ( j ) ( n ) 1 n × = j 1 ′ − + − T ( n ) T ( n 1 ) c n 1 n ( n 1 ) 2 = + = � + T ( n ) T ( j ) cn + + n 1 n n 1 n telescope = j 1 − n 1 ′ n = � + T ( n ) T ( 1 ) c 2 nT ( n ) 2 T ( j ) cn = + � + + = n 1 2 i 1 n j 1 1 = i 2 = − O (log n ) n 2 � − − = + − 2 ( n 1 ) T ( n 1 ) 2 T ( j ) c ( n 1 ) n = T ( n ) � i 1 = O (log n ) = j 1 + 1 ′ n − − − = − + nT ( n ) ( n 1 ) T ( n 1 ) 2 T ( n 1 ) c n = = + − + ′ ( ) ( log ) T n O n n nT ( n ) ( n 1 ) T ( n 1 ) c n

  9. Quicksort Randomization • Quicksort • A general tool to improve algorithms with bad worst-case but good average-case performance – With random input data, quicksort runs in • “Robin Hood Effect” expected Ο ( n log n ) time • The worst case is still there but we almost • Randomized Quicksort certainly won’t see it – With high probability, randomized quicksort runs • Stay tuned..... in Ο ( n log n ) time Quicksort : Quiz # 1 Quicksort : Quiz #2 • If all the elements are the same, p p r r Θ ( n ) ... ... A A – how does Partition divide the array ? ≤ c ≥ c – what is the running time of Quicksort ? p p r r Θ ( n ) ... ... A A > c < c = c

  10. Selection Selection Algorithms • Input : A set A of n (distinct) numbers and a � Sort the A and then pick the i th element number i , with 1 ≤ i ≤ n O( n log n ) • Output : The element x in A that is larger than � Build a (max) heap from the n elements of A exactly i - 1 other elements of A Extract-Max i times O( n + i log n ) = O( n log n ) Selection Selection in Expected Linear Time • Minimum : select the 1 st smallest element : Θ ( n ) i • Maximum : select the n th smallest element : Θ ( n ) k elements Partition • Select the i th smallest element : Θ ( n ) ? if i ≤ k i Selection

  11. Selection in Expected Linear Time Selection in Expected Linear Time i Random i zed- Sel ect i on( A, p, r , i ) { k elements i f p = r t hen r et ur n A[ p] Partition j = Random i zed- Par t i t i on( A, p, r ) k = j - p + 1 i f i < k i - k if i > k r et ur n Random i zed- Sel ect i on( A, p, j , i ) el se r et ur n Random i zed- Sel ect i on( A, j +1, r , i - k ) Selection } Randomized Selection : Analysis Selection in Worst-Case Linear Time • T ( n ) ≤ T ( max( k, n - k ) ) + Θ ( n ) • Choose Partition that guarantees a good split • Median-of-median-of-five partitioning − n 1 1 ( ) � � ≤ − + − + Θ T ( n ) T (max( 1 , n 1 )) T (max( k , n k ) ( n ) � � � n = – guarantee 30%-70% split in worst case � k 1 � − n 1 1 � � ≤ − + + Θ T ( n 1 ) 2 T ( k ) ( n ) � � – selection achieves a linear time in worst case � � � n = k n / 2 � � � � − n 1 2 = + Θ T ( k ) ( n ) � n = k n / 2 � � [CLR p. 1 88- 1 89] = O ( n )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend