Top Banner
Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity) - Investigates computational complexity of common algorithms Credit to: GoTa presentation slides. 1 ADS Lecture 8
20

Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

Dec 29, 2015

Download

Documents

Sheena Doyle
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

AlgorithmInput Output

An algorithm is a step-by-step procedure forsolving a problem in a finite amount of time.

Chapter 4. Algorithm Analysis (complexity)

- Investigates computational complexity of common algorithms

Credit to: GoTa presentation slides.

1ADS Lecture 8

Page 2: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

What affects runtime of a program?

• the machine it runs on• the programming language• the efficiency of the compiler• the size of the input• the efficiency/complexity of the algorithm?

What has the greatest effect?

Page 3: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

What affects runtime of an algorithm?

• the size of the input• the efficiency/complexity of the algorithm?

We measure the number of times the“principal activity” of the algorithm isexecuted for a given input size n

One easy to understand example is search, findinga piece of data in a data set

N is the size of the data set“principal activity” might be comparison of key with data

What are we interested in? Best case, average case, or worst case?

Page 4: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

What affects runtime of an algorithm?

Therefore we express the complexity of an algorithmas a function of the size of the input

This function tells how the number of times the “principal activity” performed grows as the input size grows.

This does not give us an exact figure, but a growth rate.This allows us to compare algorithms theoretically

Page 5: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

5

Running Time (§3.1)

• Most algorithms transform input objects into output objects.

• The running time of an algorithm typically grows with the input size.

• Average case time is often difficult to determine.

• We focus on the worst case running time.– Easier to analyze– Crucial to applications

such as games, finance and robotics

0

20

40

60

80

100

120

Runnin

g T

ime

1000 2000 3000 4000

Input Size

best caseaverage caseworst case

ADS Lecture 8

Page 6: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

6

Experimental Studies

• Write a program implementing the algorithm

• Run the program with inputs of varying size and composition

• Use a method like System.currentTimeMillis() to get an accurate measure of the actual running time

• Plot the results

0

1000

2000

3000

4000

5000

6000

7000

8000

9000

0 50 100

Input Size

Tim

e (

ms)

ADS Lecture 8

Page 7: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

7

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 experiment. • In order to compare two algorithms, the same hardware

and software environments must be used

ADS Lecture 8

Theoretical Analysis

• Uses a high-level description of the algorithm instead of an implementation

• Characterizes running time as a function of the input size, n.

• Takes into account all possible inputs• Allows us to evaluate the speed of an algorithm

independent of the hardware/software environment

Page 8: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

8

Primitive Operations

• Basic computations performed by an algorithm

• Identifiable in pseudocode• Largely independent from

the programming language• Exact definition not

important (we will see why later)

• Assumed to take a constant amount of time

• Examples:– Evaluating an

expression– Assigning a

value to a variable

– Indexing into an array

– Calling a method– Returning from a

method

ADS Lecture 8

Page 9: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

9

Counting Primitive Operations (Goodrich §3.4)

• By inspecting the pseudocode, 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] 2for 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

ADS Lecture 8

Page 10: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

10

Estimating Running Time

• Algorithm arrayMax executes 8n 3 primitive operations in the worst case. Define:a = Time taken by the fastest primitive operationb = Time taken by the slowest primitive operation

• Let T(n) be worst-case time of arrayMax. Thena (8n 3) T(n) b(8n 3)

• Hence, the running time T(n) is bounded by two linear functions

ADS Lecture 8

Page 11: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

11

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 algorithm arrayMax

ADS Lecture 8

Page 12: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

12

Constant Factors

• The growth rate is not affected by– constant factors or – lower-order terms

• Examples– 102n 105 is a linear function– 105n2 108n is a quadratic function– Think of the shape of the curves if we plot them

ADS Lecture 8

Page 13: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

13

Big-Oh Notation (§3.4)

Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n0 such that

f(n) cg(n) for n n0

ADS Lecture 8

Page 14: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

14

Big-Oh Notation (§3.4)

Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n0 such that

f(n) cg(n) for n n0

• Example: 2n 10 is O(n) 2n 10 cn

(c 2) n 10

n 10(c 2)

Pick c 3 and n0 10

ADS Lecture 8

Page 15: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

15

Big-Oh Notation (§3.4)

Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants c and n0 such that

f(n) cg(n) for n n0

• Example: 2n 10 is O(n) 2n 10 cn

(c 2) n 10

n 10(c 2)

Pick c 3 and n0 10

ADS Lecture 8

• Example: the function f(n) = n2 is not O(n) (it is O(n2))

n2 cn

n c– Since c must be a constant, inequality can’t hold for all

n.

Page 16: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

16

More Big-Oh Examples

7n-2

3n3 + 20n2 + 5

3 log n + 5

ADS Lecture 8

Page 17: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

17

More Big-Oh Examples

7n-2

7n-2 is O(n)need c > 0 and n0 1 such that 7n-2 c•n for n n0

this is true for c = 7 and n0 = 1 3n3 + 20n2 + 5

3 log n + 5

ADS Lecture 8

Page 18: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

18

More Big-Oh Examples

7n-2

7n-2 is O(n)need c > 0 and n0 1 such that 7n-2 c•n for n n0

this is true for c = 7 and n0 = 1 3n3 + 20n2 + 5

3n3 + 20n2 + 5 is O(n3)need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n

n0

this is true for c = 4 and n0 = 21 3 log n + 5

ADS Lecture 8

Page 19: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

More Big-Oh Examples

7n-2

7n-2 is O(n)need c > 0 and n0 1 such that 7n-2 c•n for n n0

this is true for c = 7 and n0 = 1 3n3 + 20n2 + 5

3n3 + 20n2 + 5 is O(n3)need c > 0 and n0 1 such that 3n3 + 20n2 + 5 c•n3 for n

n0

this is true for c = 4 and n0 = 21 3 log n + 5

3 log n + 5 is O(log n)need c > 0 and n0 1 such that 3 log n + 5 c•log n for n

n0

this is true for c = 8 and n0 = 2

Page 20: Algorithm Input Output An algorithm is a step-by-step procedure for solving a problem in a finite amount of time. Chapter 4. Algorithm Analysis (complexity)

20

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 YesADS Lecture 8