Download - Chapter 9
![Page 1: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/1.jpg)
Data Structures Using C++ 1
Chapter 9
Search Algorithms
![Page 2: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/2.jpg)
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
![Page 3: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/3.jpg)
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
![Page 4: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/4.jpg)
Data Structures Using C++ 4
Sequential Search
template<class elemType>int arrayListType<elemType>::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
![Page 5: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/5.jpg)
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
![Page 6: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/6.jpg)
Data Structures Using C++ 6
Search AlgorithmsSuppose 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:
![Page 7: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/7.jpg)
Data Structures Using C++ 7
Ordered Lists as Arrays
template<class elemType>class orderedArrayListType: public arrayListType<elemType>{public: orderedArrayListType(int size = 100); //constructor ... //We will add the necessary members as needed.private: //We will add the necessary members as needed.}
![Page 8: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/8.jpg)
Data Structures Using C++ 8
Ordered Lists as Linked Lists
template<class elemType>
class orderedLinkedListType: public linkedListType<elemType>
{
public:
...
}
![Page 9: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/9.jpg)
Data Structures Using C++ 9
Binary Search
![Page 10: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/10.jpg)
Data Structures Using C++ 10
Binary Search: middle element
first + last
2mid =
![Page 11: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/11.jpg)
Data Structures Using C++ 11
Binary Searchtemplate<class elemType>int orderedArrayListType<elemType>::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
![Page 12: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/12.jpg)
Data Structures Using C++ 12
Binary Search: Example
![Page 13: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/13.jpg)
Data Structures Using C++ 13
Binary Search: Example
• Unsuccessful search
• Total number of comparisons is 6
![Page 14: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/14.jpg)
Data Structures Using C++ 14
Performance of Binary Search
![Page 15: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/15.jpg)
Data Structures Using C++ 15
Performance of Binary Search
![Page 16: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/16.jpg)
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
![Page 17: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/17.jpg)
Data Structures Using C++ 17
Binary Search and Insertion
template<class elemType>class orderedArrayListType: public arrayListType<elemType>{public: void insertOrd(const elemType&); int binarySearch(const elemType& item); orderedArrayListType(int size = 100);};
![Page 18: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/18.jpg)
Data Structures Using C++ 18
Search Algorithm Analysis Summary
![Page 19: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/19.jpg)
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.
![Page 20: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/20.jpg)
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
![Page 21: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/21.jpg)
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
![Page 22: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/22.jpg)
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
![Page 23: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/23.jpg)
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<int>(key[j]);
return (sum % HTSize);
}//end hashFunction
![Page 24: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/24.jpg)
Data Structures Using C++ 24
Collision Resolution
• Algorithms to handle collisions
• Two categories of collision resolution techniques– Open addressing (closed hashing)– Chaining (open hashing)
![Page 25: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/25.jpg)
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;
![Page 26: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/26.jpg)
Data Structures Using C++ 26
Linear Probing
![Page 27: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/27.jpg)
Data Structures Using C++ 27
Linear Probing
![Page 28: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/28.jpg)
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
![Page 29: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/29.jpg)
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
![Page 30: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/30.jpg)
Data Structures Using C++ 30
Deletion: Open Addressing
![Page 31: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/31.jpg)
Data Structures Using C++ 31
Deletion: Open Addressing
![Page 32: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/32.jpg)
Data Structures Using C++ 32
Collision Resolution: Chaining (Open Hashing)
![Page 33: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/33.jpg)
Data Structures Using C++ 33
Hashing Analysis
Let
Then a is called the load factor
![Page 34: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/34.jpg)
Data Structures Using C++ 34
Linear Probing: Average Number of Comparisons
1. Successful search
2. Unsuccessful search
![Page 35: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/35.jpg)
Data Structures Using C++ 35
Quadratic Probing: Average Number of Comparisons
1. Successful search
2. Unsuccessful search
![Page 36: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/36.jpg)
Data Structures Using C++ 36
Chaining: Average Number of Comparisons
1. Successful search
2. Unsuccessful search
![Page 37: Chapter 9](https://reader033.vdocuments.net/reader033/viewer/2022051401/56813af4550346895da37363/html5/thumbnails/37.jpg)
Data Structures Using C++ 37
Chapter Summary
• Search Algorithms– Sequential– Binary
• Algorithm Analysis• Hashing
– Hash Table– Hash function– Collision Resolution