Design & Analysis of Algorithm (CSC-321)
Lecture 2,3
Computer Sciences Department
Bahria University (Karachi Campus)
Basic MATHEMATIC FOR ANALYSIS OF
ALGORITHM
Learning Outcomes
In this Lecture will learn ......
To review basic mathematics of Summation
To solve a harder example
Summations
• Used summation to analyze the time
complexity of a loop
For i: 1 to 10
Print i
end
Properties of summation
Arithmetic series
For n >= 0
Geometric series
Harmonic series
• For n >= 0
Quadratic series
Example
Let I(j) is the time to execute inner most loop
Let M(i) is execute the inner most loop from
j: 1 to 2i times
Simplification
• By using the property we can split summation
into two sum
• Now we can solve each summation separately
Now we can calculate the running time T(n) of
the outer most loop by apply summation from i=
1 to n on M(i), as calculated above
where
After simplification, we have
running time is Θ(𝑛3 ).
Analysis of Algorithms
• O(1): Time complexity of a function (or set of
statements) is considered as O(1) if it doesn’t
contain loop, recursion and call to any other
non-constant time function.
// set of non-recursive and non-loop
statements
For example swap() function has O(1) time
complexity .
• A loop or recursion that runs a constant
number of times is also considered as O(1).
For example the following loop is O(1).
// Here c is a constant
for (int i = 1; i <= c; i++)
{
// some O(1) expressions
}
• 2) O(n): Time Complexity of a loop is
considered as O(n) if the loop variables is
incremented / decremented by a constant
amount. For example following functions have
O(n) time complexity
// Here c is a positive integer constant
for (int i = 1; i <= n; i += c) {
// some O(1) expressions
}
for (int i = n; i > 0; i -= c) {
// some O(1) expressions
}
• 3) O(nc): Time complexity of nested loops is
equal to the number of times the innermost
statement is executed. For example the
following sample loops have O(n2) time
complexity.
• For example Selection sort and Insertion Sort have O(n2) time complexity.
for (int i = 1; i <=n; i += c) {
for (int j = 1; j <=n; j += c)
{
// some O(1) expressions }
}}
for (int i = n; i > 0; i -= c)
{ for (int j = i+1; j <=n; j += c)
{ // some O(1) expressions } }
• 4) O(Logn) Time Complexity of a loop is
considered as O(Logn) if the loop variables is
divided / multiplied by a constant amount.
• For example Binary Search(refer iterative
implementation) has O(Logn) time complexity.
for (int i = 1; i <=n; i *= c)
{
// some O(1) expressions
}
for (int i = n; i > 0; i /= c)
{
// some O(1) expressions
}
The series that we get in first loop is 1, c, c2, c3, … ck.
Practice Question
• For
Practice question
Consider the following questions for above
code ignoring compiler optimization.
a) What does the above code do?
b) What is the time complexity of above code?
Conclusion---
After this lecture we are able to ......
Analyze the time complexity of Brute Force
algorithm
Review basic mathematics of Summation