1 Data Structures Searching Techniques Namiq Sultan.

Slides:



Advertisements
Similar presentations
Hash Tables CSC220 Winter What is strength of b-tree? Can we make an array to be as fast search and insert as B-tree and LL?
Advertisements

CSCE 3400 Data Structures & Algorithm Analysis
Data Structures Using C++ 2E
© 2004 Goodrich, Tamassia Hash Tables1  
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.
Hashing Techniques.
Dictionaries and Their Implementations
1 Hashing (Walls & Mirrors - end of Chapter 12). 2 I hate quotations. Tell me what you know. – Ralph Waldo Emerson.
1 Chapter 9 Maps and Dictionaries. 2 A basic problem We have to store some records and perform the following: add new record add new record delete record.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Hash Tables1 Part E Hash Tables  
Hash Tables1 Part E Hash Tables  
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.
Hash Tables. Container of elements where each element has an associated key Each key is mapped to a value that determines the table cell where element.
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
COSC 2007 Data Structures II
Hash Table March COP 3502, UCF.
Data Structures Using Java1 Chapter 8 Search Algorithms.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
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.
1 Hash table. 2 Objective To learn: Hash function Linear probing Quadratic probing Chained hash table.
1 Hash table. 2 A basic problem We have to store some records and perform the following:  add new record  delete record  search a record by key Find.
Comp 335 File Structures Hashing.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
Prof. Amr Goneid, AUC1 CSCI 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 5. Dictionaries(2): Hash Tables.
Can’t provide fast insertion/removal and fast lookup at the same time Vectors, Linked Lists, Stack, Queues, Deques 4 Data Structures - CSCI 102 Copyright.
© 2004 Goodrich, Tamassia Hash Tables1  
HASHING PROJECT 1. SEARCHING DATA STRUCTURES Consider a set of data with N data items stored in some data structure We must be able to insert, delete.
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.
CS201: Data Structures and Discrete Mathematics I Hash Table.
WEEK 1 Hashing CE222 Dr. Senem Kumova Metin
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
1 Hashing - Introduction Dictionary = a dynamic set that supports the operations INSERT, DELETE, SEARCH Dictionary = a dynamic set that supports the operations.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
Hashing is a method to store data in an array so that sorting, searching, inserting and deleting data is fast. For this every record needs unique key.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Hashing Chapter 7 Section 3. What is hashing? Hashing is using a 1-D array to implement a dictionary o This implementation is called a "hash table" Items.
Hash Table March COP 3502, UCF 1. Outline Hash Table: – Motivation – Direct Access Table – Hash Table Solutions for Collision Problem: – Open.
COSC 2007 Data Structures II Chapter 13 Advanced Implementation of Tables IV.
Hashing Suppose we want to search for a data item in a huge data record tables How long will it take? – It depends on the data structure – (unsorted) linked.
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
Data Structures Using C++
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
1 the hash table. hash table A hash table consists of two major components …
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
Searching Tables Table: sequence of (key,information) pairs (key,information) pair is a record key uniquely identifies information, so no duplicate records.
Hash Tables Ellen Walker CPSC 201 Data Structures Hiram College.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
Hashing CSE 2011 Winter July 2018.
Review Graph Directed Graph Undirected Graph Sub-Graph
Advanced Associative Structures
Hash Table.
Hash Tables.
Dictionaries and Their Implementations
CS202 - Fundamental Structures of Computer Science II
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Ch Hash Tables Array or linked list Binary search trees
Ch. 13 Hash Tables  .
Presentation transcript:

1 Data Structures Searching Techniques Namiq Sultan

2 Searching Techniques Searching is a process of checking and finding an element from a list of elements. 1. Linear or Sequential Searching 2. Binary Searching 3. Fibanocci Search

3 LINEAR OR SEQUENTIAL SEARCHING In linear search, each element of an array is read one by one sequentially and it is compared with the desired element. A search will be unsuccessful if all the elements are read and the desired element is not found.

4 ALGORITHM FOR LINEAR SEARCH Let A be an array of n elements, A[0],A[1],A[2],... A[n-1], “data” is the element to be searched. Then this algorithm will find the location “loc” of data in A. Set loc = – 1, if the search is unsuccessful. Input an array a of n elements and “data” to be searched Initialize loc = – 1, and i = 0 Repeat If (data = a[i]) then loc = i End if If (loc > -1) then display “data is found and searching is successful” Else display “data is not found and searching is unsuccessful” End if i++ End repeat

5 PROGRAMMING SEQUENTIAL SEARCH void main() { char opt; int arr[20],n,i,item; cout << “\nHow many elements in the array : ”; cin >> n; for(i=0; i<n; i++) { cout << “\nEnter element ” << i+1 << “: “; cin >> arr[i]; } cout << “\n\nPress any key to continue....”; cin.get();

6 PROGRAMMING SEQUENTIAL SEARCH do{ cout<< “\nEnter the element to be searched : ”; cin >> item; //Input the item to be searched for(i=0;i < n;i++){ if(item == arr[i]){ cout << (“Item found at position : ”, i+1); break; } }/*End of for*/ if (i == n) cout << “\nItem “ << item << “ not found...\n”; cout << “\n\nPress (Y/y) to continue : ”; cin >> opt; }while(opt == ‘Y’ || opt == ‘y’); }

7 BINARY SEARCH Binary search is an extremely efficient algorithm when it is compared to linear search. Binary search technique searches “data” in minimum possible comparisons. Suppose the given array is a sorted one, otherwise first we have to sort the array elements.

8 ALGORITHM OF BINARY SEARCH Input an array A of n elements and “data” to be searched LB = 0, UB = n-1, mid = int ((LB+UB)/2) while (LB <= UB) and (A[mid] != data) If (data < A[mid]) then UB = mid–1 Else LB = mid + 1 End if mid = int ((LB + UB)/2) End while If (A[mid]== data) then Display “the data found” Else Display “the data is not found” End if

9 PROGRAM OF BINARY SEARCH void main() { char opt; const n=10; int arr[20], start, end, middle, i, item; for(i=0; i < n; i++) { cout << “\nEnter element: ” << i+1 << “ : “; cin >> arr[i]; } cout <<(“\n\nPress any key to continue...”); cin.get(); do { cout<<“\nEnter the element to be searched : ”; cin>>item;

10 start=0; end=n – 1; middle=(start + end)/2; while(item != arr[middle] && start < end) { if (item > arr[middle]) start=middle+1; else end=middle-1; middle=(start+end)/2; } if (item==arr[middle]) cout<<“Item found at position ”<<middle+1<<endl; else if(start>end) cout<<“Item not found in array\n”; cout<<“\n\nPree (Y/y) to continue : ”; cin >> opt; }while(opt == ‘Y’ || opt == ‘y’); }/*End of main()*/ PROGRAM OF BINARY SEARCH

11 Hashing is a technique where we can compute the location of the desired record in order to retrieve it in a single access (or comparison). The information to be retrieved is stored in a hash table which is best thought of as an array of m locations, called buckets. The hash table have a key/value relationship. The keys are unique, that is, no two distinct values have the same key. Hash table has only a few methods, such as insert, erase, and find. HASH TABLE

12 Suppose “persons” is a hash table that will hold up to 1000 values. Each value consists of a unique 3-digit integer (the key), and a name. HASH TABLE keyname index

13 HASH TABLE keyvalue 108 Jalal351 Dilshad 453Ali WHERE SHOULD WE STORE THE FOLLOWING PERSONS ? Persons [351] = “ Jalal ” ; persons [108] = “ Dilshad ” ; Persons [435] = “ Ali ” ;

14 HASH TABLE Now for something slightly different: suppose “persons” is a hash table that holds up to 1000 values. Each value consists of a 11-digit telephone number (the key), and a name. persons [ ] = “Jalal”; persons [ ] = “Dilshad”; persons [ ] = “Ali”; persons [ ] = “Dina”; WHERE SHOULD THESE VALUES BE STORED? To make these values fit into the table, we need to mod by the table size; i.e., key %     256 !!!!! Collision

15 HASH TABLE keyvalue Jalal Dilshad Ali

16 HASH TABLE when two different keys map to the same index, that is called a collision. hashing: an algorithm that transforms a key into an array index. The algorithm has two parts: 1. a hash function: an easily computable operation on the key that returns an index in the array buckets; Hash(Key) = Integer 2. a collision handler.

Hash Function 17 Input: a field of a record; usually its key K (student id, name, … ) Compute index function H(K) H(K): K  A To find the address of the record. H(K) is address of the record with key K Example 1: Key is student id (six digits), we have 100,000 records positions (0- 99,999) H(K): student_id mod  mod =  mod =  mod = 1005

Hash Function 18 Example 2: Key is a name H(K): Take first two characters, multiply their ASCII decimal values, add the last three digits and take its mod 9 as H(K) K = Lokman  76 * 111 = 8436 H(K) = = 13 mod 9 = 4 keyvalue Lokman 5

Collisions Usually, ideal hashing is not possible (or at least not guaranteed). Some data is bound to hash to the same table element, in which case, we have a collision. How do we solve this problem?

Hashing 20 Collision Handlers Chaining Linear Probing (Open Addressing) Double Hashing Quadratic Hashing

21 Chaining Jalal Dilshad Ali Dina Chaining is simple, but requires additional memory outside the table For each element of the table, a linked list is maintained to hold data that map to the same location. This list can grow as items are entered into the list.

Linear Probing The idea is that even though these number hash to the same location, they need to be given a slot based on their hash number index. Using linear probing, the entries are placed into the next available position. Consider the data with keys: 24, 42, 34,62,73 into a table of size 10. These entries can be placed into the table at the following locations:

Linear Probing 24 mod10 = 4. Position is free. 24 placed into element 4 42 mod10 = 2. Position is free. 42 placed into element 2 34 mod10 = 4. Position is occupied. Try next place in the table (5). 34 placed into position mod10 = 2. Position is occupied. Try next place in the table (3). 62 placed into position mod10 = 3. Position is occupied. Try next place in the table (4). Same problem. Try (5). Then (6). 73 is placed into position 6.

Advantages and Disadvantages of Hash Table + Very fast insertion and searching. + Relatively easy to program as compared to trees. - Based on arrays, hence difficult to expand. - No convenient way to visit the items in a hash table in any kind of order. 24

Example //data structure to hold id and data, our data-structure we want to use struct person{ int id;//to hold a unique id for each person char name[20];// name of a person }; class hasher{ person dt[11];//the table to hold hashed data structs int numel;//number of elements in table, to check if it's full public: hasher(); int hash(int id); int rehash(int id); void add(person d); void output(); };

Example /*this is the function to give hashed id, it's a simple one... It's good to use a prime number for table length, thats why I used 11, it's better because we reduce our collisions*/ int hasher::hash(int id) { return (id%11); } /*in case of any collision we use rehash function instead of hash*/ int hasher::rehash(int id) { return ((id+1)%11); } hasher::hasher() { //create an array of data structure int i; for(i=0; i<=10; i++) { dt[i].id = -1; //set all ids to -1 to show they're empty strcpy(dt[i].name,"");//set all name values to empty } numel = 0; }

Example void hasher::add(person p) { if(numel < 11) {//table has empty places... int hashed = hash(p.id); if (dt[hashed].id == -1) { //slot is empty, assign new data dt[hashed].id = p.id; strcpy(dt[hashed].name, p.name); } else {//we need to rehash the id while (1) { //try every place in table to find an empty place hashed = rehash(hashed); if (dt[hashed].id == -1) { dt[hashed].id = p.id; strcpy(dt[hashed].name, p.name); break; } else cout<<"Table is full\n\n"; }

Example void hasher::output() { int i; for(i=0; i<11; i++) { cout \t“ << dt[i].id << "\t“ << dt[i].name << endl; } int main() { person d[11]={ 123, "Ahmed Ali", 733, "Basim Taha", 380, "Careen Fady", 890, "Emad Salah", 460, "Fatin Ahmed", 156, "Ghali Shukri", 222, "Hadi Ali", 101, "Iman Edward", 444, "Jamal Nassir", 611, "Karim Morad", 999, "Lolo Kareem“ };

Example hasher h1; int i=0; for (i=0; i<11; i++) { h1.add(d[i]);//add this record to table } h1.output();//see the output return 0; }