Algorithm Analysis. Performance Factors

Similar documents
PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

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

Algorithmic Analysis. Go go Big O(h)!

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

Introduction to the Analysis of Algorithms. Algorithm

Algorithm Analysis. Big Oh

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

ASYMPTOTIC COMPLEXITY

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

Searching & Sorting in Java Bubble Sort

Algorithms A Look At Efficiency

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

SEARCHING AND SORTING HINT AT ASYMPTOTIC COMPLEXITY

Principles of Algorithm Analysis. Biostatistics 615/815

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?

ASYMPTOTIC COMPLEXITY

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

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

Recitation 9. Prelim Review

We will use the following code as an example throughout the next two topics:

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

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

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

UNIT 1 ANALYSIS OF ALGORITHMS

Computer Science 1000: Part #2. Algorithms

Computer Science 4U Unit 1. Programming Concepts and Skills Algorithms

Sorting. Task Description. Selection Sort. Should we worry about speed?

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

Complexity, General. Standard approach: count the number of primitive operations executed.

Algorithm Analysis. CENG 707 Data Structures and Algorithms

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

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

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

for (int outercounter = nums.length - 1; outercounter > 0 && swappedthatturn; outercounter --

Measuring algorithm efficiency

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

Sorting. Weiss chapter , 8.6

Lecture 5 Sorting Arrays

Analysis of Algorithms. CS 1037a Topic 13

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Chapter 10 - Notes Applications of Arrays

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

Arrays.

12/1/2016. Sorting. Savitch Chapter 7.4. Why sort. Easier to search (binary search) Sorting used as a step in many algorithms

csci 210: Data Structures Program Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis

What is an algorithm?

SEARCHING, SORTING, AND ASYMPTOTIC COMPLEXITY. Lecture 11 CS2110 Spring 2016

1 5,9,2,7,6,10,4,3,8,1 The first number (5) is automatically the first number of the sorted list

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

DESIGN AND ANALYSIS OF ALGORITHMS. Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Data Structures and Algorithms

Classic Data Structures Introduction UNIT I

Lecture 2: Algorithm Analysis

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Asymptotic Analysis Spring 2018 Discussion 7: February 27, 2018

O(n): printing a list of n items to the screen, looking at each item once.

CS S-02 Algorithm Analysis 1

Walls & Mirrors Chapter 9. Algorithm Efficiency and Sorting

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

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Searching, Sorting. Arizona State University 1

ECE 122. Engineering Problem Solving Using Java

Computers in Engineering COMP 208. Where s Waldo? Linear Search. Searching and Sorting Michael A. Hawker

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

Computational biology course IST 2015/2016

Chapter Fourteen Bonus Lessons: Algorithms and Efficiency

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

Chapter 2: Complexity Analysis

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

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

Sorting (Weiss chapter )

Introduction to Computer Science Midterm 3 Fall, Points

The University Of Michigan. EECS402 Lecture 07. Andrew M. Morgan. Sorting Arrays. Element Order Of Arrays

Introduction to Computer Science

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

Choice of C++ as Language

SORTING AND SEARCHING

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

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

Introduction to Computer Science

CSE 373: Data Structures and Algorithms

Algorithm Analysis and Design

Data Structures and Algorithms Chapter 2

CS 12 Fall 2003 Solutions for mid-term exam #2

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

The Limits of Sorting Divide-and-Conquer Comparison Sorts II

Complexity of Algorithms. Andreas Klappenecker

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

Recursion. Example R1

CS 106 Introduction to Computer Science I

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

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

Lecture 5: Running Time Evaluation

public static <E extends Comparable<? super E>>void BubbleSort(E[] array )

Programming II (CS300)

Searching and Sorting

Lecture 6 Sorting and Searching

Transcription:

Algorithm Analysis How can we demonstrate that one algorithm is superior to another without being misled by any of the following problems: Special cases Every algorithm has certain inputs that allow it to perform far better than would be expected. Small data sets An algorithm may not display its inefficiencies if the data set on which it is run is too small. Hardware differences Software differences 9/10/2003 Algorithm Analysis 1 Performance Factors There are many factors that influence the performance of an algorithm Number of copies/comparisons made Number of statements executed Varying implementations on different machines Variations in the input set Most often we will estimate the performance of an algorithm by analyzing the algorithm 9/10/2003 Algorithm Analysis 2 1

Measuring Efficiency The efficiency of an algorithm is a measure of the amount of resources consumed in solving a problem of size n. The resource we are most interested in is time We can use the same techniques we will present to analyze the consumption of other resources, such as memory space. It would seem that the most obvious way to measure the efficiency of an algorithm is to run it and measure how much processor time is needed 9/10/2003 Algorithm Analysis 3 Asymptotic Analysis The time it takes to solve a problem is usually an increasing function of the size of the problem The bigger the problem the longer it will take to solve. We need a formula that associates n, the problem size, with t, the time required to obtain a solution. We can get this type of information using asymptotic analysis to develop expressions of the form: t O[f(n)] 9/10/2003 Algorithm Analysis 4 2

Formal Definition t O(f(n)) if there are constants c and n 0 such that t cf(n) when n n 0 Note that the constant is not part of the function used to describe the performance of the algorithm (i.e. there is no O(2n), only O(n)) Big O gives the worst case performance, the average performance of the algorithm might be better. 9/10/2003 Algorithm Analysis 5 Big Oh If the order of the algorithm were, for example, O(n 3 ), then the relationships between t and n would be given by a formula of the form t <= M n 3 Although we often do not know anything about the value of the constant M, we can say that If the size of the problem doubles, the total time needed to solve the problem will increase about eightfold If the problem size triples, the overall running time will be about 27 times greater. 9/10/2003 Algorithm Analysis 6 3

Performance Classification f(n) 1 log n n n log n n 2 n 3 Classification Constant: run time is fixed, and does not depend upon n. Most instructions are executed once, or only a few times, regardless of the amount of information being processed Logarithmic: when n increases, so does run time, but much slower. When n doubles, log n increases by a constant, but does not double until n increases to n 2. Common in programs which solve large problems by transforming them into smaller problems. Linear: run time varies directly with n. Typically, a small amount of processing is done on each element. When n doubles, run time slightly more than doubles. Common in programs which break a problem down into smaller sub-problems, solves them independently, then combines solutions Quadratic: when n doubles, runtime increases fourfold. Practical only for small problems; typically the program processes all pairs of input (e.g. in a double nested loop). Cubic: when n doubles, runtime increases eightfold 2 n Exponential: when n doubles, run time squares. This is often the result of a natural, brute force solution. 9/10/2003 Algorithm Analysis 7 How To Use It For reasonably large problems we always want to select an algorithm of the lowest order possible. If algorithm A is O[f(n)] and algorithm B is O[g(n)], then algorithm A is said to be of a lower order than B if f(n) < g(n) for all n greater than some constant k. So given a choice of an algorithm that is O(n 2 ) and an algorithm than O(n 3 ) we would go for the O(n 2 ) algorithm. 9/10/2003 Algorithm Analysis 8 4

Algorithm Efficiencies 9/10/2003 Algorithm Analysis 9 The Critical Section When analyzing an algorithm, we do not care about the behavior of each statement We focus our analysis on the part of the algorithm where the greatest amount of its time is spent A critical section has the following characteristics: It is an operation central to the functioning of the algorithm, and its behavior typifies the overall behavior of the algorithm It is contained inside the most deeply nested loops of the algorithm and is executed as often as any other section of the algorithm. 9/10/2003 Algorithm Analysis 10 5

The Critical Section The critical section can be said to be at the "heart" of the algorithm We can characterize the overall efficiency of an algorithm by counting how many times this critical section is executed as a function of the problem size The critical section dominates the completion time If an algorithm is divided into two parts, the first taking O(f(n)) followed by a second that takes O(g(n)), then the overall complexity of the algorithm is O(max[f(n), g(n)]). The slowest and most time-consuming section determines the overall efficiency of the algorithm. 9/10/2003 Algorithm Analysis 11 Analogy Imagine that you have the job of delivering a gift to someone living in another city thousands of miles away The job involves the following three steps: wrapping the gift driving 2,000 miles to the destination city delivering the package to the proper person The critical section obviously step 2. Changing step 2 to flying instead of driving would have a profound impact of the completion time of the task. 9/10/2003 Algorithm Analysis 12 6

Sequential Search public static int search( int array[], int target ) { // Assume the target is not in the array int position = -1; O(n) where n is the length of the array Linear // Step through the array until the target is found // or the entire array has been searched. for ( int i=0; i < array.length && position == -1; i=i+1 ) { // Is the current element what we are looking for? if ( array[ i ] == target ) { position = i; // Return the element's position return position; 9/10/2003 Algorithm Analysis 13 Binary Search public static int search( int array[], int target ) { int start = 0; // The start of the search region int end = array.length; // The end of the search region int position = -1; // Position of the target O(log 2 n) where n is the length of the array Logarithmic // While there's something to search and we have not found it while ( start <= end && position == -1 ) { int middle = (start + end) / 2; // Determine where the target should be if ( target < array[ middle ] ) { end = middle 1; // Smaller must be in left half else if ( target > array[ middle ] ) { start = middle + 1; // Larger must be in right half else { position = middle; // Found it!! return position; 9/10/2003 Algorithm Analysis 14 7

Selection Sort public static void selectionsort( int array[] ) { // Find the smallest element and move it to the top // array.length 1 times. The result is a sorted array for ( int i = 0; i < array.length - 1; i = i + 1 ) { for ( int j = i + 1; j < array.length; j = j + 1 ) { if ( array[ j ] < array[ i ] ) { // Elements out of order -- swap int tmp = array[ i ]; array[ i ] = array[ j ]; array[ j ] = tmp; O(n 2 ) where n is the length of the array Quadractic 9/10/2003 Algorithm Analysis 15 Bubble Sort public static void bubblesort( int array[] ) { int i = 0; // How many elements are sorted - initially none boolean swap; // Was a swap made during this pass? O(n 2 ) where n is the length of the array Quadractic // Keep making passes through the array until it is sorted do { swap = false; // Swap adjacent elements that are out of order for ( int j = 0; j < array.length - i - 1; j++ ) { // If the two elements are out of order - swap them if ( array[ j ] > array[ j + 1 ] ) { int tmp = array[ j ]; array[ j ] = array[ j + 1 ]; array[ j + 1 ] = tmp; swap = true; // Made a swap array might not be sorted i = i + 1; // One more element in the correct place while ( swap ); 9/10/2003 Algorithm Analysis 16 8

Insertion Sort O(n 2 ) where n is the length of the array Quadractic public static void insertionsort( int array[] ) { // i marks the beginning of the unsorted array for ( int i = 1; i < nums.length; i = i + 1 ) { // Insert the first element in the unsorted portion of // the array into the sorted portion. Works from the // bottom of the sorted portion to the top for ( int j=i-1; j>=0 && nums[j+1] < nums[j]; j=j+1 ) { // Since we have not found the correct position for the // new element - move the elements in the sorted // portion down one int tmp = nums[ j ]; nums[ j ] = nums[ j + 1 ]; nums[ j + 1 ] = tmp; 9/10/2003 Algorithm Analysis 17 9