Sum this up for me. Let s write a method to calculate the sum from 1 to some n. Gauss also has a way of solving this. Which one is more efficient?

Similar documents
Building Java Programs

Searching and Sorting

Building Java Programs Chapter 13

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Algorithmic Complexity

Lecture 2: Implementing ADTs

Outline. runtime of programs algorithm efficiency Big-O notation List interface Array lists

CS 231 Data Structures and Algorithms Fall Algorithm Analysis Lecture 16 October 10, Prof. Zadia Codabux

Lecture 1: Welcome! CSE 373: Data Structures and Algorithms CSE WI - KASEY CHAMPION 1

ECE 122. Engineering Problem Solving Using Java

10/5/2016. Comparing Algorithms. Analyzing Code ( worst case ) Example. Analyzing Code. Binary Search. Linear Search

Introduction to the Analysis of Algorithms. Algorithm

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 10: Asymptotic Complexity and

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

CSE373: Data Structures and Algorithms Lecture 4: Asymptotic Analysis. Aaron Bauer Winter 2014

Algorithms A Look At Efficiency

CMPSCI 187: Programming With Data Structures. Lecture 5: Analysis of Algorithms Overview 16 September 2011

Algorithm Analysis. Performance Factors

Exam Questions. How to Answer

Algorithm Analysis. CENG 707 Data Structures and Algorithms

15110 Principles of Computing, Carnegie Mellon University - CORTINA. Binary Search. Required: List L of n unique elements.

Principles of Algorithm Analysis. Biostatistics 615/815

Chapter 11. Analysis of Algorithms

Data Structures Lecture 3 Order Notation and Recursion

What did we talk about last time? Finished hunters and prey Class variables Constants Class constants Started Big Oh notation

Algorithms. Algorithms 1.4 ANALYSIS OF ALGORITHMS

Algorithm. Lecture3: Algorithm Analysis. Empirical Analysis. Algorithm Performance

ASYMPTOTIC COMPLEXITY

CSE 146. Asymptotic Analysis Interview Question of the Day Homework 1 & Project 1 Work Session

Algorithm efficiency can be measured in terms of: Time Space Other resources such as processors, network packets, etc.

Algorithmic Analysis. Go go Big O(h)!

csci 210: Data Structures Program Analysis

CSC 222: Object-Oriented Programming. Fall 2015

Algorithm Analysis. Big Oh

CS 261 Data Structures. Big-Oh Analysis: A Review

What is an algorithm?

For searching and sorting algorithms, this is particularly dependent on the number of data elements.

CSC 222: Object-Oriented Programming Spring 2012

CS 3410 Ch 5 (DS*), 23 (IJP^)

Algorithm Analysis. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Algorithm Efficiency, Big O Notation, and Javadoc

Choice of C++ as Language

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

How do we compare algorithms meaningfully? (i.e.) the same algorithm will 1) run at different speeds 2) require different amounts of space

[ 11.2, 11.3, 11.4] Analysis of Algorithms. Complexity of Algorithms. 400 lecture note # Overview

Arrays.

O(1) How long does a function take to run? CS61A Lecture 6

Computational Complexity: Measuring the Efficiency of Algorithms

CSE 332: Data Structures & Parallelism Lecture 5: Algorithm Analysis II. Ruth Anderson Winter 2018

4.1 Performance. Running Time. The Challenge. Scientific Method. Reasons to Analyze Algorithms. Algorithmic Successes

Computer Science 210 Data Structures Siena College Fall Topic Notes: Complexity and Asymptotic Analysis

Sorting & Searching (and a Tower)

CSCI 261 Computer Science II

Is This Algorithm Fast? Algorithm Analysis. Attendance Question 1. Grading Algorithms

Topic Number 2 Efficiency Complexity Algorithm Analysis

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Week 4, Wednesday (Spring 2015). Dr. Yoder. Sec 051. Page 1 of 5

Analysis of Algorithms. CS 1037a Topic 13

Chapter 2: Complexity Analysis

Algorithm Analysis. This is based on Chapter 4 of the text.

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSE 143 Lecture 4. Implementing ArrayIntList; Binary Search. reading:

CSCI-1200 Data Structures Spring 2018 Lecture 7 Order Notation & Basic Recursion

CS 221 Review. Mason Vail

Checking for duplicates Maximum density Battling computers and algorithms Barometer Instructions Big O expressions. John Edgar 2

Another Sorting Algorithm

Analysis of Algorithms 1 / 18

Ch 8. Searching and Sorting Arrays Part 1. Definitions of Search and Sort

Analysis of algorithms

Chapter 8 Search and Sort

CS 310: Order Notation (aka Big-O and friends)

CSE 143 Lecture 2. reading:

CSc 110, Spring 2017 Lecture 39: searching

CS261 Data Structures. Ordered Bag Dynamic Array Implementa;on

Data Structures Lecture 8

COLLECTIONS & ITERATORS cs2420 Introduction to Algorithms and Data Structures Spring 2015

10/11/2013. Chapter 3. Objectives. Objectives (continued) Introduction. Attributes of Algorithms. Introduction. The Efficiency of Algorithms

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

CSE 143. Complexity Analysis. Program Efficiency. Constant Time Statements. Big Oh notation. Analyzing Loops. Constant Time Statements (2) CSE 143 1

Introduction to Analysis of Algorithms

6. Asymptotics: The Big-O and Other Notations

CSE 214 Computer Science II Searching

asymptotic growth rate or order compare two functions, but ignore constant factors, small inputs

ALGORITHM ANALYSIS. cs2420 Introduction to Algorithms and Data Structures Spring 2015

Analysis of Algorithms. 5-Dec-16

algorithm evaluation Performance

MPATE-GE 2618: C Programming for Music Technology. Unit 4.2

What is an Algorithm?

9/3/12. Outline. Part 5. Computational Complexity (1) Software cost factors. Software cost factors (cont d) Algorithm and Computational Complexity

UNIT 5B Binary Search

Java How to Program, 9/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Measuring algorithm efficiency

Today s Outline. CSE 326: Data Structures Asymptotic Analysis. Analyzing Algorithms. Analyzing Algorithms: Why Bother? Hannah Takes a Break

CS171:Introduction to Computer Science II. Algorithm Analysis. Li Xiong

Chapter 3: The Efficiency of Algorithms

Recall from Last Time: Big-Oh Notation

Programming II (CS300)

EE 368. Week 6 (Notes)

Efficiency and Recursion. We determine the efficiency of an algorithm by seeing by how much the algorithm s runtime varies with the problem size.

CSE 373 MAY 24 TH ANALYSIS AND NON- COMPARISON SORTING

Transcription:

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 <= n; i++) { sum += i; return sum; Gauss also has a way of solving this public static int sum2(int n) { return n * (n + 1) / 2; Which one is more efficient? 3

Runtime Efficiency (13.2) efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc. most commonly refers to run time We want to be able to compare different algorithms to see which is more efficient 4

Efficiency Try 1 Let s time the methods! n = 1 sum1 took 0ms, sum2 took 0ms n = 5 sum1 took 0ms, sum2 took 0ms n = 10 sum1 took 0ms, sum2 took 0ms n = 100 sum1 took 0ms, sum2 took 0ms n = 1,000 sum1 took 0ms, 1ms, sum2 took 0ms n = 10,000,000 sum1 took 10ms, 8ms, 3ms, sum2 took 0ms n = 100,000,000 sum1 took 121ms, 47ms, 43ms, sum2 took 0ms n = 2,147,483,647 sum1 took1570ms, 784ms, 804ms, sum2 took 0ms Downsides Different computers give different run times The same computer gives different results!!! D:< 5

Efficiency Try 2 Let s count number of steps our algorithm takes to run Assume the following: Any single Java statement takes same amount of time to run. int x = 5; boolean b = (5 + 1 * 2) < 15 + 3; System.out.println( Hello ); A loop's runtime, if the loop repeats N times, is N times the runtime of the statements in its body. A method call's runtime is measured by the total runtime of the statements inside the method's body. 6

Efficiency examples statement1; statement2; statement3; 3 for (int i = 1; i <= N; i++) { statement4; for (int i = 1; i <= N; i++) { statement5; statement6; statement7; N 3N 4N + 3 7

Efficiency examples 2 for (int i = 1; i <= N; i++) { for (int j = 1; j <= N; j++) { statement1; for (int i = 1; i <= N; i++) { statement2; statement3; statement4; statement5; N 2 4N N 2 + 4N How many statements will execute if N = 10? If N = 1000? 8

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 <= n; i++) { sum += i; return sum; 1 1 N N + 2 Gauss also has a way of solving this public static int sum2(int n) { return n * (n + 1) / 2; 1 1 Which one is more efficient? 9

Visualizing Difference 10

Algorithm growth rates (13.2) We measure runtime in proportion to the input data size, N. growth rate: Change in runtime as N changes. Say an algorithm runs 0.4N 3 + 25N 2 + 8N + 17 statements. Consider the runtime when N is extremely large. We ignore constants like 25 because they are tiny next to N. The highest-order term (N 3 ) dominates the overall runtime. We say that this algorithm runs "on the order of" N 3. or O(N 3 ) for short ("Big-Oh of N cubed") 11

Complexity classes complexity class: A category of algorithm efficiency based on the algorithm's relationship to the input size N. Class Big-Oh If you double N,... Example constant O(1) unchanged 10ms logarithmic O(log2 N) increases slightly 175ms linear O(N) doubles 3.2 sec log-linear O(N log2 N) slightly more than doubles 6 sec quadratic O(N 2 ) quadruples 1 min 42 sec cubic O(N 3 ) multiplies by 8 55 min............ exponential O(2 N ) multiplies drastically 5 * 10 61 years 12

Complexity classes http://recursive-design.com/blog/2010/12/07/comp-sci-101-big-o-notation/ - post about a Google interview 13

Sequential search sequential search: Locates a target value in an array / list by examining each element from start to finish. Used in indexof. How many elements will it need to examine? Example: Searching the array below for the value 42: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103 i The array is sorted. Could we take advantage of this? 21

Binary search (13.1) binary search: Locates a target value in a sorted array or list by successively eliminating half of the array from consideration. How many elements will it need to examine? Example: Searching the array below for the value 42: index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103 min mid max 22

Sequential search What is its complexity class? public int indexof(int value) { for (int i = 0; i < size; i++) { if (elementdata[i] == value) { return i; return -1; // not found N index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103 On average, "only" N/2 elements are visited 1/2 is a constant that can be ignored 25

Binary search binary search successively eliminates half of the elements. Algorithm: Examine the middle element of the array. If it is too big, eliminate the right half of the array and repeat. If it is too small, eliminate the left half of the array and repeat. Else it is the value we're searching for, so stop. Which indexes does the algorithm examine to find value 42? What is the runtime complexity class of binary search? index 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 2 7 10 15 20 22 25 30 36 42 50 56 68 85 92 103 min mid max 26

Binary search runtime For an array of size N, it eliminates ½ until 1 element remains. N, N/2, N/4, N/8,..., 4, 2, 1 How many divisions does it take? Think of it from the other direction: How many times do I have to multiply by 2 to reach N? 1, 2, 4, 8,..., N/4, N/2, N Call this number of multiplications "x". 2 x = N x = log2 N Binary search is in the logarithmic complexity class. 27

Collection efficiency Efficiency of our Java's ArrayList and LinkedList methods: Method ArrayList LinkedList add O(1)* O(1)** add(index, value) O(N) O(N) indexof O(N) O(N) get O(1) O(N) remove O(N) O(N) set O(1) O(N) size O(1) O(1)*** * Most of the time! ** Assuming we have a reference to the back of the list *** Assuming we have a size field 28