1 Introduction
1 Introduction
Tech
Course Code: AIML303/AIDS303/IOT303
Course Name: Design & Analysis of Algorithms
By:
Dr. Monika Bansal
Course Objectives
Course Outcomes
Course Prerequisites
Nptel Course:
https://nptel.ac.in/courses/106101060
Lab Exercises
Goals of the Course
• What is this course about?
• Algorithms
• Design - How do you create an algorithm?
• Analysis – How efficient is it?
• Correctness – How sure are you that it works for all
input?
• Data Structures
• Role in efficient algorithms
• Data structures for common problems
Algorithm
Algorithm
Input Output
• Algorithm Specification
• Flowchart – useful for small and simple algorithms.
• Pseudocode – English like statements follows some
conventions.
Pseudocode conventions
• Indentation - block structure
• Loops - while, for, and repeat-until, , interpretations is similar to
programming languages, such as C, C++, Java, Python, etc.
• Conditions - if-else, interpretations is similar to programming
languages, such as C, C++, Java, Python, etc.
• Comment – “//”
• while
• repeat-until
Conditional Statements
Algorithm
Pseudocode Example
1
2
3
4
5
6
7
Exercise-1
• Depends on
• input size
• input quality (partially ordered)
• Kinds of analysis
• Worst case (standard)
• Average case (sometimes)
• Best case (never)
Time Complexity Computation: Example-1
Time Complexity Computation: Example-2
Time Complexity Computation: Example-3
Exercise-3
BIG IDEAS:
• Ignore machine dependent constants,
otherwise impossible to verify and to compare algorithms
“Asymptotic Analysis”
Asymptotic Notation
n0 n
Big-Oh Notation: Asymptotic Upper Bound
Want g(n) to be
• T(n) = f(n) = O(g(n)) simple.
• if 0 <= f(n) <= c*g(n) for all n >= n0,
where c & n0 are constants > 0
c*g(n)
f(n)
n0 n
• O-notation is used to give an upper bound on a function, to within a constant factor.
The figure shows the intuition behind O-notation. For all values of n at and to the
right of n0, the value of the function f(n) is on or below cg(n).
• We write f(n) = O(g(n) to indicate that a function f(n) is a member of the set O(g(n)).
Big-Oh Notation: Asymptotic Upper Bound
f(n)
c*g(n)
n
n0
2. Set lower boundary (Big Omega ‘Ω’) for above functions, i.e. find function g(n),
and constants (c and no) such that condition f(n) = Ω(g(n)) is satisfied.
Notation: Asymptotic Tight Bound
n0 n
-notation
DEF:
(g(n)) = { f (n) : there exist positive constants c1, c2, and
n0 such that 0 c1 g(n) f (n) c2 g(n)
for all n n0 }
Basic manipulations:
• Drop low-order terms; ignore leading constants.
• Example: 3n3 + 90n2 – 5n + 6046 = (n3)
Notation: Asymptotic Tight Bound
function func(n)
1. x ← 0;
2. for i ← 1 to n do
3. for j ← 1 to n do
4. x ← x + (i - j);
5. return(x);
Practice Exercise-2
What is the time complexity of below code?
function func(n)
1. x ← 0;
2. for i ← 1 to n do
3. for j ← 1 to i do
4. x ← x + (i - j);
5. return(x);
Practice Exercise-3
function func(n)
1. x ← 0;
2. for i ← 1 to n do
3. for j ← i to n do
4. x ← x + (i - j);
5. return(x);
Practice Exercise-4
What is the time complexity of below code?
function func(n)
1. x ← 0;
2. for i ← 1 to n do
3. for j ← 1 to n do
4. x ← x + (i - j);
5. return(x);
Practice Exercise-5
What is the time complexity of below code?
function func(n)
1. x ← 0;
2. for i ← 1 to n do
3. for j ← 1 to i do
4. x ← x + (i - j);
5. return(x);
Exercise-5
Write pseudocode and find upper bound, lower bound, and tight
bound (θ) for following problems:
1.Binary Search
2.Linear Search
3.Selection Sort
4.Bubble Sort
5.Matrix Multiplication (two and three dimensional)
Big-Oh, Theta, Omega