ANALYSIS OF ALGORITHMS

Similar documents
Data Structures and Algorithms

Analysis of Algorithms

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

Elementary maths for GMT. Algorithm analysis Part I

Algorithm Analysis. Applied Algorithmics COMP526. Algorithm Analysis. Algorithm Analysis via experiments

Outline and Reading. Analysis of Algorithms 1

Data Structures Lecture 8

Choice of C++ as Language

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

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

(Refer Slide Time: 1:27)

Algorithms and Data Structures

Algorithm Analysis. October 12, CMPE 250 Algorithm Analysis October 12, / 66

L6,7: Time & Space Complexity

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

Data Structures and Algorithms

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Asymptotic Analysis of Algorithms

Analysis of Algorithms

Agenda. The worst algorithm in the history of humanity. Asymptotic notations: Big-O, Big-Omega, Theta. An iterative solution

Data Structures and Algorithms. Part 2

Analysis of Algorithm. Chapter 2

Algorithm Analysis. Gunnar Gotshalks. AlgAnalysis 1

Lecture 5: Running Time Evaluation

Algorithm Analysis. (Algorithm Analysis ) Data Structures and Programming Spring / 48

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

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

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

CSc 225 Algorithms and Data Structures I Case Studies

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

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: April 1, 2015

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

Data structure and algorithm in Python

Algorithm. Algorithm Analysis. Algorithm. Algorithm. Analyzing Sorting Algorithms (Insertion Sort) Analyzing Algorithms 8/31/2017

Lecture 2: Algorithm Analysis

Data Structures and Algorithms Chapter 2

csci 210: Data Structures Program Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis

MergeSort, Recurrences, Asymptotic Analysis Scribe: Michael P. Kim Date: September 28, 2016 Edited by Ofir Geri

Assignment 1 (concept): Solutions

Introduction to the Analysis of Algorithms. Algorithm

1 Introduction. 2 InsertionSort. 2.1 Correctness of InsertionSort

Running Time. Analysis of Algorithms. Experimental Studies. Limitations of Experiments

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

CS302 Topic: Algorithm Analysis. Thursday, Sept. 22, 2005

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

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

CS240 Fall Mike Lam, Professor. Algorithm Analysis

0.1 Welcome. 0.2 Insertion sort. Jessica Su (some portions copied from CLRS)

Introduction to Data Structure

INTRODUCTION. Analysis: Determination of time and space requirements of the algorithm

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

Running Time ( 3.1) Analysis of Algorithms. Experimental Studies. Limitations of Experiments

Analysis of Algorithms

Algorithm Analysis. Algorithm Efficiency Best, Worst, Average Cases Asymptotic Analysis (Big Oh) Space Complexity

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

Chapter 2: Complexity Analysis

UNIT 1 ANALYSIS OF ALGORITHMS

Introduction to Computers & Programming

Introduction to Computer Science

Scientific Computing. Algorithm Analysis

EECS 477: Introduction to algorithms. Lecture 6

CS:3330 (22c:31) Algorithms

Programming II (CS300)

Algorithms and Data Structures

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

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

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

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

Data Structures and Algorithms. Analysis of Algorithms

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

University of Petra Faculty of Information Technology

Overview. CSE 101: Design and Analysis of Algorithms Lecture 1

Introduction to Computer Science

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

Analysis of Algorithms. CSE Data Structures April 10, 2002

INTRODUCTION. Objective: - Algorithms - Techniques - Analysis. Algorithms:

Complexity Analysis of an Algorithm

Organisation. Assessment

ASYMPTOTIC COMPLEXITY

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

Algorithms A Look At Efficiency

CSc 225 Algorithms and Data Structures I Algorithm Analysis

Complexity of Algorithms. Andreas Klappenecker

CSE 373: Data Structures and Algorithms

CS S-02 Algorithm Analysis 1

Recursion (Part 3) 1

The growth of functions. (Chapter 3)

Data Structures, Algorithms & Data Science Platforms

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

Sankalchand Patel College of Engineering - Visnagar Department of Computer Engineering and Information Technology. Assignment

Outline and Reading. Analysis of Algorithms. Running Time. Experimental Studies. Limitations of Experiments. Theoretical Analysis

Cpt S 223 Course Overview. Cpt S 223, Fall 2007 Copyright: Washington State University

Algorithmic Analysis. Go go Big O(h)!

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

COMP Analysis of Algorithms & Data Structures

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

Complexity of Algorithms

Recall from Last Time: Big-Oh Notation

Transcription:

ANALYSIS OF ALGORITHMS Running Time Pseudo-Code Asymptotic Notation Asymptotic Analysis Mathematical facts T(n) n = 4 Input Algorithm Output 1

Average Case vs. Worst Case Running Time of an Algorithm An algorithm may run faster on certain data sets than on others. Finding the average case can be very difficult, so typically algorithms are measured by the worst-case time complexity. Also, in certain application domains (e.g., air traffic control, surgery, IP lookup) knowing the worst-case time complexity is of crucial importance. 5 ms worst-case Time 4 ms 3 ms 2 ms } average-case? best-case 1 ms A B C D E F G Input 2

Measuring the Running Time How should we measure the running time of an algorithm? Experimental Study - Write a program that implements the algorithm - Run the program with data sets of varying size and composition. - Use a method like System.currentTimeMillis() to get an accurate measure of the actual running time. - The resulting data set should look something like: t (ms) 60 50 40 30 20 10 0 50 100 n 3

Beyond Experimental Studies Experimental studies have several limitations: - It is necessary to implement and test the algorithm in order to determine its running time. - Experiments can be done only on a limited set of inputs, and may not be indicative of the running time on other inputs not included in the experiment. - In order to compare two algorithms, the same hardware and software environments should be used. We will now develop a general methodology for analyzing the running time of algorithms that - Uses a high-level description of the algorithm instead of testing one of its implementations. - Takes into account all possible inputs. - Allows one to evaluate the efficiency of any algorithm in a way that is independent from the hardware and software environment. 4

Pseudo-Code Pseudo-code is a description of an algorithm that is more structured than usual prose but less formal than a programming language. Example: finding the maximum element of an array. Algorithm arraymax(a, n): Input: An array A storing n integers. Output: The maximum element in A. currentmax A[0] for i 1 to n 1 do if currentmax < A[i] then currentmax A[i] return currentmax Pseudo-code is our preferred notation for describing algorithms. However, pseudo-code hides program design issues. 5

What is Pseudo-Code? A mixture of natural language and high-level programming concepts that describes the main ideas behind a generic implementation of a data structure or algorithm. - Expressions: use standard mathematical symbols to describe numeric and boolean expressions - use for assignment ( = in Java) - use = for the equality relationship ( == in Java) - Method Declarations: - Algorithm name(param1, param2) - Programming Constructs: - decision structures: if... then... [else... ] - while-loops: while... do - repeat-loops: repeat... until... - for-loop: for... do - array indexing: A[i] - Methods: - calls: object method(args) - returns: return value 6

Primitive Operations: Low-level computations that are largely independent from the programming language and can be identified in pseudocode, e.g: - calling a method and returning from a method - performing an arithmetic operation (e.g. addition) - comparing two numbers, etc. By inspecting the pseudo-code, we can count the number of primitive operations executed by an algorithm. Example: Algorithm arraymax(a, n): Input: An array A storing n integers. Output: The maximum element in A. currentmax A[0] for i 1 to n 1 do if currentmax < A[i] then currentmax A[i] return currentmax 7

Asymptotic Notation Goal: to simplify analysis by getting rid of unneeded information - like rounding 1,000,001 1,000,000 - we want to say in a formal way 3n 2 n 2 The Big-Oh Notation given functions f(n) and g(n), we say that f(n) is O(g(n)) if and only if there are positive constants c and n 0 such that f(n) cg(n) for n n 0 f(n) = 2n + 6 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 c g(n) = 4n g(n) = n 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 n 8

n 2 is not O(n) Another Example we cannot find c and n 0 such that n 2 cn for n n 0 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 f(n) = n 2 c g(n) g(n) = n 2 0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 n 9

Asymptotic Notation (cont.) Note: Even though it is correct to say 7n - 3 is O(n 3 ), a better statement is 7n - 3 is O(n), that is, one should make the approximation as tight as possible Simple Rule: Drop lower order terms and constant factors. -7n - 3 is O(n) -8n 2 log n + 5n 2 + n is O(n 2 log n) Special classes of algorithms: - logarithmic: O(log n) - linear O(n) - quadratic O(n 2 ) - polynomial O(n k ), k 1 - exponential O(a n ), n > 1 Relatives of the Big-Oh Ω(f(n)): Big Omega Θ(f(n)): Big Theta 10

Asymptotic Analysis of The Running Time Use the Big-Oh notation to express the number of primitive operations executed as a function of the input size. For example, we say that the arraymax algorithm runs in O(n) time. Comparing the asymptotic running time - an algorithm that runs in O(n) time is better than one that runs in O(n 2 ) time - similarly, O(log n) is better than O(n) - hierarchy of functions: - log n << n << n 2 << n 3 << 2 n Caution! - Beware of very large constant factors. An algorithm running in time 1,000,000 n is still O(n) but might be less efficient on your data set than one running in time 2n 2, which is O(n 2 ) 11

Example of Asymptotic Analysis An algorithm for computing prefix averages Algorithm prefixaverages1(x): Input: An n-element array X of numbers. Output: An n-element array A of numbers such that A[i] is the average of elements X[0],..., X[i]. Let A be an array of n numbers. for i 0 to n - 1 do a 0 for j 0 to i do a a + X[j] A[i] a/(i + 1) return array A Analysis... 12

A Quick Math Review Arithmetic progressions: - An example n i = 1 i = 1 + 2 + 3 + + n = + n -------------- 2 n 2 - two visual representations n+1 n n...... 3 3 2 2 1 1 0 1 2 n/2 0 1 2 3 n 13

Another Example A better algorithm for computing prefix averages: Algorithm prefixaverages2(x): Input: An n-element array X of numbers. Output: An n-element array A of numbers such that A[i] is the average of elements X[0],..., X[i]. Let A be an array of n numbers. s 0 for i 0 to n - 1 do s s + X[i] A[i] s/(i + 1) return array A Analysis... 14

Math You Need to Review Logarithms and Exponents - properties of logarithms: log b (xy) = log b x + log b y log b (x/y) = log b x - log b y log b x α = αlog b x log b a= log x a log x b - properties of exponentials: a (b+c) = a b a c a bc = (a b ) c a b /a c = a (b-c) b = a b c = a log a b c*log a b 15

Floor Ceiling More Math to Review x = the largest integer x x = the smallest integer x Summations - general definition: i t = s f() i = f( s) + f( s+ 1) + f( s+ 2) + + f() t - where f is a function, s is the start index, and t is the end index Geometric progression: f(i) = a i - given an integer n 0 and a real number 0 < a 1 n i = 0 a i 1 a a 2 a n 1 an + 1 = + + + + = -------------------- 1 a - geometric progressions exhibit exponential growth 16

Advanced Topics: Simple Justification Techniques By Example - Find an example - Find a counter example The Contra Attack - Find a contradiction in the negative statement - Contrapositive Induction - Prove the base case - Prove that any case n implies the next case (n +1) is also true Loop invariants - Prove initial claim S 0 - Show that S i-1 implies S i will be true after iteration i 17

Advanced Topics: Other Justification Techniques Proof by Excessive Waving of Hands Proof by Incomprehensible Diagram Proof by Very Large Bribes - see instructor or TAs after class Proof by Violent Metaphor - Don t argue with anyone who always assumes a sequence consists of hand grenades The Emperor s New Clothes Method - This proof is so obvious only an idiot wouldn t be able to understand it. 18