Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures Using Java1 Chapter 8 Search Algorithms.

Similar presentations


Presentation on theme: "Data Structures Using Java1 Chapter 8 Search Algorithms."— Presentation transcript:

1 Data Structures Using Java1 Chapter 8 Search Algorithms

2 Data Structures Using Java2 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

3 Data Structures Using Java3 class ArrayListClass: Basic Operations isEmpty isFull listSize maxListSixe Print isItemAtEqual insertAt insertEnd removeAt retrieveAt replaceAt clearList seqSearch insert remove copyList

4 Data Structures Using Java4 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

5 Data Structures Using Java5 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

6 Data Structures Using Java6 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

7 Data Structures Using Java7 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

8 Data Structures Using Java8 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:

9 Data Structures Using Java9 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

10 Data Structures Using Java10 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

11 Data Structures Using Java11 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

12 Data Structures Using Java12 Binary Search

13 Data Structures Using Java13 Binary Search: middle element first + last 2 mid =

14 Data Structures Using Java14 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

15 Data Structures Using Java15 Binary Search: Example

16 Data Structures Using Java16 Binary Search: Example Unsuccessful search Total number of comparisons is 6

17 Data Structures Using Java17 Performance of Binary Search

18 Data Structures Using Java18 Performance of Binary Search

19 Data Structures Using Java19 Performance of Binary Search Unsuccessful search –for a list of length n, a binary search makes approximately 2*log 2 (n + 1) key comparisons Successful search –for a list of length n, on average, a binary search makes 2*log 2 n – 4 key comparisons

20 Data Structures Using Java20 Algorithm to Insert into an Ordered List Use algorithm similar to binary search algorithm to find place where item is to be inserted if the item is already in this list output an appropriate message else use the method insertAt to insert the item in the list

21 Data Structures Using Java21 Search Algorithm Analysis Summary

22 Data Structures Using Java22 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.

23 Data Structures Using Java23 Hashing Main objectives to choosing hash methods: –Choose a hash method that is easy to compute –Minimize the number of collisions

24 Data Structures Using Java24 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

25 Data Structures Using Java25 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

26 Data Structures Using Java26 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

27 Data Structures Using Java27 Collision Resolution Algorithms to handle collisions Two categories of collision resolution techniques –Open addressing (closed hashing) –Chaining (open hashing)

28 Data Structures Using Java28 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. Then 0 = h(X) = HTSize – 1 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

29 Data Structures Using Java29 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;

30 Data Structures Using Java30 Linear Probing

31 Data Structures Using Java31 Linear Probing

32 Data Structures Using Java32 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

33 Data Structures Using Java33 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

34 Data Structures Using Java34 Deletion: Open Addressing In open addressing, when an item is deleted, its position in the array cannot be marked as empty

35 Data Structures Using Java35 Deletion: Open Addressing

36 Data Structures Using Java36 Deletion: Open Addressing

37 Data Structures Using Java37 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

38 Data Structures Using Java38 Collision Resolution: Chaining (Open Hashing)

39 Data Structures Using Java39 Hashing Analysis Let Then a is called the load factor

40 Data Structures Using Java40 Linear Probing: Average Number of Comparisons 1. Successful search 2. Unsuccessful search

41 Data Structures Using Java41 Quadratic Probing: Average Number of Comparisons 1. Successful search 2. Unsuccessful search

42 Data Structures Using Java42 Chaining: Average Number of Comparisons 1. Successful search 2. Unsuccessful search

43 Data Structures Using Java43 Chapter Summary Search Algorithms –Sequential –Binary Algorithm Analysis Hashing –Hash Table –Hash method –Collision Resolution


Download ppt "Data Structures Using Java1 Chapter 8 Search Algorithms."

Similar presentations


Ads by Google