CS 280 Data Structures Professor John Peterson. Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm.

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

12-Apr-15 Analysis of Algorithms. 2 Time and space To analyze an algorithm means: developing a formula for predicting how fast an algorithm is, based.
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Algorithm Analysis.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
the fourth iteration of this loop is shown here
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Complexity Analysis (Part I)
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.
Complexity Analysis (Part I)
CS 280 Data Structures Professor John Peterson. Homework #1 Remember: this is due by class Wednesday! Ask questions now or by /wiki
CS 280 Data Structures Professor John Peterson. Goals Understand “Programming in the small” Java programming Know what’s under the hood in complex libraries.
CS 280 Data Structures Professor John Peterson. Example: log(N) This is where things get hairy! How would you compute Log 10 (N) in a very approximate.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
CS 280 Data Structures Professor John Peterson. Log Complexity int j = n; while (j > 0) { System.out.println(j); j = j / 2; /* Integer division! */ }
Professor John Peterson
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Analysis of Algorithm.
Algorithm/Running Time Analysis. Running Time Why do we need to analyze the running time of a program? Option 1: Run the program and time it –Why is this.
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.
Chapter 2.6 Comparison of Algorithms modified from Clifford A. Shaffer and George Bebis.
Week 2 CS 361: Advanced Data Structures and Algorithms
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Recursion, Complexity, and Sorting By Andrew Zeng.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
Grading Exams 60 % Lab 20 % Participation 5% Quizes 15%
Chapter 10 Algorithm Analysis.  Introduction  Generalizing Running Time  Doing a Timing Analysis  Big-Oh Notation  Analyzing Some Simple Programs.
CS101 Computer Programming I Chapter 4 Extra Examples.
Chapter 5 Algorithms (2) Introduction to CS 1 st Semester, 2015 Sanghyun Park.
Chapter 2 Computational Complexity. Computational Complexity Compares growth of two functions Independent of constant multipliers and lower-order effects.
Time Complexity. Solving a computational program Describing the general steps of the solution –Algorithm’s course Use abstract data types and pseudo code.
CompSci Analyzing Algorithms  Consider three solutions to SortByFreqs, also code used in Anagram assignment  Sort, then scan looking for changes.
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.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
CPS 100e 5.1 Inheritance and Interfaces l Inheritance models an "is-a" relationship  A dog is a mammal, an ArrayList is a List, a square is a shape, …
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithms April-May 2013 Dr. Youn-Hee Han The Project for the Establishing the Korea ㅡ Vietnam College of Technology in Bac Giang.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Analysis of Algorithms
Analysis: Algorithms and Data Structures
COMP108 Algorithmic Foundations Algorithm efficiency
Thought for the Day “Years wrinkle the skin, but to give up enthusiasm wrinkles the soul.” – Douglas MacArthur.
Introduction to complexity
Assignment 2 Tze calculations.
Running Time Performance analysis
Algorithm Efficiency Algorithm efficiency
Complexity Analysis.
CS 213: Data Structures and Algorithms
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Introduction to Data Structures
CS 201 Fundamental Structures of Computer Science
Analysis of Algorithms
Analyzing an Algorithm Computing the Order of Magnitude Big O Notation
Searching, Sorting, and Asymptotic Complexity
Analysis of Algorithms
CSE 373 Optional Section Led by Yuanwei, Luyi Apr
8. Comparison of Algorithms
Analysis of Algorithms
Algorithm/Running Time Analysis
Complexity Analysis (Part I)
Analysis of Algorithms
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

CS 280 Data Structures Professor John Peterson

Big O Notation We use a mathematical notation called “Big O” to talk about the performance of an algorithm. O(n 2 ) means that an algorithm runs in time proportional to n 2 (the “size” of the input squared). Note that there is no constant – it’s not 23n 2 or something like that – this compares general algorithms to each other. Constant factors are hard to measure and understand!

Program Example Consider the following program: for (int i = 0; i < n; i++) { System.out.print(i);} This program takes O(n) time to execute. This assumes that all of the statements in the program execute in constant time. (Do they?)

Iteration This is a special case of a general idea: the complexity of code within a loop is calculated by multiplying the number of times the loop is executed by the complexity of the loop body. What is the complexity of this: for (int i = 0; i < n*n; i++) System.out.print(i);

Nesting How about this? for (int i = 0; i < n; i++) for (int j = 0; j < n, j++) for (int k = 0; k < n; k++) System.out.println(i+j+k);

Conditionals The if statement is trickier – you need to choose the greater complexity of the alternatives. Sometimes you can use extra information to avoid always assuming the worst. While loops are related – you need to assume the worst in general.

If for (int i = 0; i < n; i++) { if (a[i] == 0) { for (int j = 0; j < n; j++) { System.out.print (i + “ “ + j); }}} What is the input to this one? What does the complexity really depend on?

Best, Worst, and Average When we don’t know what will happen with an “if”, we can make a number of assumptions: The worst possible outcome (highest complexity) The best possible outcome (lowest complexity) An average between the best and worst – this often hard to describe

A Simple Method boolean find(int [] a, int v) { for (int i = 0; i < a.length; i++) { if (a[i] == v) return true;} return false; } What is “n” in this case? What is the best case complexity? What is the worst case? Average?

Method Calls What do you do about a method call? You need to know how complex the method is GIVEN THE PARAMETER VALUES. This can be really tricky! The worst case is always an option if nothing is known about the parameters.

Example: log(N) This is where things get hairy! How would you compute Log 10 (N) in a very approximate manner? What does “Log(N)” mean mathematically? How might we get this in a real piece of code? Why don’t we really need to know the base of the logarithm?

Log Complexity int j = n; while (j > 0) { System.out.println(j); j = j / 2; /* Integer division! */ } What would this print for n = 10? 100?