Академический Документы
Профессиональный Документы
Культура Документы
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
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
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);
Solusi 2
Berjalan 6 loop
6 = jml digit utk menyimpan angka 100 (max N)
Analysis of Algorithms
Estimate the running time
Estimate the memory space required.
Time and space depend on the input size.
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
16
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)
6n
Analysis of Algorithms
2(n 1)
2(n 1)
1
17
18
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
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