Session+2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 65

DCIT 204

Data Structures and


Algorithms 1

Session 2 - Fundamentals of the Analysis of


Algorithm Efficiency
Course Writer: Dr Kofi Sarpong Adu-Manu
Contact Information: ksadu-manu@ug.edu.gh

College of Basic and Applied Sciences


School of Physical and Mathematical Sciences
2020/2021 – 2022/2023
Analysis of algorithms
• Issues:
– correctness
– time efficiency
– space efficiency
– optimality

• Approaches:
– theoretical analysis
– empirical analysis
Constant time operations
• In practice, designing an efficient algorithm aims to lower the
amount of time that an algorithm runs. However, a single algorithm
can always execute more quickly on a faster processor. Therefore,
the theoretical analysis of an algorithm describes runtime in terms
of number of constant time operations, not nanoseconds.

• A constant time operation is an operation that, for a given


processor, always operates in the same amount of time, regardless
of input values.
Constant time vs. non-constant time
operations
Identifying constant time operations
• The programming language being used, as well as the hardware
running the code, both affect what is and what is not a constant time
operation.

• Ex: Most modern processors perform arithmetic operations on


integers and floating point values at a fixed rate that is unaffected
by operand values.

• Part of the reason for this is that the floating point and integer
values have a fixed size. The table below summarizes operations
that are generally considered constant time operations.
Common constant time operations.
Operation Example

w = 10.4
Addition, subtraction, multiplication, and division of fixed x = 3.4
size integer or floating point values. y = 2.0
z = (w - x) / y

x = 1000
Assignment of a reference, pointer, or other fixed size data y = x
value. a = true
b = a
a = 100
Comparison of two fixed size data values. b = 200
if (b > a) { ... }

x = arr[index]
Read or write an array element at a particular index.
arr[index + 1] = x + 1
Growth of functions and complexity
• Upper and lower bounds
An algorithm with runtime complexity T(N) has a lower bound and an
upper bound.
– Lower bound: A function f(N) that is ≤ the best case T(N), for
all values of N ≥ 1.
– Upper bound: A function f(N) that is ≥ the worst case T(N), for
all values of N ≥ 1.
• Ex: An algorithm with best case runtime T(N)=7N+36 and worst
case runtime T(N)=3N^2+10N+17, has a lower bound f(N)=7N and
an upper bound f(N)=3N^2.
• These lower and upper bounds provide a general picture of the
runtime, while using simpler functions than the exact runtime.
Upper and lower bounds in the context of
runtime complexity
This section presents
upper and lower
bounds specifically in
the context of
algorithm complexity
analysis. The
constraint N ≥ 1 is
included because of
the assumption that
every algorithm
presented in this book
operates on a dataset
with at least 1 item.
Theoretical analysis of time efficiency
Time efficiency is analyzed by determining the number of
repetitions of the basic operation as a function of input size

• Basic operation: the operation that contributes most towards


the running time of the algorithm
input size

T(n) ≈ copC(n)
running time
Number of times
execution time basic operation is
for basic operation executed
Input size and basic operation examples

Problem Input size measure Basic operation

Searching for key in a Number of list’s items, i.e.


Key comparison
list of n items n

Multiplication of two Matrix dimensions or total Multiplication of two


matrices number of elements numbers

Checking primality of a n’size = number of digits


Division
given integer n (in binary representation)

Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
Empirical analysis of time efficiency
• Select a specific (typical) sample of inputs

• Use physical unit of time (e.g., milliseconds)


or
Count actual number of basic operation’s executions

• Analyze the empirical data


Best-case, average-case, worst-case

For some algorithms efficiency depends on form of input:

• Worst case: Cworst(n) – maximum over inputs of size n

• Best case: Cbest(n) – minimum over inputs of size n

• Average case: Cavg(n) – “average” over inputs of size n


– Number of times the basic operation will be executed on
typical input
– NOT the average of worst and best case
– Expected number of basic operations considered as a random
variable under some assumption about the probability
distribution of all possible inputs
Example: Sequential search

• Worst case
• Best case
• Average case
Types of formulas for basic operation’s count

• Exact formula
e.g., C(n) = n(n-1)/2

• Formula indicating order of growth with specific multiplicative


constant
e.g., C(n) ≈ 0.5 n2

• Formula indicating order of growth with unknown multiplicative


constant
e.g., C(n) ≈ cn2
Order of growth
• Most important: Order of growth within a constant multiple as
n→∞

• Example:
– How much faster will algorithm run on computer that is twice
as fast?

– How much longer does it take to solve problem of double


input size?
Values of some important functions as n  
Growth rates and asymptotic notations
• An additional simplification can factor out the constant from a bounding
function, leaving a function that categorizes the algorithm's growth rate. Ex:
Instead of saying that an algorithm's runtime function has an upper bound
of 30N2, the algorithm could be described as having a worst case growth rate
of N2.

• Asymptotic notation is the classification of runtime complexity that uses


functions that indicate only the growth rate of a bounding function. Three
asymptotic notations are commonly used in complexity analysis:

– O notation provides a growth rate for an algorithm's upper bound.


– Ω notation provides a growth rate for an algorithm's lower bound.
– Θ notation provides a growth rate that is both an upper and lower bound.
Notations for algorithm complexity
analysis.
Notation General form Meaning

A positive constant c exists such that, for all N ≥


O T(N)=O(f(N))
1, T(N)≤c∗f(N).

A positive constant c exists such that, for all N ≥


Ω T(N)=Ω(f(N))
1, T(N)≥c∗f(N).

Θ T(N)=Θ(f(N)) T(N)=O(f(N)) and T(N)=Ω(f(N)).


Asymptotic order of growth
A way of comparing functions that ignores constant factors and small
input sizes

• O(g(n)): class of functions f(n) that grow no faster than g(n)

• Θ(g(n)): class of functions f(n) that grow at same rate as g(n)

• Ω(g(n)): class of functions f(n) that grow at least as fast as g(n)


Formal Definition of Big O, Omega, Theta

• Big O (O()) describes


the upper bound of the
complexity.
• Omega (Ω()) describes
the lower bound of the
complexity.
• Theta (Θ()) describes
the exact bound of the
complexity.
Big O notation
• Big O notation is a mathematical way of describing how a function (running
time of an algorithm) generally behaves in relation to the input size. In Big O
notation, all functions that have the same growth rate (as determined by the
highest order term of the function) are characterized using the same Big O
notation. In essence, all functions that have the same growth rate are considered
equivalent in Big O notation.

• Given a function that describes the running time of an algorithm, the Big O
notation for that function can be determined using the following rules:
– If f(N) is a sum of several terms, the highest order term (the
one with the fastest growth rate) is kept and others are
discarded.
– If f(N) has a term that is a product of several factors, all
constants (those that are not in terms of N) are omitted.
Determining Big O notation of a function.
Big-oh
Big O notation of composite functions
• The following rules are used to determine the Big O notation of
composite functions: c denotes a constant

Rules for determining Big O notation of composite functions.

Composite function Big O notation


c · O(f(N)) O(f(N))
c + O(f(N)) O(f(N))
g(N) · O(f(N)) O(g(N) · f(N))
g(N) + O(f(N)) O(g(N) + f(N))
Runtime growth rate
• One consideration in evaluating algorithms is that the efficiency of
the algorithm is most critical for large input sizes.

• Small inputs are likely to result in fast running times because N is


small, so efficiency is less of a concern.

• The table below shows the runtime to perform f(N) instructions for
different functions f and different values of N. For large N, the
difference in computation time varies greatly with the rate of
growth of the function f. The data assumes that a single instruction
takes 1 μs to execute.
Growth rates for different input sizes.
N=
Function N = 10 N = 50 N = 100 N = 1000 N = 10000 100000

log2N 3.3 μs 5.65 μs 6.6 μs 9.9 μs 13.3 μs 16.6 μs

N 10 μs 50 μs 100 μs 1000 μs 10 ms 100 ms

Nlog2N .03 ms .28 ms .66 ms .099 s .132 s 1.66 s

N2 .1 ms 2.5 ms 10 ms 1s 100 s 2.7 hours

N3 1 ms .125 s 1s 16.7 min 11.57 days 31.7 years

2N .001 s 35.7 years > 1000 years


Common Big O complexities
• Many commonly used algorithms have running time functions that
belong to one of a handful of growth functions. These common Big
O notations are summarized in the following table.
• The table shows the Big O notation, the common word used to
describe algorithms that belong to that notation, and an example
with source code. Clearly, the best algorithm is one that has
constant time complexity.
• Unfortunately, not all problems can be solved using constant
complexity algorithms. In fact, in many cases, computer scientists
have proven that certain types of problems can only be solved using
quadratic or exponential algorithms.
Runtime complexities for various code
examples.
Notation Name Example pseudocode
FindMin(x, y) {
if (x < y) {
return x }
O(1) Constant else {
return y
}
}
BinarySearch(numbers, N, key) {
mid = 0
low = 0
high = N - 1
O(log N) Logarithmic while (high >= low) { mid = (high + low) / 2
if (numbers[mid] < key) {
low = mid + 1 } else if (numbers[mid] > key)
{ high = mid - 1 } else { return mid } }
return -1 // not found }
Runtime complexities for various code
examples
LinearSearch(numbers, numbersSize, key) {
for (i = 0; i < numbersSize; ++i)
O(N) Linear
{ if (numbers[i] == key) { return i } } return -
1 // not found }

MergeSort(numbers, i, k) {
j = 0
if (i < k) {
O(N log N) Linearithmic j = (i + k) / 2 // Find midpoint
MergeSort(numbers, i, j) // Sort left part
MergeSort(numbers, j + 1, k) // Sort right part
Merge(numbers, i, j, k) // Merge parts } }
Runtime complexities for various code
examples
SelectionSort(numbers, numbersSize) {
for (i = 0; i < numbersSize; ++i)
{ indexSmallest = i for (j = i + 1; j <
numbersSize; ++j) { if (numbers[j] <
O(N2) Quadratic
numbers[indexSmallest]) { indexSmallest = j
} } temp = numbers[i] numbers[i] =
numbers[indexSmallest]
numbers[indexSmallest] = temp } }
Fibonacci(N) { if ((1 == N) || (2 == N))
O(cN) Exponential { return 1 } return Fibonacci(N-1) +
Fibonacci(N-2) }
Big O complexity chart
• 1. O(1) has the least complexity
• 2. O(log(n)) is more complex than
O(1), but less complex than
polynomials
• 3. Complexity of polynomials
increases as the exponent increases
• 4. Exponentials have greater
complexity than polynomials as
long as the coefficients are positive
multiples of n
• 5. Factorials have greater
complexity than exponentials
• 6. Multiplying terms (When
multiplying, the complexity will be
greater than the original, but no more
than the equivalence of multiplying
something that is more complex.)
Omega Notation (Ω-notation)

• Omega notation represents the lower bound of the


running time of an algorithm.

• Thus, it provides the best case complexity of an


algorithm.
Big-omega

Big-Omega (Ω) notation gives a


lower bound for a function f(n) to
within a constant factor.

We write f(n) = Ω(g(n)), If there


are positive constants n0 and c
such that, to the right of n0 the f(n)
always lies on or above c*g(n).

Ω(g(n)) = { f(n) : There exist


positive constant c and n0 such
that 0 ≤ c g(n) ≤ f(n), for all n ≤
n0}
Theta Notation (Θ-notation)
• Theta notation encloses the function from above and
below.

• Since it represents the upper and the lower bound of


the running time of an algorithm, it is used for
analyzing the average-case complexity of an
algorithm.
Big-theta
• Big-Theta(Θ) notation gives
bound for a function f(n) to
within a constant factor.
• We write f(n) = Θ(g(n)), If
there are positive constants n0
and c1 and c2 such that, to the
right of n0 the f(n) always
lies between c1*g(n) and
c2*g(n) inclusive.
• Θ(g(n)) = {f(n) : There exist
positive constant c1, c2 and
n0 such that 0 ≤ c1 g(n) ≤ f(n)
≤ c2 g(n), for all n ≥ n0}
Establishing order of growth using the definition

Definition: f(n) is in O(g(n)) if order of growth of f(n) ≤ order of


growth of g(n) (within constant multiple),
i.e., there exist positive constant c and non-negative integer n0 such
that
f(n) ≤ c g(n) for every n ≥ n0

Examples:
• 10n2 is O(n2)

• 5n+20 is O(n)
Solve more examples
• Visit:

https://math.libretexts.org/Courses/Saint_Mary's_College_Notre_Dam
e_IN/SMC%3A_MATH_339_-_Discrete_Mathematics_(Rohatgi)/Tex
t/4%3A_Algorithms/4.1%3A_Big-O_Notation

Try your hands on the excercises by clicking to see the next page
Some properties of asymptotic order of growth

• f(n)  O(f(n))

• f(n)  O(g(n)) iff g(n) (f(n))

• If f (n)  O(g (n)) and g(n)  O(h(n)) , then f(n)  O(h(n))

Note similarity with a ≤ b

• If f1(n)  O(g1(n)) and f2(n)  O(g2(n)) , then


f1(n) + f2(n)  O(max{g1(n), g2(n)})
Establishing order of growth using limits
0 order of growth of T(n) < order of growth of g(n)

c > 0 order of growth of T(n) = order of growth of g(n)


lim T(n)/g(n) =
n→∞
∞ order of growth of T(n) > order of growth of g(n)

Examples:
• 10n vs. n2

• n(n+1)/2 vs. n2
L’Hôpital’s rule and Stirling’s formula
L’Hôpital’s rule: If limn f(n) = limn g(n) =  and
the derivatives f´, g´ exist, then
lim f(n) lim f ´(n)
=
n g(n) n g ´(n)
Example: log n vs. n

Stirling’s formula: n!  (2n)1/2 (n/e)n


Example: 2n vs. n!
Orders of growth of some important functions
• All logarithmic functions loga n belong to the same class
(log n) no matter what the logarithm’s base a > 1 is

• All polynomials of the same degree k belong to the same class: aknk
+ ak-1nk-1 + … + a0  (nk)

• Exponential functions an have different orders of growth for


different a’s

• order log n < order n (>0) < order an < order n! < order nn
Basic asymptotic efficiency classes
1 constant
log n logarithmic
n linear
n log n n-log-n or linearithmic
n2 quadratic
n3 cubic
2n exponential
n! factorial
Time efficiency of nonrecursive algorithms
General Plan for Analysis
• Decide on parameter n indicating input size

• Identify algorithm’s basic operation

• Determine worst, average, and best cases for input of size n

• Set up a sum for the number of times the basic operation is


executed

• Simplify the sum using standard formulas and rules (see


Appendix A)
Useful summation formulas and rules
liu1 = 1+1+ ⋯ +1 = u - l + 1
In particular, liu1 = n - 1 + 1 = n  (n)

1in i = 1+2+ ⋯ +n = n(n+1)/2  n2/2  (n2)

1in i2 = 12+22+ ⋯ +n2 = n(n+1)(2n+1)/6  n3/3  (n3)

0in ai = 1 + a + ⋯ + an = (an+1 - 1)/(a - 1) for any a  1


In particular, 0in 2i = 20 + 21 + ⋯ + 2n = 2n+1 - 1  (2n )

(ai ± bi ) = ai ± bi cai = cai liuai = limai + m+1iuai


Example 1: Maximum element
Example 2: Element uniqueness problem
Example 3: Matrix multiplication
Example 4: Gaussian elimination
Algorithm GaussianElimination(A[0..n-1,0..n])
//Implements Gaussian elimination of an n-by-(n+1) matrix A
for i  0 to n - 2 do
for j  i + 1 to n - 1 do
for k  i to n do
A[j,k]  A[j,k] - A[i,k]  A[j,i] / A[i,i]

Find the efficiency class and a constant factor improvement.


Example 5: Counting binary digits

It cannot be investigated the way the previous examples are.


Plan for Analysis of Recursive Algorithms
• Decide on a parameter indicating an input’s size.

• Identify the algorithm’s basic operation.

• Check whether the number of times the basic op. is executed


may vary on different inputs of the same size. (If it may, the
worst, average, and best cases must be investigated separately.)

• Set up a recurrence relation with an appropriate initial condition


expressing the number of times the basic op. is executed.

• Solve the recurrence (or, at the very least, establish its solution’s
order of growth) by backward substitutions or another method.
Example 1: Recursive evaluation of n!
Definition: n ! = 1  2  …  (n-1)  n for n ≥ 1 and 0! = 1

Recursive definition of n!: F(n) = F(n-1)  n for n ≥ 1 and


F(0) = 1

Size:
Basic operation:
Recurrence relation:
Solving the recurrence for M(n)

M(n) = M(n-1) + 1, M(0) = 0


Example 2: Tower of Hanoi Puzzel
Steps to solve the problem
Three simple rules are followed:
• Only one disk can be moved
at a time.
• Each move consists of taking
the upper disk from one of
the stacks and placing it on
top of another stack. In other
words, a disk can only be
moved if it is the uppermost
disk on a stack.
• No larger disk may be placed
on top of a smaller disk.
The Tower of Hanoi Puzzle - Moves

• Move the first disk from A to C


• Move the first disk from A to B
• Move the first disk from C to B
• Move the first disk from A to C
• Move the first disk from B to A
• Move the first disk from B to C
• Move the first disk from A to C
Animated image for Tower of Hanoi
Solving recurrence for number of moves

M(n) = 2M(n-1) + 1, M(1) = 1


Tree of calls for the Tower of Hanoi Puzzle

n-1 n-1

n-2 n-2 n-2 n-2


... ... ...
2 2 2 2

1 1 1 1 1 1 1 1
Example 3: Counting #bits
Fibonacci numbers

The Fibonacci numbers:


0, 1, 1, 2, 3, 5, 8, 13, 21, …

The Fibonacci recurrence:


F(n) = F(n-1) + F(n-2)
F(0) = 0
F(1) = 1

General 2nd order linear homogeneous recurrence with


constant coefficients:
aX(n) + bX(n-1) + cX(n-2) = 0
Solving aX(n) + bX(n-1) + cX(n-2) = 0

• Set up the characteristic equation (quadratic)


ar2 + br + c = 0

• Solve to obtain roots r1 and r2

• General solution to the recurrence


if r1 and r2 are two distinct real roots: X(n) = αr1n + βr2n
if r1 = r2 = r are two equal real roots: X(n) = αrn + βnr n

• Particular solution can be found by using initial conditions


Application to the Fibonacci numbers

F(n) = F(n-1) + F(n-2) or F(n) - F(n-1) - F(n-2) = 0

Characteristic equation:

Roots of the characteristic equation:

General solution to the recurrence:

Particular solution for F(0) =0, F(1)=1:


Computing Fibonacci numbers
1. Definition-based recursive algorithm

2. Nonrecursive definition-based algorithm

3. Explicit formula algorithm

4. Logarithmic algorithm based on formula:


F(n-1) F(n) n
0 1
=
F(n) F(n+1) 1 1

for n≥1, assuming an efficient way of computing matrix powers.


Reference
Levitin, A. (2012). Introduction to the Design and
Analysis of Algorithms ( 3rd Edition). Harlow: Addison
Wesley.
Acknowledgement

Pearson Education, Inc. Upper Saddle River, NJ.


All Rights Reserved.

You might also like