You are on page 1of 16

Notasi Big O

Growth of Function
• Function = algoritma
• Bagaimana pertumbuhan algoritma
relatif terhadap jumlah data?
• Pertumbuhan
– Waktu yg diperlukan
– Space yg diperlukan
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 4
Running Time (§3.1)
• Most algorithms transform best case

input objects into output o average case


worst case
bjects. 120

• The running time of an alg 100


orithm typically grows with

Running Time
80
the input size.
60
• Average case time is often
difficult to determine. 40

• We focus on the worst case 20

running time. 0
1000 2000 3000 4000
– Easier to analyze Input Size
– Crucial to applications such a
s games, finance and robotics
Analysis of Algorithms 5
Experimental Studies

• Write a program impleme 9000

nting the algorithm 8000

• Run the program with inp 7000

uts of varying size and co 6000

Time (ms)
mposition 5000
• Use a method like System. 4000
currentTimeMillis() to get an 3000
accurate measure of the a
2000
ctual running time
1000
• Plot the results
0
0 50 100
Input Size
Analysis of Algorithms 6
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, the
same hardware and software environme
nts must be used

Analysis of Algorithms 7
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 hardwar
e/software environment

Analysis of Algorithms 8
Pseudocode (§3.2)
Example: find max eleme
• High-level description nt of an array
of an algorithm
• More structured than Algorithm arrayMax(A, n)
English prose Input array A of n integers
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
describing algorithms currentMax  A[i]
• Hides program design return currentMax
issues

Analysis of Algorithms 9
Pseudocode Details

• Control flow • Expressions


– if … then … [else …]  Assignment
– while … do … (like  in Java)
– repeat … until …  Equality testing
– for … do … (like  in Java)
– Indentation replaces braces n2 Superscripts and other
mathematical formattin
• Method declaration g allowed
Algorithm method (arg [, arg…])
Input …
Output …

Analysis of Algorithms 10
Primitive Operations (time unit)

• Basic computations perform


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

Analysis of Algorithms 11
Counting Primitive Operatio
ns (§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] 1
for (i =1; i<n; i++)
(i=1 once, i<n n times, i++ (n-1) times)
if A[i]  currentMax then (n  1)
currentMax  A[i] (n  1)
return currentMax 1
Total 4n
Analysis of Algorithms 12
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 13
Growth Rate of Running Time

• Changing the hardware/ software envi


ronment
– 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 14
n logn n nlogn n2 n3 2n

4 2 4 8 16 64 16
8 3 8 24 64 512 256

16 4 16 64 256 4,096 65,536

32 5 32 160 1,024 32,768 4,294,967,296

64 6 64 384 4,094 262,144 1.84 * 1019

128 7 128 896 16,384 2,097,152 3.40 * 1038

256 8 256 2,048 65,536 16,777,216 1.15 * 1077

512 9 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 15
Big-Oh Notation
• To simplify the running time estimation,
for a function f(n), we ignore the const
ants and lower order terms.

Example: 10n3+4n2-4n+5 is O(n3).

Analysis of Algorithms 16

You might also like