470 likes | 678 Views
Chapter 8. Search Algorithms. Chapter Objectives. Learn the various search algorithms Explore how to implement the sequential and binary search algorithms Discover how the sequential and binary search algorithms perform Become aware of the lower bound on comparison-based search algorithms
E N D
Chapter 8 Search Algorithms Data Structures Using Java
Chapter Objectives • Learn the various search algorithms • Explore how to implement the sequential and binary search algorithms • Discover how the sequential and binary search algorithms perform • Become aware of the lower bound on comparison-based search algorithms • Learn about hashing Data Structures Using Java
isEmpty isFull listSize maxListSixe Print isItemAtEqual insertAt insertEnd removeAt retrieveAt replaceAt clearList seqSearch insert remove copyList class ArrayListClass: Basic Operations Data Structures Using Java
Search Algorithms • Associated with each item in a data set is a special member (the key of the item) that uniquely identifies the item in the data set • Keys are used in such operations as searching, sorting, insertion, and deletion • Analysis of the algorithms enables programmers to decide which algorithm to use for a specific application Data Structures Using Java
Sequential Search • Starts at the first element in the list • Continues until either the item is found in the list or the entire list is searched • Works the same for both array-based and linked lists Data Structures Using Java
Sequential Search public int seqSearch(DataElement searchItem) { int loc; boolean found = false; for(loc = 0; loc < length; loc++) if(list[loc].equals(searchItem)) { found = true; break; } if(found) return loc; else return -1; }//end seqSearch Data Structures Using Java
Search Algorithms • Search item: target • To determine the average number of comparisons in the successful case of the sequential search algorithm: • Consider all possible cases • Find the number of comparisons for each case • Add the number of comparisons and divide by the number of cases Data Structures Using Java
Sequential Search Analysis Suppose that there are n elements in the list. The following expression gives the average number of comparisons: It is known that Therefore, the following expression gives the average number of comparisons made by the sequential search in the successful case: Data Structures Using Java
Ordered Lists as Arrays • List is ordered if its elements are ordered according to some criteria • Elements of a list usually in ascending order • Define ordered list as an ADT Data Structures Using Java
Ordered Lists as Arrays • Several operations can be performed on an ordered list; similar to the operations performed on an arbitrary list • Determining whether the list is empty or full • Determining the length of the list • Printing the list • Clearing the list • Using inheritance, derive class to implement ordered lists from class ArrayListClass Data Structures Using Java
Binary Search Algorithm • Very fast • Uses “divide and conquer” technique to search list • First, search item compared with middle element of list • If the search item is less than middle element of list, restrict the search to first half of list • Otherwise, search second half of list Data Structures Using Java
Binary Search Data Structures Using Java
Binary Search: middle element first + last mid = 2 Data Structures Using Java
Binary Search public int binarySearch(DataElement item) { int first = 0; int last = length - 1; int mid = -1; boolean found = false; while(first <= last && !found) { mid = (first + last) / 2; if(list[mid].equals(item)) found = true; else if(list[mid].compareTo(item) > 0) last = mid - 1; else first = mid + 1; } if(found) return mid; else return -1; }//end binarySearch Data Structures Using Java
Binary Search: Example Data Structures Using Java
Binary Search: Example • Unsuccessful search • Total number of comparisons is 6 Data Structures Using Java
Performance of Binary Search Data Structures Using Java
Performance of Binary Search Data Structures Using Java
Performance of Binary Search • Unsuccessful search • for a list of length n, a binary search makes approximately 2*log2(n + 1) key comparisons • Successful search • for a list of length n, on average, a binary search makes 2*log2n – 4 key comparisons Data Structures Using Java
Algorithm to Insert into an Ordered List Use algorithm similar to binary search algorithm to find place where item is to be inserted ifthe item is already in this list output an appropriate message else use the method insertAt to insert the item in the list Data Structures Using Java
Search Algorithm Analysis Summary Data Structures Using Java
Lower Bound on Comparison-Based Search • Theorem: Let L be a list of size n > 1. Suppose that the elements of L are sorted. If SRH(n) denotes the minimum number of comparisons needed, in the worst case, by using a comparison-based algorithm to recognize whether an element x is in L, then SRH(n) = log2(n + 1). • Corollary:The binary search algorithm is the optimal worst-case algorithm for solving search problems by the comparison method. Data Structures Using Java
Hashing • Main objectives to choosing hash methods: • Choose a hash method that is easy to compute • Minimize the number of collisions Data Structures Using Java
Commonly Used Hash Methods • Mid-Square • Hash method, h, computed by squaring the identifier • Using appropriate number of bits from the middle of the square to obtain the bucket address • Middle bits of a square usually depend on all the characters, it is expected that different keys will yield different hash addresses with high probability, even if some of the characters are the same Data Structures Using Java
Commonly Used Hash Methods • Folding • Key X is partitioned into parts such that all the parts, except possibly the last parts, are of equal length • Parts then added, in convenient way, to obtain hash address • Division (Modular arithmetic) • Key X is converted into an integer iX • This integer divided by size of hash table to get remainder, giving address of X in HT Data Structures Using Java
Commonly Used Hash Methods Suppose that each key is a string. The following Java method uses the division method to compute the address of the key: int hashmethod(String insertKey) { int sum = 0; for(int j = 0; j <= insertKey.length(); j++) sum = sum + (int)(insertKey.charAt(j)); return (sum % HTSize); }//end hashmethod Data Structures Using Java
Collision Resolution • Algorithms to handle collisions • Two categories of collision resolution techniques • Open addressing (closed hashing) • Chaining (open hashing) Data Structures Using Java
Open Addressing: Linear Probing • Suppose that an item with key X is to be inserted in HT • Use hash function to compute index h(X) of item in HT • Suppose h(X) = t. • If HT[t] is empty, store item into array slot. • Suppose HT[t] already occupied by another item; collision occurs • Linear probing: starting at location t, search array sequentially to find next available array slot Data Structures Using Java
Collision Resolution: Open Addressing Pseudocode implementing linear probing: hIndex = hashmethod(insertKey); found = false; while(HT[hIndex] != emptyKey && !found) if(HT[hIndex].key == key) found = true; else hIndex = (hIndex + 1) % HTSize; if(found) System.out.println(”Duplicate items not allowed”); else HT[hIndex] = newItem; Data Structures Using Java
Linear Probing Data Structures Using Java
Linear Probing Data Structures Using Java
Random Probing • Uses a random number generator to find the next available slot • ith slot in the probe sequence is: (h(X) + ri) % HTSize where ri is the ith value in a random permutation of the numbers 1 to HTSize – 1 • All insertions and searches use the same sequence of random numbers Data Structures Using Java
Quadratic Probing • Reduces primary clustering • We do not know if it probes all the positions in the table • When HTSize is prime, quadratic probing probes about half the table before repeating the probe sequence Data Structures Using Java
Deletion: Open Addressing • In open addressing, when an item is deleted, its position in the array cannot be marked as empty Data Structures Using Java
Deletion: Open Addressing Data Structures Using Java
Deletion: Open Addressing Data Structures Using Java
Chaining • For each key X (in item), find h(X) = t, where 0 = t = HTSize – 1. Item with this key inserted in linked list (which may be empty) pointed to by HT[t]. • For nonidentical keys X1 and X2, if h(X1) = h(X2), items with keys X1 and X2 inserted in same linked list • To delete an item R, from hash table, search hash table to find where in linked list R exists. Then adjust links at appropriate locations and delete R Data Structures Using Java
Collision Resolution: Chaining (Open Hashing) Data Structures Using Java
Hashing Analysis Let Then a is called the load factor Data Structures Using Java
Linear Probing: Average Number of Comparisons 1. Successful search 2. Unsuccessful search Data Structures Using Java
Quadratic Probing: Average Number of Comparisons 1. Successful search 2. Unsuccessful search Data Structures Using Java
Chaining: Average Number of Comparisons 1. Successful search 2. Unsuccessful search Data Structures Using Java
Chapter Summary • Search Algorithms • Sequential • Binary • Algorithm Analysis • Hashing • Hash Table • Hash method • Collision Resolution Data Structures Using Java