LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.

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

Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
HST 952 Computing for Biomedical Scientists Lecture 10.
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
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
Introduction to Analysis of Algorithms
Complexity Analysis (Part I)
Program Design and Development
Professor John Peterson
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.
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.
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
Abstraction IS 101Y/CMSC 101 Computational Thinking and Design Tuesday, September 17, 2013 Carolyn Seaman University of Maryland, Baltimore County.
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.
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.
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.
Analysis of Algorithms
Lecture 4. RAM Model, Space and Time Complexity
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)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  Based upon B (& other) earlier languages  Since its creation,
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
Extracting Performance Functions. Basic Operations n The number of Basic Operations performed must be proportional to the run time n Counting techniques.
Week 12 - Wednesday.  What did we talk about last time?  Asymptotic notation.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
CSC 107 – Programming For Science. Announcements  Locations of Macs to use outside of lab time  Can find on Library ground floor (6) & main floor (6)
CSC 107 – Programming For Science. History of C  Dennis Ritchie developed C from 1969 – 1973  While at Bell Labs, created language to develop Unix 
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.
Program Development C# Programming January 30, 2007 Professor J. Sciame.
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.
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.
CSC 212 – Data Structures Lecture 15: Big-Oh Notation.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
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.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
1 7.Algorithm Efficiency These factors vary from one machine/compiler (platform) to another  Count the number of times instructions are executed So, measure.
CSC 108H: Introduction to Computer Programming Summer 2011 Marek Janicki.
Complexity Analysis (Part I)
Introduction to Analysis of Algorithms
Analysis of Algorithms
Introduction to Algorithms
Efficiency (Chapter 2).
Revision of C++.
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
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures

Analysis Techniques  Running time is important, …  … but comparing times impossible in many cases  Single problem may have lots of ways to be solved  Many implementations possible for a solution

Pseudo-Code  Only for human eyes  Unimportant & implementation details ignored  Serves very real purpose, but language not real  Used when outlining, designing, & analyzing  Language-like manner to system, though not formal

Pseudo-Code  Include important details  Loops, assignments, method calls, etc.  Anything that you find helpful analyzing algorithm  Understanding algorithm is only goal  Feel free to ignore punctuation & formalisms  Allow people to understand and analyze; nothing else

Pseudo-code Example Algorithm factorial(n) returnVariable  1 while (n > 0) returnVariable  returnVariable * n n  n – 1 endwhile return returnVariable

Big-Oh Notation  Expresses an algorithm’s complexity  Worst-case analysis of performance  Usually closely correlated with execution time  Approaches compared using big-Oh notation  Requires algorithms only  Saves time implementing all the algorithms  Avoids unrelated details: CPU, typing speed, compiler

Algorithmic Analysis

Algorithm Analysis  Execution time with n inputs on 4GHz machine: n = 10n = 50n = 100n = 1000n = 10 6 O(n log n)9 ns50 ns175 ns2500 ns5 ms O(n 2 )25 ns625 ns2250 ns ns4 min O(n 5 )25000 ns72.5 ms2.7 s2.9 days1x10 13 yrs O(2 n )2500 ns3.25 days1 x yrs1 x yrs Too long! O(n!)1 ms1.4 x yrs7 x yrs Too long!

 Want results for large data sets  Nobody cares about 2 minute worst case run  Limit considerations to only the major details  Ignore multipliers  So, O (5 n ) = O (2 n ) = O ( n )  Multipliers usually implementation-specific  Does extra 20ms matter versus 4 minutes?  Ignore lesser terms  So, O ( n 5 + n 2 ) = O ( n 5 )  Does 17 minutes matter after 3x10 13 years? Big-Oh Notation

What is n ?  Big-Oh analysis always relative to input size  But determining input size is not always clear  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

 Big-Oh counts primitive operations executed  Primitive operations include:  Assignments  Method calls  Performing arithmetic operation  Comparing two values  Getting entry from an array  Following a reference  Returning a value from a method Analyzing an Algorithm

Primitive Statements  Execute in constant time: O (1)  Fastest possible time  Sequence of primitives also runs in constant time  So long as input does not affect sequence  Constant multipliers ignored; only large sets matter  O (5) = O (5 * 1) = O (1)

for (int i = 0; i < n.length; i++){} -or- while (i < n) { i++; }  Each loop executed n times  Primitive statements only within the loop  Big-Oh complexity of each of these loops is: = O ( n ) Simple Loops

for (int i = 0; i < n.length; i++){} int i = 0; while (i < n) { i++; }  Each loop executed n times  Add complexities of sequential code  Big-Oh complexity of this code is: = O ( n ) + O ( n ) = O (2 n ) = O ( n ) Sequential Loops Add

for (int i = 0; i < n; i += 2) { } So, i  0, 2, 4, 6,..., n  Loop executes n / 2 times  Primitive statements only within the loop  Total big-Oh complexity of loop is: = O ( n / 2 ) = O (½ * n ) = O ( n ) More Complicated Loops

for (int i = 1; i < n; i *= 2) { } So, i  1, 2, 4, 8,..., n  Loop executes log 2 n times  Primitive statements only within the loop  Total big-Oh complexity of loop is: = O (log 2 n ) Really Complicated Loops

for (int i = 1; i < n; i *= 3) { } So, i  1, 3, 9, 27,..., n  Loop executes log 3 n times  Primitive statements only within the loop  Total big-Oh complexity of loop is: = O (log 3 n ) = O ( log 3 / log 2 * log n ) = O (log n ) Really Complicated Loops

Big-Oh Loop Complexity  Based upon how loop control variable increases:  Does not change: O (1) or O (∞) time  Increase by constant value: O ( n ) time  Constant multiple increase: O (log n ) time

for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { } }  Inner loop ( j ) executes in O ( n ) time  n times executing outer loop ( i )  But outer loop pass takes O ( n ) time  Total big-Oh complexity of these nested loops: = O ( n ) * O ( n ) = O ( n 2 ) Nested Loops Multiply

 Continue week #8 assignment  Due by 5PM next Tuesday  Examine programming assignment #3  Messages are not always sent to everyone!  Read section 4.3 in book before class  How do we prove big-Oh notations? Before Next Lecture…