02.09.09IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms.

Slides:



Advertisements
Similar presentations
Hashing.
Advertisements

Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
CSCE 3400 Data Structures & Algorithm Analysis
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.
Hashing Techniques.
© 2006 Pearson Addison-Wesley. All rights reserved13 A-1 Chapter 13 Hash Tables.
Hash Tables and Associative Containers CS-212 Dick Steflik.
Hashing Text Read Weiss, §5.1 – 5.5 Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision.
Hashing COMP171 Fall Hashing 2 Hash table * Support the following operations n Find n Insert n Delete. (deletions may be unnecessary in some applications)
Design and Analysis of Algorithms - Chapter 71 Hashing b A very efficient method for implementing a dictionary, i.e., a set with the operations: – insert.
COMP 171 Data Structures and Algorithms Tutorial 10 Hash Tables.
Hashing General idea: Get a large array
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved13 B-1 Chapter 13 (excerpts) Advanced Implementation of Tables CS102 Sections 51 and 52 Marc Smith and.
Lecture 6 Hashing. Motivating Example Want to store a list whose elements are integers between 1 and 5 Will define an array of size 5, and if the list.
CS 206 Introduction to Computer Science II 04 / 06 / 2009 Instructor: Michael Eckmann.
Searching Searching: –Mainly used for: Fetching / Retrieving the Information such as, –Select query on a database. –Important thing is: Retrieval of Information.
1 Hash Tables  a hash table is an array of size Tsize  has index positions 0.. Tsize-1  two types of hash tables  open hash table  array element type.
Hash Table March COP 3502, UCF.
Searching Chapter 2.
Data Structures Week 6 Further Data Structures The story so far  We understand the notion of an abstract data type.  Saw some fundamental operations.
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.
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.
© 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.
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.
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 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 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.
Data Structures and Algorithms Hashing First Year M. B. Fayek CUFE 2010.
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,
Hashing 8 April Example Consider a situation where we want to make a list of records for students currently doing the BSU CS degree, with each.
Chapter 5: Hashing Part I - Hash Tables. Hashing  What is Hashing?  Direct Access Tables  Hash Tables 2.
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.
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.
CHAPTER 8 SEARCHING CSEB324 DATA STRUCTURES & ALGORITHM.
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.
Review for Final Exam – cs411/511 Definitions (5 questions, 2 points each) Algorithm Analysis (3 questions, 3 points each) General Questions (3 questions,
Hashtables. An Abstract data type that supports the following operations: –Insert –Find –Remove Search trees can be used for the same operations but require.
Hashing 1 Hashing. Hashing 2 Hashing … * Again, a (dynamic) set of elements in which we do ‘search’, ‘insert’, and ‘delete’ n Linear ones: lists, stacks,
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.
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,
Hash Tables ADT Data Dictionary, with two operations – Insert an item, – Search for (and retrieve) an item How should we implement a data dictionary? –
Hashing Goal Perform inserts, deletes, and finds in constant average time Topics Hash table, hash function, collisions Collision handling Separate chaining.
TOPIC 5 ASSIGNMENT SORTING, HASH TABLES & LINKED LISTS Yerusha Nuh & Ivan Yu.
Data Structures Using C++ 2E
Hashing, Hash Function, Collision & Deletion
Hashing CSE 2011 Winter July 2018.
Hash Tables (Chapter 13) Part 2.
Data Structures Using C++ 2E
Review Graph Directed Graph Undirected Graph Sub-Graph
Advanced Associative Structures
Hash Table.
Resolving collisions: Open addressing
CS202 - Fundamental Structures of Computer Science II
Advanced Implementation of Tables
Ch Hash Tables Array or linked list Binary search trees
Ch. 13 Hash Tables  .
Lecture-Hashing.
Presentation transcript:

IT 60101: Lecture #151 Foundation of Computing Systems Lecture 15 Searching Algorithms

IT 60101: Lecture #152 Searching Techniques Several searching techniques Linear search –Sequential search with array –Sequential search with linked list –Binary search –Interpolation –Hashing Nonlinear search –Tree search »Binary tree search: AVL, Red-black, Splay trees search –Graph search »DFS, BFS

IT 60101: Lecture #153 Linear Search with Array

IT 60101: Lecture #154 Complexity Analysis: Linear Search Case 1: The key matches with the first element T(n) = 1 Case 2: Key does not exist T(n) = n Case 3: The key is present at any location in the array

IT 60101: Lecture #155 Complexity Analysis: Summary CaseNumber of key comparisonsAsymptotic complexityRemark Case 1T(n) = 1T(n) = O(1)Best case Case 2T(n) = nT(n) = O(n)Worst case Case 3T(n) = O(n)Average case

IT 60101: Lecture #156 Linear Search with Linked List

IT 60101: Lecture #157 Complexity Analysis: Linear Search with Linked List CaseNumber of key comparisonsAsymptotic complexityRemark Case 1T(n) = 1T(n) = O(1)Best case Case 2T(n) = O(n)Average case Case 3T(n) = O(n)Worst case

IT 60101: Lecture #158 Binary Search

IT 60101: Lecture #159 Complexity Analysis of Binary Search

IT 60101: Lecture #1510 Complexity Analysis of Binary Search Let n be the total number of elements in the list under search and there exist an integer k such that For successful search: –If, then the binary search algorithm requires at least one comparison and at most k comparisons. For unsuccessful search: –If, then the binary search algorithm requires k comparisons. –If, then the binary search algorithm requires either k-1 or k number of comparisons.

IT 60101: Lecture #1511 Binary Search: Complexity Analysis Best case T(n) = 1 Worst case T(n) =

IT 60101: Lecture #1512 Binary Search: Complexity Analysis Average Case –Successful search –Unsuccessful search

IT 60101: Lecture #1513 Interpolation Search

IT 60101: Lecture #1514 Interpolation Search: Complexity Analysis Interpolation search Successful1 Unsuccessful Best caseWorst caseAverage case

IT 60101: Lecture #1515 Comparison: Successful Search

IT 60101: Lecture #1516 Comparison: Unsuccessful Search

IT 60101: Lecture #1517 Nonlinear Search Techniques

IT 60101: Lecture #1518 Nonlinear Search Techniques: Binary Search Tree Search Successful search Unsuccessful search Remark Minimum number of comparisons Maximum number of comparisons Minimum number of comparisons Maximum number of comparisons Case 11Best Case 2 12n-132n+1 Worst Case 3 1 Average

IT 60101: Lecture #1519 Graph Search : DFS 1.Push the starting vertex into the stack OPEN 2.While OPEN is not empty do 3.POP a vertex V 4.If V is not in VISIT 5.Visit the vertex V 6.Store V in VISIT 7.Push all the adjacent vertex of V onto OPEN 8.EndIf 9.EndWhile 10.Stop

IT 60101: Lecture #1520 DFS: Example

IT 60101: Lecture #1521 Graph Search: BFS

IT 60101: Lecture #1522 Complexity of Graph Search T(n) = n.e T(n) = n(n-1)

IT 60101: Lecture #1523 Hashing Techniques Address calculation search

IT 60101: Lecture #1524 Hashing Techniques Hashing Is a mappinf from key to its index (location)

IT 60101: Lecture #1525 Some Hash Functions Division Method H(k) = k (mod h)if indices starts from 0 H(k) = k (mod h) + 1if indices start from 1 Examples H(31) = 31(mod 13) = 5 H(31) = 31 (mod 13) + 1 = 6

IT 60101: Lecture #1526 Some Hash Functions Midsquare Method k: k2: H(k): Limitations Computationally expensive

IT 60101: Lecture #1527 Some Hash Functions Folding Method H(k) = k 1 + k k n Example k: Chopping: Pure folding: = 136 = 169 = 180 Fold shifting: = 190 = 133 = 234 Fold boundary: = 154 = 241 = 207

IT 60101: Lecture #1528 Collision Resolution Techniques Closed hashing –also called linear probing Open hashing –also called chaining.

IT 60101: Lecture #1529 Closed Hashing The search will continue until any one of the following cases occurs: The key value is found. Unoccupied (or empty) location is encountered. It reaches to the location where the search was started.

IT 60101: Lecture #1530 Closed Hashing Example Input: Hash function: H(k) = k mod 7 + 1

IT 60101: Lecture #1531 Closed Hashing Example Input: Hash function: H(k) = k mod 7 + 1

IT 60101: Lecture #1532 Drawback of Closed Hashing Clustering –key values are clustered in large groups and as a result sequential search becomes slower and slower. Following are some solutions known to avoid this situation: Random probing Double hashing or rehashing Quadratic probing

IT 60101: Lecture #1533 Open Hashing Also called separate chaining –This method uses a hash table as an array of pointers –Each pointer points a linked list hash table is an array of list headers.

IT 60101: Lecture #1534 Advantages of Open Hashing 1.Overflow situation never arises. Hash table maintains lists which can contain any number of key values. 2.Collision resolution can be achieved very efficiently if the lists maintain an ordering of keys, so that keys can be searched quickly. 3.Insertion and deletion become quick and easy task in open hashing. Deletion proceeds in exactly the same way as deletion of a node in single linked list. 4.Finally, open hashing is best suitable in applications where number of key values varies drastically as it uses dynamic storage management policy.

IT 60101: Lecture #1535 Analysis of Hashing S( ) = Average number of probes for a successful search. U( ) = Average number of probes for an unsuccessful search.

IT 60101: Lecture #1536 Analysis of Hashing Closed hashing with random probing Close hashing with open probing Open hashing

IT 60101: Lecture #1537 Analysis of Hashing

IT 60101: Lecture #1538