04 Analisa Algoritma
04 Analisa Algoritma
Sunu Wibirama
Referensi
✦ Cormen, T.H., Leiserson, C.E., Rivest,
R.L., Stein, C., Introduction to Algorithms
2nd Edition, Massachusetts: MIT Press,
2002
✦ Sedgewick, R., Algorithms in C++ Part
1-4, Massachusetts: Addison-Wesley
Publisher, 1998
✦ Video lecture MIT Opencourseware
ke-1: Introduction
✦ Video lecture IIT Kharagpur, India
ke-18: Complexity of Algorithm
Video lecture MIT
Opencourseware ke-1:
Introduction
• Fungsionalitas (functionality)
• Keamanan (security)
• Kesederhanaan (simplicity)
c2 g(n) cg(n)
c1 g(n)
f (n)
n n
n0 n0 n0
f (n) = !(g(n)) f (n) = O(g(n)) f (n) = "
Teorema Matematika:
(a) (b) (c)
0 ≤ f (Nwithin
) ≤ constant
cg(Nfactors.
) untukWe writesemua N ≥if there
the minimum possible value; any greater value would also work. (a) !-notation bounds a
f (n) = !(g(n)) N 0 }exist positive constants n0, c1,
that to the right of n 0 , the value of f (n) always lies between c1 g(n) and c2 g(n) inclus
Engineering: notation gives an upper bound for a function to within a constant factor. We write f (n)
if there are positive constants n 0 and c such that to the right of n 0 , the value of f (n) alw
c1 ≤ − ≤ c2 .
2 n
Macam-macam Parameter N
Sebagian besar instruksi dieksekusi satu kali atau dalam
1 jumlah yang tidak terlalu banyak (waktu eksekusi
konstan)
kompleksitas algoritma
Figure 1-1 shows how the various measures of complexity compare with one another. The horizontal
axis represents the size of the problem — for example, the number of records to process in a search algo-
rithm. The vertical axis represents the computational effort required by algorithms of each class. This is
not indicative of the running time or the CPU cycles consumed; it merely gives an indication of how the
computational resources will increase as the size of the problem to be solved increases.
O(N2)
(seconds)
O(1)
O(log N)
Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998
Formula Kondisi Periodik
• Formula 2: kondisi periodik muncul pada program rekursif yang
membagi input menjadi dua bagian hanya dengan satu langkah
Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998
Formula Kondisi Periodik
• Formula 3: kondisi periodik muncul pada program rekursif yang
membagi input menjadi dua bagian, namun perlu memeriksa
tiap-tiap input
Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998
Formula Kondisi Periodik
• Formula 4: kondisi periodik muncul pada program rekursif yang
mengolah input secara linear sebelum, pada saat, dan sesudah
membagi input menjadi dua bagian
Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998
Formula Kondisi Periodik
• Formula 5: kondisi periodik muncul pada program rekursif yang
membagi input menjadi dua bagian, kemudian mengerjakan input
lain dengan kapasitas konstan
Sedgewick, R., Algorithms in C++ Part 1-4, Massachusetts: Addison-Wesley Publisher, 1998
Jenis analisa waktu eksekusi
• Strategi Worst case (umum digunakan)
T(N) = waktu eksekusi maksimum untuk
jumlah data N
• Strategi Average case (jarang digunakan)
T(N) = waktu yang diharapkan dari sebuah
algoritma untuk mengeksekusi data
sejumlah N. Perlu ada asumsi statistik untuk
distribusi data masukan
Analisa worst case
f (N ) Untuk N besar
lim →0
N →∞ g(N ) f(N) lebih efisien dari g(N)
!"#$%&'()#*$'+$,'&-./0
Implementasi: Insertion Sort
!"#$%& 1"23"45"((¢?/*(?+*(6*(?"² 78(43%&"'10
123*%)#4
!"#$%& <((+((=((>((?((@
'$%#$%& +((?((=((@((<((>
1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"
!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0-
Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002
!"#$%&'()*(+,-'./+),(-)./
2 + 1 3 4 5
!"#$%&'()*(+,-'./+),(-)./
Implementasi:!"#$%&'()*(+,-'./+),(-)./
Insertion
+1
2
2+
+
12
3
33 Sort
4
44
5
55
1
!"#$%&'()*(+,-'./+),(-)./!"#$%&'()*(+,-'./+),(-)./
+ 1 2 3 4 5 + 1 2 3 4 5
+1 2+ 32 43 54 15
!"#$%&'()*(+,-'./+),(-)./
1 + 2 3 4 5
!"#$%&'()*(+,-'./+),(-)./
++1 12+ 2
1
3
3
4
4
5
5
++ 31 22 43 54 15
+ 1 2 3 4 5
2 + 3 1 4 5 + 2 1 3 4 5
!"#$%&'()*(+,-'./+),(-)./
1 + 2 3 4 5
!"#$%&'()*(+,-'./+),(-)./ ++ 32 21 43 54 15
++ 11 22 33 44 55 + 4 2 1 3 5
2 + 3 4 1 5 1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"
!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/-
+ 4 2 5 1 3 &%">
1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"
+ 2 1 3 4 !"#$"%&"'()*(+,,-
5 !"#$%&'(#)%"*#%*+,-%$)#./0 ./0/1
+ 2 3 4 1 5
1%23$)-.#*4 566789*:$);*<=*<>/?)">
566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%"
?"&*1.?$,>0*:=*@>)0>$0%"
+ 3 2 4 1 5
1%23$)-.#*4 566789*:$);*<=*<>/?)"> ?"&*1.?$,>0*:=*@>)0>$0%" 1%23$)-.#*4
%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0 !"#$"%&"'()*(+,,- ./01
!"#$"%&"'()*(+,,- !"#$%&'(#)%"*#%*+,-%$)#./0
!"#$%&'(#)%"*#%*+,-%$)#./0 ./0/)
./0/1
Cormen, T.H., Leiserson, C.E., Rivest, R.L., Stein, C., Introduction to Algorithms 2nd Edition, Massachusetts: MIT Press, 2002
2 do key ← A[ j ] c2 n−1
3case occurs andInsert
!if theso they A[ take
array is nothe
already
j ] into time.sorted.
sorted For each j = 2, 3, . . . , n, we then find
that A[i] ≤ key in line 5 when i has its initial value of j − 1. Thus t j = 1 2.2 for Analyzing algorithms
sequence A[1 . . j − 1]. 0 n−1
j = 2, 3, . .I.NSERTION
, n, and the-Sbest-case ORT (A) running time is cost times
4 i ← j −1 c4 n!− 1
5T (n) = 1while c1 n for
+ ic2j> (n← 0−and 21)to+A[i]
length[A]
c4 (n>−key 1) + c5 (n − 1)c+ 5 8c (n − 1)cn 1 t n
j =2 j case occurs if the array is already sorte
2 do key ← A[ j ] !cn 2 n − 1 that A[i] ≤ key in line 5 when i has i
6 = (c1 + doc2A[i+ c+ + 1] c ← + A[i]
c )n − (c + c + c c
56+ c ) . (t − 1)
3 !4 5
Insert A[8 j ] into 2the sorted
4 8
!nj =2 j j = 2, 3, . . . , n, and the best-case runni
7This running time can i← i − 1 sequence
be expressed as an A[1+ b .for − 1]. c7
. j constants a and0j =2
(t j −depend
b that n1)− 1 Ton (n) = c1 n + c2 (n − 1) + c4 (n − 1
8the statement A[i + 1] ←
4 costs ci ; iti is←thuskey j −a 1linear function of n. c 8 n − c4 1 n!− 1
n = (c1 + c2 + c4 + c5 + c8 )n −
5 is in reverse
If the array whilesorted i > 0order—that
and A[i] >is,keyin decreasing corder—the 5 tj
worst
j =2
Theresults.
running time of the algorithm is the sum !
case 6 We must compare do A[i each+element
1] ← A[i] A[of j ] running
with eachtimes element
c6
for each
in thestatementThis running time can be expressed as a
n entire
(t
j =2 the j − 1)
executed; a statement
sorted subarray A[1 . . that
j − takes
1], and i steps
cso t = tofor
j execute
j = 2, and
3, . .is. , executed
n. Noting times
nthat
! n will
statement costs ci ; it is thus a linear
i ← i − 15 (t j If−the
contribute c7i n to the total running c7
of1) array is in reverse sorted orde
j
time. To compute T (n), the runningj =2 time
I!
n
NSERTION
8+
n(n-S ORT1) , we sum A[i + the ← key of the cost and times columns,
1]products c8 − 1 case results. We must compare each ele
n obtaining
j= −1 sorted subarray A[1 . . j − 1], and so t j
2 nis the sum" n running times for each statement
j =2 The running time of the algorithm" of ! n
T (n) = cexecuted; n(n + 1)
1 n + c2 (na− 1) + c 4 (n − 1) + c5 t j + c 6 (t
statement that takes ci steps to execute and is executed n times will − 1j − 1) j =
and 2
j =2 5 j =2
! n contribute c i n to the total running time. To compute T (n), the running
j =2 time of
n(n " n− 1)
( j − 1) I= NSERTION -S ORT, we sum the products of the cost and times columns, and obtaining
+ c7 2 (t j − 1) + c8 (n − 1) . n
j =2
" n " n ! n(n − 1)
j =2 ( j − 1) =
(see Appendix T (n)A for = a creview1 n + cof 2 (nhow− 1)to+solve − 1) summations),
c4 (nthese + c5 t j + cwe 6 find − 1)
(t jthat j in
=2
2
theEven
worstforcase,
inputs of a given
the running timesize, an algorithm’s
of I NSERTION -S ORT runningis j =2 time may j =2 depend on
which input of that size is given. " n For example, " in I NSERTION # -S ORT, the(see best Appendix A for a review of how t
+ c7 (t j − 1) + c8 (nn(n − 1) + .1) the worst case, the running time of I NSE
T (n) = c1 n + c2 (n − 1) + c4 (n − 1) + c5 −1
j =2 2
"
5 This characteristic does # " # T (n) = c1 n + c2 (n − 1) + c4 (n − 1
n(n not necessarily
−inputs
1) of a given hold for
n(n −size, a resource such as memory.
1) an algorithm’s running time may A statement that
Even for
+ c6of memory and is +executed
c7 + cnot − 1) depend on" # "
references m words n times does 8 (nnecessarily consume mn words of n(n − 1) n(
which input 2 of that size is given. 2 For example, in I NSERTION -S ORT , the+ best
c + c
memory in total. $c % $ % 6
2
7
5 c 6 c 7 c 5 c 6 c 7
= + + n 2 + c1 + c2 + c4 + − − + c8 n $c
5 c6 c7 % 2 $
2 2 2 2 2 2 = + + n + c1 +
5− + c4 + c5 +
(c2characteristic c8 )not
. necessarily hold for a resource such as memory. A statement that 2 2 2
This does
references m words of memory and is executed n2 times does not necessarily consume mn words of
This worst-case
memory in total.time can be expressed as an + bn + c for constants a,This
running b, worst-case running time can be exp O(n ) −2(c2 + c4 + c5 + c8 ) .
Implementasi: Insertion Sort
Worst-case (seluruh input diurutkan terbalik):
N
T (N ) = ∑ O( j) = O(n ) 2
Deret aritmatika
j=2
• Komputer B:
Merge Sort, lebih lambat untuk N kecil,
memproses N data dengan waktu eksekusi c2 N log N
Kecepatan eksekusi prosesor 107 instruksi/detik
• Diketahui: c1 c2
Jika c1=2, c2=50, N = 106 ,
mana algoritma yang lebih efisien?
Kesimpulan
• Analisa algoritma diperlukan untuk
perbandingan algoritma tanpa tergantung
spesifikasi mesin