Beruflich Dokumente
Kultur Dokumente
Chapter I:
Algorithms
FAP:UC
Algorithm
FAP:UC
Algorithm
FAP:UC
Guiding Principles
Input
Output
Effectiveness
FAP:UC
Definiteness
Finiteness
Analysis of Algorithms
FAP:UC
Design:
Write an algorithm that will solve the problem according
to the requirement.
Analysis:
Trying to come up with another algorithm and compare it
with the previous one.
FAP:UC
Verification:
Consist of 3 aspects:
Program
Proving
FAP:UC
FAP:UC
FAP:UC
10
O(log n)
program slightly slower as N grows; normally in programs
that solve a big problem by transforming it into a small
problem, cutting the size by some constant factor.
O(n)
linear; proportional to the size of N
FAP:UC
11
O(n2)
quadratic; can be seen in algorithms that process all pairs of
data items.
O(nk)
polynomial; algorithms that process polynomial of data
items.
FAP:UC
12
13
P1
___
___
___
___
___
___
P2
___
___
___
___
___
___
14
P1
10
50
100
150
200
300
P2
0.5
12.5
50
112.5
200
450
P2 is more faster and efficient for N <= 20, but for N>20, P1
proves to be more faster.
FAP:UC
15
Big-O
Iterations
Estimated Time
Logarithmic
O(logn)
14
Microsecond
Linear
O(n)
10,000
Seconds
Linear
Logarithmic
O(nlogn)
140,000
Seconds
Quadratic
O(n2)
10,0002
Minutes
Polynomial
O(nk)
10,000k
Hours
Exponential
O(cn)
210,000
Intractable
Factorial
O(n!)
10,000!
Intractable
FAP:UC
16
General Rules
Rule 1: For Loops
The running time for a for loop is at most the running time of
the statements inside the for loop (including tests) times the
number of iterations.
17
General Rules
Rule 4: if/else Statement
For the fragment:
if(cond)
S1;
else S2;
the running time of an if/else statement is never more than
the running time of the test plus the larger of the running
times of S1 and S2.
FAP:UC
18
Exercise:
Determine the frequency count and the corresponding
Big-Oh Notation.
1.
K = 500;
for(j=1; j<=K; j++)
x = x + 1;
n = 200;
Frequency Count
1
1 + K + 1 + K
K
1
FAP:UC
19
Exercise:
1. {
for(j=1; j<=n; j++)
for(k=1; k<=n; k++)
{
c[j][k] = 0;
for(l=1; l<=n; l++)
c[j][k] = c[j][k] * b[l][k];
}
}
FAP:UC
20
Exercise:
2. for(k = 1; k<=n+1; k++)
{
a++;
b++;
}
3. for(l=1; l<=c; l++)
for(j=c; j>=1; j--)
System.out.println(l + j);
for(k=1; k<=c; k++)
System.out.println(k);
4. while(x <= 1)
{
k = k + 1;
x = x + 1;
}
FAP:UC
21
Exercise:
5. ctr = 1;
while ctr(<=n-2)
{ for(k=1; k<=n; k++)
System.out.println(ctr = +ctr);
ctr = ctr + 1;
}
6. for(l=4; l<=n-5; l++)
for(j=5; j<=n+10; j++)
for(k=1; k<=n*n*2; k++)
y = y + 1;
a = a + 1;
FAP:UC
22