CSC 212 – Data Structures Lecture 15: Big-Oh Notation.

Slides:



Advertisements
Similar presentations
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 10.
ALG0183 Algorithms & Data Structures Lecture 3 Algorithm Analysis 8/25/20091 ALG0183 Algorithms & Data Structures by Dr Andy Brooks Weiss Chapter 5 Sahni.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
CSC401 – Analysis of Algorithms Lecture Notes 1 Introduction
© 2004 Goodrich, Tamassia 1 Lecture 01 Algorithm Analysis Topics Basic concepts Theoretical Analysis Concept of big-oh Choose lower order algorithms Relatives.
Introduction to Analysis of Algorithms
Analysis of Algorithms Algorithm Input Output. Analysis of Algorithms2 Outline and Reading Running time (§1.1) Pseudo-code (§1.1) Counting primitive operations.
Complexity Analysis (Part I)
Analysis of Algorithms (Chapter 4)
Complexity Analysis (Part I)
Analysis of Algorithms1 Estimate the running time Estimate the memory space required. Time and space depend on the input size.
Fall 2006CSC311: Data Structures1 Chapter 4 Analysis Tools Objectives –Experiment analysis of algorithms and limitations –Theoretical Analysis of algorithms.
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.
Introduction to Analysis of Algorithms Prof. Thomas Costello (reorganized by Prof. Karen Daniels)
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
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.
Lecture 16: Big-Oh Notation
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
SIGCSE Tradeoffs, intuition analysis, understanding big-Oh aka O-notation Owen Astrachan
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.
Week 2 CS 361: Advanced Data Structures and Algorithms
SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY Lecture 12 CS2110 – Fall 2009.
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.
{ CS203 Lecture 7 John Hurley Cal State LA. 2 Execution Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
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.
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)
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,
Complexity Analysis Chapter 1.
COMP 232 Intro Lecture. Introduction to Course Me – Dr. John Sigle Purpose/goals of the course Purpose/goals Prerequisites - COMP 132 (Java skill & Eclipse)
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 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
1 Analysis of Algorithms CS 105 Introduction to Data Structures and Algorithms.
Complexity of Algorithms
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
Algorithm Analysis Data Structures and Algorithms (60-254)
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.
M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 1.
Analysis of algorithms. What are we going to learn? Need to say that some algorithms are “better” than others Criteria for evaluation Structure of programs.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
COSC 1P03 Data Structures and Abstraction 2.1 Analysis of Algorithms Only Adam had no mother-in-law. That's how we know he lived in paradise.
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.
A Introduction to Computing II Lecture 5: Complexity of Algorithms Fall Session 2000.
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.
1 Algorithms Searching and Sorting Algorithm Efficiency.
1 COMP9024: Data Structures and Algorithms Week Two: Analysis of Algorithms Hui Wu Session 2, 2014
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Introduction to Analysis of Algorithms
Introduction to complexity
Analysis of Algorithms
Introduction to Algorithms
Revision of C++.
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

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

Question of the Day Use the numbers two, three, four, and five, one addition operator and one equality operator  Write mathematical equation (e.g., not in Java) so equality operator holds = 3 2

Analysis Techniques Running time is important, … … but cannot always compare  Lots of ways to solve a single problem  Solutions have lots of implementations

Pseudo-Code Only for human eyes  Ignore unimportant & implementation details  Instead use "pseudo-code" Pseudo-code isn't real  Used for outlining, designing, & analysis  No formal definition or “proper” writing  Use a language-like manner

Pseudo-Code Include important details  Loops, assignments, method calls, etc.  Helps better analyze algorithm Remember only to understand algorithm  Ignore punctuation and formalisms  Written to allow people to understand and analyze

Pseudo-code Example int factorial(n, n  Z + ) returnVariable  1 while (n > 0) returnVariable  returnVariable * n n  n – 1 return returnVariable

Big-Oh Notation Computes code complexity  Worst-case analysis of performance  Related to total execution time Used to compare approaches  Only requires algorithms  Not need to implement everything  Avoids unrelated details E.g., Compiler, CPU, users’ typing speed

Algorithmic Analysis

Algorithm Analysis Approximate time to run a program with n inputs on 1GHz machine: n = 10n = 50n = 100n = 1000n = 10 6 O(n log n)35 ns200 ns700 ns10000 ns20 ms O(n 2 )100 ns2500 ns10000 ns1 ms17 min O(n 5 )0.1 ms0.3 s10.8 s11.6 days3x10 13 years O(2 n )1000 ns13 days4 x years Too long! O(n!)4 ms Too long!

Big-Oh Notation Want results for large data sets  Worst case is 2 minutes -- nobody cares  Only consider major details Ignore multipliers  So, O(5n) = O(2n) = O(n)  Multipliers usually implementation-specific Ignore lesser terms  So, O(n 5 + n 2 ) = O(n 5 )  Does 17 minutes matter after 3x10 13 years?

What is n ? Analysis in respect to size of input  Question is what is size of input? Quick rules of thumb:  Analyze values below x  n = x  Analyze data in an array  n = size of array  Analyze linked list  n = size of linked list  Analyze 2 arrays  n = sum of array sizes

Analyzing an Algorithm Counts primitive operations executed  Assignments  Method calls  Performing arithmetic operation  Comparing two values  Indexing into array  Following a reference  Returning a method

Primitive Statements Run in constant time: O(1)  Fastest time possible Sequences also run in constant time  True if sequence not impacted by input  O(5) = O(5 * 1) = O(1)  Big-Oh is rough estimate – ignore constant multipliers

Simple Loops for (int i = 0; i < n.length; i++) { } while (i < n) { i++; }  Each loop executed n times  Loops only contain primitive statements  Total complexity of each loop is: O(n)

More Complicated Loops for (int i = 0; i < n; i += 2) { }  i assigned 0, 2, 4, 6,... until larger than n  Loop executes n / 2 times  Loop only contains primitive statements  Total complexity of loop is: O(n/2) = O(½ * n) = O(n)

Even More Complicated Loops for (int i = 1; i < n; i *= 2) { }  i assigned 1, 2, 4, 8,... until larger than n  Loop executes log 2 n times  Loop only contains primitive statements  Total complexity of loop is: O(log 2 n ) = O(log n)

Even More Complicated Loops for (int i = 1; i < n; i *= 3) { }  i assigned 1, 3, 9, 27,... until larger than n  Loop executes log 3 n times  Loop only contains primitive statements  Total complexity of loop is: O(log 3 n ) = O(log 3/log 2 * log n) = O(log n)

Loop Time Complexity When loop control variable increases:  Does not change: takes O(1) or O(∞) time  By constant value: takes O(n) time  By constant multiple: takes O(log n) time

Nested Loops for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { } }  Inner loop ( j ) executes in O(n) time  Outer loop ( i ) executes n times But each pass takes O(n) time, not O(1) time  Total complexity of nested loops: O(n) * O(n) = O(n 2 )

Your Turn Get back into groups and do activity

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