Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.

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
Complexity Analysis (Part I)
CISC220 Spring 2010 James Atlas Lecture 06: Linked Lists (2), Big O Notation.
Cmpt-225 Algorithm Efficiency.
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Computer Science 2 Data Structures and Algorithms V section 2 Intro to “big o” Lists Professor: Evan Korth New York University 1.
CS 206 Introduction to Computer Science II 01 / 28 / 2009 Instructor: Michael Eckmann.
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
Algorithm Analysis (Big O)
Algorithm analysis and design Introduction to Algorithms week1
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Mathematics Review and Asymptotic Notation
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)
Complexity Analysis Chapter 1.
Analysis of Algorithms
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
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.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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.
Introduction to Analysis of Algorithms CS342 S2004.
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.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
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.
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.
Data Structures I (CPCS-204) Week # 2: Algorithm Analysis tools Dr. Omar Batarfi Dr. Yahya Dahab Dr. Imtiaz Khan.
(Complexity) Analysis of Algorithms Algorithm Input Output 1Analysis of Algorithms.
Complexity Analysis (Part I)
Introduction to Analysis of 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 Analysing Costs
Implementing ArrayList Part 1
More complexity analysis & Binary Search
Big-Oh and Execution Time: A Review
Building Java Programs
GC 211:Data Structures 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.
Algorithm Efficiency Chapter 10.
Advanced Analysis of Algorithms
Programming and Data Structure
CSE 2010: Algorithms and Data Structures Algorithms
CSE 373, Copyright S. Tanimoto, 2002 Asymptotic Analysis -
Asymptotic Notations Algorithms perform f(n) basic operations to accomplish task Identify that function Identify size of problem (n) Count number of operations.
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
CSE 373, Copyright S. Tanimoto, 2001 Asymptotic Analysis -
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

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

RECAP  iterators, comparators, comparables TODAY  introduction to analysing cost 2 RECAP-TODAY

Analysing Costs (in general) 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” 3

Benchmarking: program cost 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 4

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.  best case (easy, but not interesting)  worst case (usually easy)  average case (harder)  The precise number of steps is not required  3.47 n n + 53 steps  3n log(n) + 5n - 3 steps  Rather, we are interested in the complexity class 5

Analysis: The Big Picture  Only care about with what shape cost grows with input size ,000,000 2,000,000 3,000,000 f 1 (n) f 2 (n) f 1 (n)= n n + 2 f 2 (n)= n input size cost

Analysis: Notation s  g(n) f(n) n0n0 input size f(n) is O(g(n)), if there are two positive constants, s and n 0 with f(n)  s  | g(n) |, for all n  n 0 Big-O Notation cost

Big-O Notation  Only care for large input sets and ignore constant factors 3.47 n n steps  O(n 2 ) 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 8

 “Asymptotic cost”, or “big-O” cost describes how cost grows with input size Big-O classes 9 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(n 2 ) quadratic costs grows like n  n : severely limits problem size

Big-O costs 10 NO(1) Constant O(log N) Logarithmic O(N) Linear O(N log N)O(N^2) Quadratic O(N^3) Cubic O(N!) Factorial E E+18 Cost O(n) input size O(n log n) O(n 2 ) O(n 3 ) O(log n) O(1)

Manageable Problem Sizes 11 N1 min1 h1 day1 week 1 year n 6      n log n 2.8      n2n      10 6 n3n      n2n  High asymptotic cost severely limits problem size

 Of importance  comparing data  moving data  anything you consider to be “expensive” 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; } What is a “step” ? ← Key Step 12

ArrayList: get, set, remove  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 ? how many steps ? n 13

ArrayList: add (at some position) 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++; }  Cost of add(index, value):  key step?  worst case:  average case: n 14

ArraySet costs Costs:  contains, add, remove: O(n) 15