Computer Science 3 03A-Searching

Slides:



Advertisements
Similar presentations
Garfield AP Computer Science
Advertisements

CSE Lecture 3 – Algorithms I
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
CS4413 Divide-and-Conquer
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 104 Introduction to Computer Science and Graphics Problems Data Structure & Algorithms (3) Recurrence Relation 11/11 ~ 11/14/2008 Yang Song.
Efficiency of Algorithms February 11th. Efficiency of an algorithm worst case efficiency is the maximum number of steps that an algorithm can take for.
Analysis of Algorithm.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
Asymptotic Notations Iterative Algorithms and their analysis
Iterative Algorithm Analysis & Asymptotic Notations
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Chapter 11 Arrays Continued
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
1 Searching and Sorting Linear Search Binary Search.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
CSC 211 Data Structures Lecture 13
1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Amortized Analysis and Heaps Intro David Kauchak cs302 Spring 2013.
1 5. Abstract Data Structures & Algorithms 5.6 Algorithm Evaluation.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Sorts, CompareTo Method and Strings
16 Searching and Sorting.
Data Structures I (CPCS-204)
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Recitation 13 Searching and Sorting.
COMP 53 – Week Seven Big O Sorting.
Chapter 7 Single-Dimensional Arrays
Computer Science 101 A Survey of Computer Science
Introduction to Algorithms
Sorting by Tammy Bailey
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Teach A level Computing: Algorithms and Data Structures
Topics discussed in this section:
Searching CSCE 121 J. Michael Moore.
Building Java Programs
Algorithm design and Analysis
Searching.
Announcements P2 is due tomorrow Prelim on Monday
Chapter 8 Search and Sort
CISC181 Introduction to Computer Science Dr
MSIS 655 Advanced Business Applications Programming
COSC 320 Advanced Data Structures and Algorithm Analysis
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
25 Searching and Sorting Many slides modified by Prof. L. Lilien (even many without an explicit message indicating an update). Slides added or modified.
Search,Sort,Recursion.
Searching CLRS, Sections 9.1 – 9.3.
CS2011 Introduction to Programming I Arrays (I)
24 Searching and Sorting.
Topic 24 sorting and searching arrays
Arrays Week 2.
Linear Search (Area Code Example)
Recursion Chapter 11.
Chapter 23 Searching and Sorting
Amortized Analysis and Heaps Intro
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Linear and Binary Search
CSE 332: Parallel Algorithms
Discrete Mathematics CS 2610
Searching.
Presentation transcript:

Computer Science 3 03A-Searching Sean P. Strout (sps@cs.rit.edu) 5/6/2019 CS3 - 03A - Searching (v1.01)

A sequential search runs in linear time Computer Science 3 (4003-233) Sequential Search A sequential search runs in linear time Best Case: search for 10 O(1) Average Case: search for 2 O(6) = O(n/2) Worst Case: search for 11,99 O(12) = O(n) If collection size doubles, the search time doubles 1 2 3 4 5 6 7 8 9 10 11 10 7 9 4 12 2 5 8 1 6 3 11 5/6/2019 CS3 - 03A - Searching (v1.01)

What happens to the search times if the data is now ordered? Computer Science 3 (4003-233) Sequential Search What happens to the search times if the data is now ordered? For elements in the array, i.e. 59 For elements not in the array, i.e.: 0, 31, 99 1 2 3 4 5 6 7 8 9 10 11 5 10 12 15 22 26 32 36 40 44 59 64 For elements in the array, the search times are the same. For elements not in the array: element < n0: O(1) n0 < element < n11: O(n/2) element > n11: O(n) 5/6/2019 CS3 - 03A - Searching (v1.01)

Computer Science 3 (4003-233) Binary Search With a binary search, the size of the array is halved on each iteration: i.e. search for 22 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 #1: 22 < 32, go left half the distance #2: 22 > 12, go right half the distance #3: 22 = 22, element found 5/6/2019 CS3 - 03A - Searching (v1.01)

What happens when an element is not found? Computer Science 3 (4003-233) Binary Search What happens when an element is not found? i.e. search for 11 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 #1: 11 < 32, go left half the distance #2: 11 < 12, go left half the distance #3: 11 > 5, go right half the distance #4: 11 > 10 can’t go right half the distance, element not found 5/6/2019 CS3 - 03A - Searching (v1.01)

Computer Science 3 (4003-233) Binary Search The search algorithm uses three indexes to mark the start, middle and end positions i.e. search for 22 If the middle element is greater than the target, the end moves to middle - 1 and new middle = (start + end)/2 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 5/6/2019 CS3 - 03A - Searching (v1.01)

Computer Science 3 (4003-233) Binary Search If the middle element is greater than the value, the start moves to middle + 1 and new middle = (start + end)/2 If the middle element equals the value, the search stops and the element is found 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 5/6/2019 CS3 - 03A - Searching (v1.01)

Computer Science 3 (4003-233) Binary Search If the element is not in the collection, the start index will eventually exceed the end index i.e. search for 65 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 5/6/2019 CS3 - 03A - Searching (v1.01)

Computer Science 3 (4003-233) Binary Search 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start middle end 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 end start 5/6/2019 CS3 - 03A - Searching (v1.01)

Given the follow collection using a binary search: Computer Science 3 (4003-233) Binary Search Given the follow collection using a binary search: How many accesses will it take to locate element: 32 44 5 99 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 32: 1 44: 2 5: 3 99: 4 5/6/2019 CS3 - 03A - Searching (v1.01)

A binary search happens in logarithmic time Computer Science 3 (4003-233) Binary Search A binary search happens in logarithmic time Best Case: O(1) Average Case: O(log2n) Worst Case: O(log2n) x=log2n, where x is the power we raise 2 to, to get n log21 = 0 (20 = 1) log22 = 1 (21 = 2) log24 = 2 (22 = 4) log28 = 3 (23 = 8) O(log2n) grows slower than O(n) but the collection must be sorted first 5/6/2019 CS3 - 03A - Searching (v1.01)

Binary Search - Testing Your Understanding Computer Science 3 (4003-233) Binary Search - Testing Your Understanding Write a sample program, TestSearch.java, which demonstrates a binary search of your InstrumentedArray from lab2 The main method should: Read in a single argument which is the Integer to search for Create an InstrumentedArray with the following Integer values: 1-128 Call the search method: // Returns the position of the target in the // array, or -1 if is not present public static int binarySearch(InstrumentedArray array, int target); 5/6/2019 CS3 - 03A - Searching (v1.01)

Binary Search - Testing Your Understanding Computer Science 3 (4003-233) Binary Search - Testing Your Understanding main method continued: Print out the results of the search. Position = -1 if not found. Array accessed # times. Value i at position #. Verify the algorithm is O(log2128)= 7 Search for 0, 1, 63, 64, 65 and 128 Search for elements not in the collection What elements give an access time of 3? I have a perl script which will run all the tests and print results/statistics. Copy it to your local directory to run: cp /usr/local/pub/sps/courses/cs3/search/BinarySearch/run.pl . Access time of 3: 16, 48, 80, 112 5/6/2019 CS3 - 03A - Searching (v1.01)

Consider the binary search time when N=1000 Computer Science 3 (4003-233) Indexed Binary Search Consider the binary search time when N=1000 O(log2N) = O(log21000) = ~9.97 accesses The search time on a large data set can be improved by building an index table for looking up the subset range for the target value 1 2 ... 998 999 1 2 3 ... 999 1000 5/6/2019 CS3 - 03A - Searching (v1.01)

For example, if we are searching for the value 186 Computer Science 3 (4003-233) Indexed Binary Search 1 2 3 4 5 6 7 8 9 10 index: 99 199 299 399 499 599 699 799 899 999 99 199 999 1 ... 100 ... 200 ... 1000 For example, if we are searching for the value 186 Start range = index[186/100] = index[1] = 99 O(1) = 1 End range = index[186/100 + 1] = index[2] = 199 Binary search the range from 99-199 (N=100): O(log2N) = O(log2100) = ~6.64 Total search time = 1 + 1 + 6.64 = ~8.64 accesses 5/6/2019 CS3 - 03A - Searching (v1.01)

Given the following indexed collection using a binary search: Computer Science 3 (4003-233) Indexed Binary Search Given the following indexed collection using a binary search: How many accesses will it take to find the element: 250 275 262 256 251 1 2 3 4 5 6 7 8 9 10 index: 99 199 299 399 499 599 699 799 899 999 99 199 299 999 1 ... 100 ... 200 ... 300 ... 1000 250: 3 275: 4 262: 5 256: 6 251: 8 5/6/2019 CS3 - 03A - Searching (v1.01)

Computer Science 3 (4003-233) Ternary Search A ternary search builds on the idea of binary search by splitting the search space into thirds (vs. halves) For each iteration, compute third & probe and compare element at the probe: If target is less than, move end to probe - 1 If target is greater than, move start to probe + 1 and recompute probe as end - third. Compare the element at probe: If less than, end = probe - 1 If greater than, start = probe + 1 third = (end - start) / 3 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 start probe = start + third end 5/6/2019 CS3 - 03A - Searching (v1.01)

Trace through a search for 44 and 12 : Computer Science 3 (4003-233) Ternary Search Trace through a search for 44 and 12 : The ternary search time is O(log3N) 1 2 3 4 5 6 7 8 9 10 11 12 5 10 12 15 22 26 32 36 40 44 59 64 68 FIND 44: step 1: third = 4 start = 0, end = 12, probe = 4 (value is greater) start = 5, end = 12, probe = 8 (value is greater) start = 9, end = 12 (search in this range) step 2: third = 1 start = 9, end = 12, probe = 10 (value is less) start = 9, end = 9 step3: third = 0 start = 9, end = 9, probe = 9 (value is equal) FIND 12: start = 0, end = 12, probe = 4 (value is less) start = 0, end = 3 start = 0, end = 3, probe = 1 (value is greater) start = 2, end = 3, probe = 2 (value is equal) 5/6/2019 CS3 - 03A - Searching (v1.01)

To run you must specify an unused port: ACME Project For part 1, an echo server for simulating the bank is being provided for you: /usr/local/pub/sps/courses/cs3/projects/ACME/EchoBank.java To run you must specify an unused port: % java EchoBank 9125 EchoBank listening on port 9125 When a client successful connects to the echo server: Accepted socket[addr=/#.#.#.#,port=####,localport=9125] When a client closes the socket to the echo server: ATM has terminated the connection 5/6/2019 CS3 - 03A - Searching (v1.01)

Received message VALIDATE 12345 0 ACME Project The echo server will continuously read BankingMessage objects from the client: Received message VALIDATE 12345 0 The echo server will always send back a BankingMessage object to the client indicating the operation was successful: Sent message SUCCESS_RESPONSE 12345 100000 The echo server can only accept one client connection at a time, and must be terminated manually Eclipse: Running ATM and EchoServer and selecting their console output 5/6/2019 CS3 - 03A - Searching (v1.01)

Revision History Revision History v1.00, 3/14/05 12:38 PM, sps Computer Science 3 (4003-233) Revision History Revision History v1.00, 3/14/05 12:38 PM, sps Initial revision. 5/6/2019 CS3 - 03A - Searching (v1.01)