DOC-20230123-WA0005
DOC-20230123-WA0005
Algorithm
running time
memory space.
Approaches:
• theoretical analysis
• empirical analysis
Expressions
• Use standard mathematical symbols to describe numeric and
boolean expressions
For I =1 to n
C[i] = A[i] + B[i]
for I =1 to n
C[i] = A[i] + B[i]
Basic operation: the operation that contributes the most towards the
running time of the algorithm
How to identify identify the basic or primitive operation of an
algorithm ? The basic operation is usually selected to be the operation
that:
• is needed to solve the problem
• is the most time consuming
• is the most frequently used
For example, most sorting algorithms work by comparing
elements (keys) of a list being sorted with each other; for such
algorithms, the basic operation is a key comparison.
Visiting a vertex or
Typical graph problem #vertices and/or edges
traversing an edge
input size
T(n) ≈ copC(n)
running time execution time Number of times
for basic operation basic operation is
or cost executed
for I =1 to n
C[i] = A[i] + B[i]
Number of output = n
42
Csc2202 by S.H. Muhammad 42
Order of Growth
The rate at which the running time increases as a function
of input is called rate of growth or order of growth.
For large values of n, it is the function’s order of growth that
counts
For example in the below case , we can approximate it to n4
since , n4 is the highest rate of growth
4 2 4
100n + 3n + 60n+ 5000 ≈ n
2
1000n + nlong + 2 ≈ 2
n n
Exact formula
e.g., C(n) = n(n-1)/2
Two assumptions:
• Probability of successful search is p (0 ≤ p ≤ 1)
• Search key can be at any index with equal probability
(uniform distribution)
Cavg(n) = Expected # of comparisons = Expected # of
comparisons for success + Expected # of
comparisons if k is not in the list
Worst case
n key comparisons
Best case
1 comparisons
Dr John
Sargeant
cook wirelessly
Self-driving Cars
Robotic……..Have you seen as movie called Chappie :)
Mobile Health through e-monitoring
Drones
Big data
0 infinity
c × g(n)
t(n)
Doesn’t
matter
n
n0
t(n) є O(g(n))
5n+20 is in O(n)
CS 307 Fundamentals of 91
Computer Science
Csc2202 by S.H. Muhammad 91
O-Notation (contd.)
Function A Function #2
n3 + 2n2 100n2 + 1000
n0.1 log n
vs.
n + 100n0.1 2n + 10 log n
5n5 n!
n-152n/100 1000n15
82log n 3n7 + 7n
O(n2)
n3 + 2n2 100n2 + 1000
O(log n)
n0.1 log n
vs.
O(n) TIE
n + 100n0.1 2n + 10 log n
O(n5)
5n5 n!
O(n15)
n-152n/100 1000n15
O(n6) why???
82log n 3n7 + 7n
asymptotic bounds
for all n ≥ 5
for all n ≥ 1
Must find SOME constants c and n0 that satisfy the asymptotic notation relation
t(n)
c × g(n)
Doesn’t
matter
n
n0
t(n) є Ω(g(n))
c1 × g(n)
t(n)
c2 × g(n)
Doesn’t
matter
n
n0
t(n) є Θ(g(n))
f(n) ∈ O(f(n))
Examples:
• 10n vs. n2
• n(n+1)/2 vs. n2
1 2
Compare the order of growth n(n−1) and n
2
1
Ans :
2
Ans = ∞
n
2 grows very fast,but n!grows still faster.
n
We can write symbolically that n! ∈Ω(2 )
Csc2202 by S.H. Muhammad 117
Basic asymptotic efficiency classes
1 constant
log n logarithmic
n linear
n log n n-log-n
n2 quadratic
n3 cubic
2n exponential
n! factorial
Problem 1
sum = 0;
for( i = 0; i < n; i++)
– sum++;
• properties of logarithms:
logb(xy) = logbx + logby
logb (x/y) = logbx - logby
logbxa = alogbx
logba= logxa/logxb
• properties of exponentials:
a(b+c) = aba c
abc = (ab)c
ab /ac = a(b-c)
b = a logab
bc = a c*logab
Dr John
Sargeant
ALGORITHM MaxElement(A[0..n-1])
//Determines largest element
maxval <- A[0]
for i <- 1 to n-1 do
if A[i] > maxval
Input size: n
maxval <- A[i]
Basic operation: > or <-
return maxval
ALGORITHM UniqueElements(A[0..n-1])
//Determines whether all elements are //distinct
for i <- 0 to n-2 do
for j <- i+1 to n-1 do Input size: n
Basic operation: A[i] = A[j]
if A[i] = A[j] Does C(n) depend on type of input?
return false
return true
/ executes 𝑛 times
for (i=1; i<=n; i++)
{
m = m + 2; // constant time, c
}
Total time = a constant 𝑐 × 𝑛 = 𝑐 𝑛 = 𝑂(𝑛).
Let us assume that the loop is executing some 𝑘 times. That means at 𝑘︎︎th step 2︎i = 𝑛
and we come out of loop.
Size: n
Basic operation: multiplication
Recurrence relation: M(n) = M(n-1) + 1
M(0) = 0
Csc2202 by S.H. Muhammad 163
Analysis of Recursive (contd.)
Recurrence relation
M(n) = M(n-1) + 1
M(0) = 0
By Backward substitution
n 1’s
Size: n
M(0) = 0
Csc2202 by S.H. Muhammad 166
Example 2: The Tower of Hanoi Puzzle
n-1 n-1
1 1 1 1 1 1 1 1
A(n) = A(
⎣n )/ +21,⎦ A(1) = 0
A(
2)k= A( 2) +k −1,1 A( )=1 0 the Smoothness Rule)
(using
2
= (A( ) + 1) + 1 = A( )+2
2 k −2 2 k −2
= A( )+i
= A( 2) k+−ki= k + 0
=
2 k −k
log 2 n
Csc2202 by S.H. Muhammad 170
Smoothness Rule
Decrease-by-one recurrences
• A decrease-by-one algorithm solves a problem by exploiting a relationship
between a given instance of size n and a smaller size n – 1.
• Example: n!
• The recurrence equation for investigating the time efficiency of such
algorithms typically has the form
T(n) = T(n-1) + f(n)
Decrease-by-a-constant-factor recurrences
• A decrease-by-a-constant-factor algorithm solves a problem by dividing its
given instance of size n into several smaller instances of size n/b, solving
each of them recursively, and then, if necessary, combining the solutions to
the smaller instances into a solution to the given instance.
• Example: binary search.
• The recurrence equation for investigating the time efficiency of such
algorithms typically has the form
T(n) = aT(n/b) + f (n)
2. Examples:
1. T(n) = T(n/2) + 1
Θ(log n)
2. T(n) = 2T(n/2) + n
3. T(n) = 3T(n/2) + n Θ(nlog n)
4. T(n) = T(n/2) + n
Θ(nlog23)
Θ(n)