Professional Documents
Culture Documents
PCC-CS404
Design and Analysis of Algorithms
Introduction Class
And
Revision Class
Algorithm
Algorithm is a step-by-step procedure, which
defines a set of instructions to be executed in a
certain order to get the desired output.
Characteristics of an Algorithm
• Unambiguous
• Input
• Output
• Finiteness
• Feasibility
• Independent
2
Need of Algorithm
• To understand the basic idea of the problem.
• The Algorithm gives a clear description of requirements and goal of the problem to
the designer.
3
Need of Algorithm
• A good design can produce a good solution.
• To measure the behavior (or performance) of the methods in all cases (best cases,
worst cases, average cases)
• With the help of an algorithm, we can also identify the resources (memory, input-
output) cycles required by the algorithm.
• We can measure and analyze the complexity (time and space) of the problems
concerning input size without implementing and running it; it will reduce the cost of
design.
4
Algorithm Design Techniques
• Divide and Conquer Approach
• Greedy Technique
• Dynamic Programming
• Backtracking Algorithm
• Randomized Algorithms
5
Analysis of algorithm
• The analysis is a process of estimating the efficiency of an algorithm. There
are two fundamental parameters based on which we can analysis the
algorithm:
6
Analysis of algorithm
• Generally, we make three types of analysis, which is as follows:
• Worst-case time complexity: For 'n' input size, the worst-case time complexity can be defined as
the maximum amount of time needed by an algorithm to complete its execution. Thus, it is
nothing but a function defined by the maximum number of steps performed on an instance
having an input size of n.
• Average case time complexity: For 'n' input size, the average-case time complexity can be
defined as the average amount of time needed by an algorithm to complete its execution. Thus,
it is nothing but a function defined by the average number of steps performed on an instance
having an input size of n.
• Best case time complexity: For 'n' input size, the best-case time complexity can be defined as the
minimum amount of time needed by an algorithm to complete its execution. Thus, it is nothing
but a function defined by the minimum number of steps performed on an instance having an
input size of n.
7
Analyzing Algorithms
• Predict the amount of resources required:
• memory: how much space is needed?
• computational time: how fast the algorithm runs?
8
Algorithm Analysis: Example
• Algo:MIN (a[1], …, a[n])
m ← a[1];
for i ← 2 to n
if a[i] < m
then m ← a[i];
• Running time:
– the number of primitive operations (steps) executed
before termination
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
• Order (rate) of growth:
– The leading term of the formula
– Expresses the asymptotic behavior of the algorithm
9
Asymptotic Analysis
10
Asymtotic Analysis(cont.)
• Best Case Complexity:
– the function defined by the minimum number of steps
taken on any instance of size n
• Average Case Complexity:
– the function defined by the average number of steps
taken on any instance of size n
Average Case
Complexity
Best Case
Complexity
N
(input size)
Asymptotic Notations
13
Big-O
f n O g n : there exist positive constants c and n 0 such that
0 f n cg n for all n n0
14
Big-O(cont.)
Visualization of O(g(n))
cg(n)
f(n)
n0
15
Big-O(cont.)
Examples:
n2 = O(n2):
16
Big-O(cont.)
17
Big-O(cont.)
Examples :
• Prove that: 20 n 2 n 5 O n
2 2
• Let c = 21 and n0 = 4
• 21n2 > 20n2 + 2n + 5 for all n > 4
n2 > 2n + 5 for all n > 4
TRUE
18
Big Omega – Notation
• () – A lower bound
f n g n : there exist positive constants c and n 0 such that
0 f n cg n for all n n0
Consider F(n) = n2
– n2 = (n)
– Let c = 1, n0 = 2
– For all n 2, n2 > 1 n
19
Big Omega – Notation(cont.)
Visualization of (g(n))
f(n)
cg(n)
n0
20
-Notation
f n g n f n O g n AND f n g n
21
-Notation(cont.)
Visualization of (g(n))
c2g(n)
f(n)
c1g(n)
n0
22
Classifying functions by their
Asymptotic Growth Rates
• O(g(n)), Big-Oh of g of n, the Asymptotic Upper
Bound;
• (g(n)), Theta of g of n, the Asymptotic Tight
Bound; and
• (g(n)), Omega of g of n, the Asymptotic Lower
Bound.
Growth of Functions
24
END
25