CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.

Slides:



Advertisements
Similar presentations
HASH TABLE. HASH TABLE a group of people could be arranged in a database like this: Hashing is the transformation of a string of characters into a.
Advertisements

Skip List & Hashing CSE, POSTECH.
Data Structures Using C++ 2E
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.
Nov 12, 2009IAT 8001 Hash Table Bucket Sort. Nov 12, 2009IAT 8002  An array in which items are not stored consecutively - their place of storage is calculated.
Hashing Techniques.
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.
Previous Lecture Revision Previous Lecture Revision Hashing Searching : –The Main purpose of computer is to store & retrieve –Locating for a record is.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
CS 206 Introduction to Computer Science II 11 / 17 / 2008 Instructor: Michael Eckmann.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Hash Tables1 Part E Hash Tables  
Sequential Search slides. Searching Searching : –Information retrieval is one of the most important application of computers. –EG: Looking for a Name.
CS 206 Introduction to Computer Science II 11 / 12 / 2008 Instructor: Michael Eckmann.
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.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
COSC 2007 Data Structures II
ICS220 – Data Structures and Algorithms Lecture 10 Dr. Ken Cosh.
Searching Chapter 2.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Data Structures Using C++1 Chapter 9 Search Algorithms.
1 Chapter 5 Hashing General ideas Methods of implementing the hash table Comparison among these methods Applications of hashing Compare hash tables with.
Data Structures and Algorithm Analysis Hashing Lecturer: Jing Liu Homepage:
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture8.
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 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 م. م علي عبد الكريم حبيب.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (continued) Advanced Implementation of Tables.
TECH Computer Science Dynamic Sets and Searching Analysis Technique  Amortized Analysis // average cost of each operation in the worst case Dynamic Sets.
Appendix E-A Hashing Modified. Chapter Scope Concept of hashing Hashing functions Collision handling – Open addressing – Buckets – Chaining Deletions.
CSC 211 Data Structures Lecture 13
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
1 HASHING Course teacher: Moona Kanwal. 2 Hashing Mathematical concept –To define any number as set of numbers in given interval –To cut down part of.
Hashing Hashing is another method for sorting and searching data.
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.
Chapter 12 Hash Table. ● So far, the best worst-case time for searching is O(log n). ● Hash tables  average search time of O(1).  worst case search.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
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 Basis Ideas A data structure that allows insertion, deletion and search in O(1) in average. A data structure that allows insertion, deletion and.
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.
ISOM MIS 215 Module 5 – Binary Trees. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Chapter 13 C Advanced Implementations of Tables – Hash Tables.
Data Structures Using C++
Chapter 9 Hashing Dr. Youssef Harrath
1 Hashing by Adlane Habed School of Computer Science University of Windsor May 6, 2005.
Hashing COMP171. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
Hashing. Hashing is the transformation of a string of characters into a usually shorter fixed-length value or key that represents the original string.
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.
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
Advanced Associative Structures
Hash Table.
Hash Table.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
Sequential Search slides
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Presentation transcript:

CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM

SEARCHING Searching : Retrieving the Information Stored in the computer is one of the most important applications of computer. Example of Applications Looking for a Name by giving the telephone number. Databases - student record, staff record, sales record Internet - search engine : Yahoo, Google 2

TYPES OF SEARCHING  Serial Search ◦ simplest, O(n)  Binary Search ◦ average-case O(log n)  Search by Hashing ◦ better average-case performance 3

SEQUENTIAL SEARCHING Start at the first piece of data and look at it and if it no the data you want then keep going until you find what you are looking for or until you have reached the last. 4

SEQUENTIAL SEARCH - ALGORITHM 1. Initialize a counter to zero 2. While the target is not found and the counter does not equal the last subscript in the array do steps 3 and Increment the counter 4. If array [counter] equals the target value then found becomes true. 5. When the loop ends return whether the target value was or was not found. 5

SEQUENTIAL SEARCH - ALGORITHM int LinearSearch (int array[], int target){ int i = 0, result = -1; bool found = 0; while( (i<n) && (found ==0) ) { //n is the array size if (array[i] == target){ result =i; //target position found = 1; //to break loop } i++; } return result; } 6

SEQUENTIAL SEARCH 7 Two Kinds of result 1) Successful 2) UnSuccessful Best Case - If the element to be searched is in the first location then the number of comparison is one. Worst Case - nth element O(n) -> (last) Average Case - 1/2 n (n+1)

SEQUENTIAL SEARCH Advantage : It is useful for limited sized data sets as it is simple and does not require data to be structured in any way Disadvantage : Time consuming (large list) 8

BINARY SEARCH How it works? Binary - Two. List is broken down to two parts and searched Working : Compare the target with the one in the center of the list and then restrict our attention to only the first or second half depending on whether the target comes before or after the central one. (we reduce the length to be searched by half) 9

BINARY SEARCH Rule - numbers must be in sorted order Steps: 1.Mark the first element as LOW and last element as HI 2.Find the mid point -> MID = (LOW + HI) /2 3.Check If MID = key, return index, where key is the target. If key > MID, LOW = MID +1 and carry on the same process If key > MID, HI = MID-1 and carry on the same process 4.Repeat the same process until you find the value (target) or if LOW and HI cross each other 10

BINARY SEARCH Algorithm low = 0; hi = n – 1; while (low <= hi ) mid = (low + hi) /2; if (key = = k(mid)) return(mid); if (key < k(mid)) hi = mid – 1; else low = mid + 1; end while return (-1); 11

BINARY SEARCH- EXAMPLE LOW =0 HI=11 MID = (LOW + HI) /2 = 11/2 =5 Target : 62 MID = (6+11)/2 = Compare Mid with Search Value - Yes it matches, So print out Element Found Compare MID with Search value - MID is Smaller than Search Value So move LOW to MID + 1 and proceed LOW HI=11

BINARY SEARCH- EXAMPLE LOW =0 HI=11 MID = (LOW + HI) /2 = 11/2 =5 Target : 9 MID = (0+4)/2 = Compare Mid with Search value - MID is Greater than Search Value So move HI to MID - 1 and proceed Compare MID with Search value - MID is Smaller than Search Value So move LOW to MID + 1 and proceed FIRST =0 LOW=MID - 1 = 5-1 = 4

14 MID = (3+4)/2 = Compare MID with Search value - MID is Greater than Search Value So move HI to MID - 1 and proceed MID = (3+3)/2 = Compare MID with Search value - MID is Greater than Search Value So move HI to MID - 1 and proceed Compare MID with Search value - LOW and HI cross each other, so target is Not Found in the list. MID = (3+3)/2 = BINARY SEARCH- EXAMPLE 2

EXERCISE Consider this : Using binary search method,  search 15  search

HASHING - INTRODUCTION Consider this : We have an array of all students in tiny college and the student id numbers from 1 to 200. Now if we want to look for any particular student we simply go to the location of their student id( KEY - value used for searching) and retrieve. If the students’ Id is 10 digit long then we need to convert that large number into a smaller number. 16

THE HASH FUNCTION The Keys may be integer, characters or some kind of data and we use the hash function to turn them into integers in the range of 0..m-1 where m is the size of the array of records. This function may or may not be simple. The term hash and hashing come from the very fact that the conversion from key to index really does ‘hash’ the key as in many cases the index resulting from hashing the key bears absolutely no resemblance to the original key at all. 17

HASHING Hashing is the ideal search, we would know exactly where the data are and go directly there. A search technique in which the required record/ key is located by using a function. 18 Key (k) Index/ reference Hash Function H (k) Example: Table size / array size is 10; therefore the index range is 0-9; if key is 29, H (key) = key % 10. So, H(29) = 29 % 10 = 9. Therefore, key 29 will store in array with index 9

THE HASH FUNCTIONS Two uses of hash function: ◦ Access the list element ◦ Location store the element There are several type of hash function: ◦ Truncation ◦ Modular Arithmetic (Division) ◦ Digit Extraction Method 19

HASHING METHOD - TRUNCATION Exercise: ◦ Write the hash function to truncate first 3 digits of a given 5 digits number.  E.g. key = 26785; Hash (index) = 85 ; Hash (key) = ?? ◦ Write the hash function to truncate last 3 digits of a given 5 digits number.  E.g. key = 26785; Hash (index) = 26; Hash (key) =?? ◦ Write the hash function to truncate first digits and last 2 digits of a given 5 digits number.  E.g. key = 26785; Hash (index) = 67; Hash (key) =?? 20 key %100 key /1000 key /100%100 or key %10000/100

HASHING METHOD - MODULAR ARITHMETIC The key is converted into an integer, the integer is then divided by the size of the index range and the remainder is taken as the index position of the record. index = key % size As an example, suppose we have an 5 digit integer as a key and that there are 1000 records (and room for 1000 records). The hash function would then be: index = KEY % size e.g., % 1000 If we are very lucky! our keys might be such that there is only 1 key that maps to each index. Of course we might still have the situation where two keys map to the same index (e.g , 43456) - this is called a COLLISION. 21

WHAT IS COLLISION SAME INDEX TWO If the hashing Function gives out SAME INDEX for TWO keys then it is called a collision e.g. : 123 % 10 index will be % 10 index will be again 3 We cannot store two values in the same location 22

HOW TO RESOLVE COLLISIONS? Two methods are discussed here: 1.Resolving collisions by OPEN ADDRESSING Linear probing Quadratic probing 2.Resolving collisions by CHAINING 23

RESOLVING COLLISIONS BY OPEN ADDRESSING: Linear Probing : ◦ Start at the point where the collision occurred and do a sequential search through the table for an empty location. Improvement : ◦ Circular Probing : After reaching the end start probing from the first 24

EXAMPLE - LINEAR PROBING The records with the keys 456, 235, 268, 428, 104, 273, 126, 316 are to be stored in an array of size 10 using the hash function.Hash function = (key) mod 10 Solution: 25 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Keys Index

RESOLVING COLLISIONS BY OPEN ADDRESSING: Quadratic Probing: If there is a collision at the address ‘h’, this method probes the table at locations ( Hash value + i 2 ) mod (table_ size) with i=1,2,3……….. 26

Resolving collisions by Open addressing: Example: Insert the following data into a hash table using quadratic probing as the collision resolution strategy. Assume tableSize = 10, h(key)=key % 10. The keys are [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Hash (17) = 17%10 = 7 17 Hash (26) = 26 %10 = 6 26 Hash (38) = 38 %10 = 8 38 Hash (9) = 9 %10 = 9 9 Hash (7) = 7 %10 = 7 f(i) = i 2 ; f(1) = 1 2 = 1 h 1 (7) =(hash (7) + 1) %10 = (7 + 1) %10 = 8 f(i) = i 2 ; f(2) = 2 2 = 4 h 2 (7) =(hash (7) + 4) %10 = (7 + 4) %10 = 1 7

RESOLVING COLLISIONS BY CHAINING Implemented using Linked List Whenever a collision occurs a new node is created and the new value is stored and linked to the old value. Key Index

The End 29