CMPT 120 Topic: Searching – Part 2

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Data Structures Review Session 1
Algorithm Efficiency and Sorting
Analysis of Algorithm.
CS107 Introduction to Computer Science Lecture 7, 8 An Introduction to Algorithms: Efficiency of algorithms.
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.
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.
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
Reynolds 2006 Complexity1 Complexity Analysis Algorithm: –A sequence of computations that operates on some set of inputs and produces a result in a finite.
Analysis of Algorithms
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
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.
Lecture 4 on Data Structure Array. Prepared by, Jesmin Akhter, Lecturer, IIT, JU Searching : Linear search Searching refers to the operation of finding.
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.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
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.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 8A Binary Search (Concepts)
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.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Recursion Riley Chapter 14 Sections 14.1 – 14.5 (14.6 optional)
CS1010 Programming Methodology
Topic: Python Lists – Part 1
Topic: Programming Languages and their Evolution + Intro to Scratch
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
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
CMPT 120 Topic: Searching – Part 1
Search Lesson Outline Searching Lesson Outline
CS1010 Programming Methodology
Big O: Make it Simple Determine how complex the algorithm is, in relative to the size of the problem (e.g: List to be sorted) 'O' Stands for 'Order' -
Teach A level Computing: Algorithms and Data Structures
Algorithms Chapter 3 With Question/Answer Animations
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Data Structures Review Session
Searching and Sorting 1-D Arrays
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Binary Search A binary search algorithm finds the position of a specified value within a sorted array. Binary search is a technique for searching an ordered.
Principles of Computing – UFCFA3-30-1
Data Structures: Searching
Data Structures Introduction
Searching and Sorting Hint at Asymptotic Complexity
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
Searching and Sorting Hint at Asymptotic Complexity
Principles of Computing – UFCFA3-30-1
CSC 143 Java Searching.
Topic: Iterative Statements – Part 2 -> for loop
Searching and Sorting Hint at Asymptotic Complexity
Searching and Sorting Hint at Asymptotic Complexity
CMPT 120 Lecture 30 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 32 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 33 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 26 – Unit 5 – Internet and Big Data
CMPT 225 Lecture 6 – Review of Complexity Analysis using the Big O notation + Comparing List ADT class implementations.
CMPT 225 Lecture 10 – Merge Sort.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

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

Learning Outcome At the end of this course, a student is expected to: Create (design), analyze, and explain the behaviour of simple algorithms: … Describe and illustrate the operation of linear search, binary search, and an O(n2) sorting algorithms Analyze the running time of simple iterative algorithms Compare the running time of algorithms; determine if one algorithm is more time efficient than another Create (design) small to medium size programs using Python: Create programs that search lists and strings

Last Lecture Linear Search Time complexity and Big O notation

Today’s Menu We shall look at another searching algorithm

Book: Le Scaphandre et le Papillon

Locked In Syndrome Locked-in syndrome (LIS) is a condition in which a patient is aware but cannot move or communicate verbally due to complete paralysis of nearly all voluntary muscles in the body except for the eyes. Source: https://en.wikipedia.org/wiki/Locked-in_syndrome Jean-Dominique Bauby could only blink one eye

How did he write the book?

Activity A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Instructions: Pair up Think of a solution: What questions would JD Bauby’ s personal assistant ask JD in order to construct the words of his book? Remember: Jean-Dominique could only blink one eye Test it One of you is JD Bauby - think of a word The other is the personal assistant - try to guess the word by asking Q’s Switch!

One possible algorithm -> binary search algorithm Question 1: does your word start with a letter <= M? Possible answer: yes, so we can ignore ½ of the alphabet A B C D E F G H I J K L M N O P Q R S T U V W X Y Z no, so we can ignore the other ½

One possible algorithm -> binary search algorithm Next question: Question 2: does your word start with a letter <= G? Possible answer: yes, so we can ignore ½ of the alphabet A B C D E F G H I J K L M no, so we can ignore the other ½ OR Question 2: does your word start with a letter <= T? N O P Q R S T U V W X Y Z

One possible algorithm -> binary search algorithm Next question: Question 3: does your word start with a letter <= D? Possible answer: yes, so we can ignore ½ of the alphabet A B C D E F G no, so we can ignore ½ of the alphabet OR Question 3: does your word start with a letter <= J? H I J K L M Question 3: does your word start with a letter <= Q? N O P Q R S T Question 3: does your word start with a letter <= W? U V W X Y Z etc...

Another example Suppose we have a sorted list 1 3 4 7 9 11 12 14 21 Using Binary Search algorithm, we can search for target = 7 without having to look at every element 1 3 4 7 9 11 12 14 21

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

Animation: How it works – 1 We start with a list and a target = 7 1 3 4 7 9 11 12 14 21 We find the middle element 1 3 4 7 9 11 12 14 21 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) 1 3 4 7 9 11 12 14 21 and we consider only the part in which target could be located We repeat steps 2 and 3 until we found target or run out of list.

Animation: How it works – 2 We find the middle element 1 3 4 7 9 11 12 14 21 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) 1 3 4 7 9 11 12 14 21 and we consider only the part in which target could be located We repeat steps 2 and 3 until we found target or run out of list.

Animation: How it works - 3 We find the middle element 1 3 4 7 9 11 12 14 21 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) 1 3 4 7 9 11 12 14 21 and we consider only the part in which target could be located We repeat steps 2 and 3 until we found target or run out of list.

Animation: How it works - 4 We find the middle element 1 3 4 7 9 11 12 14 21 Is this element == target? Yes, then we are done! 

Binary Search algorithm - iterative PreCondition: data must be sorted binarySearch(list, target) set position to TARGET_NOT_FOUND set target to not found if list not empty while target not found AND have not looked at or discarded every element of list find middle element of list if middle element == target set position to position of target in original list set target to found else if target < middle element list = first half of list list = last half of list return position We ignore 2nd half of the list and middle element We ignore 1st half of the list and middle element

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

Question: BTW, can we quickly tell if target is not in a list?

How long did it take Mr. Bauby to write his book? How long does it take to guess the first letter of a word X average length of a word in English (or French) X number of words in JD Bauby’s book? Let’s just focus on how long does it take to find the target in data using the binary search algorithm? 

Time efficiency of binary search algorithm Question: What is the time complexity (efficiency) of the binary search algorithm? Answer: For binary search algorithm What are the worst case scenarios? What is its the “critical operation”? Let’s follow the procedure from our last lecture 1. Count -> 2. Count as a f(n) -> 3. Matching Big O notation ->

Binary search algorithm -> 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 At each iteration: We partition the list in half, ignoring one half and searching the other Size of data in 1st iteration: n Size of data in 2nd iteration : n/2 Size of data in 3rd iteration : n/4 Size of data in 4th iteration : n/8 …. Size of data in T-1th iteration : 2 Size of data in Tth iteration : 1

Binary search algorithm -> complexity analysis

Binary search algorithm -> complexity analysis

Time complexity of binary search algorithm Result of binary search algorithm complexity analysis: The worst case scenario of the binary search algorithm is of order log2 n i.e., has a time complexity (efficiency) of O(log2 n) since the time required (i.e., number of times the critical operation is executed) by the binary search algorithm (under the worst case scenario) to find “target” in a list of length n is proportional to the log of the number of elements in the list, i.e., n

Comparing binary search to linear search 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

Big O notation O(n2) O(n) O(log n) O(1) # of times algorithm executes a “critical operation” O(log n) O(1) 1 Length/size of data As n goes to infinity

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?

Summary Looked at the second way of searching Binary search algorithm Compare and contrast linear and binary searching algorithms

Next Lecture Sorting algorithm and its time efficiency