CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Chapter 9: Searching, Sorting, and Algorithm Analysis
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.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Recitation 9 Programming for Engineers in Python.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Complexity (Running Time)
Searching Arrays Linear search Binary search small arrays
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 Section 2.3 Complexity of Algorithms. 2 Computational Complexity Measure of algorithm efficiency in terms of: –Time: how long it takes computer to solve.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
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.
Algorithms. Problems, Algorithms, Programs Problem - a well defined task. –Sort a list of numbers. –Find a particular item in a list. –Find a winning.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
UNIT 18 Searching and Sorting.
Analysis of Algorithms
Chapter 19: Searching and Sorting 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)
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Computer Science Searching & Sorting.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
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.
CSC 211 Data Structures Lecture 13
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
Data Structure Introduction.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
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.
Intro To Algorithms Searching and Sorting. Searching A common task for a computer is to find a block of data A common task for a computer is to find a.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
CMPT 120 Topic: Sorting Algorithms – Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n -> O(n) i.e., has.
 1 Searching. Why searching? Searching is everywhere. Searching is an essential operation for a dictionary and other data structures. Understand the.
Searching CS 110: Data Structures and Algorithms First Semester,
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Topic: Python Lists – Part 1
CMPT 120 Topic: Searching – Part 2
CMPT 120 Topic: Searching – Part 2
Searching Given a collection and an element (key) to find… Output
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
CMPT 120 Topic: Searching – Part 1
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Winter 2018 CISC101 12/1/2018 CISC101 Reminders
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Standard Version of Starting Out with C++, 4th Edition
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Linear Search Binary Search Tree
Data Structures Sorted Arrays
And now for something completely different . . .
Principles of Computing – UFCFA3-30-1
CMPT 120 Lecture 32 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 33 – Unit 5 – Internet and Big Data
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)

In this lecture We shall look at another searching algorithm 2

Learning Outcome At the end of this course, a student is expected to: Create (design), analyze, and explain the behaviour of simple algorithms: … Solve problems by designing simple algorithms, e.g., basic calculations, searching in strings and lists, counting, calculating running sums and products … Describe and illustrate various ways of searching Create (design), analyze, and explain the behaviour of simple algorithms: … Analyze the running time of simple iterative algorithms Compare the running time of algorithms; determine if one algorithm is more efficient than another Create (design) small to medium size programs using Python: … Create programs that search lists and strings 3

From last lecture: Could we search more efficiently? Are there other ways of searching (other search algorithms) that would be more time efficient than O(n)? What if we sort our list first, could this help us search more efficiently? 4

Let’s see … Suppose we have a sorted list Using Binary Search, we can search for target = 7 without having to look at every element

How binary search works – In a nutshell Binary search uses the fact that the list is sorted ! Find element in the middle -> 9 Since we are looking for 7 and 7 < 9, then there is no need to search the second half of the list We can ignore half of the list right away! Then we repeat the above steps Bottom line: using binary search, we do not need to look at every element to search a list 6

Note regarding Slides 8 to 11 The following slides (Slides 8 to 17) are animated so they may not make much sense if they printed To get the most from these slides, one must run them as a “slide show” in PowerPoint (or, perhaps, using other similar applications) 7

How it works We start with a list and a target = We find the middle element 3.Is this element == target? Yes, then we are done! No, then we throw away half of the list in which we know target cannot be located (grey part) We repeat steps 2 and 3 until we found target or run out of list. and we consider only the part in which target could be located 8

How it works We find the middle element 3.Is this element == target? Yes, then we are done! No, then we throw away half of the list in which we know target cannot be located (grey part) We repeat steps 2 and 3 until we found target or run out of list. and we consider only the part in which target could be located 9

How it works We find the middle element 3.Is this element == target? Yes, then we are done! No, then we throw away half of the list in which we know target cannot be located (grey part) We repeat steps 2 and 3 until we found target or run out of list and we consider only the part in which target could be located 10

How it works We find the middle element 3.Is this element == target? Yes, then we are done!

Binary Search algorithm - iterative PreCondition: data must be sorted position = TARGET_NOT_FOUND targetFound = false if list not empty while list is not empty AND not targetFound find middle of list if middle element == target position = position of target in original list targetFound = true else if target < middle element list = first half of list else list = last half of list return position We ignore 2 nd half of the list and middle element We ignore 1 st half of the list and middle element 12

# Precondition: list must be sorted def BinarySearch (aList, target, low, high): result = -1 # returning the index of target or -1 (out of range value) first = 0 # index representing the start of list last = len(aList) - 1 # index representing the end of list # loop until all elements of list have either been discarded or compared while first <= last: mid = (first + last) // 2 # determine middle of list if aList[mid] == target : # target found … return mid # … returns index of list element containing target # otherwise, target must be in second half of list elif aList[mid] < target : first = mid + 1 # otherwise, target must be in first half of list elif aList[mid] > target : last = mid - 1 return result # return invalid index if target not in list Binary Search - Python iterative program 13

# Precondition: list must be sorted # TARGET_ELEMENT_NOT_FOUND = -1 def BinarySearch (aList, target, low, high): TARGET_ELEMENT_NOT_FOUND = -1 if high < low: result = TARGET_ELEMENT_NOT_FOUND else: mid = (low + high) // 2 if aList[mid] > target : result = BinarySearch (aList, target, low, mid-1) elif aList[mid] < target : result = BinarySearch (aList, target, mid+1, high) else: result = mid # target found return result Inspired from Binary Search - Python recursive program 14

Question: Can we quickly tell if target is not in a list? 15

Binary Search - Advantages and disadvantages Advantages 1.Faster because does not have to look at every element (at every iteration, ignores ½ of list) Disadvantages 1.List must be sorted 2.A bit more complicated to implement (iterative or recursive) and test 16

How much faster is Binary Search over Linear Search? What is the time complexity of binary search 17

So far… … we know that worst case scenario of linear search is of order n -> O(n) i.e., has a time complexity of O(n) Review: what does this mean? 18

Review: How we compute the time complexity of an algorithm From our lecture notes on linear search: 1.Count the number of times “critical operations” are executed For linear search, the “critical operations” were the statements in the loop of the linear search 2.Express this count as a function of n 19

Binary search complexity analysis For binary search What are the worst case scenarios? What are its the “critical operations”? 20

Binary search complexity analysis What is the time complexity of the worst case scenario (e.g., target not in list) of the binary search? We compare the middle element with target It is not found We then partition the list in half at each iteration, ignore one half and search the other Size of data in 1 st step: n Size of data in 2 nd step: n/2 Size of data in 3 rd step: n/4 Size of data in 4 th step: n/8 …. Size of data in T-1 th step: 2 Size of data in T th step: 1 21

Binary Search - Worst case scenario 22

Binary Search - Worst case scenario 23

Result of Binary search complexity analysis The worst case scenario of binary search is of order log 2 n i.e., has a time complexity of O(log 2 n) 24

How much faster is Binary Search over Linear Search? We can compare algorithms that perform the same task, like our searching algorithms, by looking at their time complexity We often do this comparison considering n as it goes to infinity For example: how much “faster” is binary search over linear search as n goes to infinity ? To answer this question, let’s have a look at the diagram on next slide 25

Comparing various order of n’s Number of “critical operations” the algorithm executes As n goes to infinity 26 Source:

Comparing binary search to linear search Binary search is much faster than linear search when searching large data But the data must first be sorted Great if data is already sorted, but if this is not the case … How much work does this sorting requires? 27