SlideShare a Scribd company logo
ECE 250 Algorithms and Data Structures
Douglas Wilhelm Harder, M.Math. LEL
Department of Electrical and Computer Engineering
University of Waterloo
Waterloo, Ontario, Canada
ece.uwaterloo.ca
dwharder@gmail.com
© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.
Algorithm Analysis
2
Asymptotic Analysis
Outline
In this topic, we will examine code to determine the run time of
various operations
We will introduce machine instructions
We will calculate the run times of:
– Operators +, -, =, +=, ++, etc.
– Control statements if, for, while, do-while, switch
– Functions
– Recursive functions
3
Asymptotic Analysis
Motivation
The goal of algorithm analysis is to take a block of code and
determine the asymptotic run time or asymptotic memory
requirements based on various parameters
– Given an array of size n:
• Selection sort requires Q(n2) time
• Merge sort, quick sort, and heap sort all require Q(n ln(n)) time
– However:
• Merge sort requires Q(n) additional memory
• Quick sort requires Q(ln(n)) additional memory
• Heap sort requires Q(1) memory
4
Asymptotic Analysis
Motivation
The asymptotic behaviour of algorithms indicates the ability to scale
– Suppose we are sorting an array of size n
Selection sort has a run time of Q(n2)
– 2n entries requires (2n)2 = 4n2
• Four times as long to sort
– 10n entries requires (10n)2 = 100n2
• One hundred times as long to sort
5
Asymptotic Analysis
Motivation
The other sorting algorithms have Q(n ln(n)) run times
– 2n entries require (2n) ln(2n) = (2n) (ln(n) + 1) = 2(n ln(n)) + 2n
– 10n entries require (10n) ln(10n) = (10n) (ln(n) + 1) = 10(n ln(n)) + 10n
In each case, it requires Q(n) more time
However:
– Merge sort will require twice and 10 times as much memory
– Quick sort will require one or four additional memory locations
– Heap sort will not require any additional memory
6
Asymptotic Analysis
Motivation
We will see algorithms which run in Q(nlg(3)) time
– lg(3) ≈ 1.585
– For 2n entries require (2n)lg(3) = 2lg(3) nlg(3) = 3 nlg(3)
• Three times as long to sort
– 10n entries require (10n)lg(3) = 10lg(3) nlg(3) = 38.5 nlg(3)
• 38 times as long to sort
7
Asymptotic Analysis
Motivation
We will see algorithms which run in Q(nlg(3)) time
– lg(3) ≈ 1.585
– For 2n entries require (2n)lg(3) = 2lg(3) nlg(3) = 3 nlg(3)
• Three times as long to sort
– 10n entries require (10n)lg(3) = 10lg(3) nlg(3) = 38.5 nlg(3)
• 38 times as long to sort
Binary search runs in Q(ln(n)) time:
– Doubling the size requires one additional search
8
Asymptotic Analysis
Motivation
If we are storing objects which are not related, the hash table has, in
many cases, optimal characteristics:
– Many operations are Q(1)
– I.e., the run times are independent of the number of objects being
stored
If we are required to store both objects and relations, both memory
and time will increase
– Our goal will be to minimize this increase
9
Asymptotic Analysis
Motivation
To properly investigate the determination of run times asymptotically:
– We will begin with machine instructions
– Discuss operations
– Control statements
• Conditional statements and loops
– Functions
– Recursive functions
10
Asymptotic Analysis
Machine Instructions
Given any processor, it is capable of performing only a limited
number of operations
These operations are called instructions
The collection of instructions is called the instruction set
– The exact set of instructions differs between processors
– MIPS, ARM, x86, 6800, 68k
– You will see another in the ColdFire in ECE 222
• Derived from the 68000, derived from the 6800
11
Asymptotic Analysis
Machine Instructions
Any instruction runs in a fixed amount of time (an integral number of
CPU cycles)
An example on the Coldfire is:
0x06870000000F
which adds 15 to the 7th data register
As humans are not good at hex, this can be programmed in
assembly language as
ADDI.L #$F, D7
– More in ECE 222
12
Asymptotic Analysis
Machine Instructions
Assembly language has an almost one-to-one translation to
machine instructions
– Assembly language is a low-level programming language
Other programming languages are higher-level:
Fortran, Pascal, Matlab, Java, C++, and C#
The adjective “high” refers to the level of abstraction:
– Java, C++, and C# have abstractions such as OO
– Matlab and Fortran have operations which do not map to relatively small
number of machine instructions:
>> 1.27^2.9 % 1.27**2.9 in Fortran
2.0000036616123606774
13
Asymptotic Analysis
Machine Instructions
The C programming language (C++ without objects and other
abstractions) can be referred to as a mid-level programming
language
– There is abstraction, but the language is closely tied to the standard
capabilities
– There is a closer relationship between operators and machine
instructions
Consider the operation a += b;
– Assume that the compiler has already has the value of the variable a in
register D1 and perhaps b is a variable stored at the location stored in
address register A1, this is then converted to the single instruction
ADD (A1), D1
14
Asymptotic Analysis
Operators
Because each machine instruction can be executed in a fixed
number of cycles, we may assume each operation requires a fixed
number of cycles
– The time required for any operator is Q(1) including:
• Retrieving/storing variables from memory
• Variable assignment =
• Integer operations + - * / % ++ --
• Logical operations && || !
• Bitwise operations & | ^ ~
• Relational operations == != < <= => >
• Memory allocation and deallocation new delete
15
Asymptotic Analysis
Operators
Of these, memory allocation and deallocation are the slowest by a
significant factor
– A quick test on eceunix shows a factor of over 100
– They require communication with the operation system
– This does not account for the time required to call the constructor and
destructor
Note that after memory is allocated, the constructor is run
– The constructor may not run in Q(1) time
16
Asymptotic Analysis
Blocks of Operations
Each operation runs in Q(1) time and therefore any fixed number
of operations also run in Q(1) time, for example:
// Swap variables a and b
int tmp = a;
a = b;
b = tmp;
// Update a sequence of values
// ece.uwaterloo.ca/~ece250/Algorithms/Skip_lists/src/Skip_list.h
++index;
prev_modulus = modulus;
modulus = next_modulus;
next_modulus = modulus_table[index];
17
Asymptotic Analysis
Blocks of Operations
Seldom will you find large blocks of operations without any
additional control statements
This example rearranges an AVL tree structure
Tree_node *lrl = left->right->left;
Tree_node *lrr = left->right->right;
parent = left->right;
parent->left = left;
parent->right = this;
left->right = lrl;
left = lrr;
Run time: Q(1)
18
Asymptotic Analysis
Blocks in Sequence
Suppose you have now analyzed a number of blocks of code run in
sequence
template <typename T>
void update_capacity( int delta ) {
T *array_old = array;
int capacity_old = array_capacity;
array_capacity += delta;
array = new T[array_capacity];
for ( int i = 0; i < capacity_old; ++i ) {
array[i] = array_old[i];
}
delete[] array_old;
}
To calculate the total run time, add the entries: Q(1 + n + 1) = Q(n)
Q(1)
Q(n)
Q(1) or W(n)
19
Asymptotic Analysis
Blocks in Sequence
This is taken from code found at
http://ece.uwaterloo.ca/~ece250/Algorithms/Sparse_systems/
template <int M, int N>
Matrix<M, N> &Matrix<M, N>::operator= ( Matrix<M, N> const &A ) {
if ( &A == this ) {
return *this;
}
if ( capacity != A.capacity ) {
delete [] column_index;
delete [] off_diagonal;
capacity = A.capacity;
column_index = new int[capacity];
off_diagonal = new double[capacity];
}
for ( int i = 0; i < minMN; ++i ) {
diagonal[i] = A.diagonal[i];
}
for ( int i = 0; i <= M; ++i ) {
row_index[i] = A.row_index[i];
}
for ( int i = 0; i < A.size(); ++i ) {
column_index[i] = A.column_index[i];
off_diagonal[i] = A.off_diagonal[i];
}
return *this;
}
Q(1)
Q(1)
Q(min(M, N))
Q(n)
Q(1)
Q(M)
Q(1 + 1 + min(M, N) + M + n + 1)
= Q(M + n)
Note that min(M, N) ≤ M but we
cannot say anything about M and n
20
Asymptotic Analysis
Blocks in Sequence
Other examples include:
– Run three blocks of code which are Q(1), Q(n2), and Q(n)
Total run time Q(1 + n2 + n) = Q(n2)
– Run two blocks of code which are Q(n ln(n)), and Q(n1.5)
Total run time Q(n ln(n) + n1.5) = Q(n1.5)
Recall this linear ordering from the previous topic
– When considering a sum, take the dominant term
21
Asymptotic Analysis
Control Statements
Next we will look at the following control statements
These are statements which potentially alter the execution of
instructions
– Conditional statements
if, switch
– Condition-controlled loops
for, while, do-while
– Count-controlled loops
for i from 1 to 10 do ... end do; # Maple
– Collection-controlled loops
foreach ( int i in array ) { ... } // C#
22
Asymptotic Analysis
Control Statements
Given any collection of nested control statements, it is always
necessary to work inside out
– Determine the run times of the inner-most statements and work your
way out
23
Asymptotic Analysis
Control Statements
Given
if ( condition ) {
// true body
} else {
// false body
}
The run time of a conditional statement is:
– the run time of the condition (the test), plus
– the run time of the body which is run
In most cases, the run time of the condition is Q(1)
24
Asymptotic Analysis
Control Statements
In some cases, it is easy to determine which statement must be run:
int factorial ( int n ) {
if ( n == 0 ) {
return 1;
} else {
return n * factorial ( n – 1 );
}
}
25
Asymptotic Analysis
Control Statements
In others, it is less obvious
– Find the maximum entry in an array:
int find_max( int *array, int n ) {
max = array[0];
for ( int i = 1; i < n; ++i ) {
if ( array[i] > max ) {
max = array[i];
}
}
return max;
}
26
Asymptotic Analysis
Analysis of Statements
In this case, we don’t know
If we had information about the distribution of the entries of the
array, we may be able to determine it
– if the list is sorted (ascending) it will always be run
– if the list is sorted (descending) it will be run once
– if the list is uniformly randomly distributed, then???
27
Asymptotic Analysis
Condition-controlled Loops
The C++ for loop is a condition controlled statement:
for ( int i = 0; i < N; ++i ) {
// ...
}
is identical to
int i = 0; // initialization
while ( i < N ) { // condition
// ...
++i; // increment
}
28
Asymptotic Analysis
Condition-controlled Loops
The initialization, condition, and increment usually are single
statements running in Q(1)
for ( int i = 0; i < N; ++i ) {
// ...
}
29
Asymptotic Analysis
Condition-controlled Loops
The initialization, condition, and increment statements are usually
Q(1)
For example,
for ( int i = 0; i < n; ++i ) {
// ...
}
Assuming there are no break or return statements in the loop,
the run time is W(n)
30
Asymptotic Analysis
Condition-controlled Loops
If the body does not depend on the variable (in this example, i),
then the run time of
for ( int i = 0; i < n; ++i ) {
// code which is Theta(f(n))
}
is Q(n f(n))
If the body is O(f(n)), then the run time of the loop is O(n f(n))
31
Asymptotic Analysis
Condition-controlled Loops
For example,
int sum = 0;
for ( int i = 0; i < n; ++i ) {
sum += 1; Theta(1)
}
This code has run time
Q(n·1) = Q(n)
32
Asymptotic Analysis
Condition-controlled Loops
Another example example,
int sum = 0;
for ( int i = 0; i < n; ++i ) {
for ( int j = 0; j < n; ++j ) {
sum += 1; Theta(1)
}
}
The previous example showed that the inner loop is Q(n), thus the
outer loop is
Q(n·n) = Q(n2)
33
Asymptotic Analysis
Analysis of Repetition Statements
Suppose with each loop, we use a linear search an array of size m:
for ( int i = 0; i < n; ++i ) {
// search through an array of size m
// O( m );
}
The inner loop is O(m) and thus the outer loop is
O(n m)
34
Asymptotic Analysis
Conditional Statements
Consider this example
void Disjoint_sets::clear() {
if ( sets == n ) {
return;
}
max_height = 0;
num_disjoint_sets = n;
for ( int i = 0; i < n; ++i ) {
parent[i] = i;
tree_height[i] = 0;
}
}
Q(n)
Q(1)
Q(1)



Q

Q

otherwise
)
(
)
1
(
)
(
Tclear
n
n
sets
n
Q(1)
35
Asymptotic Analysis
Analysis of Repetition Statements
If the body does depends on the variable (in this example, i), then
the run time of
for ( int i = 0; i < n; ++i ) {
// code which is Theta(f(i,n))
}
is and if the body is
O(f(i, n)), the result is
 
 
1
0
1 1 ,
n
i
f i n


 
Q  
 
 

 
 
1
0
O 1 1 ,
n
i
f i n


 
 
 
 

36
Asymptotic Analysis
Analysis of Repetition Statements
For example,
int sum = 0;
for ( int i = 0; i < n; ++i ) {
for ( int j = 0; j < i; ++j ) {
sum += i + j;
}
}
The inner loop is Q(1 + i(1 + 1) ) = Q(i) hence the outer is
 
 
 
1 1
2
0 0
1
1 1 1 1
2
n n
i i
n n
i n i n n
 
 

 
   
Q    Q    Q    Q
 
   
     
 
37
Asymptotic Analysis
Analysis of Repetition Statements
As another example:
int sum = 0;
for ( int i = 0; i < n; ++i ) {
for ( int j = 0; j < i; ++j ) {
for ( int k = 0; k < j; ++k ) {
sum += i + j + k;
}
}
}
From inside to out:
Q(1)
Q(j)
Q(i2)
Q(n3)
38
Asymptotic Analysis
Control Statements
Switch statements appear to be nested if statements:
switch( i ) {
case 1: /* do stuff */ break;
case 2: /* do other stuff */ break;
case 3: /* do even more stuff */ break;
case 4: /* well, do stuff */ break;
case 5: /* tired yet? */ break;
default: /* do default stuff */
}
39
Asymptotic Analysis
Control Statements
Thus, a switch statement would appear to run in O(n) time where n
is the number of cases, the same as nested if statements
– Why then not use:
if ( i == 1 ) { /* do stuff */ }
else if ( i == 2 ) { /* do other stuff */ }
else if ( i == 3 ) { /* do even more stuff */ }
else if ( i == 4 ) { /* well, do stuff */ }
else if ( i == 5 ) { /* tired yet? */ }
else { /* do default stuff */ }
40
Asymptotic Analysis
Control Statements
Question:
Why would you introduce something into
programming language which is redundant?
There are reasons for this:
– your name is Larry Wall and you are creating the Perl (not PERL)
programming language
– you are introducing software engineering constructs, for example,
classes
41
Asymptotic Analysis
Control Statements
However, switch statements were included in the original C
language... why?
First, you may recall that the cases must be actual values, either:
– integers
– characters
For example, you cannot have a case with a variable, e.g.,
case n: /* do something */ break; //bad
42
Asymptotic Analysis
Control Statements
The compiler looks at the different cases and calculates an
appropriate jump
For example, assume:
– the cases are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
– each case requires a maximum of 24 bytes (for example, six
instructions)
Then the compiler simply makes a jump size based on the variable,
jumping ahead either 0, 24, 48, 72, ..., or 240 instructions
43
Asymptotic Analysis
Serial Statements
Suppose we run one block of code followed by another block of
code
Such code is said to be run serially
If the first block of code is O(f(n)) and the second is O(g(n)), then the
run time of two blocks of code is
O( f(n) + g(n) )
which usually (for algorithms not including function calls) simplifies
to one or the other
44
Asymptotic Analysis
Serial Statements
Consider the following two problems:
– search through a random list of size n to find the maximum entry, and
– search through a random list of size n to find if it contains a particular
entry
What is the proper means of describing the run time of these two
algorithms?
45
Asymptotic Analysis
Serial Statements
Searching for the maximum entry requires that each element in the
array be examined, thus, it must run in Q(n) time
Searching for a particular entry may end earlier: for example, the
first entry we are searching for may be the one we are looking for,
thus, it runs in O(n) time
46
Asymptotic Analysis
Serial Statements
Therefore:
– if the leading term is big-Q, then the result must be big-Q, otherwise
– if the leading term is big-O, we can say the result is big-O
For example,
O(n) + O(n2) + O(n4) = O(n + n2 + n4) = O(n4)
O(n) + Q(n2) = Q(n2)
O(n2) + Q(n) = O(n2)
O(n2) + Q(n2) = Q(n2)
47
Asymptotic Analysis
Functions
A function (or subroutine) is code which has been separated out,
either to:
– and repeated operations
• e.g., mathematical functions
– group related tasks
• e.g., initialization
48
Asymptotic Analysis
Functions
Because a subroutine (function) can be called from anywhere, we
must:
– prepare the appropriate environment
– deal with arguments (parameters)
– jump to the subroutine
– execute the subroutine
– deal with the return value
– clean up
49
Asymptotic Analysis
Functions
Fortunately, this is such a common task that all modern processors
have instructions that perform most of these steps in one instruction
Thus, we will assume that the overhead required to make a function
call and to return is Q(1) an
– We will discuss this later (stacks/ECE 222)
50
Asymptotic Analysis
Functions
Because any function requires the overhead of a function call and
return, we will always assume that
Tf = W(1)
That is, it is impossible for any function call to have a zero run time
51
Asymptotic Analysis
Functions
Thus, given a function f(n) (the run time of which depends on n) we
will associate the run time of f(n) by some function Tf(n)
– We may write this to T(n)
Because the run time of any function is at least O(1), we will include
the time required to both call and return from the function in the run
time
52
Asymptotic Analysis
Functions
Consider this function:
void Disjoint_sets::set_union( int m, int n ) {
m = find( m );
n = find( n );
if ( m == n ) {
return;
}
--num_disjoint_sets;
if ( tree_height[m] >= tree_height[n] ) {
parent[n] = m;
if ( tree_height[m] == tree_height[n] ) {
++( tree_height[m] );
max_height = std::max( max_height, tree_height[m] );
}
} else {
parent[m] = n;
}
}
Q(1)
2Tfind
Tset_union= 2Tfind + Q(1)
53
Asymptotic Analysis
Recursive Functions
A function is relatively simple (and boring) if it simply performs
operations and calls other functions
Most interesting functions designed to solve problems usually end
up calling themselves
– Such a function is said to be recursive
54
Asymptotic Analysis
Recursive Functions
As an example, we could implement the factorial function
recursively:
int factorial( int n ) {
if ( n <= 1 ) {
return 1;
} else {
return n * factorial( n – 1 );
}
}
(1)
Q
T( 1) (1)
n  Q
!
55
Asymptotic Analysis
Recursive Functions
Thus, we may analyze the run time of this function as follows:
We don’t have to worry about the time of the conditional (Q(1)) nor is
there a probability involved with the conditional statement








1
)
1
(
)
1
(
T
1
)
1
(
)
(
T
n
n
n
n
Θ
Θ
!
!
56
Asymptotic Analysis
Recursive Functions
The analysis of the run time of this function yields a recurrence
relation:
T!(n) = T!(n – 1) + Q(1) T!(1) = Q(1)
This recurrence relation has Landau symbols…
– Replace each Landau symbol with a representative function:
T!(n) = T!(n – 1) + 1 T!(1) = 1
57
Asymptotic Analysis
Recursive Functions
Thus, to find the run time of the factorial function, we need to solve
T!(n) = T!(n – 1) + 1 T!(1) = 1
The fastest way to solve this is with Maple:
> rsolve( {T(n) = T(n – 1) + 1, T(1) = 1}, T(n) );
n
Thus, T!(n) = Q(n)
58
Asymptotic Analysis
Recursive Functions
Unfortunately, you don’t have Maple on the examination, thus, we
can examine the first few steps:
T!(n) = T!(n – 1) + 1
= T!(n – 2) + 1 + 1 = T!(n – 2) + 2
= T!(n – 3) + 3
From this, we see a pattern:
T!(n) = T!(n – k) + k
59
Asymptotic Analysis
Recursive Functions
If k = n – 1 then
T!(n) = T!(n – (n – 1)) + n – 1
= T!(1) + n – 1
= 1 + n – 1 = n
Thus, T!(n) = Q(n)
60
Asymptotic Analysis
Recursive Functions
Incidentally, we may write the factorial function using the ternary ?:
operator
– Ternary operators take three arguments—C++ has only one
int factorial( int n ) {
return (n <= 1) ? 1 : n * factorial( n – 1 );
}
61
Asymptotic Analysis
Recursive Functions
Suppose we want to sort a array of n items
We could:
– go through the list and find the largest item
– swap the last entry in the list with that largest item
– then, go on and sort the rest of the array
This is called selection sort
62
Asymptotic Analysis
Recursive Functions
void sort( int * array, int n ) {
if ( n <= 1 ) {
return; // special case: 0 or 1 items are always sorted
}
int posn = 0; // assume the first entry is the smallest
int max = array[posn];
for ( int i = 1; i < n; ++i ) { // search through the remaining entries
if ( array[i] > max ) { // if a larger one is found
posn = i; // update both the position and value
max = array[posn];
}
}
int tmp = array[n - 1]; // swap the largest entry with the last
array[n - 1] = array[posn];
array[posn] = tmp;
sort( array, n – 1 ); // sort everything else
}
63
Asymptotic Analysis
Recursive Functions
We could call this function as follows:
int array[7] = {5, 8, 3, 6, 2, 4, 7};
sort( array, 7 ); // sort an array of seven items
64
Asymptotic Analysis
Recursive Functions
The first call finds the largest element
65
Asymptotic Analysis
Recursive Functions
The next call finds the 2nd-largest element
66
Asymptotic Analysis
Recursive Functions
The third finds the 3rd-largest
67
Asymptotic Analysis
Recursive Functions
And the 4th
68
Asymptotic Analysis
Recursive Functions
And the 5th
69
Asymptotic Analysis
Recursive Functions
Finally the 6th
70
Asymptotic Analysis
Recursive Functions
And the array is sorted:
71
Asymptotic Analysis
Recursive Functions
Analyzing the function, we get:
72
Asymptotic Analysis
Recursive Functions
Thus, replacing each Landau symbol with a representative, we are
required to solve the recurrence relation
T(n) = T(n – 1) + n T(1) = 1
The easy way to solve this is with Maple:
> rsolve( {T(n) = T(n – 1) + n, T(1) = 1}, T(n) );
> expand( % );
  
1 n ( )

n 1









n
2
1

1
2
n
1
2
n
2
73
Asymptotic Analysis
Recursive Functions
Consequently, the sorting routine has the run time
T(n) = Q(n2)
To see this by hand, consider the following
 
2
)
1
(
1
)
1
T(
)
2
(
)
1
(
)
3
T(
)
1
(
)
2
T(
)
1
(
)
2
T(
)
1
T(
)
T(
1
2
2





























 


n
n
i
i
i
n
n
n
n
n
n
n
n
n
n
n
n
n
n
i
n
i
n
i

74
Asymptotic Analysis
Recursive Functions
Consider, instead, a binary search of a sorted list:
– Check the middle entry
– If we do not find it, check either the left- or right-hand side, as
appropriate
Thus, T(n) = T((n – 1)/2) + Q(1)
75
Asymptotic Analysis
Recursive Functions
Also, if n = 1, then T(1) = Q(1)
Thus we have to solve:
Solving this can be difficult, in general, so we will consider only
special values of n
Assume n = 2k – 1 where k is an integer
Then (n – 1)/2 = (2k – 1 – 1)/2 = 2k – 1 – 1












 

 1
1
2
1
T
1
1
)
T( n
n
n
n
76
Asymptotic Analysis
Recursive Functions
For example, searching a list of size 31 requires us to check the
center
If it is not found, we must check one of the two halves, each of which
is size 15
31 = 25 – 1
15 = 24 – 1
77
Asymptotic Analysis
Recursive Functions
Thus, we can write

2
)
1
2
T(
1
1
2
1
1
2
T
1
)
1
2
T(
1
2
1
1
2
T
)
1
2
T(
)
T(
2
1
1












 













 







k
k
k
k
k
n
78
Asymptotic Analysis
Recursive Functions
Notice the pattern with one more step:

3
)
1
2
T(
2
)
1
2
T(
1
1
2
1
1
2
T
1
)
1
2
T(
3
2
1
1















 









k
k
k
k
79
Asymptotic Analysis
Recursive Functions
Thus, in general, we may deduce that after k – 1 steps:
because T(1) = 1
k
k
k
n
k
k
k












1
)
1
T(
1
)
1
2
T(
)
1
2
T(
)
T(
)
1
(
80
Asymptotic Analysis
Recursive Functions
Thus, T(n) = k, but n = 2k – 1
Therefore k = lg(n + 1)
However, recall that f(n) = Q(g(n)) if for
Thus, T(n) = Q(lg(n + 1)) = Q(ln(n))
 
 
   
     
1
lg 1 1 ln 2 1
lim lim lim
1
ln 1 ln 2 ln 2
n n n
n n n
n n
n
  
 
  

 
 
lim
n
f n
c
g n

 0 c
  
81
Asymptotic Analysis
Cases
As well as determining the run time of an algorithm, because the
data may not be deterministic, we may be interested in:
– Best-case run time
– Average-case run time
– Worst-case run time
In many cases, these will be significantly different
82
Asymptotic Analysis
Cases
Searching a list linearly is simple enough
We will count the number of comparisons
– Best case:
• The first element is the one we’re looking for: O(1)
– Worst case:
• The last element is the one we’re looking for, or it is not in the list: O(n)
– Average case?
• We need some information about the list...
83
Asymptotic Analysis
Cases
Assume the case we are looking for is in the list and equally likely
distributed
If the list is of size n, then there is a 1/n chance of it being in the kth
location
Thus, we sum
which is O(n)
1
1 1 ( 1) 1
2 2
n
k
n n n
k
n n

 
 

84
Asymptotic Analysis
Cases
Suppose we have a different distribution:
– there is a 50% chance that the element is the first
– for each subsequent element, the probability is reduced by ½
We could write:
1 1
?
2 2
n
k k
k k
i i

 
 
 
85
Asymptotic Analysis
Cases
You’re not expected to know this for the final, however, for interest:
> sum( k/2^k, k = 1..infinity );
2
Thus, the average case is O(1)
86
Asymptotic Analysis
Cases
Previously, we had an example where we were looking for the
number of times a particular assignment statement was executed:
int find_max( int * array, int n ) {
max = array[0];
for ( int i = 1; i < n; ++i ) {
if ( array[i] > max ) {
max = array[i];
}
}
return max;
}
87
Asymptotic Analysis
Cases
This example is taken from Preiss
– The best case was once (first element is largest)
– The worst case was n times
For the average case, we must consider:
– What is the probability that the ith object is the largest of the first i
objects?
88
Asymptotic Analysis
Cases
To consider this question, we must assume that elements in the
array are evenly distributed
Thus, given a sub-list of size k, the probability that any one element
is the largest is 1/k
Thus, given a value i, there are i + 1 objects, hence
?
1
1
1
1
1
0


 
 


n
i
n
i
i
i
89
Asymptotic Analysis
Cases
We can approximate the sum by an integral – what is the area
under:
90
Asymptotic Analysis
Cases
We can approximate this by the 1/(x + 1) integrated from 0 to n
91
Asymptotic Analysis
Cases
From calculus:
How about the error? Our approximation would be useless if the
error was O(n)
1
1
1
0 1
1 1
ln( ) ln( 1) ln(1) ln( 1)
1
n n
n
dx dx x n n
x x


      

 
92
Asymptotic Analysis
Cases
Consider the following image which highlights the errors
– The errors can be fit into the box [0, 1] × [0, 1]
93
Asymptotic Analysis
Cases
Consequently, the error must be < 1
In fact, it converges to g ≈ 0.57721566490
– Therefore, the error is Q(1)
94
Asymptotic Analysis
Cases
Thus, the number of times that the assignment statement will be
executed, assuming an even distribution is O(ln(n))
95
Asymptotic Analysis
Cases
Thus, the total run of:
int find_max( int *array, int n ) {
max = array[0];
for ( int i = 1; i < n; ++i ) {
if ( array[i] > max ) {
max = array[i];
}
}
return max;
}
is    
1
1
1
1 1 1 ln( )
1
n
i
n n n
i


 
 
Q    Q    Q
 
 

 
 

96
Asymptotic Analysis
Summary
In these slides we have looked at:
– The run times of
• Operators
• Control statements
• Functions
• Recursive functions
– We have also defined best-, worst-, and average-case scenarios
We will be considering all of these each time we inspect any
algorithm used in this class
97
Asymptotic Analysis
Summary
In this topic, we looked at:
– Justification for using analysis
– Quadratic and polynomial growth
– Counting machine instructions
– Landau symbols o O QWw
– Big-Q as an equivalence relation
– Little-o as a linear ordering for the equivalence classes
98
Asymptotic Analysis
References
Wikipedia, https://en.wikipedia.org/wiki/Mathematical_induction
These slides are provided for the ECE 250 Algorithms and Data Structures course. The
material in it reflects Douglas W. Harder’s best judgment in light of the information available to
him at the time of preparation. Any reliance on these course slides by any party for any other
purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for
damages, if any, suffered by any party as a result of decisions made or actions based on these
course slides for any other purpose than that for which it was intended.

More Related Content

PPTX
Analysis of algorithms
PDF
Parallel R in snow (english after 2nd slide)
PPTX
Analysis of Algorithms_Under Graduate Class Slide
PPTX
VCE Unit 01 (1).pptx
PPT
Design and analysis of algorithm in Computer Science
PPTX
Design Analysis of Alogorithm 1 ppt 2024.pptx
PPTX
Analysis of Algorithm full version 2024.pptx
PPTX
Data Structure Algorithm -Algorithm Complexity
Analysis of algorithms
Parallel R in snow (english after 2nd slide)
Analysis of Algorithms_Under Graduate Class Slide
VCE Unit 01 (1).pptx
Design and analysis of algorithm in Computer Science
Design Analysis of Alogorithm 1 ppt 2024.pptx
Analysis of Algorithm full version 2024.pptx
Data Structure Algorithm -Algorithm Complexity

Similar to Algorithm analysis.pptx (20)

PDF
DAA - chapter 1.pdf
PPT
Code Tuning
PDF
Concurrency
PPTX
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
PDF
CS-323 DAA.pdf
PDF
In Memory Database In Action by Tanel Poder and Kerry Osborne
PDF
Oracle Database In-Memory Option in Action
PPTX
synchronization in operating system structure
PPTX
AA_Unit 1_part-I.pptx
PDF
04 performance
PPTX
Unit i basic concepts of algorithms
PPT
lecture1.ppt
PPT
C++ Notes PPT.ppt
PPTX
Searching Algorithms
PPTX
System integration through queues
PDF
Data Analytics and Simulation in Parallel with MATLAB*
PPT
14 queuing
PDF
Python for Machine Learning
PPTX
Design & Analysis of Algorithm course .pptx
PDF
Verification of Concurrent and Distributed Systems
DAA - chapter 1.pdf
Code Tuning
Concurrency
Data Structures and Agorithm: DS 22 Analysis of Algorithm.pptx
CS-323 DAA.pdf
In Memory Database In Action by Tanel Poder and Kerry Osborne
Oracle Database In-Memory Option in Action
synchronization in operating system structure
AA_Unit 1_part-I.pptx
04 performance
Unit i basic concepts of algorithms
lecture1.ppt
C++ Notes PPT.ppt
Searching Algorithms
System integration through queues
Data Analytics and Simulation in Parallel with MATLAB*
14 queuing
Python for Machine Learning
Design & Analysis of Algorithm course .pptx
Verification of Concurrent and Distributed Systems
Ad

More from DrBashirMSaad (14)

PDF
googlechromedevelopertools-120424180007-phpapp01.pdf
PDF
Pico-R3-A4 Pinout for ARM embedded system
PDF
Arabic Fonts character foreigners for learner
PPT
DS basics functions.ppt
PDF
Searching Informed Search.pdf
PPT
procress and threads.ppt
PPTX
2.02.Data_structures_and_algorithms (1).pptx
PDF
1b-150720094704-lva1-app6892.pdf
PPT
01_intro-cpp.ppt
PPT
Lecture 7-BinarySearchTrees.ppt
PPT
lec06-programming.ppt
PPT
CS351-L1.ppt
PPT
Matlab_Simulink_Tutorial.ppt
PDF
002 AWSSlides.pdf
googlechromedevelopertools-120424180007-phpapp01.pdf
Pico-R3-A4 Pinout for ARM embedded system
Arabic Fonts character foreigners for learner
DS basics functions.ppt
Searching Informed Search.pdf
procress and threads.ppt
2.02.Data_structures_and_algorithms (1).pptx
1b-150720094704-lva1-app6892.pdf
01_intro-cpp.ppt
Lecture 7-BinarySearchTrees.ppt
lec06-programming.ppt
CS351-L1.ppt
Matlab_Simulink_Tutorial.ppt
002 AWSSlides.pdf
Ad

Recently uploaded (20)

PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
history of c programming in notes for students .pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
iTop VPN Free 5.6.0.5262 Crack latest version 2025
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Why Generative AI is the Future of Content, Code & Creativity?
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
wealthsignaloriginal-com-DS-text-... (1).pdf
Digital Systems & Binary Numbers (comprehensive )
Operating system designcfffgfgggggggvggggggggg
How to Choose the Right IT Partner for Your Business in Malaysia
Designing Intelligence for the Shop Floor.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Understanding Forklifts - TECH EHS Solution
history of c programming in notes for students .pptx
Wondershare Filmora 15 Crack With Activation Key [2025
iTop VPN Free 5.6.0.5262 Crack latest version 2025
Which alternative to Crystal Reports is best for small or large businesses.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PTS Company Brochure 2025 (1).pdf.......
Navsoft: AI-Powered Business Solutions & Custom Software Development
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Why Generative AI is the Future of Content, Code & Creativity?
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus

Algorithm analysis.pptx

  • 1. ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca dwharder@gmail.com © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Algorithm Analysis
  • 2. 2 Asymptotic Analysis Outline In this topic, we will examine code to determine the run time of various operations We will introduce machine instructions We will calculate the run times of: – Operators +, -, =, +=, ++, etc. – Control statements if, for, while, do-while, switch – Functions – Recursive functions
  • 3. 3 Asymptotic Analysis Motivation The goal of algorithm analysis is to take a block of code and determine the asymptotic run time or asymptotic memory requirements based on various parameters – Given an array of size n: • Selection sort requires Q(n2) time • Merge sort, quick sort, and heap sort all require Q(n ln(n)) time – However: • Merge sort requires Q(n) additional memory • Quick sort requires Q(ln(n)) additional memory • Heap sort requires Q(1) memory
  • 4. 4 Asymptotic Analysis Motivation The asymptotic behaviour of algorithms indicates the ability to scale – Suppose we are sorting an array of size n Selection sort has a run time of Q(n2) – 2n entries requires (2n)2 = 4n2 • Four times as long to sort – 10n entries requires (10n)2 = 100n2 • One hundred times as long to sort
  • 5. 5 Asymptotic Analysis Motivation The other sorting algorithms have Q(n ln(n)) run times – 2n entries require (2n) ln(2n) = (2n) (ln(n) + 1) = 2(n ln(n)) + 2n – 10n entries require (10n) ln(10n) = (10n) (ln(n) + 1) = 10(n ln(n)) + 10n In each case, it requires Q(n) more time However: – Merge sort will require twice and 10 times as much memory – Quick sort will require one or four additional memory locations – Heap sort will not require any additional memory
  • 6. 6 Asymptotic Analysis Motivation We will see algorithms which run in Q(nlg(3)) time – lg(3) ≈ 1.585 – For 2n entries require (2n)lg(3) = 2lg(3) nlg(3) = 3 nlg(3) • Three times as long to sort – 10n entries require (10n)lg(3) = 10lg(3) nlg(3) = 38.5 nlg(3) • 38 times as long to sort
  • 7. 7 Asymptotic Analysis Motivation We will see algorithms which run in Q(nlg(3)) time – lg(3) ≈ 1.585 – For 2n entries require (2n)lg(3) = 2lg(3) nlg(3) = 3 nlg(3) • Three times as long to sort – 10n entries require (10n)lg(3) = 10lg(3) nlg(3) = 38.5 nlg(3) • 38 times as long to sort Binary search runs in Q(ln(n)) time: – Doubling the size requires one additional search
  • 8. 8 Asymptotic Analysis Motivation If we are storing objects which are not related, the hash table has, in many cases, optimal characteristics: – Many operations are Q(1) – I.e., the run times are independent of the number of objects being stored If we are required to store both objects and relations, both memory and time will increase – Our goal will be to minimize this increase
  • 9. 9 Asymptotic Analysis Motivation To properly investigate the determination of run times asymptotically: – We will begin with machine instructions – Discuss operations – Control statements • Conditional statements and loops – Functions – Recursive functions
  • 10. 10 Asymptotic Analysis Machine Instructions Given any processor, it is capable of performing only a limited number of operations These operations are called instructions The collection of instructions is called the instruction set – The exact set of instructions differs between processors – MIPS, ARM, x86, 6800, 68k – You will see another in the ColdFire in ECE 222 • Derived from the 68000, derived from the 6800
  • 11. 11 Asymptotic Analysis Machine Instructions Any instruction runs in a fixed amount of time (an integral number of CPU cycles) An example on the Coldfire is: 0x06870000000F which adds 15 to the 7th data register As humans are not good at hex, this can be programmed in assembly language as ADDI.L #$F, D7 – More in ECE 222
  • 12. 12 Asymptotic Analysis Machine Instructions Assembly language has an almost one-to-one translation to machine instructions – Assembly language is a low-level programming language Other programming languages are higher-level: Fortran, Pascal, Matlab, Java, C++, and C# The adjective “high” refers to the level of abstraction: – Java, C++, and C# have abstractions such as OO – Matlab and Fortran have operations which do not map to relatively small number of machine instructions: >> 1.27^2.9 % 1.27**2.9 in Fortran 2.0000036616123606774
  • 13. 13 Asymptotic Analysis Machine Instructions The C programming language (C++ without objects and other abstractions) can be referred to as a mid-level programming language – There is abstraction, but the language is closely tied to the standard capabilities – There is a closer relationship between operators and machine instructions Consider the operation a += b; – Assume that the compiler has already has the value of the variable a in register D1 and perhaps b is a variable stored at the location stored in address register A1, this is then converted to the single instruction ADD (A1), D1
  • 14. 14 Asymptotic Analysis Operators Because each machine instruction can be executed in a fixed number of cycles, we may assume each operation requires a fixed number of cycles – The time required for any operator is Q(1) including: • Retrieving/storing variables from memory • Variable assignment = • Integer operations + - * / % ++ -- • Logical operations && || ! • Bitwise operations & | ^ ~ • Relational operations == != < <= => > • Memory allocation and deallocation new delete
  • 15. 15 Asymptotic Analysis Operators Of these, memory allocation and deallocation are the slowest by a significant factor – A quick test on eceunix shows a factor of over 100 – They require communication with the operation system – This does not account for the time required to call the constructor and destructor Note that after memory is allocated, the constructor is run – The constructor may not run in Q(1) time
  • 16. 16 Asymptotic Analysis Blocks of Operations Each operation runs in Q(1) time and therefore any fixed number of operations also run in Q(1) time, for example: // Swap variables a and b int tmp = a; a = b; b = tmp; // Update a sequence of values // ece.uwaterloo.ca/~ece250/Algorithms/Skip_lists/src/Skip_list.h ++index; prev_modulus = modulus; modulus = next_modulus; next_modulus = modulus_table[index];
  • 17. 17 Asymptotic Analysis Blocks of Operations Seldom will you find large blocks of operations without any additional control statements This example rearranges an AVL tree structure Tree_node *lrl = left->right->left; Tree_node *lrr = left->right->right; parent = left->right; parent->left = left; parent->right = this; left->right = lrl; left = lrr; Run time: Q(1)
  • 18. 18 Asymptotic Analysis Blocks in Sequence Suppose you have now analyzed a number of blocks of code run in sequence template <typename T> void update_capacity( int delta ) { T *array_old = array; int capacity_old = array_capacity; array_capacity += delta; array = new T[array_capacity]; for ( int i = 0; i < capacity_old; ++i ) { array[i] = array_old[i]; } delete[] array_old; } To calculate the total run time, add the entries: Q(1 + n + 1) = Q(n) Q(1) Q(n) Q(1) or W(n)
  • 19. 19 Asymptotic Analysis Blocks in Sequence This is taken from code found at http://ece.uwaterloo.ca/~ece250/Algorithms/Sparse_systems/ template <int M, int N> Matrix<M, N> &Matrix<M, N>::operator= ( Matrix<M, N> const &A ) { if ( &A == this ) { return *this; } if ( capacity != A.capacity ) { delete [] column_index; delete [] off_diagonal; capacity = A.capacity; column_index = new int[capacity]; off_diagonal = new double[capacity]; } for ( int i = 0; i < minMN; ++i ) { diagonal[i] = A.diagonal[i]; } for ( int i = 0; i <= M; ++i ) { row_index[i] = A.row_index[i]; } for ( int i = 0; i < A.size(); ++i ) { column_index[i] = A.column_index[i]; off_diagonal[i] = A.off_diagonal[i]; } return *this; } Q(1) Q(1) Q(min(M, N)) Q(n) Q(1) Q(M) Q(1 + 1 + min(M, N) + M + n + 1) = Q(M + n) Note that min(M, N) ≤ M but we cannot say anything about M and n
  • 20. 20 Asymptotic Analysis Blocks in Sequence Other examples include: – Run three blocks of code which are Q(1), Q(n2), and Q(n) Total run time Q(1 + n2 + n) = Q(n2) – Run two blocks of code which are Q(n ln(n)), and Q(n1.5) Total run time Q(n ln(n) + n1.5) = Q(n1.5) Recall this linear ordering from the previous topic – When considering a sum, take the dominant term
  • 21. 21 Asymptotic Analysis Control Statements Next we will look at the following control statements These are statements which potentially alter the execution of instructions – Conditional statements if, switch – Condition-controlled loops for, while, do-while – Count-controlled loops for i from 1 to 10 do ... end do; # Maple – Collection-controlled loops foreach ( int i in array ) { ... } // C#
  • 22. 22 Asymptotic Analysis Control Statements Given any collection of nested control statements, it is always necessary to work inside out – Determine the run times of the inner-most statements and work your way out
  • 23. 23 Asymptotic Analysis Control Statements Given if ( condition ) { // true body } else { // false body } The run time of a conditional statement is: – the run time of the condition (the test), plus – the run time of the body which is run In most cases, the run time of the condition is Q(1)
  • 24. 24 Asymptotic Analysis Control Statements In some cases, it is easy to determine which statement must be run: int factorial ( int n ) { if ( n == 0 ) { return 1; } else { return n * factorial ( n – 1 ); } }
  • 25. 25 Asymptotic Analysis Control Statements In others, it is less obvious – Find the maximum entry in an array: int find_max( int *array, int n ) { max = array[0]; for ( int i = 1; i < n; ++i ) { if ( array[i] > max ) { max = array[i]; } } return max; }
  • 26. 26 Asymptotic Analysis Analysis of Statements In this case, we don’t know If we had information about the distribution of the entries of the array, we may be able to determine it – if the list is sorted (ascending) it will always be run – if the list is sorted (descending) it will be run once – if the list is uniformly randomly distributed, then???
  • 27. 27 Asymptotic Analysis Condition-controlled Loops The C++ for loop is a condition controlled statement: for ( int i = 0; i < N; ++i ) { // ... } is identical to int i = 0; // initialization while ( i < N ) { // condition // ... ++i; // increment }
  • 28. 28 Asymptotic Analysis Condition-controlled Loops The initialization, condition, and increment usually are single statements running in Q(1) for ( int i = 0; i < N; ++i ) { // ... }
  • 29. 29 Asymptotic Analysis Condition-controlled Loops The initialization, condition, and increment statements are usually Q(1) For example, for ( int i = 0; i < n; ++i ) { // ... } Assuming there are no break or return statements in the loop, the run time is W(n)
  • 30. 30 Asymptotic Analysis Condition-controlled Loops If the body does not depend on the variable (in this example, i), then the run time of for ( int i = 0; i < n; ++i ) { // code which is Theta(f(n)) } is Q(n f(n)) If the body is O(f(n)), then the run time of the loop is O(n f(n))
  • 31. 31 Asymptotic Analysis Condition-controlled Loops For example, int sum = 0; for ( int i = 0; i < n; ++i ) { sum += 1; Theta(1) } This code has run time Q(n·1) = Q(n)
  • 32. 32 Asymptotic Analysis Condition-controlled Loops Another example example, int sum = 0; for ( int i = 0; i < n; ++i ) { for ( int j = 0; j < n; ++j ) { sum += 1; Theta(1) } } The previous example showed that the inner loop is Q(n), thus the outer loop is Q(n·n) = Q(n2)
  • 33. 33 Asymptotic Analysis Analysis of Repetition Statements Suppose with each loop, we use a linear search an array of size m: for ( int i = 0; i < n; ++i ) { // search through an array of size m // O( m ); } The inner loop is O(m) and thus the outer loop is O(n m)
  • 34. 34 Asymptotic Analysis Conditional Statements Consider this example void Disjoint_sets::clear() { if ( sets == n ) { return; } max_height = 0; num_disjoint_sets = n; for ( int i = 0; i < n; ++i ) { parent[i] = i; tree_height[i] = 0; } } Q(n) Q(1) Q(1)    Q  Q  otherwise ) ( ) 1 ( ) ( Tclear n n sets n Q(1)
  • 35. 35 Asymptotic Analysis Analysis of Repetition Statements If the body does depends on the variable (in this example, i), then the run time of for ( int i = 0; i < n; ++i ) { // code which is Theta(f(i,n)) } is and if the body is O(f(i, n)), the result is     1 0 1 1 , n i f i n     Q            1 0 O 1 1 , n i f i n           
  • 36. 36 Asymptotic Analysis Analysis of Repetition Statements For example, int sum = 0; for ( int i = 0; i < n; ++i ) { for ( int j = 0; j < i; ++j ) { sum += i + j; } } The inner loop is Q(1 + i(1 + 1) ) = Q(i) hence the outer is       1 1 2 0 0 1 1 1 1 1 2 n n i i n n i n i n n            Q    Q    Q    Q              
  • 37. 37 Asymptotic Analysis Analysis of Repetition Statements As another example: int sum = 0; for ( int i = 0; i < n; ++i ) { for ( int j = 0; j < i; ++j ) { for ( int k = 0; k < j; ++k ) { sum += i + j + k; } } } From inside to out: Q(1) Q(j) Q(i2) Q(n3)
  • 38. 38 Asymptotic Analysis Control Statements Switch statements appear to be nested if statements: switch( i ) { case 1: /* do stuff */ break; case 2: /* do other stuff */ break; case 3: /* do even more stuff */ break; case 4: /* well, do stuff */ break; case 5: /* tired yet? */ break; default: /* do default stuff */ }
  • 39. 39 Asymptotic Analysis Control Statements Thus, a switch statement would appear to run in O(n) time where n is the number of cases, the same as nested if statements – Why then not use: if ( i == 1 ) { /* do stuff */ } else if ( i == 2 ) { /* do other stuff */ } else if ( i == 3 ) { /* do even more stuff */ } else if ( i == 4 ) { /* well, do stuff */ } else if ( i == 5 ) { /* tired yet? */ } else { /* do default stuff */ }
  • 40. 40 Asymptotic Analysis Control Statements Question: Why would you introduce something into programming language which is redundant? There are reasons for this: – your name is Larry Wall and you are creating the Perl (not PERL) programming language – you are introducing software engineering constructs, for example, classes
  • 41. 41 Asymptotic Analysis Control Statements However, switch statements were included in the original C language... why? First, you may recall that the cases must be actual values, either: – integers – characters For example, you cannot have a case with a variable, e.g., case n: /* do something */ break; //bad
  • 42. 42 Asymptotic Analysis Control Statements The compiler looks at the different cases and calculates an appropriate jump For example, assume: – the cases are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 – each case requires a maximum of 24 bytes (for example, six instructions) Then the compiler simply makes a jump size based on the variable, jumping ahead either 0, 24, 48, 72, ..., or 240 instructions
  • 43. 43 Asymptotic Analysis Serial Statements Suppose we run one block of code followed by another block of code Such code is said to be run serially If the first block of code is O(f(n)) and the second is O(g(n)), then the run time of two blocks of code is O( f(n) + g(n) ) which usually (for algorithms not including function calls) simplifies to one or the other
  • 44. 44 Asymptotic Analysis Serial Statements Consider the following two problems: – search through a random list of size n to find the maximum entry, and – search through a random list of size n to find if it contains a particular entry What is the proper means of describing the run time of these two algorithms?
  • 45. 45 Asymptotic Analysis Serial Statements Searching for the maximum entry requires that each element in the array be examined, thus, it must run in Q(n) time Searching for a particular entry may end earlier: for example, the first entry we are searching for may be the one we are looking for, thus, it runs in O(n) time
  • 46. 46 Asymptotic Analysis Serial Statements Therefore: – if the leading term is big-Q, then the result must be big-Q, otherwise – if the leading term is big-O, we can say the result is big-O For example, O(n) + O(n2) + O(n4) = O(n + n2 + n4) = O(n4) O(n) + Q(n2) = Q(n2) O(n2) + Q(n) = O(n2) O(n2) + Q(n2) = Q(n2)
  • 47. 47 Asymptotic Analysis Functions A function (or subroutine) is code which has been separated out, either to: – and repeated operations • e.g., mathematical functions – group related tasks • e.g., initialization
  • 48. 48 Asymptotic Analysis Functions Because a subroutine (function) can be called from anywhere, we must: – prepare the appropriate environment – deal with arguments (parameters) – jump to the subroutine – execute the subroutine – deal with the return value – clean up
  • 49. 49 Asymptotic Analysis Functions Fortunately, this is such a common task that all modern processors have instructions that perform most of these steps in one instruction Thus, we will assume that the overhead required to make a function call and to return is Q(1) an – We will discuss this later (stacks/ECE 222)
  • 50. 50 Asymptotic Analysis Functions Because any function requires the overhead of a function call and return, we will always assume that Tf = W(1) That is, it is impossible for any function call to have a zero run time
  • 51. 51 Asymptotic Analysis Functions Thus, given a function f(n) (the run time of which depends on n) we will associate the run time of f(n) by some function Tf(n) – We may write this to T(n) Because the run time of any function is at least O(1), we will include the time required to both call and return from the function in the run time
  • 52. 52 Asymptotic Analysis Functions Consider this function: void Disjoint_sets::set_union( int m, int n ) { m = find( m ); n = find( n ); if ( m == n ) { return; } --num_disjoint_sets; if ( tree_height[m] >= tree_height[n] ) { parent[n] = m; if ( tree_height[m] == tree_height[n] ) { ++( tree_height[m] ); max_height = std::max( max_height, tree_height[m] ); } } else { parent[m] = n; } } Q(1) 2Tfind Tset_union= 2Tfind + Q(1)
  • 53. 53 Asymptotic Analysis Recursive Functions A function is relatively simple (and boring) if it simply performs operations and calls other functions Most interesting functions designed to solve problems usually end up calling themselves – Such a function is said to be recursive
  • 54. 54 Asymptotic Analysis Recursive Functions As an example, we could implement the factorial function recursively: int factorial( int n ) { if ( n <= 1 ) { return 1; } else { return n * factorial( n – 1 ); } } (1) Q T( 1) (1) n  Q !
  • 55. 55 Asymptotic Analysis Recursive Functions Thus, we may analyze the run time of this function as follows: We don’t have to worry about the time of the conditional (Q(1)) nor is there a probability involved with the conditional statement         1 ) 1 ( ) 1 ( T 1 ) 1 ( ) ( T n n n n Θ Θ ! !
  • 56. 56 Asymptotic Analysis Recursive Functions The analysis of the run time of this function yields a recurrence relation: T!(n) = T!(n – 1) + Q(1) T!(1) = Q(1) This recurrence relation has Landau symbols… – Replace each Landau symbol with a representative function: T!(n) = T!(n – 1) + 1 T!(1) = 1
  • 57. 57 Asymptotic Analysis Recursive Functions Thus, to find the run time of the factorial function, we need to solve T!(n) = T!(n – 1) + 1 T!(1) = 1 The fastest way to solve this is with Maple: > rsolve( {T(n) = T(n – 1) + 1, T(1) = 1}, T(n) ); n Thus, T!(n) = Q(n)
  • 58. 58 Asymptotic Analysis Recursive Functions Unfortunately, you don’t have Maple on the examination, thus, we can examine the first few steps: T!(n) = T!(n – 1) + 1 = T!(n – 2) + 1 + 1 = T!(n – 2) + 2 = T!(n – 3) + 3 From this, we see a pattern: T!(n) = T!(n – k) + k
  • 59. 59 Asymptotic Analysis Recursive Functions If k = n – 1 then T!(n) = T!(n – (n – 1)) + n – 1 = T!(1) + n – 1 = 1 + n – 1 = n Thus, T!(n) = Q(n)
  • 60. 60 Asymptotic Analysis Recursive Functions Incidentally, we may write the factorial function using the ternary ?: operator – Ternary operators take three arguments—C++ has only one int factorial( int n ) { return (n <= 1) ? 1 : n * factorial( n – 1 ); }
  • 61. 61 Asymptotic Analysis Recursive Functions Suppose we want to sort a array of n items We could: – go through the list and find the largest item – swap the last entry in the list with that largest item – then, go on and sort the rest of the array This is called selection sort
  • 62. 62 Asymptotic Analysis Recursive Functions void sort( int * array, int n ) { if ( n <= 1 ) { return; // special case: 0 or 1 items are always sorted } int posn = 0; // assume the first entry is the smallest int max = array[posn]; for ( int i = 1; i < n; ++i ) { // search through the remaining entries if ( array[i] > max ) { // if a larger one is found posn = i; // update both the position and value max = array[posn]; } } int tmp = array[n - 1]; // swap the largest entry with the last array[n - 1] = array[posn]; array[posn] = tmp; sort( array, n – 1 ); // sort everything else }
  • 63. 63 Asymptotic Analysis Recursive Functions We could call this function as follows: int array[7] = {5, 8, 3, 6, 2, 4, 7}; sort( array, 7 ); // sort an array of seven items
  • 64. 64 Asymptotic Analysis Recursive Functions The first call finds the largest element
  • 65. 65 Asymptotic Analysis Recursive Functions The next call finds the 2nd-largest element
  • 66. 66 Asymptotic Analysis Recursive Functions The third finds the 3rd-largest
  • 72. 72 Asymptotic Analysis Recursive Functions Thus, replacing each Landau symbol with a representative, we are required to solve the recurrence relation T(n) = T(n – 1) + n T(1) = 1 The easy way to solve this is with Maple: > rsolve( {T(n) = T(n – 1) + n, T(1) = 1}, T(n) ); > expand( % );    1 n ( )  n 1          n 2 1  1 2 n 1 2 n 2
  • 73. 73 Asymptotic Analysis Recursive Functions Consequently, the sorting routine has the run time T(n) = Q(n2) To see this by hand, consider the following   2 ) 1 ( 1 ) 1 T( ) 2 ( ) 1 ( ) 3 T( ) 1 ( ) 2 T( ) 1 ( ) 2 T( ) 1 T( ) T( 1 2 2                                  n n i i i n n n n n n n n n n n n n n i n i n i 
  • 74. 74 Asymptotic Analysis Recursive Functions Consider, instead, a binary search of a sorted list: – Check the middle entry – If we do not find it, check either the left- or right-hand side, as appropriate Thus, T(n) = T((n – 1)/2) + Q(1)
  • 75. 75 Asymptotic Analysis Recursive Functions Also, if n = 1, then T(1) = Q(1) Thus we have to solve: Solving this can be difficult, in general, so we will consider only special values of n Assume n = 2k – 1 where k is an integer Then (n – 1)/2 = (2k – 1 – 1)/2 = 2k – 1 – 1                 1 1 2 1 T 1 1 ) T( n n n n
  • 76. 76 Asymptotic Analysis Recursive Functions For example, searching a list of size 31 requires us to check the center If it is not found, we must check one of the two halves, each of which is size 15 31 = 25 – 1 15 = 24 – 1
  • 77. 77 Asymptotic Analysis Recursive Functions Thus, we can write  2 ) 1 2 T( 1 1 2 1 1 2 T 1 ) 1 2 T( 1 2 1 1 2 T ) 1 2 T( ) T( 2 1 1                                     k k k k k n
  • 78. 78 Asymptotic Analysis Recursive Functions Notice the pattern with one more step:  3 ) 1 2 T( 2 ) 1 2 T( 1 1 2 1 1 2 T 1 ) 1 2 T( 3 2 1 1                           k k k k
  • 79. 79 Asymptotic Analysis Recursive Functions Thus, in general, we may deduce that after k – 1 steps: because T(1) = 1 k k k n k k k             1 ) 1 T( 1 ) 1 2 T( ) 1 2 T( ) T( ) 1 (
  • 80. 80 Asymptotic Analysis Recursive Functions Thus, T(n) = k, but n = 2k – 1 Therefore k = lg(n + 1) However, recall that f(n) = Q(g(n)) if for Thus, T(n) = Q(lg(n + 1)) = Q(ln(n))               1 lg 1 1 ln 2 1 lim lim lim 1 ln 1 ln 2 ln 2 n n n n n n n n n              lim n f n c g n   0 c   
  • 81. 81 Asymptotic Analysis Cases As well as determining the run time of an algorithm, because the data may not be deterministic, we may be interested in: – Best-case run time – Average-case run time – Worst-case run time In many cases, these will be significantly different
  • 82. 82 Asymptotic Analysis Cases Searching a list linearly is simple enough We will count the number of comparisons – Best case: • The first element is the one we’re looking for: O(1) – Worst case: • The last element is the one we’re looking for, or it is not in the list: O(n) – Average case? • We need some information about the list...
  • 83. 83 Asymptotic Analysis Cases Assume the case we are looking for is in the list and equally likely distributed If the list is of size n, then there is a 1/n chance of it being in the kth location Thus, we sum which is O(n) 1 1 1 ( 1) 1 2 2 n k n n n k n n      
  • 84. 84 Asymptotic Analysis Cases Suppose we have a different distribution: – there is a 50% chance that the element is the first – for each subsequent element, the probability is reduced by ½ We could write: 1 1 ? 2 2 n k k k k i i       
  • 85. 85 Asymptotic Analysis Cases You’re not expected to know this for the final, however, for interest: > sum( k/2^k, k = 1..infinity ); 2 Thus, the average case is O(1)
  • 86. 86 Asymptotic Analysis Cases Previously, we had an example where we were looking for the number of times a particular assignment statement was executed: int find_max( int * array, int n ) { max = array[0]; for ( int i = 1; i < n; ++i ) { if ( array[i] > max ) { max = array[i]; } } return max; }
  • 87. 87 Asymptotic Analysis Cases This example is taken from Preiss – The best case was once (first element is largest) – The worst case was n times For the average case, we must consider: – What is the probability that the ith object is the largest of the first i objects?
  • 88. 88 Asymptotic Analysis Cases To consider this question, we must assume that elements in the array are evenly distributed Thus, given a sub-list of size k, the probability that any one element is the largest is 1/k Thus, given a value i, there are i + 1 objects, hence ? 1 1 1 1 1 0         n i n i i i
  • 89. 89 Asymptotic Analysis Cases We can approximate the sum by an integral – what is the area under:
  • 90. 90 Asymptotic Analysis Cases We can approximate this by the 1/(x + 1) integrated from 0 to n
  • 91. 91 Asymptotic Analysis Cases From calculus: How about the error? Our approximation would be useless if the error was O(n) 1 1 1 0 1 1 1 ln( ) ln( 1) ln(1) ln( 1) 1 n n n dx dx x n n x x            
  • 92. 92 Asymptotic Analysis Cases Consider the following image which highlights the errors – The errors can be fit into the box [0, 1] × [0, 1]
  • 93. 93 Asymptotic Analysis Cases Consequently, the error must be < 1 In fact, it converges to g ≈ 0.57721566490 – Therefore, the error is Q(1)
  • 94. 94 Asymptotic Analysis Cases Thus, the number of times that the assignment statement will be executed, assuming an even distribution is O(ln(n))
  • 95. 95 Asymptotic Analysis Cases Thus, the total run of: int find_max( int *array, int n ) { max = array[0]; for ( int i = 1; i < n; ++i ) { if ( array[i] > max ) { max = array[i]; } } return max; } is     1 1 1 1 1 1 ln( ) 1 n i n n n i       Q    Q    Q          
  • 96. 96 Asymptotic Analysis Summary In these slides we have looked at: – The run times of • Operators • Control statements • Functions • Recursive functions – We have also defined best-, worst-, and average-case scenarios We will be considering all of these each time we inspect any algorithm used in this class
  • 97. 97 Asymptotic Analysis Summary In this topic, we looked at: – Justification for using analysis – Quadratic and polynomial growth – Counting machine instructions – Landau symbols o O QWw – Big-Q as an equivalence relation – Little-o as a linear ordering for the equivalence classes
  • 98. 98 Asymptotic Analysis References Wikipedia, https://en.wikipedia.org/wiki/Mathematical_induction These slides are provided for the ECE 250 Algorithms and Data Structures course. The material in it reflects Douglas W. Harder’s best judgment in light of the information available to him at the time of preparation. Any reliance on these course slides by any party for any other purpose are the responsibility of such parties. Douglas W. Harder accepts no responsibility for damages, if any, suffered by any party as a result of decisions made or actions based on these course slides for any other purpose than that for which it was intended.