 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
MATH 224 – Discrete Mathematics
Counting the bits Analysis of Algorithms Will it run on a larger problem? When will it fail?
HST 952 Computing for Biomedical Scientists Lecture 9.
Proofs, Recursion, and Analysis of Algorithms Mathematical Structures for Computer Science Chapter 2 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProofs,
Analysis And Algorithms CMSC 201. Search Sometimes, we use the location of a piece of information in a list to store information. If I have the list [4,
 1 Sorting. For computer, sorting is the process of ordering data. [ ]  [ ] [ “Tom”, “Michael”, “Betty” ]  [ “Betty”, “Michael”,
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Complexity (Running Time)
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.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Searching and Sorting Gary Wong.
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Analysis of Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
1 CS 177 Week 16 Recitation Recursion. 2 Objective To understand and be able to program recursively by breaking down a problem into sub problems and joining.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Order Statistics. Order statistics Given an input of n values and an integer i, we wish to find the i’th largest value. There are i-1 elements smaller.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
COMP20010: Algorithms and Imperative Programming Lecture 4 Ordered Dictionaries and Binary Search Trees AVL Trees.
Computing Science 1P Lecture 21: Wednesday 18 th April Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSC 211 Data Structures Lecture 13
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Introduction to Algorithms: Verification, Complexity, and Searching (2) Andy Wang Data Structures, Algorithms, and Generic Programming.
More on Efficiency Issues. Greatest Common Divisor given two numbers n and m find their greatest common divisor possible approach –find the common primes.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting.
Chapter 6 Recursion. Solving simple problems Iteration can be replaced by a recursive function Recursion is the process of a function calling itself.
(c) , University of Washington18a-1 CSC 143 Java Searching and Recursion N&H Chapters 13, 17.
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 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Computability O(n) exercises. Searching. Shuffling Homework: review examples. Research other shuffling.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
Algorithm Analysis 1.
CMPT 120 Topic: Searching – Part 2
Algorithmic complexity: Speed of algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Sorting by Tammy Bailey
Teach A level Computing: Algorithms and Data Structures
Linear and Binary Search
Adapted from Pearson Education, Inc.
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Topic 1: Problem Solving
Algorithmic complexity: Speed of algorithms
Linear Search Binary Search Tree
And now for something completely different . . .
Algorithmic complexity: Speed of algorithms
Searching.
Presentation transcript:

 1 Searching

Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the efficiency between searching algorithms. 2

Back to counting Counting 2’s in a given list. counter = 0 for e in L: if e == counter: counter += 1 Counting is a repeated searching. Search 2; Remove the 2, Search another 2, Remove the 2, …; The repetition is the number of 2s. 3

Searching Searching is an operation for a given data, basically a list. You find within a file. A file is equivalent to a big string. A string is kind of a tuple with characters. A tuple is essentially a non-modifiable list! =) A search operation find a set of elements satisfies a given condition; cond() function. cond( )  True 4

Linear Search A basic search operation is looking for each element in an order. Advantages: Simple No need of extra memory to remember previously searched elements. Essentially no difference with random search (we will prove!) 5

Linear Search def linear_search(L, cond): for (j,e) in enumerate(L): if cond(e): # cond(e) == True return (j, e) return (-1, None) A search op. should give the element’s position as well as its value because we have the value already! 6

Linear Search Disadvantage: Better methods are possible with a few assumptions. The number of comparisons (the number of cond() calls) is proportional to the number of items. Let’s do some mathematics. 7

Linear Search Given a list of distinct numbers, let’s find from the first element. linear_search(L, L[0]) #  1 cond() call linear_search(L, L[1]) #  2 cond() calls linear_search(L, L[2]) #  3 cond() calls … linear_search(L, L[n]) #  n+1 cond() calls 8

Linear Search The expected number of comparisons is 9

Better way? Let’s play a number game. A has a number between 1 and 99 and B gives his/her number. If A’s number is smaller than B’s, say ‘down’; Otherwise, say ‘up’. Repeat this until B finds A’s number. 10

 11 Do you need 45 questions in average? You can ask 2’s multiples or 3’s multiples to skip a few numbers

Skip Search Let’s say finding an element with m’s multiples as skip search! However, we need comp() function which tells ‘up’, ‘down’, or ‘bingo!’ instead of cond() function. def comp(n, c): return “bingo!” if n==c else ( “up” if n>c else “down”) 12

Skip Search The structure of skip_search until comp() function says “down” we ask m’s multiples from 1. While “up”, skip m-1 numbers each question. If comp() says bingo!, we saved questions. If comp() says “down”, we try linear_search starting from the previous number asked. 13

Skip Search 14 Target number: 23 Asking numbers: Compared to linear search (23 questions), only 8 questions were required

Skip Search 15 What is the expected number of questions? To find 1  1 question To find 2  2 questions (skip) + 1 question (linear) To find 3  2 questions (skip) + 2 question (linear) To find 4  2 questions (skip) + 3 question (linear) To find 5  2 questions (skip) To find 6  2 questions (skip) + 3 question (linear) …

Skip Search 16 What is the expected number of questions? 1: 1, 2: 2+1, 3: 2+2, 4: 2+3, 5: 2+4, 6: 2, 7: 3+1, 8: 3+2, 9: 3+3, 10: 3+4, 3 … 1 + 2*5+( )+3*5+( )+…+ 19*5+( ) = 1+(2+3+4+…+19)*5+( )*18 = 1+sum(range(2,20))*5+10*18 = 1126

Skip Search Compared to 45 questions in linear search, In general, Asymptotically, it didn’t improve …. 17

The matter of efficiency 18 In general, it doesn’t matter and its linear performance is acceptable. However, if the number of items are more than millions, billions, or trillions, … More worse, if a nested searching is required? for l1 in L1: L2 = search(X, l1) for l2 in L2: x = search(X, l2)

Adaptive Search The cause of inefficiency is the fixed number of skips for each “up” answer. In the equation, m << n so that it is nothing but 1 We could try to skip questions in an adaptive or incremental way skip 1, 2, 4, 8, 16 numbers for each “up” answer 19

Adaptive Search If the answer is bingo!, then okay If the answer is down, then repeat this adaptive search! Let’s see how it works 20

Adaptive Search 21 Target number: 88 Asking numbers: Even for relatively higher number 88, it needs only 14 questions (19 for the skip, 88 for the linear)

Adaptive Search Analysis to find 1  1 question to find 2  2 question to find 3  question … to find 2^n  n question to find 2^n + 1  n+1 question The analysis is difficult due to its recursive structure. 22

Adaptive Search Approximate Analysis Suppose that we needed n questions until we get the first “down”. We skipped … + 2^(n-1) numbers and learned that the answer is less than 2^(n). For the second question, we have the search range to 2^(n-1) – 1, which is reduced, because 2^(n-1) + 2^(n- 1) = 2^n 23

Adaptive Search Approximate Analysis To find from 2^(n-1) – 1 numbers, the maximal number of questions will be n-1 question. If we repeat, we need at most n-2 questions to find the number. We need 1, 2, …, n questions to find a number within a range from 1 to 2^n

Adaptive Search Approximate Analysis We need only 1+2+…+n questions to find a number between 1 and 2^n – 1. If we are looking within a range between 1 and n, we can take a log() function since log (2^n) = n The approximated required number of questions are 25

Adaptive Search Our adaptive (or incremental) search is efficient than the linear search of the skip search! Making 3^n or 5^n will not change its efficiency asymptotically. In fact, this is the upper bound for search in an ordered sequence. There is a formalized way to do this called the binary search. 26

 27 Binary Search

Adaptive Search vs. Binary Search The efficiency is essentially same. The assumption is that the sequence must be ordered before any search operation. There must be the comp() function which will answer your question with either of “bingo!”, “up”, or “down”. Fortunately, we have for numbers and strings. The ‘<‘ and ‘==‘ operator will answer your question. 28

Binary Search We know the number of candidate items to search. The idea is to begin with the number of candidates and decrease by the factor of 2. In other words, search either of the lower halves or the upper halves for each question. The search range reduces by half. 100, 50, 25, 12, 6, 3, 1 29

Binary Search Let’s look with visualization. Find 24 from 10 numbers

Binary Search How many variables we need? The index for the lower and upper bounds: begin, limit What is the initial values for begin and limit, respectively? begin = 0 limit = len(L) – 1 What is the middle index, mid? mid = (begin+limit) / 2 31

Binary Search (begin, limit) = [0, 9] mid = (0+9)/2 = 4 (integer way!) Call comp() function: comp(L[mid], 24)  comp(L[4], 24)  comp(22, 24)  it says “up” The next range will be (begin, limit) = [mid+1, 9] = [5, 9] because we need the upper halves. 32

Binary Search (begin, limit) = [5, 9] mid = (5+9)/2 = 7 (integer way!) Call comp() function: comp(L[mid], 24) = comp(L[7], 24) = comp(92, 24)  it says “down” The next range will be (begin, limit) = [5, mid+1] = [5, 6] because we need the upper halves. 33

Binary Search (begin, limit) = [5, 6] mid = (5+6)/2 = 5 (integer way!) Call comp() function: comp(L[mid], 24) = comp(L[5], 24) = comp(24, 24)  it says “bingo!” 34

Binary Search The binary search is recursive with modified ranges at each call. 35 def binary_search(L, e, begin=None, limit=None): begin = 0 if begin is None else begin limit = len(L) – 1 if limit is None else limit mid = (begin+limit)/2 if L[mid] == e: return (mid, e) elif begin >= limit: return (-1, e) # no item in L elif e < L[mid]: # down return binary_search(L, e, begin, mid+1) return binary_search(L, e, mid+1, limit) # up

 36 Root Finding Bisection