Elementary maths for GMT. Algorithm analysis Part I

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

Data Structures Lecture 8

Analysis of Algorithms

Outline and Reading. Analysis of Algorithms 1

Algorithms and Data Structures

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

Data Structures and Algorithms

ANALYSIS OF ALGORITHMS

Analysis of Algorithms

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

Choice of C++ as Language

Chapter 6 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

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

Data Structures and Algorithms. Analysis of Algorithms

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

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

(Refer Slide Time: 1:27)

Data Structures and Algorithms

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

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

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

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

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

Analysis of Algorithm. Chapter 2

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

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

Pseudocode ( 1.1) Analysis of Algorithms. Primitive Operations. Pseudocode Details. Running Time ( 1.1) Estimating performance

Lecture 5: Running Time Evaluation

Partha Sarathi Mandal

Asymptotic Analysis of Algorithms

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Analysis of Algorithms

CH ALGORITHM ANALYSIS CH6. STACKS, QUEUES, AND DEQUES

Algorithms and Data Structures

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

PROGRAM EFFICIENCY & COMPLEXITY ANALYSIS

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

Data structure and algorithm in Python

RUNNING TIME ANALYSIS. Problem Solving with Computers-II

Introduction to Data Structure

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. College of Computing & Information Technology King Abdulaziz University. CPCS-204 Data Structures I

Introduction to the Analysis of Algorithms. Algorithm

Programming II (CS300)

csci 210: Data Structures Program Analysis

CS240 Fall Mike Lam, Professor. Algorithm Analysis

Classic Data Structures Introduction UNIT I

CS:3330 (22c:31) Algorithms

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

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

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

UNIT 1 ANALYSIS OF ALGORITHMS

Assignment 1 (concept): Solutions

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

Adam Blank Lecture 2 Winter 2017 CSE 332. Data Structures and Parallelism

Computer Science Approach to problem solving

Introduction to Computers & Programming

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

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

CSE 373 APRIL 3 RD ALGORITHM ANALYSIS

Analysis of Algorithms. CSE Data Structures April 10, 2002

Complexity of Algorithms

4.1 Real-world Measurement Versus Mathematical Models

Unit 1 Chapter 4 ITERATIVE ALGORITHM DESIGN ISSUES

Complexity Analysis of an Algorithm

Lecture 2: Algorithm Analysis

Scientific Computing. Algorithm Analysis

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

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

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

Data Structures and Algorithms

Chapter 2: Complexity Analysis

Algorithms and Theory of Computation. Lecture 2: Big-O Notation Graph Algorithms

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

NCS 301 DATA STRUCTURE USING C

3. Java - Language Constructs I

L6,7: Time & Space Complexity

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

DATA STRUCTURES AND ALGORITHMS. Asymptotic analysis of the algorithms

UNIT 1 BASICS OF AN ALGORITHM

CS 6402 DESIGN AND ANALYSIS OF ALGORITHMS QUESTION BANK

Elementary maths for GMT. Algorithm analysis Part II

INTRODUCTION. An easy way to express the idea of an algorithm (very much like C/C++, Java, Pascal, Ada, )

Computer Algorithms. Introduction to Algorithm

Algorithms in Systems Engineering IE172. Midterm Review. Dr. Ted Ralphs

Introduction to Computer Science

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

TREES AND ORDERS OF GROWTH 7

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

Introduction to Analysis of Algorithms

ASYMPTOTIC COMPLEXITY

Analysis of Algorithms & Big-O. CS16: Introduction to Algorithms & Data Structures Spring 2018

HOMEWORK FILE SOLUTIONS

Analysis of Algorithms Part I: Analyzing a pseudo-code

Complexity of Algorithms. Andreas Klappenecker

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

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

Module 1: Asymptotic Time Complexity and Intro to Abstract Data Types

What is a Computer Algorithm?

ECE6095: CAD Algorithms. Optimization Techniques

Transcription:

Elementary maths for GMT Algorithm analysis Part I

Algorithms An algorithm is a step-by-step procedure for solving a problem in a finite amount of time Most algorithms transform input objects into output objects Input Algorithm Output 2

Running time The running time of an algorithm typically grows with the input size Average case time is often difficult to determine mathematically To define the running time, we focus on the worst case scenario Easier to determine Crucial and relevant to applications such as games, finance, robotics etc. Running Time 120 100 80 60 40 20 best case average case worst case 0 1000 2000 3000 4000 Input Size 3

Experimental studies Write a program implementing your algorithm Run the program with inputs of varying size and composition Use function like clock()to get an accurate measure of the actual running time Plot the results 4

Limitations of experiments It is necessary to implement the algorithm, which may be difficult Results may not be indicative of the running time on other inputs not included in the experiments In order to compare two algorithms, the same hardware and software environments must be used 5

Theoretical analysis Uses a high-level description of the algorithm instead of an implementation Characterizes running time as a function of the input size, denoted n Takes into account all possible inputs Allows us to evaluate the cost of an algorithm independently from the hardware/software environment 6

Pseudo-code High-level description of an algorithm More structured than English prose Less detailed than a program Preferred notation for describing algorithms Hides program design / implementation / syntax issues 7

Pseudo-code example How to compute the max value in an array of integers Algorithm arraymax (A, n) Input array A of n integers Output maximum element of A currentmax A [0] for i 1 to n 1 do if A[i] currentmax then currentmax A[i] return currentmax 8

Pseudo-code details Control flow if... then... [ else... ] while... do... repeat... until... for... do... Indentation replaces braces Method declaration Algorithm method ( arg [, arg...] ) Input... Output... Method call var.method (arg [, arg...]) Return value return expression Expressions assignment (like = in Java/C#/C++) = Equality testing (like == in Java/C#/C++) Superscripts (e.g. n 2 ) and other mathematical formatting allowed 9

Random Access Machine (RAM) A CPU that executes the pseudo-code A potential unbounded bank of memory cells, each of which can hold an arbitrary number or character Memory cells are numbered and accessing any cell in memory takes unit time 0 1 2 10

Important functions Eight functions often appear in algorithm analysis Constant 1 Logarithmic log n Linear n N-Log-N n log n Quadratic n 2 Cubic n 3 Exponential 2 n Factorial n! 11

Important functions Eight functions often appear in algorithm analysis Constant 1 Logarithmic log n Linear n N-Log-N n log n Quadratic n 2 Cubic n 3 Exponential 2 n Factorial n! 12

Important functions Eight functions often appear in algorithm analysis Constant 1 Logarithmic log n Linear n N-Log-N n log n Quadratic n 2 Cubic n 3 Exponential 2 n Factorial n! 13

Important functions Constant 14

Necessary math Summations Logarithms and exponentials Properties of logarithms b log xy = b log x + b log y b log x/y = b log x b log y b log x a = a b log x b log a = x log a / x log b Properties of exponentials a (b+c) = a b a c a bc b c = a a b / a c = a (b c) b = a a log b b c = a c a log b 15

Primitive operations Basic computations performed by an algorithm Identifiable in pseudo-code Largely independent from the programming language Exact definition not important (we will see why later) Assumed to take a constant amount of time in the RAM model Examples Evaluating an expression Assigning a value to a variable Indexing into an array Calling a method Returning from a method 16

Counting primitive operations By inspecting the pseudo-code, we can determine the maximum number of primitive operations executed by an algorithm, as a function of the input size Algorithm arraymax(a, n) # operations currentmax A[0] 2 for i 1 to n 1 do 2n if A[i] currentmax then 2(n 1) currentmax A[i] 2(n 1) { increment counter i } 2(n 1) return currentmax 1 Total: 8n 3 17

Estimating running time The algorithm arraymax executes 8n 3 primitive operations in the worst case If we define a as the time for the fastest primitive operation b as the time for the slowest primitive operation T(n) as the worst-case time of arraymax Then, a(8n 3) T(n) b(8n 3) Hence, the running time T(n) is bounded by two linear functions 18

Growth rate of running time Changing the hardware / software environment affects T(n) by a constant factor, but does not alter the growth rate of T(n) The linear growth rate of the running time T(n) is an intrinsic property of the algorithm arraymax 19

Constant factors The growth rate is not affected by constant factors lower-order terms Examples 10n + 10 is a linear function what if we replace + 10 by + 10 2? 10n 2 + 10 is a quadratic function what if we replace + 10 by + 10 2? what if we replace + 10 by +10n? 20

The Big-Oh Given functions f(n) and g(n), we say that f(n) is O(g n ) if there is a positive constant c and an integer constant n 0 1 such that f(n) cg(n) for all n n 0 Example: 2n + 10 is O(n) 2n + 10 c n c 2 n 10 n 10 / (c 2) Pick c = 3 and n 0 = 10 (or c = 12 and n 0 = 1,...) 21

The Big-Oh Example: the function n 2 is not O(n) n 2 c n n c The above inequality cannot be satisfied since c must be a constant 22

More Big-Oh examples 7n 2 is O(n) We need c > 0 and n 0 1 such that 7n 2 c n for n n 0 This is true for c = 7 and n 0 = 1 3n 3 + 20n 2 + 5 is O(n 3 ) We need c > 0 and n 0 1 such that 3n 3 + 20n 2 + 5 c n 3 for n n 0 This is true for c = 4 and n 0 = 21 3 log n + 5 is O(log n) We need c > 0 and n 0 1 such that 3 log n + 5 c log n for n n 0 This is true for c = 8 and n 0 = 10 23

Big-Oh and growth rate The Big-Oh notation gives an upper bound on the growth rate of a function The statement f(n) is O(g n ) means that the growth rate of f(n) is no more than the growth rate of g(n) We can use the Big-Oh notation to rank functions according to their growth rate f(n) is O(g n ) g(n) is O(f n ) g(n) grows more YES NO f(n) grows more NO YES same growth YES YES 24

Big-Oh rules If f(n) is a polynomial of degree d, then f(n) is O(n d ), so: Drop the lower-order terms Drop the constant factors Use the smallest possible class of functions We say that 2n is O(n) instead of 2n is O(n 2 ) Use the simplest expression of the class We say that 3n + 5 is O(n) instead of 3n + 5 is O(3n) 25

Asymptotic algorithm analysis The asymptotic analysis of an algorithm determines the running time in Big-Oh notation To perform the asymptotic analysis We find the worst case number of primitive operations executed as a function of the input size We express this function with Big-Oh notation Example We determine that algorithm arraymax executes at most 8n 3 primitive operations We say that algorithm arraymax runs in O(n) time Since constant factors and lower-order terms are eventually dropped anyhow, we can disregard them when counting primitives operations 26

Computing prefix averages We further illustrate asymptotic analysis with two algorithms for prefix averages The i-th prefix average of an array X is the average of the first (i+1) elements of X: X 0 + X 1 + + X i A i = i + 1 Computing the array A of prefix averages of another array X has applications to financial analysis 35 30 25 20 15 10 5 0 X A 1 2 3 4 5 6 7 27

Prefix averages Quadratic example The following algorithm computes prefix averages in quadratic time by applying the definition Algorithm prefixaveragesquad(x, n) Input array X of n integers Output array A of prefix averages of X # operations (after drop) A new array of n integers n for i 0 to n 1 do n s X [0] n for j 1 to i do 1 + 2 + + (n 1) s s + X [j] 1 + 2 + + (n 1) A [i] s / (i + 1) n return A 1 28

Prefix averages Quadratic example Arithmetic progression The running time of prefixaveragesquad is O(1 + 2 + + n) The sum of the first n integers is n n + 1 /2 There is a simple visual proof of this fact 7 6 5 4 3 2 1 Thus, the algorithm prefixaveragesquad runs in O(n 2 ) time recall that lower-order terms can be disregarded (n / 2) 0 1 2 3 4 5 6 29

Prefix averages Linear example The following algorithm computes prefix averages in a linear time by keeping a running sum Algorithm prefixaverageslinear(x, n) Input array X of n integers Output array A of prefix averages of X # operations (after drop) A new array of n integers n s 0 1 for i 0 to n 1 do n s s + X [i] n A [i] s / (i + 1) n return A 1 Algorithm prefixaverageslinear runs in O n time 30

Relatives of Big-Oh Big-Omega f(n) is Ω(g n ) if there is a constant c > 0 and an integer constant n 0 1 such that f n c g(n) for n n 0 Big-Theta f(n) is Θ(g n ) if there are constants c > 0 and c > 0 and an integer constant n 0 1 such that c g n f n c g(n) for n n 0 31

Intuition for asymptotic notation Big-Oh f n is O(g n ) if f(n) is asymptotically less than or equal to g(n) Big-Omega f n is Ω(g n ) if f(n) is asymptotically greater than or equal to g(n) Big-Theta f n is Θ(g n ) if f(n) is asymptotically equal to g(n) In Big-Omega and Big-Theta notation we also omit constants and lower-order terms 32

Examples of relatives of Big-Oh 5n 2 is Ω(n 2 ) f(n) is Ω(g n ) if there is a constant c > 0 and an integer constant n 0 1 such that f n c g(n) for n n 0 True for c = 5 and n 0 = 1 5n 2 is Ω(n) f(n) is Ω(g n ) if there is a constant c > 0 and an integer constant n 0 1 such that f n c g(n) for n n 0 True for c = 1 and n 0 = 1 5n 2 is Θ(n 2 ) f(n) is Θ(g n ) if it is Ω(n 2 ) and O(n 2 ). We have already seen the former, for the latter recall that f(n) is O(g n ) if there is a constant c > 0 and an integer constant n 0 1 such that f n c g(n) for n n 0 True for c = 5 and n 0 = 1 33