Professional Documents
Culture Documents
algorithm
Fundamental Concepts
1
Chapter 1
Fundamental
CONTENT
1.1. Algorithm and Complexity
• Problem solving
• Data structure:
For example:
• Cooking instructions
• Instructions for installing a device
• The rules make a game
• Directions from A to B
• Motorcycle repair instructions
• etc.
5
Algorithm notation
• Algorithm has the following characteristics:
• Input
• Output
• Precision
• Finiteness
• Uniqueness
• Generality
Data structure
• Set of data
• Has related relation in a predetermined problem
7
Algorithm’s Complexity
• Evaluating the computational complexity of an algorithm
is to evaluate the amount of resources of all kinds that
the algorithm requires.
• Commonly used resources:
• time
• Memory
• bandwidth
• We have: j j −1
a[k ] = a[ j ] + a[k ]
k =i k =i
• Implementation:
n −1
n2 n
i =0
(n − i ) = n + (n − 1) + ... + 1 = +
2 2
• This number is exactly equal to the number of
subsequences.
• => It seems that the resulting algorithm is very good,
because each subsequence must be considered exactly
once.
Recursive algorithm
• Better algorithm!
MaxLeft(a, i, j);
{
maxSum = -; sum = 0;
for (int k=j; k>=i; k--) {
sum = sum+a[k];
maxSum = max(sum, maxSum);
}
return maxSum;
}
Recursive algorithm
• Calculate the weight of the largest subsequence in the
right half (from a[i] to a[j]) starting from a[i] :
MaxRight(a, i, j);
{
maxSum = -; sum = 0;
for (int k=i; k<=j; k++){
sum = sum+a[k];
maxSum = max(sum, maxSum);
}
return maxSum;
}
Recursive algorithm
0 n =1
T ( n) = n n n
T ( 2 ) + T ( 2 ) + n = 2T ( 2 ) + n n 1
Recursive algorithm
Q, W, O
fg ab
f (n) = O(g(n)) a b
f (n) = W(g(n)) a b
f (n) = Q(g(n)) a = b
Properties
• Transitivity
f(n) = Q(g(n)) & g(n) = Q(h(n)) f(n) = Q(h(n))
f(n) = O(g(n)) & g(n) = O(h(n)) f(n) = O(h(n))
f(n) = W(g(n)) & g(n) = W(h(n)) f(n) = W(h(n))
• Symmetry
f(n) = Q(g(n)) if and only if g(n) = Q(f(n))
• Transpose Symmetry
f(n) = O(g(n)) if and only if g(n) = W(f(n))
Example
A B
• 5n2 + 100n 3n2 + 2
• log3(n2) log2(n3)
Recall some logarithmic functions
x a = b log b = a
x
• 1000n2+1000n = O(n2):
• 100n + 5 ≠ W(n2)
• O(1): constant
• O(log n): logarithmic
• O(n): linear
• O(n log n): superlinear
• O(n2): quadratic
• O(n3): cubic
• O(an): exponential (a > 1)
• O(nk): polynomial (k ≥ 1)
CONTENT
1.1. Algorithm and Complexity
1.2. Beginning example
1.3. Asymptotic Symbols
1.4. Pseudo Code
1.5. Algorithm analysis techniques
Algorithm description: pseudo-language
Begin or end
R=n%2
Assign instruction
N Y
R is 0 ?
Input/Output
Continue the
command
Execution flow
End
Block of instructions
Syntax:
{ S1
S1;
S2; S2
S3;
} S3
Condition instruction
Syntax
if(condition)
action false
condition
true
action
Condition instruction
Syntax:
if (B) then
false true
S1; B
else
S2; S2 S1
Loop instruction:
Syntax:
while (B) do
S;
true
B S
false
For loop
Syntax
for (initialization; condition; update)
action
initialization
false
condition
true
action
update
do-while loop
Syntax
do action
while (condition)
action
true
condition
false
Algorithm description: pseudo-language
• pseudo language
• allows both describing algorithms in everyday language
and using command structures similar to those of
programming languages.
Algorithm description: pseudo-language
• Declare variable
integer x,y;
real u, v;
boolean a, b;
char c, d;
datatype x;
• Assignment statement
x = expression;
or
x ← expression;
Ex: x ← 1+4;
y = a*y+2;
Algorithm description: pseudo-language
• Control structure
• if condition then
sequence of instructions
else
sequence of instructions
endif;
while condition do
sequence of instructions
endwhile;
Algorithm description: pseudo-language
repeat
sequence of instructions; Case statement:
until condition; Case
cond1: stat1;
for i=n1 to n2 [step d] cond2: stat2;
sequence of instructions; .
.
endfor;
.
condn: stat n;
• Input-Output endcase;
read(X); /* X can be single variable or
array */
print(data) or print(message)
Algorithm description: pseudo-language
Sequential structure.
• Suppose P and Q are two segments of the algorithm,
• Can be a command but can also be a sub-algorithm.
• Time(P), Time(Q): calculation time of P and Q respectively.
for i =1 to m do P(i);
t(i )
i =1
Typical statement
• Definition: A typical statement is a statement that is
executed at least as often as any other statement in
the algorithm.
• If we assume that the execution time of each
statement is bounded by a constant
• => To evaluate the calculation time, you can count
the number of times a typical command is executed
Example: FibIter
function Fibiter(n)
begin
i:=0; j:=1; Typical instruction
for k:=1 to n do
begin
j:= j+i; •The number of times a typical
i:= j-i; statement is executed is n.
end;
•Calculation time of the
Fibiter:= j; algorithm is O(n)
end;
Example: Algorithm1 of beginning
example
int maxSum =0;
for (int i=0; i<n; i++) {
for (int j=i; j<n; j++) {
int sum = 0;
for (int k=i; k<=j; k++)
sum += a[k];
if sum > maxSum
maxSum = sum;
}
}
Select the typical statement sum+=a[k].
=> Evaluate the calculation time of the algorithm as O(n3)
Questions?