
AMERICAN CONFERENCE ON APPLIED MATHEMATICS (MATH '08), Harvard, Massachusetts, USA, March 24-26, 2008 Multi-key Binary Search and the Related Performance AHMED TAREK Department of Math and Computer Science California University of Pennsylvania 250 University Avenue, California, PA 15419 UNITED STATES OF AMERICA [email protected] http://workforce.cup.edu/tarek Abstract: Binary Search is efficient due to it’s logarithmic time complexity. It is used to identify the position of a key in a sorted list. Often, computer applications require searching for two to more different keys at the same execution. In this paper, a hybrid algorithm to perform the binary search with 2 to m different keys (m is an integer greater than or equal to 2) in a sorted list of elements is proposed. An m-key version of the proposed algorithm requires considering (2m + 1) different cases. Correctness proof of the algorithm is established using induction on the list size, n. Time complexity of the proposed algorithm is a function of 2 variables, namely, the number of keys, m and the list size, n, and is given as, O(mlog(n)) in both the worst and the average cases. The best case complexity is linear, which is O(m). Performance of 2 and 3-key versions is compared with the classical single key version. Possible key index combinations with the multi-key search strategies are also explored. Key–Words: Multiple Keys, Multi-key Binary Search, Recursive Algorithm, Hybrid Algorithm, Logarithmic Time Complexity. 1 Introduction If the list is ascending with the smaller key located at the jth index and the larger key at the ith index, then Binary search (BS) is popular and useful due to loga- i > j and the total number of elements that lie in be- rithmic time complexity. The algorithm exhibits sig- tween is, (i ¡ j ¡ 1). nificant improvement in time with larger sizes of the In Section 2, terminology and notations used in lists. However, it applies only to an ordered list. this paper are considered. Section 3 explores the State-of-the-art research is to apply the classical bi- MKBS algorithm and shows the related analysis. The nary search technique (BST) in solving computational algorithm is illustrated using a numerical example. problems. In reference [1], the author has identi- Section 4 analyzes the time complexity, and also con- fied a major flaw in the classical BST for larger sizes siders the performance measurement issues. It also of the lists, and suggested improvements in standard compares the multi-key versions with the classical programming languages, such as C, C++, and Java. single key approach. Section 5 explores future re- In [2], the authors have explored a technique that uses search. rapid searching using a variant of the BS. However, the focus in this paper is entirely dif- 2 Terminology and Notations ferent compared to other BS research issues. An at- Following notations are used all throughout this tempt is made here to extend the traditional BST in paper. searching for multiple keys at the same execution. In left: Left-most index in a list of elements. a huge chunk of memory containing an organized list, right: Right-most index in a list. we may be interested in finding out a block having middle: Index of the middle element in a list. upper and lower bounds, and moving it to a different arr: Name of the array holding the list elements. location for further processing. small key: Holds the smallest of the keys. In this paper, a recursive multi-key binary search large key: Contains the largest of the keys. (MKBS) algorithm in searching for m different keys small pos: Positional index of the smallest key. in a list of n elements is explored. Here, m · n. large pos: Largest key position. The proposed algorithm occasionally searches using m: Total number of keys. the classical BST during computation. Since the re- n: Total number of list elements. cursive call to the next lower version is just a possibil- Time Complexity: It is the amount of computer time ity, the algorithm is hybrid, and not purely recursive. that a program needs to run to completion. ISSN: 1790-5117 104 ISBN: 978-960-6766-47-3 AMERICAN CONFERENCE ON APPLIED MATHEMATICS (MATH '08), Harvard, Massachusetts, USA, March 24-26, 2008 Space Complexity: The amount of memory space that return ¡1 a program requires to run to completion. The 2-key BS algorithm makes use of the classical 1- key version. Performance evaluation of an algorithm consists Algorithm BinarySearch 2key of performance analysis and performance measure- Purpose: This algorithm performs 2-key binary ment. Performance analysis uses theoretical and an- search. alytical tools and techniques. Performance measure- The supplied parameters are: array arr[], position ment represents the practical testing results using the of the first element: left, proposed algorithm. In this paper, both performance position of the last element: right, smaller key, and analysis and measurement are considered. larger key. 3 Multi-key Binary Search Algo- 2-key search finds out small pos, large pos for the smaller and the larger keys. rithm Require: small key < large key Ensure: left > right or keys found In the classical BST, there is a flaw. For finding while left · right do out the middle index position, the average between middle = left + (right ¡ left)=2 the left and the right is computed using, middle = if arr[middle] < small key then (left + right)=2, truncated down to the nearest in- BinarySearch 2key (arr, (middle+1), right, teger. Apparently, this assertion might appear correct, small key, large key, small pos, large pos) but it fails for large values of the integer variables, fRecursively call BinarySearch 2keyg left and right. Specifically, it fails if the sum of left else if arr[middle] = small key then and right is greater than the maximum positive inte- small pos ( middle ger value, (231 - 1). The sum overflows to a negative large pos ( BinarySearch(arr, middle+1, value, and the value stays negative when it is divided right, large key) by two. return This bug can manifest itself for arrays whose else if arr[middle] > small key and arr[middle] length in elements is 230 or greater. In [1], the au- < large key then thor refers to this error in the first classical BST, which small pos ( BinarySearch(arr, left, middle-1, was published in 1946. Following is an alternative to small key) fix this bug. large pos ( BinarySearch(arr, middle+1, right, large key) int middle = left + ((right ¡ left) = 2) (1) return else if arr[middle] = large key then MKBS algorithms are implemented recursively large pos ( middle as BinarySearch 2key, BinarySearch 3key, Bina- small pos ( binary search(arr, left, middle- rySearch 4key, ::: free-functions. Multi key search 1,small key); algorithms create a computational hierarchy founded return upon the classical single-key search. Therefore, the else if arr[middle] > large key then corrected version of the recursive BST is outlined BinarySearch 2key (arr, left, middle-1, first. small key, large key, small pos, large pos) Algorithm binary search end if Purpose: This algorithm performs 1-key recursive end while binary search. small pos ( -1 while right ¸ left do large pos ( -1 middle = left + (right ¡ left)=2 return if arr[middle] = key element then return middle 3.1 Numerical Example else if arr[middle] > key element then return binary search (arr, left, middle-1, Consider the following list with 12 integer elements. key element) frecursive call to binary searchg -112,-88,-55,-12,-5, 15, 32, 67, 79, 98, 117, 133. else ² The two given keys are: small key = -12, and return binary search (arr, middle+1, right, large key = 67. key element) ² At first, left = 0, and right = 11. As left · right, end if therefore middle = int (0 + ((11 ¡ 0)=2)) = 5. Now, end while arr[5] = 15. ISSN: 1790-5117 105 ISBN: 978-960-6766-47-3 AMERICAN CONFERENCE ON APPLIED MATHEMATICS (MATH '08), Harvard, Massachusetts, USA, March 24-26, 2008 ² As arr[5] = 15 > ¡12, and arr[5] = 15 < 67. Basis step: To avoid too much complexity, only the Therefore, 2-key search version is considered. In the basis step, small pos = binary search(arr[]; 0; 4; ¡12), and the proposition P (1) is shown to be true. With n=1, large pos = binary search(arr[] ; 6; 11; 67). After left = 0, and right = 0. Then middle = int((0+((0¡ two classical binary searches at this stage, ¡12 is 0)=2)) = 0, and left = right. found at index 3 with counting beginning at index 0. ² If arr[0] < small key, the algorithm calls itself re- Similarly, 67 is identified at index 7. The smaller key cursively with left=(middle + 1)=1, and right = 0. position is, (3 + 1) = 4, and the larger key position Since left > right, therefore, small pos = ¡1, and is, (7 + 1) = 8. Total number of elements in between large pos = ¡1. these 2 keys is, (7 ¡ 3 ¡ 1) = 3. ² If arr[0] is equal to small key, then small pos = middle = 0, and large pos = binary search 3.2 Analytical Results (arr[], middle + 1, right, large key). In this case, left=(middle + 1)=1, and right = 0. Since right < Following result holds true for an m-key BS. left, therefore, small pos = 0, and large pos = ¡1. ² If arr[0] > small key, and arr[0] < large key, Lemma 1 An m-key binary search algorithm may then, small pos = binary search(arr[], left, make recursive calls starting from its (m-1) key ver- middle ¡ 1; small key). Therefore, left = 0, and sion up to the single key version of the classical binary right = (middle ¡ 1) = ¡1, therefore, left > search in its computational hierarchy.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-