Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
HST 952 Computing for Biomedical Scientists Lecture 10.
Chapter 9: Searching, Sorting, and Algorithm Analysis
CS0007: Introduction to Computer Programming Array Algorithms.
Chapter 19: Searching and Sorting Algorithms
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Chapter 5 Efficiency and Analysis. Algorithm selection Algorithms are ordered lists of steps for solving a problem Algorithms are also abstractions of.
Introduction to Analysis of Algorithms
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
The Efficiency of Algorithms
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Elementary Data Structures and Algorithms
Lecture 3 Feb 7, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis Image representation Image processing.
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.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
 2006 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
Big Oh Algorithm Analysis with
Week 2 CS 361: Advanced Data Structures and Algorithms
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Chapter 16: Searching, Sorting, and the vector Type.
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
Today  Table/List operations  Parallel Arrays  Efficiency and Big ‘O’  Searching.
Analysis of Algorithms
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 10 A Algorithm Efficiency. © 2004 Pearson Addison-Wesley. All rights reserved 10 A-2 Determining the Efficiency of Algorithms Analysis of algorithms.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
Ch 18 – Big-O Notation: Sorting & Searching Efficiencies Our interest in the efficiency of an algorithm is based on solving problems of large size. If.
Analysis of Algorithms These slides are a modified version of the slides used by Prof. Eltabakh in his offering of CS2223 in D term 2013.
Introduction to Analysis of Algorithms COMP171 Fall 2005.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
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.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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.
1 Asymptotic Notations Iterative Algorithms and their analysis Asymptotic Notations –Big O,  Notations Review of Discrete Math –Summations –Logarithms.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
1 Algorithms  Algorithms are simply a list of steps required to solve some particular problem  They are designed as abstractions of processes carried.
Algorithm Analysis (Big O)
Week 12 - Friday.  What did we talk about last time?  Finished hunters and prey  Class variables  Constants  Class constants  Started Big Oh notation.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
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.
Vishnu Kotrajaras, PhD.1 Data Structures
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
Algorithm Analysis (not included in any exams!)
Algorithm design and Analysis
Chapter 8 Search and Sort
CS 201 Fundamental Structures of Computer Science
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
At the end of this session, learner will be able to:
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
Presentation transcript:

Algorithm Analysis with Big Oh ©Rick Mercer

Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms  Linear Search and Binary Search  Know the differences between O(n) and O(log n)  Visualize runtime differences with experiments

Algorithms continued  Computer Scientists focus on problems such as  How fast do algorithms run  How much memory does the process require  Example Applications  Make the Internet run faster  Pink-Degemark's routing algorithms  Gene Meyers determined the sequences of the Human genome using his whole genome shotgun algorithm

Analysis of Algorithms  We have ways to compare algorithms  Generally, the larger the problem, the longer it takes the algorithm to complete  Sorting 100,000 elements can take much more time than sorting 1,000 elements  and more than 10 times longer  the variable n suggests the "number of things”

A Computational Model  To summarize algorithm runtimes, we can use a computer independent model  instructions are executed sequentially  count all assignments, comparisons, and increments there is infinite memory  every simple instruction takes one unit of time

Simple Instructions  Count the simple instructions  Setting a variable has cost of 1  Comparisons such as or = have a cost of 1  Loops have a cost of n for i = 1 to n  As does each statement in the repeated part of a loop

Examples Cost set sum = 0; -> 1 set index to 1 -> 1 Total Cost: 2 Cost for i = 1 to n -> n change sum by 1 -> n Total Cost: 2n Cost set sum = 0 -> 1 for i = 0 to n -> n for j = 1 to n 2 set sum to (i+j) -> n 2 Total Cost: 2n 2 + n + 1

Total Cost of Sequential Search Cost for index = 1 to n -> n if search = item index in list -> n report index -> 0 or 1 report -1 -> 0 or 1 Total cost = 2n+1

Different Cases  The total cost of sequential search is 2n + 1  But is it always exactly 2n + 1 instructions?  The last assignment does not always execute  But does one assignment really matter?  How many times will the loop actually execute?  that depends  If search is found at index 0: _____ iterations  best case  If search is found at index n-1:_____ iterations  worst case

Typical Case of sequential (linear)  The average describes the more typical case  First, let the the entire cost be simplified to n  Assume the target has a 50/50 chance of being in the array  n comparisons are made: worst-case occurs 1/2 the time  Assume if it's in a, it's as likely to be in one index as another Half the time it is n comparisons, the other half it is n/2 comparisons  So the typical case is 3/4 n comparisons + +

The Essence of Linear Search n Plot the function this is why sequential search is also called linear search. As n increases, runtime forms a line f(n) 45 60

Linear Search Continued  This equation is a polynomial: 2n + 1  The fastest growing term is the high-order term  The high order term (which could be n 2 or n 3 ), represents the most important part of the analysis function  We refer to the rate of growth as the order of magnitude, which measure the rate of growth

Rate of Growth  Imagine two functions: f(n) = 100n g(n) = n 2 + n  When n is small, which is the bigger function?  When n is big, which is the bigger function?  We can say: g(n) grows faster than f(n)

Rate of Growth, another view Function growth and weight of terms as a percentage of all terms as n increases for f(n) = n n Conclusion: consider highest order term with the coefficient dropped, also drop all lower order terms

Binary Search  We'll see that binary search can be a more efficient algorithm for searching If the element in the middle is the target report target was found and the search is done if the key is smaller search the array to the left Otherwise search the array to the right  This process repeats until the target is found or there is nothing left to search  Each comparison narrows search by half

Binary Search Harry Bob Carl Froggie Gene Harry Igor Debbie Evan a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] left mid right left mid right Jose Datareference loop 1 loop 2

How fast is Binary Search?  Best case: 1  Worst case: when target is not in the array  At each pass, the "live" portion of the array is narrowed to half the previous size.  The series proceeds like this:  n, n/2, n/4, n/8,...  Each term in the series represents one comparison How long does it take to get to 1?  This will be the number of comparisons

Binary Search (con.)  Could start at 1 and double until we get to n 1, 2, 4, 8, 16,..., k >= n or 2 0, 2 1, 2 2, 2 3, 2 4,..., 2 c >= n  The length of this series is c+1  The question is 2 to what power c is greater than or equal to n?  if n is 8, c is 3  if n is 1024, c is 10  if n is 16,777,216, c is 24  Binary search runs O(log n) logarithmic

Comparing O(n) to O(log n) Rates of growth and logarithmic functions Power of 2 n log 2 n , ,777,216 24

Graph Illustrating Relative Growth n and log n n f(n) n log n

Other logarithm examples  The guessing game:  Guess a number from 1 to 100  try the middle, you could be right  if it is too high –check near middle of  if it is too low –check near middle of  Should find the answer in a maximum of 7 tries  If , a maximum of 2 c >= 250, c == 8  If , a maximum of 2 c >= 500, c == 9  If , a maximum of 2 c >= 1000, c == 10

Logarithmic Explosion  Assuming an infinitely large piece of paper that can be cut in half, layered, and cut in half again as often as you wish.  How many times do you need to cut and layer until paper thickness reaches the moon?  Assumptions  paper is inches thick  distance to moon is 240,000 miles –240,000 * 5,280 feet per mile * 12 inches per foot = 152,060,000,000 inches to the moon

Examples of Logarithmic Explosion  The number of bits required to store a binary number is logarithmic add 1 bit to get much larger ints  8 bits stored 256 values log = 8  log 2,147,483,648 = 31  The inventor of chess asked the Emperor to be paid like this:  1 grain of rice on the first square, 2 on the next, double grains on each successive square 2 63