Lecture 02
Lecture 02
1
Computation Model for Analysis
• To analyze an algorithm is to determine the amount of resources
necessary to execute it. These resources include computational
time, memory and communication bandwidth.
3
Pseudocode
• High-level description of an algorithm
• More structured than English prose but Less detailed than a
program
• Preferred notation for describing algorithms
• Hides program design issues
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0];
2. for i = 1 to n-1 do
3. if A[i] > currentMax then 4
4. currentMax A[i]
5. return currentMax;
Pseudocode
• Indentation indicates block structure. e.g body of loop
• Looping Constructs while, for and the conditional if-then-else
• The symbol // indicates that the reminder of the line is a comment.
• Arithmetic & logical expressions: (+, -,*,/, ) (and, or and not)
• Assignment & swap statements: a b , ab c, a → b
• Return/Exit/End: termination of an algorithm or block
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0];
2. for i = 1 to n-1 do
3. if A[i] > currentMax then 5
4. currentMax A[i]
5. return currentMax;
Pseudocode
• Local variables mostly used unless global variable explicitly defined
• If A is a structure then |A| is size of structure. If A is an Array then
n =length[A], upper bound of array. All Array elements are
accessed by name followed by index in square brackets A[i].
• Parameters are passed to a procedure by values
• Semicolons used for multiple short statement written on one line
ArrayMax(A, n)
Input: Array A of n integers
Output: maximum element of A
1. currentMax A[0]
2. for i = 1 to n-1 do
3. if A[i] > currentMax then 6
4. currentMax A[i]
5. return currentMax
Elementary Operations
• An elementary operation is an operation which takes constant time
regardless of problem size.
• The running time of an algorithm on a particular input is
determined by the number of “Elementary Operations” executed.
• Theoretical analysis on paper from a description of an algorithm
8
Instruction and Sequence
• A linear sequence of elementary operations is also performed in
constant time.
• More generally, given two program fragments P1 and P2 which run
sequentially in times t1 and t2
• use the maximum rule which states that the larger time dominates
• complexity will be max(t1,t2)
9
Sequences
• Analysing a group of consecutive statements
• The statement taking the maximum time will be the one counted
• use the maximum rule
Block #1 t1
T(n) = max(t1,t2)
Block #2 t2
for i = 1 to n do
P(i);
T(n) = nt
12
• This approach is reasonable, provided n is positive
• If n is zero or negative the relationship T(n) = nt is not valid
Repetition (Loops)
Analysing Nested Loops
for i = 0 to n do
for j = 0 to m do
P(j);
for i = 0 to n do
for j = 0 to i do
P(j);
i =0 ri + t
n n
T(n) = n + i =0 i
r 14
= n + n(n+1)/2 + tn(n+1)/2
Analysis Example
Algorithm: Number of times executed
1. n = read input from user 1
2. sum = 0 1
3. i = 0 1
4. while i < n n
n or i =0 1
n −1
5. number = read input from user
n or i =0 1
n −1
6. sum = sum + number
7. i=i+1
8. mean = sum / n
n or n −11
1 i =0
The computing time for this algorithm in terms on input size n is:
T(n) = 1 + 1 + 1 + n + n + n + n + 1 15
T(n) = 4n + 4
Another Analysis Example
i=1 ...............................1
while (i < n)................n-1
a=2+g...............n-1
i=i+1 ................n-1
if (i<=n).......................1
a=2 ....................1
else
a=3.....................1
T(n) = 1 + 3(n-1) + 1 + 1
16
=3n
Another Analysis Example
i=1...............................? 1
while (i<=10)................? 10
i=i+1...................? 10
i=1 ...............................? 1
while (i<=n)..................? n
a=2+g .................? n
i=i+1 ...................? n
if (i<=n)........................ ? 1
a=2 .................... ? 1
else
a=3..................... ? 1
17
T ( n) = ? T ( n) = 3n +24
Asymptotic Growth Rate
• Changing the hardware/software environment
• Affects T(n) by constant factor, but does not alter the growth rate of T(n)
0.05 N2 = O(N2)
3N = O(N)
Time (steps)
22
Input (size)
N = 60
Important Functions
These functions often appear in algorithm analysis:
23
A comparison of Growth-Rate Functions
Size does Matter:
What happens if we increase the input size N?
24
A comparison of Growth-Rate Functions
250
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
25
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
A comparison of Growth-Rate Functions
500
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 26
A comparison of Growth-Rate Functions
1000
f(n) = n
f(n) = log(n)
f(n) = n log(n)
f(n) = n^2
f(n) = n^3
f(n) = 2^n
0
1 3 5 7 9 11 13 15 17 19 27
A comparison of Growth-Rate Functions
5000
4000
f(n) = n
f(n) = log(n)
3000
f(n) = n log(n)
f(n) = n^2
2000 f(n) = n^3
f(n) = 2^n
1000
0
28
1
11
13
15
17
19
A comparison of Growth-Rate Functions
10000000
1000000
100000
10000
1000
100
10
1
1 4 16 64 256 1024 4096 16384 65536 29
Performance Classification
f(n) Classification
1
:
Constant run time is fixed, and does not depend upon n. Most instructions are
executed once, or only a few times, regardless of the amount of information being processed
log n Logarithmic: when n increases, so does run time, but much slower. Common in
programs which solve large problems by transforming them into smaller problems.
n Linear: run time varies directly with n. Typically, a small amount of processing is done on
each element.
n log n When n doubles, run time slightly more than doubles. Common in programs
which break a problem down into smaller sub-problems, solves them independently, then combines
solutions
n2 Quadratic: when n doubles, runtime increases fourfold. Practical only for small
problems; typically the program processes all pairs of input (e.g. in a double nested loop).
2n
30
Exponential: when n doubles, run time squares. This is often the result of a natural, “brute
force” solution.
Running Time vs. Time Complexity
• Running time is how long it takes a program to run.
Comparisons: 2n + 2
Time complexity is O(n).
34