Вы находитесь на странице: 1из 21

Notasi Big O

Permasalahan (latihan)
Badu menemukan peti harta karun
Utk membukanya diperlukan PIN 4 digit
PIN-nya adalah 4 digit terakhir dari 9 pa
ngkat N

Solusi 1
9 pangkat 10 = kira2 10 digit
Tidak muat dalam tipe data int

Hanya perlu 4 digit terakhir


123456789 : 4 digit terakhir = 6789
How to get?
123456789 mod 10000 = 6789

Solusi 1
int N= ____;
int i, pin=1;
for(i=0;i<N;i++) {
pin=(pin*9) mod 10000;
}
printf(pin=%d\n, pin);

Solusi 2
Misal N = 13
Binary = 1101

9^11 = 9^8 * 9^4 * 9^1

= 9^(2^3) * 9^(2^2) * 9^(2^0)


Misal N = 12
Binary = 1110

9^11 = 9^8 * 9^4 * 9^2

= 9^(2^3) * 9^(2^2) * 9^(2^1)

Solusi 2
int N= ____;
int i=1, pin=1, pw2 = 9;
while(i<5) { //2^5=32
if(N%2==1)
pin=(pin*pw2) mod 10000;
pw2 = pw2 * pw2;
i++; N=N>>1;
}
printf(pin=%d\n, pin);

Which one better?


Solusi 1
Berjalan N loop

Solusi 2
Berjalan 6 loop
6 = jml digit utk menyimpan angka 100 (max N)

Teori ttg Notasi Big O

Analysis of Algorithms
Estimate the running time
Estimate the memory space required.
Time and space depend on the input size.

Analysis of Algorithms

Running Time (3.1)


Most algorithms transform
input objects into output o
bjects.
The running time of an alg
orithm 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 a
s games, finance and robotics
Analysis of Algorithms

10

Experimental Studies
Write a program impleme
nting the algorithm
Run the program with inp
uts of varying size and co
mposition
Use a method like System.c
urrentTimeMillis() to get an a
ccurate measure of the ac
tual running time
Plot the results

Analysis of Algorithms

11

Limitations of Experiments
It is necessary to implement the algorith
m, which may be difficult
Results may not be indicative of the run
ning time on other inputs not included i
n the experiment.
In order to compare two algorithms, th
e same hardware and software environ
ments must be used
Analysis of Algorithms

12

Theoretical Analysis
Uses a high-level description of the al
gorithm instead of an implementation
Characterizes running time as a functi
on of the input size, n.
Takes into account all possible inputs
Allows us to evaluate the speed of an
algorithm independent of the hardwa
re/software environment
Analysis of Algorithms

13

Pseudocode (3.2)
Example: find max element
of an array

High-level description
of an algorithm
More structured than Algorithm arrayMax(A, n)
Input array A of n integers
English prose
Output maximum element of A
Less detailed than a p
currentMax A[0]
rogram
for i 1 to n 1 do
Preferred notation for
if A[i] currentMax then
currentMax A[i]
describing algorithms
return currentMax
Hides program design
issues

Analysis of Algorithms

14

Pseudocode Details
Control flow

if then [else ]

while do
repeat until
for do
Indentation replaces braces

Expressions

Method declaration
Algorithm method (arg [, arg])
Input
Output

Analysis of Algorithms

Assignment
(like in Java)
Equality testing
(like in Java)
n2 Superscripts and other
mathematical formatti
ng allowed

15

Primitive Operations (time unit)


Basic computations perform
Examples:
ed by an algorithm
Evaluating an expr
Identifiable in pseudocode
ession
Assigning a value
Largely independent from t
to a variable
he programming language
Indexing into an a
rray
Exact definition not importa
Calling a method
nt (we will see why later)
Returning from a
Assumed to take a constant
method
Comparison x==y
amount of time in the RAM
x>Y
model
Analysis of Algorithms

16

Counting Primitive Operations (


3.4)
By inspecting the pseudocode, we can determine the maximu
m number of primitive operations executed by an algorithm, a
s a function of the input size

Algorithm arrayMax(A, n)
# operations
currentMax A[0]
2
for (i =1; i<n; i++)
2n
(i=1 once, i<n n times, i++ (n-1) time
s)

if A[i] currentMax then


currentMax A[i]
return currentMax
Total

6n

Analysis of Algorithms

2(n 1)
2(n 1)
1
17

Estimating Running Time


Algorithm arrayMax executes 6n 1 primitive o
perations in the worst case.
Define:
a = Time taken by the fastest primitive operation
b = Time taken by the slowest primitive operation

Let T(n) be worst-case time of arrayMax. Then


a (8n 2) T(n) b(8n 2)
Hence, the running time T(n) is bounded by tw
o linear functions
Analysis of Algorithms

18

Growth Rate of Running Time


Changing the hardware/ software en
vironment
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 al
gorithm arrayMax
Analysis of Algorithms

19

logn

nlogn

n2

n3

2n

16

64

16

24

64

512

256

16

16

64

256

4,096

65,536

32

32

160

1,024

32,768

4,294,967,296

64

64

384

4,094

262,144

1.84 * 1019

128

128

896

16,384

2,097,152

3.40 * 1038

256

256

2,048

65,536

16,777,216

1.15 * 1077

512

512

4,608

262,144

134,217,728

1.34 * 10154

1024

10

1,024

10,240

1,048,576

1,073,741,824

1.79 * 10308

The Growth Rate of the Six Popular functions


Analysis of Algorithms

20

Big-Oh Notation
To simplify the running time estimatio
n,
for a function f(n), we ignore the con
stants and lower order terms.
Example: 10n3+4n2-4n+5 is O(n3).

Analysis of Algorithms

21

Вам также может понравиться