Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.

Slides:



Advertisements
Similar presentations
Lecture3: Algorithm Analysis Bohyung Han CSE, POSTECH CSED233: Data Structures (2014F)
Advertisements

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.
Computational Complexity 1. Time Complexity 2. Space Complexity.
Algorithmic Complexity Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Introduction to Analysis of Algorithms
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
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.
Not all algorithms are created equally Insertion of words from a dictionary into a sorted list takes a very long time. Insertion of the same words into.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Computer Science 2 Data Structures and Algorithms V section 2 Intro to “big o” Lists Professor: Evan Korth New York University 1.
Algorithm Efficiency and Sorting Bina Ramamurthy CSE116A,B.
Elementary Data Structures and Algorithms
Computer Science 2 Data Structures and Algorithms V Intro to “big o” Lists Professor: Evan Korth New York University 1.
Abstract Data Types (ADTs) Data Structures The Java Collections API
COMP s1 Computing 2 Complexity
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
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.
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
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Lecture 2 Computational Complexity
Algorithm Efficiency CS 110: Data Structures and Algorithms First Semester,
1.2. Comparing Algorithms. Learning outcomes Understand that algorithms can be compared by expressing their complexity as a function relative to the size.
Analysis of Algorithms
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
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.
Analysing Costs: ArraySet Binary Search COMP 103.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Fundamentals CSE 373 Data Structures Lecture 5. 12/26/03Fundamentals - Lecture 52 Mathematical Background Today, we will review: ›Logs and exponents ›Series.
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
RUNNING TIME 10.4 – 10.5 (P. 551 – 555). RUNNING TIME analysis of algorithms involves analyzing their effectiveness analysis of algorithms involves analyzing.
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.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 2 Prepared by İnanç TAHRALI.
Efficiency of Algorithms. Node - data : Object - link : Node + createNode() + getData() + setData() + getLink() + setLink() + addNodeAfter() + removeNodeAfter()
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
تصميم وتحليل الخوارزميات عال311 Chapter 3 Growth of Functions
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Searching Topics Sequential Search Binary Search.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
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.
GC 211:Data Structures Week 2: Algorithm Analysis Tools Slides are borrowed from Mr. Mohammad Alqahtani.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Searching – Linear and Binary Searches
Algorithmic Efficency
Introduction to Algorithms
GC 211:Data Structures Algorithm Analysis Tools
Introduction to Analysing Costs
Introduction to Algorithms
Implementing ArrayList Part 1
Big-Oh and Execution Time: A Review
Algorithm Efficiency Chapter 10.
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
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103 # Tri 2

COMP 103 9:2 Menu Cost of operations and measuring efficiency ArrayList: remove, add ArraySet: contains, remove, add Binary Search: Notes: Test next friday

COMP 103 9:3 Analysing Costs 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. Programs or Algorithms? Both programs: benchmarking algorithms: analysis

COMP 103 9:4 Benchmarking: program cost Measure: (Assignment 4 !!!) actual programs, on real machines, on specific input measure elapsed time set up for the test long start = System.currentTimeMillis(); // time from system clock in milliseconds do the test UI.println(System.currentTimeMillis() – start); measure real memory

COMP 103 9:5 Benchmarking: program cost Must manage benchmarking tests carefully to get good results. Only measure the part you need: don't include user or file input minimise screen or file output Choice of test data: choose test sets carefully use large data sets Interference from other users/processes minimise other activity average the results over many runs Effect of computer specify the computer when reporting actual times

COMP 103 9:6 Analysis: Algorithm complexity Abstract away from the details of the hardware the operating system the programming language the compiler the program the specific input Measure number of “steps” as a function of the data size. worst case (easier) average case (harder) best case (easy, but useless) Work out the number of steps: cost = 3.47 n n + 53 steps cost = 3n log(n) - 5n + 6 steps simplified into terms of different powers/functions of n

COMP 103 9:7 Analysis: Asymptotic Notation We only care about the cost when it is large ⇒ drop the lower order terms (the ones that will be insignificant with large n) cost = 3.47 n steps cost = 3n log(n) + … steps We don’t care about the constant factors (much) Actual constant will depend on the hardware ⇒ Drop the constant factors cost ∝ n 2 + … steps cost ∝ n log(n) + … steps “Assymptotic cost”, or “big-O” cost. describes how cost grows with input size cost is O(1) : fixed cost cost is O(n): grows linearly with n

COMP 103 9:8 Typical Costs log n n n log n n2n2 2n2n input size: n cost

COMP 103 9:9 Problem: What is a “step”? Count actions in the innermost loop (happen the most times) actions that happen every time round (not inside an “if”) actions involving “data values” (rather than indexes) representative actions (don’t need every action) public E remove (int index){ if (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 ← in the innermost loop

COMP 103 9:10 What’s a step: Pragmatics Count the most expensive actions: Adding 2 numbers is cheap Raising to a power is not so cheap Comparing 2 strings may be expensive Reading a line from a file may be very expensive Waiting for input from a user or another program may take forever… Sometimes we need to know about how the underlying operations are implemented in the computer to choose well (NWEN241/242).

COMP 103 9:11 Cost of ArrayList: get, set, remove Assume List contains n items. Cost of get and set: best, worst, average: O(1) ⇒ constant number of steps, regardless of n Cost of Remove: worst case: what is the worst case? how many steps? average case: what is the average case? how many steps? n

COMP 103 9:12 Cost of ArrayList: add Cost of add(index, value): what’s the key step? worst case: average case: public void add(int index, E item){ if (index count) throw new IndexOutOfBoundsException(); ensureCapacity(); for (int i=count; i > index; i--) data[i]=data[i-1]; data[index]=item; count++; } n

COMP 103 9:13 Cost of ArrayList: add at end Cost of add(value): what’s the key step? 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

COMP 103 9:14 Cost of ArrayList: add at end Average case: average over all possible states and input. OR amortised cost over time. Amortised cost: total cost of adding n items  n : first 10: cost = 1 eachtotal = 10 11th:cost = 10+1total = :cost = 1 eachtotal = 30 21st:cost = 20+1total = :cost = 1 eachtotal = 70 41st:cost = 40+1total = :cost = 1 eachtotal = 150 : - n total = Amortised cost (per item) =

COMP 103 9:15 ArrayList costs: Summary getO(1) setO(1) removeO(n) add (at i)O(n)(worst and average) add (at end)O(1)(average) O(n)(worst) O(1)(amortised average) Question: what would the amortised cost be if the array size is increased by 10 each time?

COMP 103 9:16 Typical Costs in Big ‘O’ If data/input is size nHow does it grow. O(1) “constant” cost is independent of n O(log(n)) “logarithmic” size x 10 → add a little to the cost O(n) “linear” size x 10 → 10 x the cost O(n log(n)) “en-log-en” size x 10 → bit more than 10 x O(n 2 ) “quadratic” size x 10 → 100 * the cost O(n 3 ) “cubic” size x 10 → 1000 * the cost : O(2 n ) “exponential” adding one to size -> doubles the cost => You don’t want to run this algorithm! O(n!) “factorial” adding one to size -> n the cost => You definitely don’t want this algorithm!

COMP 103 9:17 Cost of ArraySet ArraySet uses same data structure as ArrayList does not have to keep items in order Operations are: contains(item) add(item) ← always add at the end remove(item) ← don’t need to shift down – just move last item down What are the costs? contains:O(n) remove:O(1)O(n) add:O(1)O(n) n