Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Quicksort
Advertisements

MATH 224 – Discrete Mathematics
Data Structures Using C++ 2E
Data Structures Using C++ 2E
Searching and Sorting Algorithms Based on D. S
© The McGraw-Hill Companies, Inc., Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
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
@ Zhigang Zhu, CSC212 Data Structure - Section FG Lecture 19 Searching Instructor: Zhigang Zhu Department of Computer Science City College.
2 -1 Chapter 2 The Complexity of Algorithms and the Lower Bounds of Problems.
2 -1 Analysis of algorithms Best case: easiest Worst case Average case: hardest.
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.
C++ Plus Data Structures
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
CHAPTER 11 Searching. 2 Introduction Searching is the process of finding a target element among a group of items (the search pool), or determining that.
The Complexity of Algorithms and the Lower Bounds of Problems
Data Structures Using C++ 2E Chapter 11 Binary Trees and B-Trees.
Elementary Data Structures and Algorithms
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure - Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Data Structures Using Java1 Chapter 8 Search Algorithms.
Chapter 16: Searching, Sorting, and the vector Type.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
Data Structures Using C++1 Chapter 9 Search Algorithms.
Final Exam Review CS 3610/5610N Dr. Jundong Liu.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 19: Searching and Sorting Algorithms.
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.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular search.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
Data Structures Using C++ 2E Chapter 10 Sorting Algorithms.
CSC 211 Data Structures Lecture 13
“Enthusiasm releases the drive to carry you over obstacles and adds significance to all you do.” – Norman Vincent Peale Thought for the Day.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
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.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
1 Lower Bound on Comparison-based Search We have now covered lots of searching methods –Contiguous Data (Arrays) Sequential search Binary Search –Dynamic.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
Data Structures Using C++1 Chapter 10 Sorting Algorithms.
Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms.
Data Structures Using C++
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Lecture 9COMPSCI.220.FS.T Lower Bound for Sorting Complexity Each algorithm that sorts by comparing only pairs of elements must use at least 
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
1 the BSTree class  BSTreeNode has same structure as binary tree nodes  elements stored in a BSTree are a key- value pair  must be a class (or a struct)
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
WHICH SEARCH OR SORT IS BETTER?. COMPARING ALGORITHMS Time efficiency refers to how long it takes an algorithm to run Space efficiency refers to the amount.
Binary Search Manolis Koubarakis Data Structures and Programming Techniques 1.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Data Structures Using C++ 2E
Search by Hashing.
CSC212 Data Structure - Section RS
Searching CLRS, Sections 9.1 – 9.3.
CSC212 Data Structure - Section KL
Data Structures Using C++ 2E
Applications of Arrays
Presentation transcript:

Data Structures Using C++ 2E Chapter 9 Searching and Hashing Algorithms

Data Structures Using C++ 2E2 Objectives Learn 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++ 2E3 Search Algorithms Item key –Unique member of the item –Used in searching, sorting, insertion, deletion Number of key comparisons –Comparing the key of the search item with the key of an item in the list Can use class arrayListType (Chapter 3) –Implements a list and basic operations in an array

Data Structures Using C++ 2E4 Sequential Search Array-based lists –Covered in Chapter 3 Linked lists –Covered in Chapter 5 Works the same for array-based lists and linked lists See code on page 499

seqSearch in array Data Structures Using C++ 2E

6 Sequential Search Analysis Examine effect of for loop in code on page 499 Different programmers might implement same algorithm differently Computer speed affects performance

Data Structures Using C++ 2E7 Sequential Search Analysis (cont’d.) Sequential search algorithm performance –Examine worst case and average case –Count number of key comparisons Unsuccessful search –Search item not in list –Make n comparisons Conducting algorithm performance analysis –Best case: make one key comparison –Worst case: algorithm makes n comparisons

Data Structures Using C++ 2E8 Sequential Search Analysis (cont’d.) Determining the average number of comparisons –Consider all possible cases –Find number of comparisons for each case –Add number of comparisons, divide by number of cases

Data Structures Using C++ 2E9 Sequential Search Analysis (cont’d.) Determining the average number of comparisons (cont’d.)

Data Structures Using C++ 2E10 Binary Search Performed only on ordered lists Uses divide-and-conquer technique FIGURE 9-1 List of length 12 FIGURE 9-2 Search list, list[0]...list[11] FIGURE 9-3 Search list, list[6]...list[11]

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ]

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Find approximate midpoint

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Is 7 = midpoint key? NO.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Is 7 < midpoint key? YES.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Search for the target in the area before midpoint.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Find approximate midpoint

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Target = key of midpoint? NO.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Target < key of midpoint? NO.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Target > key of midpoint? YES.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Search for the target in the area after midpoint.

Data Structures Using C++ 2E Binary Search [ 0 ][ 1 ] Example: sorted array of integer keys. Target= [ 2 ][ 3 ][ 4 ][ 5 ][ 6 ] Find approximate midpoint. Is target = midpoint key? YES.

Binary Search (recursive) void search(const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle; if(size == 0) found = false; else { middle = first + size/2; if(target == a[middle]){ location = middle; found = true; } else if (target < a[middle]) // target is less than middle, so search subarray before middle search(a, first, size/2, target, found, location); else // target is greater than middle, so search subarray after middle search(a, middle+1, (size-1)/2, target, found, location); }

Binary Search (iterative)

Data Structures Using C++ 2E Relation to Binary Search Tree Corresponding complete binary search tree Array of previous example:

Data Structures Using C++ 2E Search for target = 7 Start at root: Find midpoint:

Data Structures Using C++ 2E Search left subarray: Search for target = 7 Search left subtree:

Data Structures Using C++ 2E Find approximate midpoint of subarray: Search for target = 7 Visit root of subtree:

Data Structures Using C++ 2E Search right subarray: Search for target = 7 Search right subtree:

Data Structures Using C++ 2E Binary Search: Analysis Worst case complexity? What is the maximum depth of recursive calls in binary search as function of n? Each level in the recursion, we split the array in half (divide by two). Therefore maximum recursion depth is floor(log 2 n) and worst case = O(log 2 n). Average case is also = O(log 2 n).

Data Structures Using C++ 2E30 Insertion into an Ordered List After insertion: resulting list must be ordered –Find place in the list to insert item Use algorithm similar to binary search algorithm –Slide list elements one array position down to make room for the item to be inserted –Insert the item Use function insertAt ( class arrayListType ) An unsuccessful search first, to find the place to insert Then call insertAt () to insert.

Data Structures Using C++ 2E31 Insertion into an Ordered List (cont’d.) Can also override function seqSearch –Perform sequential search on an ordered list Takes into account that elements are ordered TABLE 9-4 Number of comparisons for a list of length n

Data Structures Using C++ 2E32 Lower Bound on Comparison-Based Search Algorithms Comparison-based search algorithms –Search list by comparing target element with list elements Sequential search: order n Binary search: order log 2 n

Data Structures Using C++ 2E33 Lower Bound on Comparison-Based Search Algorithms (cont’d.) Devising a search algorithm with order less than log 2 n –Obtain lower bound on number of comparisons Cannot be comparison based

34 Lower Bound on Comparison-Based Search Algorithms (cont’d.) Proof is based on the decision tree model: Visualization of the execution of a comparison-based algorithm (e.g., search or sort). The following is the decision tree for binary search

Data Structures Using C++ 2E35 Can we do better, if not based on comparisons? For example: O(1)? Yes, using Hashing.