1 高等演算法 Homework One 暨南大學資訊工程學系 黃光璿 2004/11/11
2 Problem 1
3 Problem 2 Count the number of inversions for the permutation
4 Problem 3 Explain when we can say that an algorithm for solving a problem is optimal, in the sense of theoretical analysis. When the complexity of the algorithm matches the complexity lower bound for the problem that the algorithm intends to solve.
5 Problem 4 Show that to find the largest number in an array of n numbers requires at least n-1 comparisons. This problem asks to show that n-1 comparisons is the lower bound for any algorithm that guarantees the maximum. The claim is true under the condition that you want to find the maximum only by comparisons.
6 Let each number be a node in a graph. When two numbers are compared, link them by an edge. If only n-2 comparisons are being used, the graph cannot be connected. There are at least two components, and you cannot make sure which one contains the maximum.
7 However, in the next slide, I will show you an algorithm that compute the maximum without using any comparison.
8 The idea is simple. We can use arithmetic and logical operations to emulate the action of comparisons. a > b iff a – b > 0 Extract the sign bit of a – b and call it s ( a, b ). Then returns the maximum of a and b.
9 The computation can be even more complex. In fact, we can compute the index of the maximum without using the temporary variable.
10