Introduction to Analysing Costs

Slides:



Advertisements
Similar presentations
Razdan with contribution from others 1 Algorithm Analysis What is the Big ‘O Bout? Anshuman Razdan Div of Computing.
Advertisements

Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Introduction to Analysis of Algorithms
CS 307 Fundamentals of Computer Science 1 Asymptotic Analysis of Algorithms (how to evaluate your programming power tools) based on presentation material.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Abstract Data Types (ADTs) Data Structures The Java Collections API
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Week 2 CS 361: Advanced Data Structures and Algorithms
Mathematics Review and Asymptotic Notation
Complexity Analysis Chapter 1.
Analysing Costs: ArraySet Binary Search COMP 103.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Analysis of Algorithms CSCI Previous Evaluations of Programs Correctness – does the algorithm do what it is supposed to do? Generality – does it.
Program Efficiency & Complexity Analysis. Algorithm Review An algorithm is a definite procedure for solving a problem in finite number of steps Algorithm.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington 
Complexity of Algorithms Fundamental Data Structures and Algorithms Ananda Guna January 13, 2005.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
Complexity Analysis (Part I)
Introduction to Analysis of Algorithms
Analysis of Algorithms
Introduction to Search Algorithms
GC 211:Data Structures Week 2: Algorithm Analysis Tools
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Big-O notation.
Introduction to Algorithms
Implementing ArrayList Part 1
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
COMP 103 Sorting with Binary Trees: Tree sort, Heap sort Alex Potanin
COMP 103 HeapSort Thomas Kuehne 2013-T1 Lecture 27
More complexity analysis & Binary Search
CS 3343: Analysis of Algorithms
Big-Oh and Execution Time: A Review
Algorithm Analysis (not included in any exams!)
Building Java Programs
Algorithm An algorithm is a finite set of steps required to solve a problem. An algorithm must have following properties: Input: An algorithm must have.
GC 211:Data Structures Algorithm Analysis Tools
Introduction to Algorithms Analysis
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
Algorithm Efficiency Chapter 10.
Lecture 5: complexity reading:
CS 201 Fundamental Structures of Computer Science
Programming and Data Structure
Analysis of Algorithms
Programming and Data Structure
PAC Intro to “big o” Lists Professor: Evan Korth New York University
Algorithm Analysis Bina Ramamurthy CSE116A,B.
CSE 2010: Algorithms and Data Structures Algorithms
Building Java Programs
Searching, Sorting, and Asymptotic Complexity
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
slides created by Ethan Apter
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
David Kauchak cs161 Summer 2009
CSE 373, Copyright S. Tanimoto, 2001 Asymptotic Analysis -
Complexity Analysis (Part I)
Analysis of Algorithms
Complexity Analysis (Part I)
Presentation transcript:

Introduction to Analysing Costs COMP 103 Introduction to Analysing Costs Marcus Frean, Rashina Hoda, and Peter Andreae Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 08

RECAP-TODAY RECAP Implementing the fundamental ArrayList methods 2 RECAP Implementing the fundamental ArrayList methods AbstractList  ArrayList does not have to implement everything TODAY introduction to analysing cost

Analysing Costs (in general) 3 How can we determine the costs of a program? Time: Run the program and count the milliseconds/minutes/days. Count number of steps/operations the algorithm will take. Space: Measure the amount of memory the program occupies. Count the number of elementary data items the algorithm stores. Applies to Programs or Algorithms? Both. programs: called “benchmarking” algorithms: called “analysis”

Benchmarking: program cost 4 Measure: actual programs, on real machines, with specific input measure elapsed time System.currentTimeMillis () → time from the system clock in milliseconds measure real memory Problems: what input? ⇒ use large data sets don’t include user input other users/processes? ⇒ minimise average over many runs which computer? ⇒ specify details how to compare cross-platform? ⇒ measure cost at an abstract level

Analysis: Algorithm “complexity” 5 Abstract away from the details of the hardware, the operating system the programming language, the compiler the specific input Measure number of “steps” as a function of the data size best case (easy, but not interesting) worst case (usually easy) average case (harder) The precise number of steps is not required 3.47 n2 - 67n + 53 steps 3n log(n) + 5n - 3 steps Rather, we are interested in the complexity class

Analysis: The Big Picture 6 cost f1(n) f1(n) = 0.7772n2 + 305n + 2 3,000,000 f2(n) = 1.2724n + 100000 2,000,000 1,000,000 f2(n) input size 125 250 500 1000 2000 Only care about with what shape cost grows with input size

Analysis: Notation Big-O Notation 7 Big-O Notation f(n) is O(g(n)), if there are two positive constants, s and n0 with f(n) £ s  | g(n) |, for all n ³ n0 cost s  g(n) 3000 2000 f(n) 1000 input size 125 250 500 n0 1000 2000

Big-O Notation 8 Only care for large input sets and ignore constant factors 3.47 n2 + 102n + 10064 steps  O(n2) 3n log n + 12n steps  O(n log n) Lower-order terms become insignificant for large n Multiplication factors don’t tell us how things “scale up” We care about how cost grows with input size

Big-O classes 9 “Asymptotic cost”, or “big-O” cost describes how cost grows with input size Examples: O(1) constant cost is independent of n : Fixed cost! O(log n) logarithmic cost grows up by 1, every time n doubles : Slow growth! O(n) linear cost grows linearly with n : can often be beaten O(n2) quadratic costs grows like n  n : severely limits problem size

Big-O costs Cost input size 10 N O(1) Constant O(log N) Logarithmic Linear O(N log N) O(N^2) Quadratic O(N^3) Cubic O(N!) Factorial 1 5 0.00 2.32 11.61 25 125 120 10 3.32 33.22 100 1000 3628800 15 3.91 58.60 225 3375 1.31E+12 20 4.32 86.44 400 8000 2.43E+18 O(n3) O(n2) Cost O(n log n) O(n) O(log n) O(1) input size

Manageable Problem Sizes 11 N 1 min 1 h 1 day 1 week 1 year n 6 ´ 107 3.6 ´ 109 8.64 ´ 1010 6.05 ´ 1011 3.15´ 1013 n log n 2.8 ´ 106 1.3 ´ 108 2.75 ´ 109 1.77 ´ 1010 7.97 ´ 1011 n2 7.75 ´ 103 6.0 ´ 104 2.94 ´ 105 7.78 ´ 105 5.62 ´ 106 n3 3.91 ´ 102 1.53 ´ 13 4.42 ´ 103 8.46 ´ 103 3.16 ´ 104 2n 25 31 36 39 44 High asymptotic cost severely limits problem size

What is a “step”? Of importance comparing data moving data 12 Of importance comparing data moving data anything you consider to be “expensive” public E remove (int index){     if (index < 0 || index >= count) throw new ….Exception();     E ans = data[index];     for (int i=index+1; i< count; i++)       data[i-1]=data[i];         count--; data[count] = null;     return ans;   } ← Key Step

ArrayList: get, set, remove 13 Assume an ArrayList contains n items. Cost of get and set: best, worst, average: Cost of Remove: worst case: what is the worst case? how many steps? average case: what is the average case? n

ArrayList: add (at some position) 14 public void add(int index, E item){ if (index<0 || index>count) throw new IndexOutOfBoundsException(); ensureCapacity(); for (int i=count; i > index; i--)       data[i]=data[i-1]; data[index]=item; count++; }   Cost of add(index, value): key steps? n

ArrayList: add at end Cost of add(value): which are the key steps? 15 Cost of add(value): worst case: average case: public void add (E item) {     ensureCapacity();     data[count++] = item; } private void ensureCapacity () {     if (count < data.length) return;     E [ ] newArray = (E[ ]) (new Object[data.length * 2]);     for (int i = 0; i < count; i++)       newArray[i] = data[i];     data = newArray; }  n which are the key steps?

ArrayList: Amortised cost 16 Some single “adds” are expensive, but what is the big picture? “Amortised” cost: total cost of adding n items, divided by n: first 10: cost = 1 each total = 10 11th: cost = 10+1 total = 21 12-20: cost = 1 each total = 30 21st: cost = 20+1 total = 51 22-40: cost = 1 each total = 70 41st: cost = 40+1 total = 111 42-80: cost = 1 each total = 150 : - n total = Amortised cost ( ) = ? per item

ArrayList: Amortised cost 17 Starting with array length = 1 makes it easier to see the pattern first : cost = 1 each total = 1 2nd: cost = 1+1 total = 3 3rd: cost = 2 + 1 total = 6 4th: cost = 1 each total = 7 5th: cost = 4 + 1 total = 12 6-8: cost = 1 each total = 15 9th: cost = 8 + 1 total = 24 10-16: cost = 1 each total = 31 : n total = Amortised cost ( ) = look at pattern to right (1,3,7,15,31) -> 2^x -1 OR calculate sum i=0..k (2^i) = 2^(k+1)-1 how many copy steps are there? n = 2^steps = 2^k => k=log_2 n now insert steps into count formular steps = 2^(log2(n)+1)-1 -> n -> like previous example with a factor of ten finally: divide by n to achieve amortised cost. per item

ArrayList costs: Summary 18 get O(1) set O(1) remove O(n) add (at i) O(n) (worst and average) add (at end) O(1) (average) O(n) (worst) O(1) (amortised average) To think about: What would the amortised cost be if the array size were increased by a fixed amount (say 10) each time?