CMPT 120 Topic: Searching – Part 1

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CS 206 Introduction to Computer Science II 09 / 10 / 2008 Instructor: Michael Eckmann.
Complexity Analysis (Part I)
Cmpt-225 Algorithm Efficiency.
Cmpt-225 Simulation. Application: Simulation Simulation  A technique for modeling the behavior of both natural and human-made systems  Goal Generate.
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.
COMP s1 Computing 2 Complexity
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Running Time Big O Notation.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Analysis of Algorithms
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
© 2011 Pearson Addison-Wesley. All rights reserved 10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
CSC 211 Data Structures Lecture 13
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.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Section 1.7 Comparing Algorithms: Big-O Analysis.
LECTURE 9 CS203. Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search) and sorting (selection sort.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Topic: Binary Encoding – Part 2
Algorithm Analysis 1.
Topic: Python Lists – Part 1
Topic: Introduction to Computing Science and Programming + Algorithm
Topic: Recursion – Part 2
Outline lecture Revise arrays Entering into an array
Topic: Programming Languages and their Evolution + Intro to Scratch
Topic: Iterative Statements – Part 1 -> for loop
Analysis of Algorithms
CMPT 120 Topic: Searching – Part 2
Topic: Introduction to Computing Science and Programming + Algorithm
CMPT 120 Topic: Searching – Part 2
Big-O notation Linked lists
Analysis of Algorithms
COP 3503 FALL 2012 Shayan Javed Lecture 15
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Introduction to Algorithms
Big O notation Big O notation is used in Computer Science to describe the performance or complexity of an algorithm. Big O specifically describes the worst-case.
GC 211:Data Structures Algorithm Analysis Tools
Search Lesson Outline Searching Lesson Outline
Topic: Recursion – Part 1
CMPT 120 Topic: Python strings.
Introduction to Algorithms
Teach A level Computing: Algorithms and Data Structures
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
Efficiency (Chapter 2).
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
GC 211:Data Structures Algorithm Analysis Tools
Introduction to Data Structures
Winter 2018 CISC101 12/2/2018 CISC101 Reminders
Analysis of Algorithms
Big-O, Ω, ϴ Trevor Brown CSC263 Tutorial 1 Big-O, Ω, ϴ Trevor Brown
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Analysis of Algorithms
Introduction to Algorithm and its Complexity Lecture 1: 18 slides
Analysis of Algorithms
Algorithmic complexity
Analysis of Algorithms
CMPT 120 Lecture 29 – Unit 5 – Internet and Big Data
Analysis of Algorithms
CMPT 120 Lecture 32 – Unit 5 – Internet and Big Data
CMPT 120 Lecture 33 – 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 7 – Stack.
CMPT 225 Lecture 8 – Queue.
CMPT 225 Lecture 10 – Merge Sort.
CMPT 225 Lecture 16 – Heap Sort.
Presentation transcript:

CMPT 120 Topic: Searching – Part 1 and Intro to Time Efficiency (Algorithm Analysis)

Last Lecture Binary Encoding

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 O(n2) sorting algorithms Analyze the running time (time efficiency) of simple iterative algorithms Compare the running time (time efficiency) 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

Today’s Menu When we want to search for a particular element in our data, we can do so in a variety of ways -> We have a choice of searching algorithms So, we shall have a look at these searching algorithms But, which one should we choose? To help us answer this question, we shall analyze these algorithms, looking at their time efficiency and expressing it using the Big O notation

Searching A common problem: find an element in a sequence of element (e.g.: a string or a list)

Let’s try! Is in this sequence? 42 8 12 34 2 67 33 26 89 54

What did we do to find the target?

Linear search algorithm linearSearch(list, target) set result to value TARGET_NOT_FOUND set targetNotFound to value true if list not empty set currentElement to first element of list while targetNotFound AND have not looked at every element of list if currentElement == target set result to current element set targetNotFound to false otherwise set currentElement to next element of list return result

Observations Our linear search algorithm: finds the first occurrence of the target What if the target occurs many times in the list returns the target What else could it return?

Let’s try this linear search! ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 2

What other test cases can we use?

Test cases to test our linear search algorithm ourList = [ ] target: 7 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 4 ] target: 0 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 4 ] target: 4 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 4 ] target: 5 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 4 ] target: 8 ourList = [ 0, 1, 2, 3, 4, 5, 6, 7, 9 ] target: 4 ourList = [ 6, 6, 6, 6, 6, 6, 6, 6, 6 ] target: 0 ourList = [ 6, 6, 6, 6, 6, 6, 6, 6, 6 ] target: 6

Reminder: Generalisation guideline and testing When we design algorithms/programs/functions, we want to make sure they solve as many similar problems as possible, i.e., they work with as many different data configurations as possible -> generalisation guideline Therefore, we shall test our algorithms/programs/functions accordingly: For example: Does our search algorithm work successfully with … An empty list? A sorted list? An list containing the same element? A list containing the target once? A list containing the target several times, etc…

Linear search algorithm Advantages Simple to understand, implement and test Disadvantages Slow (time inefficient) because it looks at every element Wait a minute! Not always! Linear search “processed” some of our test cases faster than others

Linear search “processes” some of our test cases faster than others? How can we figure out whether linear search does indeed “process” some of our test cases faster than others? How can we observe and express this fact? By analyzing it! -> Algorithm Analysis By looking at the behavior of an algorithm, i.e., looking at the time an algorithm takes to execute We do this analysis by counting how many times linear search executes its critical operations when it “processes” data (i.e., a test case) Critical operations? loop iteration -> visiting each element element == target?

Let’s count # of times we do the critical operations Test Case: iteration count: ourList = [ ] target: 7 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 0 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 4 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 5 ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 8 == count:

Various scenarios Based on our results, we can conclude that the way our data is organized influences the time efficiency of linear search algorithm There are 3 scenarios Best case scenario The data is organized such that the algorithm requires minimum amount of time to execute, producing minimum iteration and == counts Average case scenario The data is organized such that the algorithm requires average amount of time to execute Worst case scenario The data is organized such that the algorithm requires maximum amount of time to execute, producing maximum iteration and == counts

Time efficiency of an algorithm Question: How can we express the time efficiency of linear search algorithm? Do we use these counts? Answer: Not quite, but it is the first step! Procedure: We count number of times critical operation(s) of an algorithm is/are executed In general, computer scientists use the count produced for the worst case scenario Express this count as a function of the size of our data -> n (in a general fashion, i.e., for all our test cases) Then we match this count, now expressed as a function of n, to one of these possible Big O notations: O(1), O(log n), O(n), O(n2), … that is the closest to it:

Synonyms How an algorithm behaves How much time an algorithm takes (requires) to execute Number of times critical operations of an algorithm are executed (counts)

Big O notation We express the time efficiency of an algorithm using this Big O notation The reason why this Big O notation uses n is because computing scientists are interested in knowing how an algorithm behaves as the size of its data, i.e., n goes to infinity So, this Big O notation actually indicates an upper bound on counts as this n goes to infinity In other words, the Big O notation describes how the algorithm behaves as the size of its data increases

Time efficiency of linear search algorithm Worst case scenario of linear search algorithm has a time efficiency of since the time required by the linear search algorithm (under the worst case scenario) to find “target” in a list of length n is directly proportional to the number of elements in the list, i.e., n

Explanation and examples of Big O notation O(log2 n) O(n) O(n2)

Big O notation! Big deal! Once we have found the Big O notation of an algorithm, what do we do with it? In other words: What’s the purpose of an algorithm’s time efficiency? Imagine we have 4 algorithms that have the same purpose Algorithm 1 executes in O(1) Algorithm 2 executes in O(log2 n) Algorithm 3 executes in O(n) Algorithm 4 executes in O(n2)

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

So? Remember this slide from Lecture 3?

Re-introducing Step 3 in Software development process If we want to solve a particular problem and we have a choice of algorithms that will solve the problem, then we can select the most time efficient algorithm by considering their time efficiency i.e., their Big O notation And this is why computing scientists analyse the behaviour of algorithms as the size of their data, i.e., n goes to infinity

Summary Looked at the first way of searching Linear search algorithm Introduced time efficiency analysis of algorithms using the Big O notation

Next Lecture Another way of searching Compare and contrast these 2 searching algorithms