1 - Algorithm Analysis
1 - Algorithm Analysis
➢ Searching
➢ Sorting
➢ Hashing
➢ Asymptotic worst case time and space complexity.
➢ Algorithm design techniques:
Greedy,
Dynamic programming and
Divide-and-conquer.
➢ Graph search
➢ Minimum spanning trees
➢ Shortest paths.
SUBJECT: Algorithms
Schedule:
➢ Introduction and Analysis of algorithm
➢ Solving recurrence relations:
(i) Substitution method
(ii) Recursive tree method
(iii) Master’s theorem
➢ Asymptotic Notations
➢ Algorithm Design Techniques:
(i) Divide and Conquer
(ii) Greedy Technique
(iii) Dynamic Programming
➢ Sorting
➢ Graph Traversals
➢ Hashing
SUBJECT: Algorithms
Applications:
➢ Divide and Conquer ➢ Greedy Technique
(i) Power of an element (i) Greedy Knapsack
(ii) Selection Procedure (ii) Job sequencing with deadline
(iii) Binary Search (iii) Minimum cost spanning tree
(iv) Find max and min element. (iv) Huffman coding
(v) Strassen’s matrix multiplication (v) Optimal merge pattern
(vi) Quick sort (vi) Single source shortest path
(vii) Merge sort ➢ Dynamic Programming
➢ Sorting (i) Longest common subsequence
Insertion sort (ii) Multistage graph
Bubble sort (iii) 0/1 knapsack
Selection sort (iv) Travelling salesman problem
Heap sort (v) Multistage graph
(vi) All pairs shortest path
SUBJECT: Algorithms
Introduction to Algorithms
SUBJECT: Algorithms
Definition of an algorithm-
➢ An algorithm is a finite set of instructions that, if followed, accomplishes a
particular task.
SUBJECT: Algorithms
Algorithms must satisfy the following :
➢ Input : Number and types of required inputs.
SUBJECT: Algorithms
Algorithm Development stages :
➢ Requirements: make sure that we understand the information that is given.
➢ Verification
SUBJECT: Algorithms
Analysis of an algorithm
➢ Analysis of algorithms refers to the task of determining how much computing
time and storage an algorithm requires.
SUBJECT: Algorithms
Analysis of an algorithm
SUBJECT: Algorithms
Analysis of an algorithm
(A) Fun()
{
for(i=1;i<n; i=3i);
X = Y + Z;
}
(B) foo()
{
i = n;
while(i>1)
i= i/2;
}
SUBJECT: Algorithms
Analysis of an algorithm
(B) foo()
{
i = n;
while(i>1)
i= i/2;
}
SUBJECT: Algorithms
SUBJECT: Algorithms
Analysis of an algorithm
foo()
{
n = 2^2^k where k is a positive integer
for( i=1; i<=n; i++)
{
j=2;
while(j<=n)
j=j^2;
}
}
SUBJECT: Algorithms
GATE-2017
Consider the following C function Time complexity of fun in terms of θ
int fun(int n) notation is:
{ (a) θ(n √n)
(b) θ(n2)
int i, j; (c) θ(n log n)
for(i=1; i<=n; i++) (d) θ(n 2 log n)
{
for (j=1; j<n; j+=i)
{
printf("%d %d", i, j);
}
}
}
SUBJECT: Algorithms
GATE-2015:
Consider the following C function. Which one of the following most closely
int fun1 (int n) approximates the return value of the function
{ fun1?
(A) n3
int i, j, k, p, q = 0; (B) n (logn)2
for (i = 1; i<n; ++i) (C) nlogn
{ (D) nlog(logn)
p = 0;
for (j = n; j > 1; j = j/2)
++p;
for (k = 1; k < p; k = k*2)
++q;
}
return q;
}
SUBJECT: Algorithms
GATE-2013
Consider the following function
int unknown(int n)
{
int i, j, k = 0;
for (i = n/2; i <= n; i++)
for (j = 2; j <= n; j = j * 2)
k = k + n/2;
return k;
}
What is the returned value of the above function?
(a) θ(n2)
(b) θ(n 2 log n)
(c) θ(n3)
(d) θ(n 3 log n)
SUBJECT: Algorithms
GATE-2007/ ISRO-2016:
Consider the following segment of C-code:
int j, n;
j = 1;
while (j <= n)
j = j*2;
The number of comparisons made in the execution of the loop for any n > 0 is:
SUBJECT: Algorithms
Glimpse of Next Session:
➢ Asymptotic Notations
➢ Comparison of Functions
SUBJECT: Algorithms
Any Queries????
SUBJECT: Algorithms
Syllabus:
➢ Algorithm Analysis
➢ Asymptotic Notations
➢ Divide and Conquer (Finding max min element, binary search, Quick sort, Merge sort, Selection
Procedure, Strassen’s Matrix Multiplication).
➢ Greedy Technique (Greedy Knapsack, Job sequencing with deadline, Huffman coding, Optimal
merge pattern, Minimum cost spanning tree, Single source shortest path)
➢ Dynamic Programming (0/1 knapsack, LCS, Multistage graph, MCM, TSP, All pairs shortest
path, SOS, Optimal cost binary search tree).
➢ Sorting
➢ Hashing
SUBJECT: Algorithms