Introduction To Design Analysis & Algorithms
Introduction To Design Analysis & Algorithms
Unit 2 Divide and Conquer: Merge sort and Binary search type strategies, Pivot based strategies – Long integer
multiplication – Maximum sub array sum - Closest Pair problem etc as examples. Greedy Algorithm -
Introduction to the method, Fractional Knapsack problem, Task Scheduling Problem, Huffman coding etc as
examples. Dynamic Programming: Introduction to the method, Fibonacci numbers, 0-1 Knapsack problem, Matrix
chain multiplication problem, Longest Common Subsequence, and other problems including problems
incorporating combinatorics as examples.
Unit 3 Backtracking, Branch and Bound 0-1 Knapsack, N- Queen problem, subset sum as some examples. String
Matching: Rabin Karp, Boyer Moore, KMP. Network Flow and Matching: Flow Algorithms Maximum Flow –
Cuts Maximum Bipartite Matching. Introduction to NP class: Definitions P, NP, NP complete, NP hard, Examples
of P and NP.
REFERENCES:
1. Thomas H Cormen, Charles E Leiserson, Ronald L Rivest and Clifford Stein,
“Introduction to Algorithms”, Third Edition, Prentice Hall of India Private Limited,
2009
2. Dasgupta S, Papadimitriou C and Vazirani U, “Algorithms”, Tata McGraw-Hill,
2009.
3. Jon Kleinberg, Eva Tardos. Algorithm Design. First Edition, Pearson Education
India; 2013.
Periodical -1
Periodical – 2
Continuous Theory
End Semester: 35
Definition:
“A finite sequence of unambiguous, executable steps or instructions, which, if followed
would ultimately terminate and give the solution of the problem”.
• Program
•Implementations of algorithms
• For specific system and platform
implementation /
design details Decision • If condition then Action1 else Action 2
● Less structured than structure • Use indentations
programs
While/ for/ • While condition do Action-1
• Repeat Action-1 until condition
repeat loops
• Object.method(param1, param2,...)
Method calls • Object is optional
• Return value
Method returns
Algorithm arrayMax
Input: N number of integers
Output: Maximum number
Step-1: Create a local variable max to store the maximum
among the list
Step-2: Initialize max with the first element initially, to
start the comparison.
Step-3: Then traverse the given array from second
element till end, and for each element:
Step-4: Compare the current element with max
Step-5: If the current element is greater than max,
then replace the value of max with the current
element.
Step-6: At the end, return and print the value of the
largest element of array stored in max
Pseudocode
Algorithm
Dr. Rimjhim Singh 8/29/2022 12
Dr. Rimjhim Singh 8/29/2022 13
Algorithms can be analyzed based on the following
parameters:
● Correctness
●Amount of Work done
● Space used
● Simplicity, clarity
● Optimality
Will it stop????
• Is it correct
?????? Dr. Rimjhim Singh 8/29/2022 20
Factors:
• Hardware
• Operating
System
• Compiler
• Size of input
• Nature of Input
• Solution
Theoretical approximation is preferred
- It characterizes the execution time of an algorithm
independently from the machine, the language and compiler.
- Analyzing variation in runtime requirements with
changing size of input data
- Comparisons of different algorithms.
Dr. Rimjhim Singh 8/29/2022 21
Analyzing Algorithms
Model of Computation:
◦ Mathematical model
◦ Asymptotic Notations
Constant factor
can be dropped
n2
Constant factor
can be dropped
O(n2)
Time complexity=??
Space Complexity=??
if (n % i === 0)
{
return false;
}
}
return true;
}
Time complexity=
Time complexity=
Time complexity=???
Time complexity=????
Time complexity=????
Example
• Show 3 log n + log log n is O(log n)
The statement “f(n) is O(g(n))” means that the growth rate of f(n)
is no more than the growth rate of g(n)
•“2n is O(n)”
• Use the simplest expression of the class
•“3n+ 5 is O(n)” instead of “3n + 5 is O(3n)”
TRUE / FALSE
????
Own understanding:
Analyze complexity of bubble sort and
merge sort by counting primitive
functions.
NOTE:
g(n) here is same on
both the sides.
Inference:
Problem-2
Let T(n) be running time for an algorithm and its lower bound is 2n2 + 1
and its upper bound is 20n2 + 5n+8.
Express in terms of Big(O), Big(Ω) and Big (θ)
Problem-3
Express complexity of Bubble sort in terms of Big(O), Big(Ω) and Big (θ)
65
x2∈O(x2)
x2∉o(x2)
x2∈o(x3)
The following are true for Big-O, but would not be true if
you used little-o:
• x² ∈ O(x²)
• x² ∈ O(x² + x)
• x² ∈ O(200 * x²)
66
67
∞ = 1/0
68
Example 2: 3n2+2n is o(n2)
Example 3: 3n2+2n is o(n3)
69
Example 4: 3n2+2n is Ω(n)
Example 5: 3n2+2n is ω(n)
70
Theta, commonly written as Θ, is an Asymptotic
Notation to denote the asymptotically tight
bound on the growth rate of runtime of an
algorithm.
For example, if f(n) is O(g(n)), and f(n) is O(h(n))
and if g(n) is O(h(n)) then g(n) is called the
asymptotically tight notation.
71
So for log(n) we could say:
log(n) is O(n^n) since log(n) grows asymptotically no faster than n^n
log(n) is O(n) since log(n) grows asymptotically no faster than n
log(n) is O(log(n)) since log(n) grows asymptotically no faster than log(n)
We went from loose upper bounds to a tight upper bound
72
73
74
75
Let a, b, and c be positive real numbers. We have
76
77
Some other observations we can make are:
f(n) = Q(g(n)) ⇔ g(n) = Q(f(n))
f(n) = O(g(n)) ⇔ g(n) = W(f(n))
f(n) = o(g(n)) ⇔ g(n) = w(f(n))
78
Examples:
O(n) + O(n2) + O(n4) = O(n + n2 + n4) = O(n4)
O(n) + Q(n2) = Q(n2)
O(n2) + Q(n) = O(n2)
O(n2) + Q(n2) = Q(n2)
79