The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the...
Transcript of The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the...
![Page 1: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/1.jpg)
The Growth of Functions
Rosen 3.2
![Page 2: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/2.jpg)
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: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/3.jpg)
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: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/4.jpg)
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: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/5.jpg)
![Page 6: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/6.jpg)
The Big-O Notation … gives the asymptotic behaviour of a function.
![Page 7: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/7.jpg)
The Big-O Notation
kwhenever x
)(.f(x)
such thatk and C constants are thereif
O(g(x)) is f(x)
xgC
f(x) is big-O of g(x)f(x) is order g(x)
C and k are called “witnesses to the relationship”There may be many witnesses
![Page 8: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/8.jpg)
The Big-O Notationexample
)( is12)( 22 xOxxxf
kxxCxx when .12 22
22222 42120 xxxxxx
Whenever x is greater than 1 the above holds,
consequently … )( is12)( 22 xOxxxf
![Page 9: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/9.jpg)
The Big-O Notationexample
Note also:
)( is12)( 22 xOxxxf
)( is12 32 xOxx
But we prefer the former.
![Page 10: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/10.jpg)
The Big-O Notation Another example
)(not is )( 2 nOnnf
i.e. not linear
impossible is This
constant a is and variablea isBut
.
somefor .2
2
Cn
Cnn
nC
n
n
knnCn
![Page 11: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/11.jpg)
The Big-O Notation Yet another example
)( is ! that Show nnOn
)(! have we1 and1With
......3.2.1
somefor .!
n
n
nOnkC
nnnnn
knnCn
![Page 12: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/12.jpg)
Complexity of bubble sort
bubbleSort(A:array,n:int) for i := 1 to n-1 for j := 1 to n-I if A[j] > A[j+1] then swap(A,j,j+1)
See 3.1, Sorting pp 172 onwards
![Page 13: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/13.jpg)
Complexity of bubble sort
bubbleSort(A:array,n:int) for i := 1 to n-1 for j := 1 to n-i if A[j] > A[j+1] then swap(A,j,j+1)
First time round the outer loop (i=1) the inner loop executes the “if” statement 1 to n-1 times
Second time round the outer loop (i=2) the inner loop executes the “if” statement 1 to n-2 times
Third time round the outer loop (i=2) the inner loop executes the “if” statement 1 to n-3 times
n-1th time round the outer loop (i=n-1) the inner loop executes the “if” statement 1 to n-(n-1) times
How many times is the inner “if” conditional statement executed(i.e. this is our principal activity)?
![Page 14: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/14.jpg)
Complexity of bubble sort
bubbleSort(A:array,n:int) for i := 1 to n-1 for j := 1 to n-i if A[j] > A[j+1] then swap(A,j,j+1)
)(
2
)1(2
)1)1)((1(
1321
))1(())2(()3()2()1(
2
1
1
nO
nn
nn
i
n
nnnnnnn
n
![Page 15: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/15.jpg)
Complexity of merge sort
))log(.( nnO
![Page 16: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/16.jpg)
![Page 17: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/17.jpg)
![Page 18: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/18.jpg)
![Page 19: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/19.jpg)
Or go see Rosen 4.4 Recursive Algorithms (page 317 onwards)
![Page 20: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/20.jpg)
![Page 21: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/21.jpg)
![Page 22: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/22.jpg)
![Page 23: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/23.jpg)
Good
Intractable
![Page 24: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/24.jpg)
![Page 25: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/25.jpg)
![Page 26: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/26.jpg)
![Page 27: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/27.jpg)
![Page 28: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/28.jpg)
A demo?
Sorting using the java demo’s?
A rough cut calculation of runtimes on different data set sizes?
![Page 29: The Growth of Functions Rosen 3.2. What affects runtime of a program? the machine it runs on the programming language the efficiency of the compiler the.](https://reader036.fdocuments.us/reader036/viewer/2022070412/5697bf771a28abf838c812ab/html5/thumbnails/29.jpg)