演算法概論 Introduction to Algorithms

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

A simple example finding the maximum of a set S of n numbers.
Prune-and-Search Method
Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:
1 Selection --Medians and Order Statistics (Chap. 9) The ith order statistic of n elements S={a 1, a 2,…, a n } : ith smallest elements Also called selection.
Divide-and-Conquer The most-well known algorithm design strategy:
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Chapter 3: Divide and Conquer
April 9, 2015Applied Discrete Mathematics Week 9: Relations 1 Solving Recurrence Relations Another Example: Give an explicit formula for the Fibonacci.
The Selection Problem. The selection problem Input: A set S of n elements Output: The k-th smallest element of S The median problem: to find the -th smallest.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Prune-and-search Strategy
© 2004 Goodrich, Tamassia Selection1. © 2004 Goodrich, Tamassia Selection2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章(Chapter 58) 伏者潛藏也
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Chapter 2: Fundamentals of the Analysis of Algorithm Efficiency
Selection1. 2 The Selection Problem Given an integer k and n elements x 1, x 2, …, x n, taken from a total order, find the k-th smallest element in this.
Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章(Chapter 58) 伏者潛藏也
Fuw-Yi Yang1 數位系統 Digital Systems Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Fuw-Yi Yang1 演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker:
© The McGraw-Hill Companies, Inc., Chapter 6 Prune-and-Search Strategy.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
Fuw-Yi Yang1 Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann Chap 13 Other Systems Department of Computer Science and Information Engineering,
1 Prune-and-Search Method 2012/10/30. A simple example: Binary search sorted sequence : (search 9) step 1  step 2  step 3  Binary search.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Fuw-Yi Yang1 數位系統 Digital Systems Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi.
Problem Definition Given a set of "n" unordered numbers we want to find the "k th " smallest number. (k is an integer between 1 and n).
Fuw-Yi Yang1 數位系統 Digital Systems Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi.
Fuw-Yi Yang1 數位系統 Digital Systems Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi.
Fuw-Yi Yang1 數位系統 Digital Systems Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi.
Fuw-Yi Yang1 數位系統 Digital Systems Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章(Chapter 58) 伏者潛藏也
Advanced Algorithms Analysis and Design
Quick-Sort 2/18/2018 3:56 AM Selection Selection.
Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章(Chapter 58) 伏者潛藏也
Applied Discrete Mathematics Week 2: Functions and Sequences
Modeling with Recurrence Relations
Median Finding Algorithm
Divide-and-Conquer 6/30/2018 9:16 AM
Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann
Textbook: Introduction to Cryptography 2nd ed. By J.A. Buchmann
Randomized Algorithms
Chapter 4 Divide-and-Conquer
Design and Analysis of Algorithms
Selection Selection 1 Quick-Sort Quick-Sort 10/30/16 13:52
演算法概論 Introduction to Algorithms
Divide-and-Conquer 7 2  9 4   2   4   7
Topic: Divide and Conquer
Applied Discrete Mathematics Week 6: Computation
CSE 373 Data Structures and Algorithms
ICS 353: Design and Analysis of Algorithms
Divide and Conquer Algorithms Part I
Solving Recurrence Relations
Applied Discrete Mathematics Week 7: Computation
Divide-and-Conquer 7 2  9 4   2   4   7
Topic: Divide and Conquer
Quick-Sort 5/7/2019 6:43 PM Selection Selection.
Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章(Chapter 58) 伏者潛藏也
The Selection Problem.
ICS 353: Design and Analysis of Algorithms
Quick-Sort 5/25/2019 6:16 PM Selection Selection.
Divide-and-Conquer 7 2  9 4   2   4   7
Discrete Mathematics CS 2610
Divide-and-Conquer 7 2  9 4   2   4   7
Presentation transcript:

演算法概論 Introduction to Algorithms Department of Computer Science and Information Engineering, Chaoyang University of Technology 朝陽科技大學資工系 Speaker: Fuw-Yi Yang 楊伏夷 伏夷非征番, 道德經 察政章(Chapter 58) 伏者潛藏也 道紀章(Chapter 14) 道無形象, 視之不可見者曰夷 Fuw-Yi Yang

Reference: K. H. Rosen, Discrete Mathematics and Its Applications, 5th ed. J. A. Dossey, A. D. Otto, L. E. Spence and C. V. Eynden, Discrete Mathematics, 4th ed. R.1 Introduction R.2 Recurrence Relations R.3 Modeling with Recurrence Relations R.4 Solving Recurrence Relations R.5 First-Order Linear Difference Equation R.6 Divide-and-Conquer Algorithms and Recurrence Relations R.7 Prune-and-Search R.8 Dynamic Programming Fuw-Yi Yang

R.7 Prune-and-Search The Selection Problem The Selection Problem A prune-and-Search algorithm to find the kth smallest element Input: A set S of n elements Output: the kth smallest element of S 1. If |S| ≦ 5, solve the problem by any method 2. Divide S into n/5 subsets. 3. Sort each subset of elements. 4. Recursively find the element x which is the medians of the median of the n/5 subsets. 5. Partition S into S1, S2, and S3, which contains the elements less than, equal to, and greater than x, respectively. (next page) Fuw-Yi Yang

R.7 Prune-and-Search The Selection Problem 5. Partition S into S1, S2, and S3, which contains the elements less than, equal to, and greater than x, respectively. 6. If |S1| ≧ k, then discard S2 and S3 and solve the problem that selects the kth smallest element from S1 during the next iteration; else if |S1| + |S2| ≧ k, then x is the kth smallest element of S; otherwise, let k = k - |S1| - |S2|. Solve the problem that selects the kth smallest element from S3 during the next iteration. Fuw-Yi Yang

R.7 Prune-and-Search The Selection Problem—worst case It follows that the number of elements greater than x is at least 3(1/2 n/5  - 2) ≧ 3n/10 – 6. Similarly, at least 3n/10 – 6 elements are less than x. Thus, in the worst case, step 6 calls algorithm recursively on at most 7n/10 + 6 elements. s x b Fuw-Yi Yang

R.7 Prune-and-Search The Selection Problem—worst case 11, 10, 23, 40, 32, 25, 76, 45, 88, 28, 13, 32, 53, 29, 31, 19, 98, 54, 16, 42, 67, 73, 55, 62, 34, 88, 63, 17, 25, 74, 35, 67, 99, 48, 78, 93, 92, 97 10 13 16 25 34 17 35 92 11 29 19 28 55 48 93 23 31 42 (x) 45 62 63 67 97 32 54 76 74 78 40 53 98 88 73 99 23 63 31 67 42 (x) 97 45 62 Fuw-Yi Yang

R.7 Prune-and-Search The Selection Problem We make the assumption that any input of fewer than 140 elements requires O(1) time. We can therefore obtain the recurrence T(n) ≦ O(1) if n < 140, T(n) ≦ T(n/5 ) + T(7n/10 + 6 ) + O(n) if n > 140. T(n) ≦ c(n/5 ) + c(7n/10 + 6 ) + an ≦ cn/5 + c + 7cn/10 + 6c + an = 9cn/10 + 7c + an = cn + (-cn/10 + 7c + an) Which is at most cn if (-cn/10 + 7c + an) ≦0. To satisfy the inequality above, n > 140, c ≧20a. Fuw-Yi Yang

R.7 Prune-and-Search Example R.7.1 Binary Search. procedure binary search (x: integer, a1, a2, …, an: increasing integers) i := 1 {i is left endpoint of search interval} j := n {j is right endpoint of search interval} while i < j begin m := integer ((i + j) /2) if x > am then i := m + 1 else j : = m end if x = ai then location := i else location := 0 Fuw-Yi Yang

R.7 Prune-and-Search The binary search algorithm reduces the search for an element in a search sequence of size n to the binary search for this element in a search sequence of size n/2, when n is even. Hence, the problem of size n has been reduced to one problem of size n/2. Two comparisons are needed to implement this reduction, one to determine which half of the list to use and the other to determine whether any terms of the list remain. Hence if f(n) is the number of comparisons required to search for an element in a search sequence of size n, then f(n) = f(n/2) + 2, when n is even. Find its time complexity. Solution: Fuw-Yi Yang

R.7 Prune-and-Search Solution: The recurrence relation is f(n) = f(n/2) + 2, i.e. a = 1, c = 2. From algorithm, it can be seen f(1) = 1. If n is a power of b, n = bk, it follows that f(n) = f(1) + 2k = 1 + 2 log bn. Thus f(n) is O(log n). Fuw-Yi Yang