Development and Testing Ch4.1. Algorithm Analysis

Slides:



Advertisements
Similar presentations
Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Advertisements

Garfield AP Computer Science
CHAPTER 2 ALGORITHM ANALYSIS 【 Definition 】 An algorithm is a finite set of instructions that, if followed, accomplishes a particular task. In addition,
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Testing and Debugging pt.2 Intro to Complexity CS221 – 2/18/09.
Writing and Testing Programs Drivers and Stubs Supplement to text.
JUnit. What is unit testing? A unit is the smallest testable part of an application. A unit test automatically verifies the correctness of the unit. There.
Complexity Analysis (Part I)
Computer Science 1620 Programming & Problem Solving.
Data Structures Review Session 1
Analysis of Algorithm.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
CS 1704 Introduction to Data Structures and Software Engineering.
Chapter 12 Recursion, Complexity, and Searching and Sorting
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.
C++ REVIEW – POINTERS AND TEST DRIVEN DEVELOPMENT.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 ADT Implementation: Recursion, Algorithm Analysis Chapter 10.
Algorithm Analysis 1.
Complexity Analysis (Part I)
Test Driven Development (TDD)
October 2nd – Dictionary ADT
CSC 427: Data Structures and Algorithm Analysis
Analysis: Algorithms and Data Structures
COP 3503 FALL 2012 Shayan Javed Lecture 16
May 17th – Comparison Sorts
CSC 427: Data Structures and Algorithm Analysis
Analysis of Algorithms
CSC 222: Object-Oriented Programming
Chapter 9: Searching, Sorting, and Algorithm Analysis
Big-O notation.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Catie Baker Spring 2015.
Simple Sorting Algorithms
Introduction to Algorithms
Algorithm Analysis CSE 2011 Winter September 2018.
Big-Oh and Execution Time: A Review
Algorithm Analysis (not included in any exams!)
Building Java Programs
CSE 143 Lecture 5 Binary search; complexity reading:
Algorithm design and Analysis
Analysis Algorithms.
CSE 143 Lecture 5 More Stacks and Queues; Complexity (Big-Oh)
Lesson 15: Processing Arrays
Algorithm Efficiency Chapter 10.
Lecture 5: complexity reading:
Coding Concepts (Basics)
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Building Java Programs
Testing Acknowledgement: Original slides by Jory Denny.
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Searching, Sorting, and Asymptotic Complexity
Searching, Sorting, and Asymptotic Complexity
CSE 373: Data Structures & Algorithms
Building Java Programs
CSC 427: Data Structures and Algorithm Analysis
CSE 143 Lecture 8 More Stacks and Queues; Complexity (Big-Oh)
Ch. 2: Getting Started.
CSE 1342 Programming Concepts
Measuring Experimental Performance
Building Java Programs
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
CS210- Lecture 2 Jun 2, 2005 Announcements Questions
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Testing Slides adopted from John Jannotti, Brown University
Presentation transcript:

Development and Testing Ch4.1. Algorithm Analysis

Development and testing

Development (one out of many perspectives) Solve Implement Write test Write code Repeat Integrate Release

Test Driven Development (TDD)

Practical Example Lets practice some TDD on the following example Your project manager at BusyBody Inc says he needs a feature implemented which determines the total amount of time a worker at the company spends at their desk. He says the number of hours each day is already being measured and is stored in an internal array in the code base.

Practical Example How do we solve this? Compute an average of an array!

Practical Example First we write a test in other words, set up the scaffolding of the code instead of a function which you don’t know if it works or not – and continue to struggle finding bugs public static double sum(double[] arr) { return Double.POSITIVE_INFINITY; //note this clearly does not work and is thus failing } public static void main() { double[] arr = {0, 1, 1, 2, 3, 5, 8}; if(sum(arr) != 20) cout << “Test failed?!?!?! I suck!” << endl; //you don’t really suck, its supposed to fail!

Practical Example Before we continue, lets review Positives Scaffolding, function interface, and test all implemented We know it is good design Tests to tell if the code is correct, before we struggle with debugging many lines of code Negatives Code isn’t written until later…..but is that really that bad? NO In fact, with TDD you code FASTER and more EFFECTIVELY than without it

Practical Example Now the code – and then run the test! public static double sum(double[] arr) { double s = 0; for(double x : arr) s += x; return s; }

Things to remember Always have code that compiles Test writing is an art that takes practice (and more learning!) Compile and test often!

Testing Frameworks Many frameworks exist CppUnit, JUnit, etc. We will be using a much more simple unit testing framework developed by me A unit test is a check of one behavior of one “unit” (e.g., function) of your code If you have downloaded the lab zip for today open it and look there Follows SETT – unit testing paradigm Setup – create data for input and predetermine the output Execute – call the function in question Test – analyze correctness and determine true/false for test Teardown – cleanup any data, close buffers, etc

Unit test example public static boolean testSum() { //setup double[] arr = {0, 1, 1, 2, 3, 5, 8}; double ans = 20; //execute double s = sum(arr); //test return s == ans; //teardown – here is empty }

TDD - Exercise Write a Java function to find the minimum of an array of integers Do test driven development, starting with a good unit test After test is created and checked, code the function Pair program!

Runtime Analysis

Big-oh Given functions 𝑓 𝑛 and 𝑔 𝑛 , we say that 𝑓 𝑛 is 𝑂 𝑔 𝑛 if there are positive constants 𝑐 and 𝑛 0 such that 𝑓 𝑛 ≤𝑐𝑔 𝑛 for 𝑛≥ 𝑛 0 We need to know how to determine 𝑓 𝑛 , 𝑐, and 𝑛 0 This is all done through experiments

Determining 𝑓 𝑛 Vary the size of the input and then determine runtime using System.nanoTime() for(int n = 2; n < MAX; n*=2) { int r = max(10, MAX/n); //number of repetitions long start = System.nanoTime(); for(int k = 0; k < r; ++k) executeFunction(); long stop = System.nanoTime(); double elapsed = (stop – start)/1.e9/r; }

Determine 𝑐 and 𝑛 0 First plot f(n) – time vs size Second plot f(n)/g(n) or time/theoretical vs size Look for where the data levels off. This will be 𝑛 0 Look for the largest value to the right of 𝑛 0 , this will be 𝑐

Together – Time linear search

Activity Determine big-oh constants for Arrays.sort(); Theoretical complexity will be 𝑂 𝑛 log 𝑛