chapter Two Analysis of Algorithms
chapter Two Analysis of Algorithms
1
Learning Outcomes
• Explain the basic techniques for the analysis of algorithms;
3
Definition (Algorithm)
• What exactly is an algorithm?
INSERTION-SORT(A)
1. for j = 2 to length[A]
2. do key A[j]
3. //insert A[j] to sorted sequence A[1..j-1]
4. i j-1
5. while i >0 and A[i]>key
6. do A[i+1] A[i] //move A[i] one position
right
7. i i-1
8. A[i+1] key
6
Example Algorithm (2)
• Finding the maximum element problem
– That is, given array A=[31, 41, 26, 41, 58], the maximum
algorithm returns 58 which is the maximum element in
the array
7
Example Algorithm (2)
An algorithm for finding the maximum element
Input: An array A storing n integers
Output:The maximum element in A
find_max(A,n):
currentMax A[0]
for i=1 to n-1 Do:
if A[i] > currentMax Theb
currentMax=A[i]
return currentMax
8
Example Algorithm (3)
• Finding the greatest common devisor problem of
two natural numbers
– Problem statement
• Determine the greatest common devisor of two natural
numbers x and y (where x < y )
– Input
• Two natural numbers a and b
– Output
• The largest natural number d which is the common
devisor of a and b
9
Example Algorithm (3)
• Algorithm for finding the greatest common
devisor of two natural numbers problem
10
Example Algorithm (3)
• GCD(33,21)=3
• 33 = 1*21 + 12
• 21 = 1*12 + 9
• 12 = 1*9 + 3
• 9 = 3*3
11
Purpose Algorithm in programs
• To accept input
– There are zero or more quantities which are externally
supplied
• To produce an output
– At least one quantity is produced
– The inputs are the data that will be transformed during the
computation to produce the output
– We must specify
• the type of data
• The amount of data
14
Essential Properties of Algorithms
• Output specified
– The outputs are the data resulting from the computation (the
intended result)
15
Essential Properties of Algorithms
• Finiteness (It Must Terminate)
– An algorithm must have the property of finiteness
– That is, every valid algorithm should have finite number of
steps
– It must complete after a finite number of steps
– It must eventually stop either with the right output or with a
statement that no solution is possible
– If you trace out the instructions of an algorithm, then for all
cases the algorithm must terminate after a finite number of
steps
– Finiteness is an issue for computer algorithms because if the
algorithm doesn’t specify when to stop (computer algorithms
often repeat instructions), the computer will continue to
repeat the instructions for ever
16
Essential Properties of Algorithms
• Definiteness (Absence of Ambiguity)
19
Essential Properties of Algorithms
• Correctness
– The first step (start step) and last step (halt step) must be
clearly noted
22
Essential Properties of Algorithms
• Simplicity
23
Essential Properties of Algorithms
• Language Independence
– Completeness
25
Analysis of algorithms
• What does it mean by analyzing algorithms?
26
Analysis of algorithms
• The objective of algorithm analysis is
– to determine how quickly an algorithm executes in
practice
– To measure either time or space requirements of an
algorithm
• What to measure
– Space utilization- amount of memory required
– Time efficiency- amount of time required to process the
data
27
Analysis of algorithms
• What to analyze
– The most important recourse to analyze is generally the
running time
– Several factors affect the running time of an a program
• Compiler used
• Computer used
• The algorithm used
• The input to the algorithm
– The first two are beyond the scope of theoretical model
– Although thy are important, we will not deal with them in the
course
– The last two are the main factors that we deal
– Typically, the size of the input is an important consideration
28
Analysis of algorithms
• Space utilization and Time efficiency depends on
many factors
– Size of input
– Speed of machine (computer used)
– Quality of source code (used algorithm)
– Quality of compiler
29
Running Time of Algorithm
• The running time of an algorithm depends on a number of
factors
• But, for most algorithm, the running time depends on the input
– An already sorted sequence is easier to sort
– Empirically
– Theoretically
31
Empirical Analysis
• What you should do
– Run the program with the data sets (the inputs) of varying
size and composition
32
Empirical Analysis
7000
like this 6000
Time (ms)
5000
4000
3000
2000
1000
0
0 50 100
I nput Size
33
Empirical Analysis
• Is an experimental study
34
Limitations of Empirical Analysis
• It can’t be used in estimating the efficiency of algorithms
because the result varies due to variations in
– Processor speed
– Input size
– SW environment
36
Limitations of Empirical Analysis
• The difficulty to know which instances to test it on
37
Theoretical Analysis
• Is in contrast to the “experimental approach”
38
Theoretical Analysis
• Takes an algorithm and produces a function T(n) which
depends on the number of operations
39
Theoretical Analysis
• Rather it uses the number of operations (which are expressed in
time units) because the number of operations do not vary with
– Processor speed
– Input size
– Current processor load
– SW environment
40
Theoretical Analysis
• What will be the number of operations for the algorithm
below using a computer having
– 100 MHZ and 750 MHZ
– Using DOS OS
– Using UNIX OS
– While printing, browsing
41
Theoretical Analysis
• Is an approach that can be used to measure or estimate the
complexity of an algorithm as the approach do not vary due to
variations in the computer systems
42
How do measure complexity of algorithms
• Two steps or phases for this
– Order of magnitude
43
How do measure complexity of algorithms
• Step one - analysis of algorithm
45
Calculating complexity of an algorithm
Step one - analysis of algorithm
– Primitive operations
• Identifiable in pseudocode
46
Primitive or Basic Operations
• Step one - analysis of algorithm
– Examples of basic operations could be
• An assignment (Assigning a value to a variable)
• Calling a method
• Returning from a method
• Evaluating an expression
– An arithmetic operation between two variable (e.g.,
Performing an arithmetic operations such as addition)
– A Comparison between to variables (two numbers)
– Indexing into an array
47
Primitive or Basic Operations
48
Running time- T(n)
• We measure execution (running) time in terms of any of the
following
– Arithmetic operations
– Assignment operations
– Loop iterations
– Comparisons
– Procedure calls (calling a method)
– Returning from a method
49
Order of Magnitude
• Refers to the rate at which the storage or time grows as a
function of problem size
• Since, we are giving the answer in terms of Big-Oh, there are lots
of short cuts that can be taken without affecting the final answer
– In real life, not all operations take exactly the same time
54
Analysis Rules
3. If-then-else statement
55
Analysis Rules
• Example - Running time of if-then- else statement
If (x>5)
executed for (i=1; i<=n; i++)
n times constant time
cout << i
else
cout << “hello”; constant time
56
Analysis Rules
• Example - Running time of if-then- else statement
59
Analysis Rules
5. Running time of a Loop statement
The running time of a loop is, at most, the running
time of the statements inside the loop (including
tests) multiplied by the number of iterations.
60
Analysis Rules
6. Running time of Nested Loop
• Analyze inside out
• The running time of a statement inside a group of nested
loops is the running time of the statement multiplied by the
product of the sizes of all the loops
62
Analysis Rules
• Example
63
1.int count(){ Time Units to Compute
int k=0; -------------------------------------------
------
cout<< “Enter an 1 for the assignment statement:
integer”; int k=0
cin>>n; 1 for the output statement.
for (i=0;i<n;i++) 1 for the input statement.
k=k+1; In the for loop:
1 assignment, n+1 tests, and n
return 0; increments.
} n loops of 2 units for an
assignment, and an addition.
1 for the return statement.
T (n)= 1+1+1+(1+n+1+n)+2n+1 =
4n+6 = O(n)
64
Time Units to Compute
3. void func() ------------------------------------------------
{ -
int x=0; 1 for the first assignment statement:
int i=0; x=0;
int j=1; 1 for the second assignment
cout<< “Enter an Integer value”; statement: i=0;
cin>>n; 1 for the third assignment statement:
while (i<n){ j=1;
x++; 1 for the output statement.
i++; 1 for the input statement.
} In the first while loop:
while (j<n) n+1 tests
{ n loops of 2 units for the two
j++; increment (addition) operations
} In the second while loop:
} n tests
n-1 increments
------------------------------------------------
-------------------
T (n)= 1+1+1+1+1+n+1+2n+n+n-1 =65
5n+5 = O(n)
2 Time Units to Compute
int total(int n) ------------------------------------------------
{ -
int sum=0; 1 for the assignment statement: int
for (int i=1;i<=n;i++) sum=0
sum=sum+1; In the for loop:
return sum; 1 assignment, n+1 tests, and n
} increments.
n loops of 2 units for an assignment,
and an addition.
1 for the return statement.
T (n)= 1+ (1+n+1+n)+2n+1 = 4n+4 =
O(n)
66
14. int sum (int n) +(1+n+1+n)+2n+1 = 4n+6 = O(n)
{ Time Units to Compute
int partial_sum = 0; ------------------------------------------------
for (int i = 1; i <= n; i++) -
partial_sum = partial_sum 1 for the assignment.
+(i * i * i); 1 assignment, n+1 tests, and n
return partial_sum; increments.
} n loops of 4 units for an assignment,
an addition, and two multiplications.
1 for the return statement.
------------------------------------------------
-------------------
T (n)= 1+(1+n+1+n)+4n+1 = 6n+4 =
O(n)
67
Formal Approach to Analysis
•For Loops: Formally
– In general, a for loop translates to a summation.
The index and bounds of the summation are the
same as the index and bounds of the for loop.
N
f
or(
in
ti=1
;i<
=N;i
++){
}
s
um =
su
m +
i
;
1N
i1
68
• Nested Loops: Formally
– Nested for loops translate into multiple
summations, one for each for loop.
}
sum= sum+i+j; 2 2M 2MN
i
1 j
1 i
1
}
69
• Consecutive Statements: Formally
• Add the running times of the separate
blocks of your code
71
if (test ==1) {
for (inti =1; i <=N; i++) { N N N
sum=sum+i;
max 1, 2
}}
i1 i1 j1
elsefor (inti =1; i <=N; i++) {
for (int j =1; j <=N; j++) { max
2
N, 2N 2N 2
sum=sum+i+j;
}}
72
if (test ==1) {
for (inti =1; i <=N; i++) { N N N
sum=sum+i; 1, 2
max
}}
i1 i1 j1
elsefor (inti =1; i <=N; i++) {
for (int j =1; j <=N; j++) { max 2
N, 2N 2N 2
sum=sum+i+j;
}}
73
Algorithm Analysis Categories
• An algorithm may run faster on certain data sets than
others
74
Best Case Analysis
• Best case is defined as which input of size n is the cheapest
among all inputs of size n
• Best case is obtained from the in the input data set that results in
best possible performance
75
Best Case Analysis
• Input
– Assumes the input data are found in the most advantageous
order for the algorithm
– For sorting, the most advantageous order is that data are
arranged in the required order
– For searching, the best case is that the required item is
found at the first position
– Question
• Which order is the most advantageous for an algorithm
that sorts 2 5 8 1 4 (ascending order)
• Input size
– Assumes that the input size is the minimum possible
76
Best Case Analysis
• This case executes or causes the fewest number of executions
• So, it computes the lower bound of T(n) and You can not do better
78
Worst Case Analysis
• Input (i.e., arrangement of data)
– Assumes that the data are arranged in the most
disadvantageous order
– For sorting, the worst case assumes that data are arranged in
opposite order
– For searching, the worst case assumes that the required item
is found at last position or missing
• Input size
– Assumes the input size to be infinite
– That is, it assumes that the input size is a very large number
– That is, it considers n infinity
79
Worst Case Analysis
• This case, causes highest number of executions to be performed
by the algorithm
80
Worst Case Analysis
• Worst case is easier to analyze and can yield useful
information
81
Average Case Analysis
• Is the complexity of an algorithm averaged over all inputs of
each size
• Computes the optimal (the one which is found most of the time)
bound of T(n)
82
Average Case Analysis
• Input (i.e., arrangement of data)
• Input size
85
Average Case Analysis
• As a result, in several of the algorithms, we limit our
analysis to determining the best and worst counts
– Easier to analyze
86
Graphical view of best, average and worst cases
best case
• The running
average case
time of an worst case
algorithm 120
input and
Running Time
80
typically 60
grows with the
40
input size
20
0
1000 2000 3000 4000
Input Size
87
Which Analysis to Use?
• Question
– How can we determine the complexity of an algorithm on any
data set?
– When is the worst case time important?
89
Why use a worst case analysis
2. For some algorithms, the worst case occurs fairly often
– That is , Worst case "may" be a typical case
Conclusion
91
Asymptotic Analysis
•Asymptotic analysis is concerned with how the running time
of an algorithm increases with the size of the input in the limit,
as the size of the input increases without bound.
•There are five notations used to describe a running time
function. These are:
• Big-Oh Notation (O)
• Big-Omega Notation ()
• Theta Notation ()
• Little-o Notation (o)
• Little-Omega Notation ()
92
The Big-Oh Notation
•It’s only concerned with what happens for very a large value of n.
•Therefore only the largest term in the expression (function) is needed.
•Big-Oh is mainly concerned with large values of n
•Big-O expresses an upper bound on the growth rate of a function, for
94
2. f(n) = 3n2 +4n+1. Show that f(n)=O(n2).
4n <=4n2 for all n>=1 and 1<=n2 for all n>=1
3n2 +4n+1<=3n2+4n2+n2 for all n>=1
<=8n2 for all n>=1
So we have shown that f(n)<=8n2 for all n>=1
Therefore, f (n) is O(n2) (c=8,k=1)
95
• Typical Orders
N O(1) O(log n) O(n) O(n log n) O(n2) O(n3)
1 1 1 1 1 1 1
2 1 1 2 2 4 8
4 1 2 4 8 16 64
8 1 3 8 24 64 512
16 1 4 16 64 256 4,096
96
Exercise:
f(n) = (3/2)n2+(5/2)n-3
Show that f(n)= O(n2)
97
Big-O Theorems
For all the following theorems, assume that f(n) is a
function of n and that k is an arbitrary constant.
Theorem 1: k is O(1)
Theorem 2: A polynomial is O(the term containing the
highest power of n).
Polynomial’s growth rate is determined by the leading term
If f(n) is a polynomial of degree d, then f(n) is O(nd)
•In general, f(n) is big-O of the dominant term of f(n).
98
Theorem 3: k*f(n) is O(f(n))
Constant factors may be ignored
E.g. f(n) =7n4+3n2+5n+1000 is O(n4)
•Theorem 4(Transitivity): If f(n) is O(g(n))and g(n) is
O(h(n)), then f(n) is O(h(n)).
99
• Theorem 6: Each of the following functions is big-O of its
successors:
• k
• logbn
• n
• nlogbn
• n2
• n to higher powers
• 2n
• 3n
• larger constants to the nth power
• n!
• nn
• f(n)= 3nlogbn + 4 logbn+2 is O(nlogbn) and )(n2) and O(2n)
100
Properties of the O Notation
•Higher powers grow faster
nr = O( ns) if 0 <= r <= s
101
• Exponential functions grow faster than powers,
i.e. nk =O( bn ) ,b > 1 and k >= 0
E.g. n20 is O( 1.05n)
102
Big-Omega Notation
•Just as O-notation provides an asymptotic upper bound
on a function, notation provides an asymptotic lower
bound.
103
Example: If f(n) =n2, then f(n)= ( n)
•In simple terms, f(n)= ( g (n)) means that
the growth rate of f(n) is greater that or equal
to g(n).
104
Theta Notation
•A function f (n) belongs to the set of (g(n)) if there exist positive
constants c1 and c2 such that it can be sandwiched between c1.g(n)
and c2.g(n), for sufficiently large values of n.
•Formal Definition: A function f (n) is (g(n)) if it is both O( g(n) )
and ( g(n) ).
•In other words, there exist constants c1, c2, and k >0 such that c1.g
(n)<=f(n)<=c2. g(n) for all n >= k
•If f(n)= (g(n)), then g(n) is an asymptotically tight bound for f(n).
•In simple terms, f(n)= (g(n)) means that f(n) and g(n) have the same
rate of growth.
105
Example:
1. If f(n)=2n+1, then f(n) = (n)
2. f(n) =2n2 then
f(n)=O(n4)
f(n)=O(n3)
f(n)= O (n2)
•All these are technically correct, but the last expression
is the best and tight one. Since 2n2 and n2 have the same
growth rate, it can be written as f(n)= (n2).
106
• Little-o Notation
• Big-Oh notation may or may not be asymptotically tight, for
example:
• 2n2 = O(n2)
• =O(n3)
• f(n)=o(g(n)) means for all c>0 there exists some k>0 such that
f(n)<c.g(n) for all n>=k. Informally, f(n)=o(g(n)) means f(n)
becomes insignificant relative to g(n) as n approaches infinity.
• Example: f(n)=3n+4 is o(n2)
• In simple terms, f(n) has less growth rate compared to g(n).
• g(n)= 2n2 g(n) =o(n3), O(n2), g(n) is not o(n2).
107
• Little-Omega ( notation)
• Little-omega () notation is to big-omega ()
notation as little-o notation is to Big-Oh notation. We
use notation to denote a lower bound that is not
asymptotically tight.
• Formal Definition: f(n)= (g(n)) if there exists a
constant no>0 such that 0<= c. g(n)<f(n) for all n>=k .
108
Example: 2n2=(n) but it’s not (n).
109
Symmetry
f(n)=(g(n)) if and only if g(n)=(f(n)).
Transpose symmetry
f(n)=O(g(n)) if and only if g(n)=(g(n),
f(n)=o(g(n)) if and only if g(n)=(g(n)).
Reflexivity
f(n)=(f(n)),
f(n)=O(f(n)),
f(n)=(f(n)).
110