Lec2-Analyzing Algos New
Lec2-Analyzing Algos New
Slides and figures have been collected from various publicly available Internet sources for
preparing the lecture slides of IT2001 course. I acknowledge and thank all the original authors
for their contribution to prepare the content.
Algorithm Specification (Pseudocode Conventions)
2
Analyzing Algorithms
◼ Criteria for judging algorithms that have a more direct relationship
to performance
❑ Storage requirement
❑ Computing time
◼ Analyzing an algorithm means predicting the resources that the
algo requires
❑ Most often it is computational time that we want to measure
◼ Generally, by analyzing several candidate algorithms for a problem,
a most efficient one can be easily identified
◼ Before we can analyze an algorithm, we must have a model of the
implementation technology that will be used, including a model for
the resources of that technology and their costs
3
Two main characteristics for programs
4
The Random Access Machine (RAM) Model
◼ We are assuming a generic one-processor, RAM model of
computation as our implementation technology, and our algos will be
implemented as a computer program
◼ In the RAM model, instructions are executed one after another, with
no concurrent operations
◼ The RAM model contains instructions commonly found in real
computer
❑ Arithmetic (add, subtract, multiply, divide, remainder, floor ceiling etc.)
❑ Data movement (load, store, copy)
❑ Control (conditional and unconditional branch, subroutine call and return )
◼ Each such instruction takes a constant amount of time
5
The RAM Model
6
Space Complexity
7
Space Complexity
◼ Data space
❑ Choosing a “smaller” data type has an effect on the
overall space usage of the program.
❑ Choosing the correct type is especially important when
working with arrays.
❑ How many bytes of memory are allocated with each of
the following declarations?
double a[100];
int matrix[rows][cols];
Components of Program Space
16
Time Complexity
◼ We desire an analytic framework that:
❑ Considers all possible inputs
❑ Allows us to evaluate the relative efficiency of any two algorithms in a way that
is independent from the h/w and s/w environment
❑ Can be performed by studying a high-level description of the algorithm without
implementing it or running experiments on it
◼ In general, the time taken by an algorithm grows with the size of the
input
◼ So, we are interested in determining the dependency of the running
time on the size of the input
◼ Analytic framework aims at associating a function f(n) with each
algorithm that characterizes the running time of the algorithm in terms
of the input size n
17
Time Complexity
◼ So, we need to define the terms “running time” and “size of input”
◼ Input size:
❑ Notion of input size depends on the problem being studied
❑ Ex: sorting: the most natural measure is the number of items in the input, array
size n
❑ Ex: if the input to an algorithm is a graph, the input size can be described by
the numbers of vertices and edges in the graph
◼ Running time of an algorithm on a particular input is the number of
primitive operations executed
❑ It is convenient to define the notion of step so that it is as machine-
independent as possible
18
Time Complexity
line
◼ We may assume that each execution of the ith line takes time ci,
where ci is a constant
19
Time Complexity
cost times Total operations
3 s:=0.0; c3 1 c3
6 return s; c6 1 c6
7} 0 - 0
Observations: (c4+c5)mn+(c3+c4)m+c3
Input size given by two numbers = amn+bm+c
If m>n: better to interchange the two for statements
If this is done total steps becomes amn+bn+c
21
Time Complexity
22
Example: Polynomial Evaluation
n n2/2 + n/2 n2
(Horner) (brute force)
5 15 25
10 55 100
20 210 400
100 5050 10000
1000 500500 1000000
Example: Polynomial Evaluation
600
500 f(n) = n2
T(n) = n2/2 + n/2
400
# of mult’s
300
200
100
g(n) = n
5 10 15 20 25 30 35
n (degree of polynomial)
Cases to Consider
◼ Best Case
❑ The least amount of work done for any input set
◼ Worst Case
❑ The most amount of work done for any input set
◼ Average Case
❑ The amount of work done averaged over all of the
possible input sets
Problem: Search
… Number 155778322
success.
Pseudocode for Serial Search
3 6 7 11 32 33 53
Binary Search
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
3 6 7 11 32 33 53
11
6 33
3 7 32 53
Search for target = 7
Find midpoint:
3 6 7 11 32 33 53
Start at root:
11
6 33
3 7 32 53
Search for target = 7
3 7 32 53
Search for target = 7
3 7 32 53
Search for target = 7
3 7 32 53
Time Complexity
◼ Remember our motive behind determining step counts:
❑ to be able to compare the time complexities of two algorithms that
62
Time Complexity
◼ One more simplifying abstraction: Order of growth
❑ We consider only the leading term in the formula, since lower order terms are
64
O-Notation (Big “oh” Notation)
◼ Asymptotic upper bound
◼ The function f(n)= O(g(n)) : read as “f of n is big oh of g of n”
◼ The function f(n)= O(g(n)) iff there exist positive
constants c and n0 such that f(n) ≤ cg(n) for all n, n ≥ n0
65
O-Notation
◼ Example: consider the function: 3n+2 = O(n)
❑ 3n+2 ≤ 4n for all n ≥ 2
◼ 10n2+2n+4 = O(n2) as 10n2+2n+4 ≤ 11n2 for all n ≥ 5
◼ 6*2n+n2 = O(2n) as 6*2n+n2 ≤ 7*2n for all n ≥ 4
◼ 2n+4 ≠ O(1)
◼ 10n2+2n+4 ≠ O(n)
◼ The statement f(n)= O(g(n)) states only that g(n) is an upper bound
on the value of f(n) for all n, n ≥ n0. It does not say anything about how
good this bound is.
66
Function values
log2n n nlog2n n2 n3 2n
0 1 0 1 1 2
1 2 2 4 8 4
2 4 8 16 64 16
3 8 24 64 512 256
68
References:
◼ Slides and figures have been collected from various Internet
sources for preparing the lecture slides of IT2001 course.
◼ I acknowledge and thank all the authors for the same.
◼ It is difficult to acknowledge all the sources though.
69