DESIGN AND ANALYSIS OF
ALGORITHMS
CS 611
Chapter Two
1 1/25/2024 Dr. E. Fadel
2.1 Asymptotic Notation
2 Introduction
2 1/25/2024 Dr. E. Fadel
Asymptotic Notation
The main purpose of algorithm analysis is to give performance guarantees.
Example guarantees are:
Have bounds on running time
Accurate
Concise
General
Easy to understand.
It is difficult to meet all these criteria simultaneously.
3 1/25/2024 Dr. E. Fadel
Asymptotic Notation
One way to characterize the running time Τ of an algorithm
is as following:
For any problem instance 𝑖, Τ(𝑖) is the running time on 𝑖.
where 𝑖 ∈ Ι the set of all inputs.
This level of detail is so overwhelming that we could not
possibly derive a theory about it.
4 1/25/2024 Dr. E. Fadel
Asymptotic Notation
A more global view of an algorithm’s performance is to
classify problem instances into classes based on size.
For example,
The size of an integer is the number of digits in its representation.
The size of a set is the number of elements in that set.
The size of an instance is always a natural number.
5 1/25/2024 Dr. E. Fadel
Asymptotic Notation
We use size(𝑖) to denote the size of instance 𝑖, and Ι𝑛 to
denote the set of instances of size 𝑛 for 𝑛 ∈ ℕ.
For the inputs of size 𝑛, we are interested in the maximum,
minimum, and average execution times.
6 1/25/2024 Dr. E. Fadel
Asymptotic Notation
Execution times have three cases:
Worst case: T(n) = max { T(i): i ∈ 𝐼𝑛}
Best case: T(n) = min { T(i): i ∈ 𝐼𝑛}
1
Average case: T(n) = σi ∈𝐼𝑛 T(i)}
|𝐼𝑛|
7 1/25/2024 Dr. E. Fadel
Asymptotic Analysis
An additional step is used to reduce data size
which, is growth rate or asymptotic analysis.
Functions 𝑓 𝑛 and 𝑔(𝑛) have the same growth rate
if there are positive constants c and d such that 𝑐
≤ 𝑓 𝑛 Τ𝑔 𝑛 ≤ 𝑑 for all sufficiently large n.
8 1/25/2024 Dr. E. Fadel
Asymptotic Analysis
Furthermore, 𝑓(𝑛) grows faster than 𝑔(𝑛) if, for all
positive constants c, we have 𝑓 𝑛 ≥ 𝑐 ∙ 𝑔 𝑛 for all
sufficiently large 𝑛.
The growth rate talks about the behavior for large
𝑛.
9 1/25/2024 Dr. E. Fadel
Asymptotic Analysis
For example, all the following functions have the same
growth rate:
𝑛2
𝑛2 +7𝑛
5𝑛2 − 7𝑛
𝑛2
+ 106 𝑛
10
10 1/25/2024 Dr. E. Fadel
Asymptotic Notation
To present asymptotic behavior, Let f(n) and g(n)
denote functions that map nonnegative integers to
nonnegative real numbers. Then the following
definitions are valid.
11 1/25/2024 Dr. E. Fadel
Asymptotic Notation
The growth rate of most algorithms discussed in the
book is polynomial, logarithmic function, or the product
of a polynomial and a logarithmic function.
12 1/25/2024 Dr. E. Fadel
Asymptotic Notation
The left-hand sides should be read as “big O of f”.
The meaning of the two functions “f (n)” and “g(n)” in
the green squares is functions f and g and the “n”
emphasizes that these are functions of the argument n.
However, in the term “g(n) ≤ c · f (n)” in the blue
square, they denote the values of the functions at the
argument n.
O(n2) is the set of all functions that grow at most
quadratically
13 1/25/2024 Dr. E. Fadel
Asymptotic Notation
The left-hand sides should be read as “big omega of f”.
Ω( f (n)) is the set of all functions that “grow at least as
fast as” f (n).
14 1/25/2024 Dr. E. Fadel
Asymptotic Notation
The left-hand sides should be read as “theta of f ”.
15 1/25/2024 Dr. E. Fadel
Asymptotic Notation
The left-hand sides should be read as “little o of f ”, and
“little omega of f ”, respectively.
The “little o” notation o( f (n)) denotes the set of all
functions that “grow strictly more slowly than” f (n).
Its twin ω ( f (n)) is rarely used and is only shown for
completeness.
16 1/25/2024 Dr. E. Fadel
Asymptotic Notation
In summary,
Asymptotic Notations are languages that allow us to analyze an
algorithm's running time by identifying its behavior as the input
size for the algorithm increases. This is also known as an
algorithm's growth rate. (Khan Academy)
17 1/25/2024 Dr. E. Fadel
2.3 Pseudocode
2 Introduction
18 1/25/2024 Dr. E. Fadel
Pseudocode
Is an abstraction and simplification of imperative
programming languages such as C, C++, Java, C#, Rust, Swift,
Python, and Pascal, combined with liberal use of
mathematical notation.
19 1/25/2024 Dr. E. Fadel
Pseudocode
The timing model for pseudocode programs includes:
Basic pseudocode instructions take constant time.
Procedure and function calls take constant time plus the time to
execute their body.
The syntax of our pseudocode is akin to that of Pascal
20 1/25/2024 Dr. E. Fadel
Pseudocode
The timing model for pseudocode programs does not
include:
The time required for compiler optimization techniques, since
constant factors are ignored in asymptotic analysis anyway.
21 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
A variable declaration “v = x : T ” where
v is the variable name.
T is the variable type
X is the variable initialization.
For example, “answer = 42 : Ν” where in Ν is a nonnegative
integer.
22 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
A type can be one of the following:
A basic type (e.g., integer, Boolean value, or pointer).
A composite type. We have predefined composite types such as
arrays, and application-specific classes.
The unspecified type “Element” is used as a placeholder for an
arbitrary type. This is used when the type is not important.Type
equation here.
23 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
Values for types can be:
Extending numeric types by the values −∞ and ∞.
Sometimes types are extended by an undefined value (denoted by
the symbol ⊥). For pointer types it is useful to have an undefined
value.
24 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
A declaration “a : Array [i.. j] of T ”
introduces an array.
25 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
A declaration “c : Class age : ℕ,
income : ℕ end” introduces a variable c
whose values are pairs of integers.
26 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
Sequences store elements in a specified order;
for example, “s = ⟨3, 1, 4, 1⟩ : Sequence of ℤ”
declares a sequence s of integers and initializes it
to contain the numbers 3, 1, 4, and 1 in that order.
27 1/25/2024 Dr. E. Fadel
Pseudocode: Variables and Elementary
Data Types
Sets play an important role in mathematical arguments, and
we shall also use them in our pseudocode.
Declarations such as “M = {3, 1, 4} : Set of N” that are
analogous to declarations of arrays or sequences will be
seen.
Sets are usually implemented as sequences.
28 1/25/2024 Dr. E. Fadel
Pseudocode: Statements
The simplest statement is an assignment
x:=E
where x is a variable and E is an expression.
29 1/25/2024 Dr. E. Fadel
Pseudocode: Statements
An assignment is easily transformed into a constant number of
Random-Access Machine (RAM)* instructions.
For example,
the statement a := a + bc is translated into
“R1 := Rb ∗ Rc;
Ra := Ra + R1”,
where Ra, Rb, and Rc stand for the registers storing a, b, and c, respectively.
30 1/25/2024 * See section 2.2 Dr. E. Fadel
Pseudocode: Statements
The conditional statement is expressed as
“if C then I else J”,
where C is a Boolean expression and I and J are statements.
If C evaluates to false (= 0), the program jumps to the first
instruction of the translation of J.
If C evaluates to true (= 1), the program continues with the
translation of I and then jumps to the instruction after the
translation of J.
31 1/25/2024 Dr. E. Fadel
Pseudocode: Statements
Note:
The statement “if C then I” is a shorthand for “if C then I
else ;” (an if–then–else with an empty “else” part).
Indentation is used to group statements.
Line break replace a semicolon for the purpose of
separating statements.
32 1/25/2024 Dr. E. Fadel
Pseudocode: Statements
The loop “repeat I until C”.
where C is a Boolean expression, and I is a group
of statements.
33 1/25/2024 Dr. E. Fadel
Pseudocode: Statements
We shall also use many other types of
loops that can be viewed as shorthand for
various repeat loops.
34 1/25/2024 Dr. E. Fadel
Pseudocode: Statements
In the following list, the shorthand on the left
expands into the statements on the right:
35 1/25/2024 Dr. E. Fadel
Pseudocode: Procedures and Functions
A subroutine with the name foo is declared in the form
“Procedure foo(D) I”
where I is the body of the procedure and D is a sequence of
variable declarations specifying the parameters of foo.
36 1/25/2024 Dr. E. Fadel
Pseudocode: Procedures and Functions
A call of foo has the form foo(P)
where P is a parameter list.
The parameter list has the same length as the variable
declaration list.
Parameter passing is either “by value” or “by reference”.
Note: As with variable declarations, we sometimes omit type declarations for
parameters if they are unimportant or clear from the context.
37 1/25/2024 Dr. E. Fadel
Pseudocode: Procedures and Functions
Compiling procedure calls into machine code is performed
using:
Inlining
Recursion stack
38 1/25/2024 Dr. E. Fadel
Pseudocode: Procedures and Functions
Functions are similar to procedures, except that they allow
the return statement to return a value.
39 1/25/2024 Dr. E. Fadel
Pseudocode: Object Orientation
A simple form of object-oriented programming (OOP) is
included to separate the interface and the implementation
of data structures.
The notation of OOP is introduced by example.
40 1/25/2024 Dr. E. Fadel
Pseudocode: Object Orientation
The definition is:
Class Complex(x, y : Number) of Number
re = x : Number
im = y : Number
Function abs : Number return re2 + im2
Function add(c′ : Complex) : Complex
return Complex(re + c′.re, im + c′.im)
Note: the types “Set of Element” and “Sequence of Element” mentioned earlier are ordinary classes.
41 1/25/2024 Dr. E. Fadel