Unit 1
Unit 1
09/02/2023
Internal evaluation Planning(TENTATIVE)
09/02/2023
TEACHING LEARNING PROCESS
*
Course Learning Outcomes
At the end of this course, learners will be able to :
ClO1: apply efficient algorithms to reduce space and time complexity of both recurrent and
non- recurrent relations.
ClO3: apply greedy and dynamic programming type techniques to solve polynomial problems.
ClO4: create exponential problems using backtracking and branch and bound approaches.
ClO5: interpret various approximation algorithms and interpret solutions to evaluate p type, np
type, npc, np hard problems.
Clo6: create greedy algorithms that are efficient in space and time complexities by using divide
and conquer, greedy, backtracking techniques
09/02/2023
Recommended Books
Learning resources
09/02/2023
Introduction- Algorithm Design
09/02/2023
What is an Algorithm?
09/02/2023
What is a problem?
Definition
• A mapping/relation between a set of input instances (domain) and an
output set (range)
Problem Specification
• Specify what a typical input instance is
• Specify what the output should be in terms of the input instance
Example: Sorting
• Input: A sequence of N numbers a1…an
• Output: the permutation (reordering) of the input sequence such that
a1 a2 … an .
09/02/2023
Types of Problems
Search: Find X in the input satisfying property Y
09/02/2023
FUNDAMENTALS OF ALGORITHM
09/02/2023
FOUR DESIRED PROPERTIES OF ALGORITHMS
Definiteness
Finiteness
Correctness
Efficiency
• Each instruction must be very basic so that it can be easily carried out.
09/02/2023
Example: Odd Number
Input: A number n
09/02/2023
HOW TO DEVISE ALGORITHMS
09/02/2023
EXPRESSING ALGORITHMS
Implementations
Pseudo-code
English
My main concern here is not the specific language used but the
clarity of your expression
09/02/2023
CORRECTNESS OF ALGORITHM
09/02/2023
VERIFYING ALGORITHM CORRECTNESS
• Algorithm Validation
• Process of checking the correctness of algorithms.
• Performed by providing all valid inputs to the algorithm and checking its
outputs with expected results.
• Gives a guarantee that the given algorithm always gives correct answers.
09/02/2023
TIME COMPLEXITY ANALYSIS
09/02/2023
ANALYZING ALGORITHMS
09/02/2023
ANALYZING ALGORITHMS CONTD…
09/02/2023
Measurement for
algorithm analysis
Subjective Objective
Measure Measures
Program Program
Time Space
Length Volume
09/02/2023
PERFORMANCE ANALYSIS
Time Complexity
•
•
Space Complexity
• The amount of memory an algorithm needs to run to completion.
09/02/2023
Algorithm Analysis Overview
Computational
Models for Asymptotic analysis
Algorithm Analysis
Logarithm Cost
Average-case Big- omega
Model
Loops and subroutine calls are not simple operations, but depend upon the
size of the data and the contents of a subroutine. We do not want “sort” to
be a single step operation.
09/02/2023
Input Size
The input size is defined formally as the number of bits used to
represent the input data.
Examples
• What is the size of a sorting input instance?
• What is the size of an “Odd number” input instance?
09/02/2023
Methods of runtime measurement
Algorithm
Analysis
09/02/2023
Measuring Complexity
09/02/2023
Specific ways of measuring step counts per execution:
09/02/2023
Case Study
• Algorithm simple(A, B, C)
Begin
A=B+1
C= A+2
D=A+B
End
2 Begin 0 - -
3 A=B+1 1 1 1
4 C= A+2 1 1 1
5 D=A+B 1 1 1
6 End 0 - -
Total 3
09/02/2023
Case Study
• Algorithm sum()
Begin
sum=0.0
for i=1 to n do
sum=sum+1
End for
Return sum
End
09/02/2023
Case Study Contd…
Step Number Algorithm Steps per Frequency Total
Segment execution
1 Algorithm 0 - -
sum()
2 Begin 0 - -
3 Sum=0.0 1 1 1
4 for i=1 to n do 1 n+1 n+1
5 Sum=sum+1 1 n n
6 End for 0 - -
7 Return sum 1 1 1
8 End 0 0 0
Total 2n+3
09/02/2023
Case Study: Insertion Sort
Algorithm InsertionSort()
Begin
for i = 2 to n
key = A[i]
j=i-1
while j > 0 AND A[j] > key
A[j+1] = A[j]
j = j -1
A[j+1] = key
End for
End
Count the number of times each line will be executed:
09/02/2023
Case Study: Insertion Sort Contd…
Step Number Algorithm Segment Steps per Frequency Total
execution
1 Algorithm 0 - -
InsertionSort()
2 Begin 0 - -
3 for i = 2 to n 1 (n-1) + 1 n
09/02/2023
Case Study: Homework
Algorithm Sum()
Begin
Sum=0.0
for i = 1 to n do
val= 2*I
Sum=Sum+val
End for
Return Sum
End
Apply step count method and analyze the algorithm segment
09/02/2023
Case Study: Homework
Algorithm Sample()
Begin
for i = 1 to n do
return i
End for
End
09/02/2023
Case Study: Homework
Algorithm Sample()
Begin
for i = 1 to n do
for j=1 to n do
A(i,j)=0
End for
End for
End
Apply step count method and analyze the algorithm segment
09/02/2023
Operation Count
09/02/2023
Rules for performing Operation Count
09/02/2023
Rules for performing Operation Count contd…
09/02/2023
Rules for performing Operation Count contd…
09/02/2023
Case Study
Algorithm swap(a,b)
Begin
temp=a
a=b
b=a
End
Perform Complexity Analysis using Operation Count
09/02/2023
Case Study Contd…
Step Number Algorithm Operations Operation Count Repetitions Total
Segment Accounted for
1 Algorithm 0 - 0 0
swap(a,b)
2 Begin 0 - 0 0
3 temp=a Assignment C1 1 C1
4 a=b Assignment C2 1 C2
5 b=a Assignment C3 1 C3
6 End 0 - - -
Total T(n) C1 + C2 +
C3
09/02/2023
Case Study
Algorithm Sample()
Begin
for k=1 to n do
A=A x k
End for
End
09/02/2023
Case Study Contd…
Step Number Algorithm Operations Operation Count Repetitions Total
Segment Accounted for
1 Algorithm 0 - 0 0
Sample()
2 Begin 0 - 0 0
3 for k=1 to n Assignment C1 n+1 (n+1) x C1
do , testing and
increment
4 A=A x k Multiplicati C2 n nC2
on
5 End for - - - -
6 End - - - -
Total T(n) (n+1) C1 +
nC2
T(n) =(C1 +C2)n +C1
09/02/2023
09/02/2023
Designing an Algorithm: Best Case, Worst Case and
Average Case Complexity
09/02/2023
Algorithms
Polynomial Exponential
09/02/2023
Measuring Complexity Again
09/02/2023
Measuring Complexity Again
09/02/2023
Best, Worst, and Average Case
09/02/2023
Simplifications
Ignore constants
•
•
Asymptotic Efficiency
•
09/02/2023
Why ignore constants?
Simplification of analysis
•
09/02/2023
the order of some common used functions
1 logn n nlogn n2 2n n! nn
09/02/2023
Asymptotic Analysis
• Goal: to simplify analysis of running time by getting rid of”details”, which may be affected by
specific implementation and hardware
• like“rounding”: 1,000,001=1,000,000
• 3n2=n2
• Capturing the essence: how the running time of an algorithm increases with the size of the input
in the limit.
• Asymptotically more efficient algorithms are best for all but small inputs
09/02/2023
Asymptotic Analysis
• 7n -3 is O(n)
09/02/2023
Asymptotic Analysis
• Use O-notation to express number of primitive operations executed as function of input size.
09/02/2023
Asymptotic Notation
09/02/2023
Asymptotic Notation
• The “big-Omega” Ὡ- Notation
• asymptotic lower bound
• f(n) is Ὡ (g(n)) if there exists constants c and n0, s.t. c g(n) <=f(n) for n >=n0
09/02/2023
Asymptotic Notation
• Used for comparisons of running times. If f(n) is o(g(n)), it is said that g(n)
dominates f(n).
09/02/2023
09/02/2023
Set Notation Comment
Analogy
• “A dog is an animal” but not “an animal is a dog”
09/02/2023
Example Function
09/02/2023
ASYMTOTIC NOTATION
09/02/2023
Measuring Complexity Again
• The worst case running time of an algorithm is the function defined by
the maximum number of steps taken on any instance of size n.
09/02/2023
Average case analysis
• Drawbacks
• Based on a probability distribution of input instances
• How do we know if distribution is correct or not?
09/02/2023
Best, Worst, and Average Case
09/02/2023
Worst case analysis
09/02/2023
Motivation for Asymptotic Analysis
09/02/2023
Simplifications
• Ignore constants
• 4n2 - 3n log n + 17.5 n - 43 n⅔ + 75 becomes
• n2 – n log n + n - n⅔ + 1
• Asymptotic Efficiency
• n2 – n log n + n - n⅔ + 1 becomes n2
09/02/2023
Why ignore constants?
• RAM model introduces errors in constants
• Do all instructions take equal time?
• Specific implementation (hardware, code optimizations) can speed up an
algorithm by constant factors
• We want to understand how effective an algorithm is independent of these
factors
• Simplification of analysis
• Much easier to analyze if we focus only on n2 rather than worrying about 3.7
n2 or 3.9 n2
09/02/2023
Asymptotic Analysis
0 infinity
09/02/2023
Big Oh , Big Omega and Big Theta
Notation
•
09/02/2023
Set Notation Comment
09/02/2023
Three Common Sets
09/02/2023
O(g(n))
09/02/2023
(g(n))
09/02/2023
(g(n))
09/02/2023
O(f(n)) and (g(n))
O( f (n)) 1
100
n2
( g (n)) 1
25
n
09/02/2023
Example Function
f(n) = 3n - 100n + 6
2
09/02/2023
Quick Questions
c n0
3n2 - 100n + 6 = O(n2)
3n2 - 100n + 6 = O(n3)
3n2 - 100n + 6 O(n)
09/02/2023
Two Other Sets
09/02/2023
Example Problems
1. What does it mean if:
f(n) O(g(n)) and g(n) O(f(n)) ???
2. Is 2n+1 = O(2n) ?
Is 22n = O(2n) ?
09/02/2023
Chapter Objectives
Conditional asymptotics
09/02/2023
Skills required
09/02/2023
Types
Recurrence Relation
09/02/2023
09/02/2023
Constant vs Variable Coefficient
In the generic linear recurrence equation
a t + a t + … + a t = f(n),
0 n 1 n−1 k n−k
Based on this fact, one can classify linear recurrence equations into two types: linear
recurrence equations with constant coefficients and those with variable coefficients.
This recurrence equation is dependent on the variable n and does not have constant
coefficients.
09/02/2023
09/02/2023
Analysis of Framework
For example, for the sequence 1, 4, 7, 10, …, one can write the recurrence equation as
follows:
T(n) = T(n − 1) + 3
T(0) = 1
•Example 4.1 What is the recurrence equation for the sequence 1000, 2000, 4000, 8000, …?
•Solution
• t0 = 1000
• t1 = 2000 = 2 × 1000 = 2 × t0
• t2 = 4000 = 2 × 2000 = 2 × t1 = 22 t0
•
• tn = 2 × t(n-1) or tn = 2n × t0
•It can be observed that tn = 2 × t(n-1) is the required recurrence equation of this problem.
09/02/2023
Example
Example 4.2 Find the recurrence equation and the initial condition of the following
sequence:
7, 21/4 , 63/16, 189/64 , …
Solution
Let the initial condition be t0 = 7. Let us observe the patterns. Let us calculate the ratios of
the successive elements as follows:
09/02/2023
Techniques for Solving
Substitution Method
Difference Method
09/02/2023
Guess and Verify Methods
Known by various names such as guess and verify, guess and test
and solution by mathematical induction or method of
substitution(Cormen)
Solution As said earlier, first make a guess of the solution and then verify it.
Guess: For making a guess, use different values of n in the recurrence equation as follows:
t0 = 1
t1 = t1−1 + 2 = t0 + 2 = 3
t2 = t2−1 + 2 = t1 + 2 = 5
t3 = t3−1 + 2 = t2 + 2 = 7
:
:
The sequence obtained (1, 3, 5, 7, …) indicates that every term differs from the previous
one by 2. This is an odd-number series. Therefore, one can guess that the solution for the
recurrence equation would be 2n + 1. As this is a non-recursive formula in terms of n, this
can be a solution. To confirm this, one should verify the guess.
09/02/2023
Examples of Input Size
09/02/2023
09/02/2023
09/02/2023
Substitution method
Involves 2 Steps
09/02/2023
Backward Substitution
The process is continued till the closed form is obtained, which can be confirmed by the observation of the common pattern
that emerges from the process of repeated substitution
09/02/2023
09/02/2023
Forward Substitution
09/02/2023
Recurrence Tree Method
09/02/2023
Recurrence Tree Method
09/02/2023
09/02/2023
09/02/2023
09/02/2023
Difference Method
09/02/2023
Polynomial Reduction
09/02/2023
09/02/2023
09/02/2023
09/02/2023
09/02/2023
Non-homogeneous Equations
09/02/2023
09/02/2023
09/02/2023
Generating Functions
09/02/2023
09/02/2023
09/02/2023
Procedure
09/02/2023
09/02/2023
09/02/2023
09/02/2023
Master Theorem
09/02/2023
09/02/2023
09/02/2023
Akra_Bazzi Theorem
•In 1998, two Lebanon-based researchers provided the solutions for the generalized form of the master theorem, which is as
follows:
•Here, a > 0, b > 1, and n0 ≥ b are integers; h(n) is a function that is in the range d1 ≤ h(n) ≤ d2 for two constants d1 and d2 and 1 ≤
n ≤ n0; and f(n) is a positive polynomial that is in the range c1g(n) ≤ f(n) ≤ c2g(n) for all x > 0 and u ∈ nb , n. If all
these conditions are satisfied and the condition a bp = 1 is true, then the solution of the recurrence is given as follows:
•This is a powerful theorem and solves almost all those recurrences that cannot be solved easily by other methods.
09/02/2023
Example
09/02/2023
Generalized Master Theorem
09/02/2023
09/02/2023
Example
09/02/2023
Cases where master theorem fails
09/02/2023
Transformation
09/02/2023
Example
09/02/2023
Range Transform
09/02/2023
Example
09/02/2023
Conditional Asymptotics
09/02/2023
Smoothness Rule
09/02/2023
Example
09/02/2023
09/02/2023