Data Structures Using C++1 Chapter 9 Search Algorithms.

Slides:



Advertisements
Similar presentations
CS Data Structures Chapter 8 Hashing.
Advertisements

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
Lecture 11 oct 6 Goals: hashing hash functions chaining closed hashing application of hashing.
Searching: Self Organizing Structures and Hashing
Data Structures Using C++ 2E
What we learn with pleasure we never forget. Alfred Mercier Smitha N Pai.
CHAPTER 10 ARRAYS II Applications and Extensions.
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.
© 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.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CS Data Structures Chapter 8 Hashing (Concentrating on Static Hashing)
Lecture 11 oct 7 Goals: hashing hash functions chaining closed hashing application of hashing.
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.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
L. Grewe. Computing hash function for a string Horner’s rule: (( … (a 0 x + a 1 ) x + a 2 ) x + … + a n-2 )x + a n-1 ) int hash( const string & key )
Searching Chapter 2.
Data Structures Using Java1 Chapter 8 Search Algorithms.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 7 Prepared by İnanç TAHRALI.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Algorithm Course Dr. Aref Rashad February Algorithms Course..... Dr. Aref Rashad Part: 4 Search Algorithms.
Comp 335 File Structures Hashing.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Hashing Hashing is another method for sorting and searching data.
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.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
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 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
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.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
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
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.
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
CSCI 210 Data Structures and Algorithms
Hashing Alexandra Stefan.
Data Structures Using C++ 2E
Advanced Associative Structures
Hash Table.
Hashing.
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.
Ch Hash Tables Array or linked list Binary search trees
What we learn with pleasure we never forget. Alfred Mercier
Data Structures Using C++ 2E
Chapter 13 Hashing © 2011 Pearson Addison-Wesley. All rights reserved.
Presentation transcript:

Data Structures Using C++1 Chapter 9 Search Algorithms

Data Structures Using C++2 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 C++3 class arrayListType: Basic Operations isEmpty isFull listSize maxListSixe Print isItemAtEqual insertAt insertEnd removeAt retrieveAt replaceAt clearList seqSearch insert remove arrayListType

Data Structures Using C++4 Sequential Search template int arrayListType ::seqSearch(const elemType& item) { int loc; bool found = false; for(loc = 0; loc < length; loc++) if(list[loc] == item) { found = true; break; } if(found) return loc; else return -1; }//end seqSearch

Data Structures Using C++5 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 C++6 Search Algorithms 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 C++7 Ordered Lists as Arrays template class orderedArrayListType: public arrayListType { public: orderedArrayListType(int size = 100); //constructor... //We will add the necessary members as needed. private: //We will add the necessary members as needed. }

Data Structures Using C++8 Ordered Lists as Linked Lists template class orderedLinkedListType: public linkedListType { public:... }

Data Structures Using C++9 Binary Search

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

Data Structures Using C++11 Binary Search template int orderedArrayListType ::binarySearch (const elemType& item) { int first = 0; int last = length - 1; int mid; bool found = false; while(first <= last && !found) { mid = (first + last) / 2; if(list[mid] == item) found = true; else if(list[mid] > item) last = mid - 1; else first = mid + 1; } if(found) return mid; else return –1; }//end binarySearch

Data Structures Using C++12 Binary Search: Example

Data Structures Using C++13 Binary Search: Example Unsuccessful search Total number of comparisons is 6

Data Structures Using C++14 Performance of Binary Search

Data Structures Using C++15 Performance of Binary Search

Data Structures Using C++16 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 C++17 Binary Search and Insertion template class orderedArrayListType: public arrayListType { public: void insertOrd(const elemType&); int binarySearch(const elemType& item); orderedArrayListType(int size = 100); };

Data Structures Using C++18 Search Algorithm Analysis Summary

Data Structures Using C++19 Lower Bound on Comparison- Based Search Theorum: 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 C++20 Hashing Main objectives to choosing hash functions: –Choose a hash function that is easy to compute –Minimize the number of collisions

Data Structures Using C++21 Commonly Used Hash Functions Mid-Square –Hash function, 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 C++22 Commonly Used Hash Functions 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 C++23 Commonly Used Hash Functions Suppose that each key is a string. The following C++ function uses the division method to compute the address of the key: int hashFunction(char *key, int keyLength) { int sum = 0; for(int j = 0; j <= keyLength; j++) sum = sum + static_cast (key[j]); return (sum % HTSize); }//end hashFunction

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

Data Structures Using C++25 Collision Resolution: Open Addressing Pseudocode implementing linear probing: hIndex = hashFunction(insertKey); found = false; while(HT[hIndex] != emptyKey && !found) if(HT[hIndex].key == key) found = true; else hIndex = (hIndex + 1) % HTSize; if(found) cerr<<”Duplicate items are not allowed.”<<endl; else HT[hIndex] = newItem;

Data Structures Using C++26 Linear Probing

Data Structures Using C++27 Linear Probing

Data Structures Using C++28 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 C++29 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 C++30 Deletion: Open Addressing

Data Structures Using C++31 Deletion: Open Addressing

Data Structures Using C++32 Collision Resolution: Chaining (Open Hashing)

Data Structures Using C++33 Hashing Analysis Let Then a is called the load factor

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

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

Data Structures Using C++36 Chaining: Average Number of Comparisons 1. Successful search 2. Unsuccessful search

Data Structures Using C++37 Chapter Summary Search Algorithms –Sequential –Binary Algorithm Analysis Hashing –Hash Table –Hash function –Collision Resolution