Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.

Slides:



Advertisements
Similar presentations
Introduction to Computer Science Theory
Advertisements

Algorithm Analysis.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
HST 952 Computing for Biomedical Scientists Lecture 10.
Sorted list matching & Experimental run-Time COP 3502.
Chapter 9: Searching, Sorting, and Algorithm Analysis
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,
Chapter 1 – Basic Concepts
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Cmpt-225 Algorithm Efficiency.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Searching Arrays Linear search Binary search small arrays
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithm.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis Aaron Bauer Winter 2014.
COMP s1 Computing 2 Complexity
Asymptotic Notations Iterative Algorithms and their analysis
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Week 2 CS 361: Advanced Data Structures and Algorithms
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Iterative Algorithm Analysis & Asymptotic Notations
Analysis of Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
1 2. Program Construction in Java. 2.8 Searching.
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.
CSC 211 Data Structures Lecture 13
Algorithm Analysis CS 400/600 – Data Structures. Algorithm Analysis2 Abstract Data Types Abstract Data Type (ADT): a definition for a data type solely.
Computer Science and Software Engineering University of Wisconsin - Platteville 8. Comparison of Algorithms Yan Shi CS/SE 2630 Lecture Notes Part of this.
Algorithms and data structures: basic definitions An algorithm is a precise set of instructions for solving a particular task. A data structure is any.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
3.3 Complexity of Algorithms
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Sorting.
Algorithm Analysis (Big O)
27-Jan-16 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Searching Topics Sequential Search Binary Search.
CISC220 Spring 2010 James Atlas Lecture 07: Big O Notation.
DS.A.1 Algorithm Analysis Chapter 2 Overview Definitions of Big-Oh and Other Notations Common Functions and Growth Rates Simple Model of Computation Worst.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Section 1.7 Comparing Algorithms: Big-O Analysis.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
19 Searching and Sorting.
Introduction to Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
Sorting by Tammy Bailey
David Kauchak CS52 – Spring 2015
Algorithm design and Analysis
MSIS 655 Advanced Business Applications Programming
Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases.
Presentation transcript:

Searching

RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number with only 10 attempts –The person must say ”higher” if the number is higher than your guess –The person must say ”lower” if the number is lower than your guess

RHS – SOC 3 Searching The magic trick obviously does not work, if the person does not ”direct” us How many guesses would we then typically need? If we increase the range to 1 up to 2000, how many more guesses will we need? –In the undirected case: 500 –In the directed case: 1

RHS – SOC 4 Searching Searching in sorted data is vastly easier than searching in unsorted data Consider an old-fashioned phone book –Given a name, find the number (easy) –Given a number, find the name (very hard) First search is Binary Search, second search is Linear Search

RHS – SOC 5 Searching Linear Search: –Given an unsorted array of data D containing n elements, check if the element e is in D –No shortcuts; got to check each element in D one by one –The time needed to do this is proportional to the size of D, i.e. proportional to n

RHS – SOC 6 Searching public boolean linearSearch(int e, int[] data, int n) { for (int i = 0; i < n; i++) { if (data[i] = e) return true; } return false; }

RHS – SOC 7 Searching Binary search: –Given a sorted array of data D containing n elements, check if the element e is in D –Let us be smarter now: Check the value in the middle of the array If the value is equal to e, we are done! If the value is larger than e, then start over with the first half of the array If the value is smaller than e, then start over with the second half of the array

RHS – SOC 8 Searching

RHS – SOC 9 Searching

RHS – SOC 10 Searching

RHS – SOC 11 Searching

RHS – SOC 12 Searching 3739 No match …

RHS – SOC 13 Searching Clearly, Binary Search is much faster than Linear Search… …but how fast is it? We can do an analysis of the so-called run- time complexity of the algorithm

RHS – SOC 14 Run-time complexity The run-time complexity gives us a measure of the expected running time of a specific algorithm Or rather, how the run- ning time is proportional to the ”size” of the input

RHS – SOC 15 Run-time complexity Example – Linear Search –Input is an array of length n –We will – on average – need to examine n/2 elements in the array –One examination of an element takes a constant amount of time –The complete running time is therefore proportional to n. –If n doubles, the running time doubles

RHS – SOC 16 Run-time complexity More formally, we could write the expected running time T(n) as T(n) = k 1 n + k 2 k 1 and k 2 are constants, which may be different for different hardware, program- ming language, and other factors We are usually only interested in the proportionality, not the exact running time

RHS – SOC 17 Run-time complexity For denoting the proportionality between the running time and the input size, we use the notation O(f(n)) meaning: the proportionality between the running time and the input size follows the function f(n)

RHS – SOC 18 Run-time complexity Examples: –O(log 2 (n))- very slow growth –O(n)- linear growth (Linear Search) –O(n 2 )- quadratic growth (fairly fast) –O(n 4 )- fast growth –O(2 n )- extremely fast growth

RHS – SOC 19 Run-time complexity n O(log 2 (n))12346 O(n) O(n 2 ) ,500 O(n 4 ) ,000160,0006 ×10 6 O(2 n )4321,0241 × ×10 15

RHS – SOC 20 Run-time complexity Given an algorithm, how do we then find the run-time complexity? We must examine the structure of loops in the algorithm, in particular nested loops We must examine the structure of method calls – very important in dealing with recursive algorithms Need to take library methods into account

RHS – SOC 21 Run-time complexity Example: Binary Search Input: Array of size n, element e to find Algorithm steps: –Until array has length 1, or e found Check value in middle of array If value is e; done – else run again with half of the array (which half depends on value)

RHS – SOC 22 Run-time complexity The time for doing Binary Search on n elements, is thus the sum of: –Doing a simple value comparison –Doing Binary Search on n/2 elements This can be written as: T(n) = 1 + T(n/2) This is a recursive function definition

RHS – SOC 23 Run-time complexity If T(n) = 1 + T(n/2) Then T(n) = 1 + (1 + T(n/4)) and generally T(n) = k + T(n/2 k ) Now write n = 2 k, and thus k = log 2 (n) Then T(n) = log 2 (n) + T(1) = O(log 2 (n))

RHS – SOC 24 Run-time complexity Since the run-time complexity of Binary Search is O(log 2 (n)), it is much faster than Linear Search If you need to search an array more than once, sort it first… …even though sorting does have a higher run-time complexity than Linear Search

RHS – SOC 25 Run-time complexity And that’s why the magic trick works: log 2 (1000) = 10

RHS – SOC 26 Exercises Review: R14.6 Programming: P14.8 Also try to program a recursive version of binary search