Algorithm Efficiency and Big-O

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

Algorithms A Look At Efficiency

Algorithm Analysis. CENG 707 Data Structures and Algorithms

Big O & ArrayList Fall 2018 Margaret Reid-Miller

CSCI 261 Computer Science II

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

Introduction to Analysis of Algorithms

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

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

Introduction to the Analysis of Algorithms. Algorithm

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

Analysis of Algorithms

UNIT 1 ANALYSIS OF ALGORITHMS

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

Algorithmic Analysis. Go go Big O(h)!

Linked List. ape hen dog cat fox. tail. head. count 5

Lecture 5: Running Time Evaluation

University of Petra Faculty of Information Technology

Chapter 2: Complexity Analysis

What is an algorithm?

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

Computational Complexity: Measuring the Efficiency of Algorithms

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Measuring algorithm efficiency

CSCA48 Winter 2018 Week 10:Algorithm Analysis. Marzieh Ahmadzadeh, Nick Cheng University of Toronto Scarborough

Data Structure Lecture#5: Algorithm Analysis (Chapter 3) U Kang Seoul National University

Arrays.

What is an Algorithm?

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

Algorithm Analysis. Big Oh

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

Remember, to manage anything, we need to measure it. So, how do we compare two implementations? ArrayBag vs. LinkedBag Which is more efficient?

Topic Number 2 Efficiency Complexity Algorithm Analysis

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

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

Introduction to Computers & Programming

Computer Science. Advanced Computer Science Topics Mike Scott Contest Director. For all coaches and contestants. CS Intro and Update

Analysis of Algorithms. CSE Data Structures April 10, 2002

Lecture 2: Algorithm Analysis

INTRODUCTION TO DATA AND PROCEDURE

CS240 Fall Mike Lam, Professor. Algorithm Analysis

ANALYSIS OF ALGORITHMS

Choice of C++ as Language

LECTURE 9 Data Structures: A systematic way of organizing and accessing data. --No single data structure works well for ALL purposes.

Analysis of Algorithm. Chapter 2

Recursion (Part 3) 1

Programming II (CS300)

Recursion. COMS W1007 Introduction to Computer Science. Christopher Conway 26 June 2003

Outline and Reading. Analysis of Algorithms 1

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?

Algorithmic Complexity

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

Algorithm Analysis. Performance Factors

Plotting run-time graphically. Plotting run-time graphically. CS241 Algorithmics - week 1 review. Prefix Averages - Algorithm #1

Elementary maths for GMT. Algorithm analysis Part II

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

Computational thinking, problem-solving and programming:

Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4) Dr. Yingwu Zhu

Implementing a List in Java. CSE 143 Java. Just an Illusion? List Interface (review) Using an Array to Implement a List.

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

UNIT-1. Chapter 1(Introduction and overview) 1. Asymptotic Notations 2. One Dimensional array 3. Multi Dimensional array 4. Pointer arrays.

CSC 1052 Algorithms & Data Structures II: Lists

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Lecture 3 Linear Data Structures: Arrays, Array Lists, Stacks, Queues and Linked Lists

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

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

QUIZ 2 Introduction to Computer Science (COMP 250) Mon. March 2, 2009 Professor Michael Langer

COMP 250. Lecture 8. stack. Sept. 25, 2017

Implementing a List in Java. CSE 143 Java. List Interface (review) Just an Illusion? Using an Array to Implement a List.

The Running Time of Programs

CSCI 136 Data Structures & Advanced Programming. Lecture 7 Spring 2018 Bill and Jon

The Complexity of Algorithms (3A) Young Won Lim 4/3/18

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

9/10/2018 Algorithms & Data Structures Analysis of Algorithms. Siyuan Jiang, Sept

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

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

ECE 2400 Computer Systems Programming Fall 2018 Topic 8: Complexity Analysis

Analysis of Algorithms. CS 1037a Topic 13

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

JAVA COLLECTION FRAMEWORK & SETS

Lecture 3 Linear Data Structures

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Computer Science Approach to problem solving

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

Sorting. Bubble Sort. Selection Sort

Java Review: Objects

Analysis of Algorithms Part I: Analyzing a pseudo-code

Data & Procedure Introduction

Same idea, different implementation. Is it important?

Array Based Lists. Collections

3. Java - Language Constructs I

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

Algorithm Performance. (the Big-O)

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

Recall from Last Time: Big-Oh Notation

Recursion. Example R1

Introduction to Object-Oriented Programming

Programming II (CS300)

Transcription:

+ Algorithm Efficiency and More List implementations + Algorithm Efficiency and Big-O Section 2.4 1

+ Algorithm Efficiency and Big-O n Getting a precise measure of the performance of an algorithm is difficult n Big-O notation expresses the performance of an algorithm as a function of the number of items to be processed n This permits algorithms to be compared for efficiency n For more than a certain number of data items, some problems cannot be solved by any computer + Linear Growth Rate If processing time increases in proportion to the number of inputs n, the algorithm grows at a linear rate public static int search(int[] x, int target) { for(int i=0; i < x.length; i++) { if (x[i]==target) return i; return -1; // target not found 2

+ Linear Growth Rate If the target is not present, the for loop will execute x.length times If the target is present the for loop will execute (on average) (x.length + 1)/2 times If processing time increases Therefore, in proportion the total execution to the number time is of inputs n, the algorithm grows at directly a linear proportional rate to x.length This is described as a growth rate of order n OR O(n) public static int search(int[] x, int target) { for(int i=0; i < x.length; i++) { if (x[i]==target) return i; return -1; // target not found + n x m Growth Rate n Processing time can be dependent on two different inputs public static boolean aredifferent(int[] x, int[] y) { for(int i=0; i < x.length; i++) { if (search(y, x[i]) = -1) return false; return true; 3

+ n x m Growth Rate (cont.) The for loop will execute x.length times But it will call search, which will execute n Processing time can be dependent y.length times on two different inputs. The total execution time is proportional to (x.length * y.length) The growth rate has an order of n x m or O(n x m) public static boolean aredifferent(int[] x, int[] y) { for(int i=0; i < x.length; i++) { if (search(y, x[i]) = -1) return false; return true; + Quadratic Growth Rate If processing time is proportional to the square of the number of inputs n, the algorithm grows at a quadratic rate (n 2 ) public static boolean areunique(int[] x) { for(int i=0; i < x.length; i++) { for(int j=0; j < x.length; j++) { if (i = j && x[i] == x[j]) return false; return true; 4

+ Quadratic Growth Rate (cont.) The for loop with i as index will execute x.length times The for loop with j as index will execute x.length times If processing time is proportional The total to number the square of times of the the number inner loop of will inputs n, the algorithm grows at a quadratic rate execute is (x.length) 2 The growth rate has an order of n 2 or O(n 2 ) public static boolean areunique(int[] x) { for(int i=0; i < x.length; i++) { for(int j=0; j < x.length; j++) { if (i = j && x[i] == x[j]) return false; return true; + Big-O Notation The O() in the previous examples can be thought of as an abbreviation of "order of magnitude" A simple way to determine the big-o notation of an algorithm is to look at the loops and to see whether the loops are nested Assuming a loop body consists only of simple statements, a single loop is O(n) a pair of nested loops is O(n 2 ) a nested pair of loops inside another is O(n 3 ) and so on... 5

+ Big-O Notation (cont.) You must also examine the number of times a loop is executed for(i=1; i < x.length; i *= 2) { // Do something with x[i] The loop body will execute k-1 times, with i having the following values: 1, 2, 4, 8, 16,..., 2 k until 2 k is greater than x.length Since 2 k-1 = x.length < 2 k and log 2 2 k is k, we know that k-1 = log 2 (x.length) < k Thus we say the loop is O(log n) (in analyzing algorithms, we use logarithms to the base 2) Logarithmic functions grow slowly as the number of data items n increases + Formal Definition of Big-O Consider the following program structure: for (int j = 0; j < n; j++) { Simple Statement Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 Simple Statement 6 Simple Statement 7... Simple Statement 30 6

+ Formal Definition of Big-O (cont.) Consider the following program structure: for (int j = 0; j < n; j++) { Simple Statement Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 Simple Statement 6 Simple Statement 7... Simple Statement 30 This nested loop executes a Simple Statement n 2 times + Formal Definition of Big-O (cont.) Consider the following program structure: for (int j = 0; j < n; j++) { Simple Statement Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 Simple Statement 6 Simple Statement 7... Simple Statement 30 This loop executes 5 Simple Statements n times (5n) 7

+ Formal Definition of Big-O (cont.) Consider the following program structure: for (int j = 0; j < n; j++) { Simple Statement Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 Simple Statement 6 Simple Statement 7... Simple Statement 30 Finally, 25 Simple Statements are executed + Formal Definition of Big-O (cont.) Consider the following program structure: for (int j = 0; j < n; j++) { Simple Statement Simple Statement 1 Simple Statement 2 Simple Statement 3 Simple Statement 4 Simple Statement 5 Simple Statement 6 Simple Statement 7... Simple Statement 30 We can conclude that the relationship between processing time and n (the number of date items processed) is: T(n) = n 2 + 5n + 25 8

+ Formal Definition of Big-O (cont.) In terms of T(n), There exist T(n) = O(f(n)) two constants, n 0 and c, greater than zero, and a function, f(n), such that for all n > n 0, cf(n) = T(n) In other words, as n gets sufficiently large (larger than n 0 ), there is some constant c for which the processing time will always be less than or equal to cf(n) cf(n) is an upper bound on performance + Formal Definition of Big-O (cont.) The growth rate of f(n) will be determined by the fastest growing term, which is the one with the largest exponent In the example, an algorithm of is more simply expressed as O(n 2 + 5n + 25) O(n 2 ) In general, it is safe to ignore all constants and to drop the lowerorder terms when determining the order of magnitude 9

+ Big-O Example 1 n Given T(n) = n 2 + 5n + 25, show that this is O(n 2 ) n Find constants n 0 and c so that, for all n > n 0, cn 2 > n 2 + 5n + 25 n Find the point where cn 2 = n 2 + 5n + 25 n Let n = n 0, and solve for c c = 1 + 5/ n 0, + 25/ n 2 0 n When n 0 is 5(1 + 5/5 + 25/25), c is 3 n So, 3n 2 > n 2 + 5n + 25 for all n > 5 n Other values of n 0 and c also work + Big-O Example 1 (cont.) 10

+ Big-O Example 2 n Programming problem 1 shows y1 = 100 * n + 10; y2 = 5 * n * n + 2; n It asks to write a program that compares y1 and y2 for values of n up to 100 in increments of 10 n Before writing the code, at what value of n will y2 consistently be greater than y1? n How does this relate to the problem: n T(n) = 5 * n * n + 2 + 100 * n + 10 n for what values of n0 and c n*n consistently larger than T(n) + Big-O Example 3 n Consider the following loop for (int j = i + 1; j < n; j++) { 3 simple statements n T(n) = 3(n 1) + 3 (n 2) + + 3 n Factoring out the 3, 3(n 1 + n 2 + n 3 + + 1) n 1 + 2 + + n 1 = (n x (n-1))/2 11

+ Big-O Example 3 (cont.) n Therefore T(n) = 1.5n 2 1.5n n When n = 0, the polynomial has the value 0 n For values of n > 1, 1.5n 2 > 1.5n 2 1.5n n Therefore T(n) is O(n 2 ) when n 0 is 1 and c is 1.5 + Big-O Example 2 (cont.) 12

+ Symbols Used in Quantifying Performance + Common Growth Rates 13

+ Different Growth Rates + Effects of Different Growth Rates 14

+ Algorithms with Exponential and Factorial Growth Rates n Algorithms with exponential and factorial growth rates have an effective practical limit on the size of the problem they can be used to solve n With an O(2 n ) algorithm, if 100 inputs takes an hour then, n 101 inputs will take 2 hours n 105 inputs will take 32 hours n 114 inputs will take 16,384 hours (almost 2 years) + Algorithms with Exponential and Factorial Growth Rates (cont.) n Encryption algorithms take advantage of this characteristic n Some cryptographic algorithms can be broken in O(2 n ) time, where n is the number of bits in the key n A key length of 40 is considered breakable by a modern computer, n but a key length of 100 bits will take a billion-billion (10 18 ) times longer than a key length of 40 15

+ ArrayList implementation n Version One: Fixed array n Version Two: Making it Generic n Version Three: Dynamic Array + Simplified List Interface (ADT) public interface SimplifiedList { public boolean add(object item); public boolean add(int index, Object item); public Object remove(int index); public Object set(int index, Object item); public Object get(int index); public boolean contains (Object item); public boolean isempty(); public void clear(); public int size(); public boolean isfull(); // interface SimplifiedList 16

+ Implementing SimplifiedList n Data Structure#1: fixed size array (capacity) n Use an array of Object to store stuff n A variable to store # entries in it n Its capacity (max size) + Version 2: Making it Generic n Change the interface and the class 17

+ Simplified List Interface (ADT) <Generic> public interface SimplifiedList<E> { public boolean add(e item); public boolean add(int index, E item); public E remove(int index); public E set(int index, E item); public E get(int index); public boolean contains (E item); public boolean isempty(); public void clear(); public int size(); public boolean isfull(); // interface SimplifiedList + Version 3: Using flexible arrays n add a method called void reallocate() that n is called when adding to an array that is at capacity n doubles the size of the array copying the current values to the new values. private void reallocate() { capacity = 2 * capacity; thedata = Arrays.copyOf(theData, capacity); 18

+ Performance of ArrayLists Fixed Size Array Dynamic Array add(o) O(1) O(n) add(i, o) O(n) O(n) remove(i) O(n) O(n) set(i, o) O(1) O(1) get(i) O(1) O(1) contains(o) O(n) O(n) clear(), size(), isempty() O(1) O(1) 19