Data Structures Using Java1 Chapter 8 Search Algorithms.

Slides:



Advertisements
Similar presentations
CS Data Structures Chapter 8 Hashing.
Advertisements

Hash Tables.
Part II Chapter 8 Hashing Introduction Consider we may perform insertion, searching and deletion on a dictionary (symbol table). Array Linked list Tree.
CSCE 3400 Data Structures & Algorithm Analysis
Skip List & Hashing CSE, POSTECH.
Data Structures Using C++ 2E
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing Chapters What is Hashing? A technique that determines an index or location for storage of an item in a data structure The hash function.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Chapter 19: Searching and Sorting Algorithms
Hashing Techniques.
Dictionaries and Hash Tables1  
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Hash Tables1 Part E Hash Tables  
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Searching Chapter 2.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Data Structures Using C++1 Chapter 9 Search Algorithms.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 19: Searching and Sorting Algorithms.
Applications of Arrays (Searching and Sorting) and Strings
Chapter 19: Searching and Sorting Algorithms
Lecture 12. Searching Algorithms and its analysis 1.
Hashing Table Professor Sin-Min Lee Department of Computer Science.
Hashing Chapter 20. Hash Table A hash table is a data structure that allows fast find, insert, and delete operations (most of the time). The simplest.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
Hash Tables1   © 2010 Goodrich, Tamassia.
Comp 335 File Structures Hashing.
CSC 211 Data Structures Lecture 13
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Hashing Hashing is another method for sorting and searching data.
© 2004 Goodrich, Tamassia Hash Tables1  
Hashing as a Dictionary Implementation Chapter 19.
Searching Given distinct keys k 1, k 2, …, k n and a collection of n records of the form »(k 1,I 1 ), (k 2,I 2 ), …, (k n, I n ) Search Problem - For key.
Lecture 12COMPSCI.220.FS.T Symbol Table and Hashing A ( symbol) table is a set of table entries, ( K,V) Each entry contains: –a unique key, K,
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 8-1 Chapter 8 Hashing Introduction to Data Structure CHAPTER 8 HASHING 8.1 Symbol Table Abstract Data.
Chapter 10 Hashing. The search time of each algorithm depend on the number n of elements of the collection S of the data. A searching technique called.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
COSC 1030 Lecture 10 Hash Table. Topics Table Hash Concept Hash Function Resolve collision Complexity Analysis.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Data Structures Using C++
Chapter 9 Hashing Dr. Youssef Harrath
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Hashing. Search Given: Distinct keys k 1, k 2, …, k n and collection T of n records of the form (k 1, I 1 ), (k 2, I 2 ), …, (k n, I n ) where I j is.
Chapter 11 (Lafore’s Book) Hash Tables Hwajung Lee.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Advanced Associative Structures
Hash Table.
CSCE 3110 Data Structures & Algorithm Analysis
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Data Structures Using C++ 2E
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures Using Java1 Chapter 8 Search Algorithms

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

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

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

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

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

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

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:

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

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

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

Data Structures Using Java12 Binary Search

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

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

Data Structures Using Java15 Binary Search: Example

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

Data Structures Using Java17 Performance of Binary Search

Data Structures Using Java18 Performance of Binary Search

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

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

Data Structures Using Java21 Search Algorithm Analysis Summary

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.

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

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

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

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

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

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

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;

Data Structures Using Java30 Linear Probing

Data Structures Using Java31 Linear Probing

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

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

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

Data Structures Using Java35 Deletion: Open Addressing

Data Structures Using Java36 Deletion: Open Addressing

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

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

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

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

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

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

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