Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques



Similar documents
Analysis of Binary Search algorithm and Selection Sort algorithm

Zabin Visram Room CS115 CS126 Searching. Binary Search

Data Structures Fibonacci Heaps, Amortized Analysis

Binary search algorithm

From Last Time: Remove (Delete) Operation

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

6. Standard Algorithms

Section IV.1: Recursive Algorithms and Recursion Trees

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

APP INVENTOR. Test Review

Symbol Tables. Introduction

Converting a Number from Decimal to Binary

Computer Science 210: Data Structures. Searching

Binary Heap Algorithms

Heaps & Priority Queues in the C++ STL 2-3 Trees

Ordered Lists and Binary Trees

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Data Structures and Algorithms

Data Structures and Data Manipulation

Cpt S 223. School of EECS, WSU

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

TREE BASIC TERMINOLOGIES

Sample Questions Csci 1112 A. Bellaachia

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Loop Invariants and Binary Search

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

DATABASE DESIGN - 1DL400

Decision Trees from large Databases: SLIQ

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Analysis of a Search Algorithm

Persistent Data Structures and Planar Point Location

Lecture Notes on Linear Search

Optimal Binary Search Trees Meet Object Oriented Programming

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

Data Structures. Chapter 8

Chapter Objectives. Chapter 9. Sequential Search. Search Algorithms. Search Algorithms. Binary Search

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Binary Heaps. CSE 373 Data Structures

Node-Based Structures Linked Lists: Implementation

Lecture Notes on Binary Search Trees

Polarization codes and the rate of polarization

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Fundamental Algorithms

For example, we have seen that a list may be searched more efficiently if it is sorted.

1 Introduction to Internet Content Distribution

Algorithms. Margaret M. Fleck. 18 October 2010

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees (BST)

1.2 Solving a System of Linear Equations

Chapter 8: Structures for Files. Truong Quynh Chi Spring- 2013

Ready, Set, Go! Math Games for Serious Minds

How To Create A Tree From A Tree In Runtime (For A Tree)

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

The Goldberg Rao Algorithm for the Maximum Flow Problem

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Full and Complete Binary Trees

Class Notes CS Creating and Using a Huffman Code. Ref: Weiss, page 433

Chapter 13. Disk Storage, Basic File Structures, and Hashing

Data Structures. Topic #12

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Fibonacci Numbers and Greatest Common Divisors. The Finonacci numbers are the numbers in the sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,...

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

6 March Array Implementation of Binary Trees

Linear Programming. March 14, 2014

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

BM307 File Organization

1 Review of Least Squares Solutions to Overdetermined Systems

Physical Data Organization

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

A Comparison of Dictionary Implementations

Pseudo code Tutorial and Exercises Teacher s Version

Classifying Large Data Sets Using SVMs with Hierarchical Clusters. Presented by :Limou Wang

International Journal of Advanced Research in Computer Science and Software Engineering

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Exam study sheet for CS2711. List of topics

A Randomized Searching Algorithm and its Performance analysis with Binary Search and Linear Search Algorithms

The ADT Binary Search Tree

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Chapter 13: Query Processing. Basic Steps in Query Processing

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Review of Hashing: Integer Keys

Introduction to Learning & Decision Trees

Euclidean Minimum Spanning Trees Based on Well Separated Pair Decompositions Chaojun Li. Advised by: Dave Mount. May 22, 2014

Why Use Binary Trees?

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

M-way Trees and B-Trees

Introduction to Parallel Programming and MapReduce

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

I. Pointwise convergence

Transcription:

Searching (Unit 6)

Outline Introduction Linear Search Ordered linear search Unordered linear search Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

In the discipline of computer science, the problem of search has assumed enormous significance It spans a variety of applications rather disciplines, beginning from searching for a key in a list of data elements to searching for a solution to a problem in its search space Innumerable problems exist where one searches for patterns images, voice, text, hyper text, photographs etc., in a repository of data or patterns, for the solution of the problems concerned.

Linear search A linear searchor sequential searchis one where a key K is searched for in a linear list L of data elements. The list L is commonly represented using a sequential data structure such as an array If L is ordered then the search is said to be an ordered linear searchand if L is unordered then it is said to be unordered linear search

Searching A question you should always ask when selecting a search algorithm is How fast does the search have to be? The reason is that, in general, the faster the algorithm is, the more complex it is. Bottom line: you don t always need to use or should use the fastest algorithm. Let s explore the following search algorithms, keeping speed in mind. Sequential (linear) search Binary search

Sequential Search on an Unordered File Basic algorithm: Get the search criterion (key) Get the first record from the file While ( (record!= key) and (still more records) ) Get the next record End_while When do we know that there wasn t a record in the file that matched the key?

Sequential Search on an Ordered File Basic algorithm: Get the search criterion (key) Get the first record from the file While ( (record < key) and (still more records) ) Get the next record End_while If( record = key ) Then success Else there is no match in the file End_else When do we know that there wasn t a record in the file that matched the key?

Sequential Search of Ordered vs. Unordered List Let s do a comparison. If the order was ascending alphabetical on customer s last names, how would the search for John Adams on the ordered list compare with the search on the unordered list? Unordered list if John Adams was in the list? if John Adams was not in the list? Ordered list if John Adams was in the list? if John Adams was not in the list?

Ordered vs Unordered (con t) How about George Washington? Unordered if George Washington was in the list? If George Washington was not in the list? Ordered if George Washington was in the list? If George Washington was not in the list? How about James Madison?

Ordered vs. Unordered (con t) Observation: the search is faster on an ordered list only when the item being searched for is not in the list. Also, keep in mind that the list has to first be placed in order for the ordered search. Conclusion: the efficiencyof these algorithms is roughly the same. So, if we need a faster search, we need a completely different algorithm. How else could we search an ordered file?

Algorithm 15.1: Procedure for ordered linear search procedure LINEAR_SEARCH_ORDERED(L, n, K) /* L[0:n-1] is a linear ordered list of data elements. K is the key to be searched for in the list. In case of unsuccessful search, the procedure prints the message KEY not found otherwise prints KEY found and returns the index i*/ i = 0; while (( i < n) and (K > L[i])) do i = i + 1; endwhile /* search for X down the list*/ if ( K = L[i]) then { print ( KEY found ); return (i);} /* Key K found. Return index i */ else print ( KEY not found ); end LINEAR_SEARCH_ORDERED.

Transpose Sequential Search Searches a list of data items for a key, checking itself against the data items one at a time in a sequence If the key is found, then it is swapped with its predecessor and the search is termed successful. The swapping of the search key with its predecessor, once it is found, favors faster search when one repeatedly looks for the key The more frequently one looks for a specific key in a list, the faster the retrievals take place in transpose sequential search, since the found key moves towards the beginning of the list with every retrieval operation Thus transpose sequential search is most successful when a few data items are repeatedly looked for in a list.

Interpolation Search Interpolation search is based on this principle of attempting to look for a key in a list of elements, by comparing the key with specific elements at calculated positions and ensuring if the key occurs before it or after it until either the key is found or not found The list of elements must be orderedand we assume that they are uniformly distributed with respect to requests.

Binary Search If we have an ordered list and we know how many things are in the list (i.e., number of records in a file), we can use a different strategy. The binary searchgets its name because the The binary searchgets its name because the algorithm continually divides the list into two parts.

How a Binary Search Works Always look at the center value. Each time you get to discard half of the remaining list. Is this fast?

How Fast is a Binary Search? Worst case: 11 items in the list took 4 tries How about the worst case for a list with 32 items? 1st try -list has 16 items 2nd try -list has 8 items 3rd try -list has 4 items 4th try -list has 2 items 5th try -list has 1 item

How Fast is a Binary Search? (con t) List has 250 items 1st try -125 items 2nd try -63 items 3rd try -32 items 4th try -16 items 5th try -8 items 6th try -4 items 7th try -2 items 8th try -1 item List has 512 items 1st try -256 items 2nd try -128 items 3rd try -64 items 4th try -32 items 5th try -16 items 6th try -8 items 7th try -4 items 8th try -2 items 9th try -1 item

What s the Pattern? List of 11 took 4 tries List of 32 took 5 tries List of 250 took 8 tries List of 512 took 9 tries 32 = 2 5 and 512 = 2 9 8 < 11 < 16 2 3 < 11 < 2 4 128 < 250 < 256 2 7 < 250 < 2 8

A Very Fast Algorithm! How long (worst case) will it take to find an item in a list 30,000 items long? 2 10 = 1024 2 13 = 8192 2 11 = 2048 2 14 = 16384 2 12 = 4096 2 15 = 32768 So, it will take only 15 tries!

Lg n Efficiency We say that the binary search algorithm runs in log 2 n time. (Also written as lg n) Lg n means the log to the base 2 of some value of n. 8 = 2 3 lg 8 = 3 16 = 2 4 lg 16 = 4 There are no algorithms that run faster than lg n time.

Binary Search Example 7 12 42 59 71 86 104 212 Looking for 89

Binary Search Example 7 12 42 59 71 86 104 212 Looking for 89

Binary Search Example 7 12 42 59 71 86 104 212 Looking for 89

Binary Search Example 7 12 42 59 71 86 104 212 89 not found 3comparisons 3= Log(8)

Binary Search Big-O An element can be found by comparing and cutting the work in half. We cut work in ½ each time How many times can we cut in half? Log 2 N Thus binary search is O(Log N).

Algorithm 15.5 Procedure for binary search procedure binary_search(l, low, high, K) /* L[low:high] is a linear ordered sublist of data elements. Initially low is set to 1 and high to n. K is the key to be searched in the list. */ if ( low > high) then {binary_search =0; print( Key not found ); exit();} else { /* key K not found*/ } low + high mid = 2 ; case : K = L[mid]: { print ( Key found ); binary_search=mid; return L[mid];} : K < L[mid]: binary_search = binary_search(l, low, mid-1, K); : K > L[mid]: binary_search = binary_search(l, mid+1, high, K); endcase end binary_search.

The decision tree for binary search supports the following characteristics: (i) the indexes of the left and the right child nodes differ by the same amount from that of the parent node This characteristic renders the search process to be uniform and therefore binary search is also termed as uniform binary search. (ii) for n elements where n = 2 t 1, the difference in the indexes of a 0 1 2... parent node and its child nodes follows the sequence 2,2,2 from the leaf upwards.

Fibonacci Search The Fibonacci number sequence is given by { 0, 1, 1, 2, 3, 5, 8, 13, 21,..} and is generated by the following recurrence relation: F F F 0 = 0 = 1 = 1 i = F i 1 + F i 22 It is interesting to note that the Fibonacci sequence finds an application in a search technique termed Fibonacci search. While binary search selects the median of the sublist as its next element for comparison, the Fibonacci search determines the next element of comparison as dictated by the Fibonacci number sequence.

Other Search Techniques Tree Search Graph Search Indexed Sequential search