Lecture 16: Big-Oh Notation

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Advance Data Structure and Algorithm COSC600 Dr. Yanggon Kim Chapter 2 Algorithm Analysis.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY Lecture 9 CS2110 – Spring 2015 We may not cover all this material.
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
HST 952 Computing for Biomedical Scientists Lecture 10.
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
Big-O and Friends. Formal definition of Big-O A function f(n) is O(g(n)) if there exist positive numbers c and N such that: f(n) = N Example: Let f(n)
Introduction to Analysis of Algorithms
Analysis of Algorithms. Time and space To analyze an algorithm means: –developing a formula for predicting how fast an algorithm is, based on the size.
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Lecture 4 Feb 5 completion of recursion (inserting into a linked list as last item) analysis of algorithms – Chapter 2.
Lecture 4 Sept 4 Goals: chapter 1 (completion) 1-d array examples Selection sorting Insertion sorting Max subsequence sum Algorithm analysis (Chapter 2)
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Lecture 3 Aug 31, 2011 Goals: Chapter 2 (algorithm analysis) Examples: Selection sorting rules for algorithm analysis discussion of lab – permutation generation.
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
CS2420: Lecture 4 Vladimir Kulyukin Computer Science Department Utah State University.
Data Structure Algorithm Analysis TA: Abbas Sarraf
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Midterm Test Overview CS221 – 3/23/09. Test Curve Current median is a 71 or C- Median will be adjusted to an 81 or B- All test scores will be +10 on.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis. Algorithm Def An algorithm is a step-by-step procedure.
Chapter 1 Algorithm Analysis
Algorithm Analysis & Complexity We saw that a linear search used n comparisons in the worst case (for an array of size n) and binary search had logn comparisons.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
Introduction to complexity. 2 Analysis of Algorithms Why do we need to analyze algorithms? –To measure the performance –Comparison of different algorithms.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Data Structures and Algorithms Lecture 5 and 6 Instructor: Quratulain Date: 15 th and 18 th September, 2009 Faculty of Computer Science, IBA.
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
CSC 201 Analysis and Design of Algorithms Lecture 04: CSC 201 Analysis and Design of Algorithms Lecture 04: Time complexity analysis in form of Big-Oh.
Iterative Algorithm Analysis & Asymptotic Notations
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
Analysis of Algorithms
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data Depends.
Object-Oriented Design CSC 212. Announcements Ask more questions!  Your fellow students have the same questions (remember, I grade the daily quizzes)
1 COMP3040 Tutorial 1 Analysis of algorithms. 2 Outline Motivation Analysis of algorithms Examples Practice questions.
Big Oh Algorithms are compared to each other by expressing their efficiency in big-oh notation Big O notation is used in Computer Science to describe the.
CSC Programming for Science Lecture 16: Debugging.
Problem of the Day  I am thinking of a question and propose 3 possible answers. Exactly one of the following is the solution. Which is it? A. Answer 1.
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.
Complexity Analysis. 2 Complexity The complexity of an algorithm quantifies the resources needed as a function of the amount of input data size. The resource.
Binary search and complexity reading:
CSC 213 – Large Scale Programming. Today’s Goal  Understand why testing code is important  Result of poor or no testing & embarrassment caused  Learn.
Question of the Day  Move one matchstick to produce a square.
LECTURE 23: LOVE THE BIG-OH CSC 212 – Data Structures.
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.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Problem of the Day  On the next slide I wrote today’s problem of the day. It has 3 possible answers. Can you guess which 1 of the following is the solution?
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Introduction to Analysis of Algorithms
Introduction to complexity
Java 4/4/2017 Recursion.
Building Java Programs
Lecture 14: binary search and complexity reading:
Lecture 15: binary search reading:
Lecture 5: complexity reading:
CS 201 Fundamental Structures of Computer Science
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
Main() { int fact; fact = Factorial(4); } main fact.
Searching and Sorting Hint at Asymptotic Complexity
Presentation transcript:

Lecture 16: Big-Oh Notation CSC 212 – Data Structures Lecture 16: Big-Oh Notation

Question of the Day What is smallest number of matchsticks moved to make this equation valid (using roman numerals) + =

Algorithm Analysis Approximate time to run a program with n inputs on 1GHz machine: n = 10 n = 50 n = 100 n = 1000 n = 106 O(n log n) 35 ns 200 ns 700 ns 10000 ns 20 ms O(n2) 100 ns 2500 ns 1 ms 17 min O(n5) 0.1 ms 0.3 s 10.8 s 11.6 days 3x1013 years O(2n) 1000 ns 13 days 4 x 1014 years Too long! O(n!) 4 ms

Big-Oh Notation Only consider very, very large data sets Nobody cares when entire program takes 2 minutes Leading multipliers are ignored O(5n) = O(2n) = O(n) Often an implementation issue, anyway Only keep the largest terms O(n5 + n2) = O(n5) What is extra 17 min. after 3x1013 years?

Big-Oh Notation Number of primitive operations executed Assignments, method calls, arithmetic operations, comparisons, indexing into arrays, following a reference, … Good, rough approximation of time Useful to compare algorithms Really good at eliminating algorithms

Big-Oh Rules of Thumb Primitive statements take O(1) time Loop from 1 - n Runs O(n) times, when adding constant amount Runs O(log n) times when multiplying by constant Multiply complexities of nested loops Add complexities of consecutive loops

Big-Oh Measures Worst-Case int power(int a, int b > 0) if a == 0 && b == 0 return -1; exp = 1; loop from 1 to b exp *= a; return exp; power takes O(n) in most cases Is O(1) when a & b are both 0, however Would still call this algorithm O(n)

Not All Nesting Is The Same public static int sum(int[][] a) { int total = 0; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[i].length; j++) { total += a[i][j]; } } return total; } Despite nested loops, this runs in O(n) time n here would be entries in entire array

Handling Method Calls Method call is O(1) operation, … … but method may take more time Must consider the complexity of the entire process Including complexity of all the methods called

Methods Calling Methods public static int sumOdds(int m) { int total = 0; for (int i = 1; i <= m; i+=2) { total += i; } return total; } public static void oddSeries(int n) { for (int i = 1; i < n; i++) { System.out.println(i + “ ” + sumOdds(n)); } oddSeries calls sumOdds n times Each call does O(n) work, takes O(n2) total time!

Justifying an Answer Important to explain your answer Saying method is O(n) does not make it O(n) Especially true for tricky & recursive methods Process can derive difficult answer May simplify the big-Oh computation Discover that big-Oh is less than thought Does not need to be formal proof But must explain your answer

Big-Oh Notation Ignoring recursive call, runs in O(1) time public static int factorial(int n) { if (n <= 2) { return 1; } else { return n * factorial(n - 1); } } Ignoring recursive call, runs in O(1) time There can at most n - 1 calls to factorial Therefore total time is n - 1 * O(1) = O(n)

Big-Oh Notation This function takes O(2n) time int fibonacci(int k) if k < 1 return k else return fibonacci(k-1) + fibonacci(k - 2) This function takes O(2n) time k is 2, makes 2 calls k is 3, makes 4 calls - 3: fibonacci(2) & 1: fibonacci(1) k is 4, makes 8 calls - 5: fibonacci(3) & 3: fibonacci(2) k is n, makes 2n-1 = 2-1 * 2n = O(2n) calls

Your Turn Get back into groups and do activity

Midterm Overview Midterm results were very mixed Max score = 99 Mean score = 79 Standard deviation = 14.03 May apply curve at end of term Will not curve any single item Some of you may need to work harder, come and ask me for help as we go on Everyone still has passing grade, however

Before Next Lecture… Finish week #6 assignment Finish lab #5 Programming assignment #2 next week