UNIT 1 (DAA)
UNIT 1 (DAA)
► Module 1:
Introduction
Characteristics of algorithm. Analysis of algorithm: Asymptotic analysis of complexity
bounds – best, average and worst-case behavior; Performance measurements of
Algorithm, Time and space trade-offs, Analysis of recursive algorithms through
recurrence relations: Substitution method, Recursion tree method and Masters’ theorem.
[8 hrs] (CO1)
Substitution method
CO4 IV
●Definiteness: Each step of an algorithm must be precise and unambiguous. The algorithm cannot iterate a
"bunch" of times.
●Finiteness: The algorithm must terminate after a finite number of steps.
●Effectiveness: Every instruction must be basic, or simple.
●Correctness: The algorithm must solve the problem accurately.
●Efficiency: The algorithm should use time and resources optimally.
●Clarity: The algorithm should be easy to understand and implement.
An algorithm is a set of step-by-step instructions that a computer executes to solve a problem or perform a
task. Algorithms can be used to solve math problems or real-life problems
Algorithm Types
• Approximate Algorithm: If it is infinite and
repeating.
► In the field of computer science and computational theory, the analysis of algorithms plays a critical role in understanding the efficiency and
performance of algorithms.
► Algorithms are the step-by-step procedures used for solving problems, and their analysis involves evaluating their time complexity, space
complexity, and sometimes other factors like correctness and stability. Here's a review of the different aspects of algorithms analysis:
► 1. Time Complexity Analysis:
• Purpose: Time complexity analysis quantifies how an algorithm's running time scales with the input's size. It helps us understand how
efficient an algorithm is in terms of time.
• Methods: Common methods for time complexity analysis include counting basic operations (e.g., comparisons, additions), using Big O
notation (e.g., O(n), O(log n)), and analyzing worst-case, average-case, and best-case scenarios.
• Applications: Time complexity analysis is crucial for selecting the most efficient algorithm for a specific problem and predicting the
algorithm's performance on large inputs.
b) Linear(variable)space complexity
2.Linear (variable)space complexity: The space needed for algorithm
is based on size.
Size of the variable ‘n’ = 1 word
Array of a values = n word
Loop variable = 1 word
Sum variable = 1 word
Example:
• InstructionSpace
It's the amount of memory used to save the compiled version of instructions.
• EnvironmentalStack
Sometimes an algorithm(function) may be called inside another algorithm(function). In such a situation, the
current variables are pushed onto the system stack, where they wait for further execution and then the call to the
inside algorithm(function) is made.
For example, If a function A() calls function B() inside it, then all th variables of the function A() will get stored
on the system stack temporarily, while the function B() is called and executed inside the funciton A().
• DataSpace
Amount of space used by the variables and constants.
► But while calculating the Space Complexity of any algorithm, we usually consider only Data Space and we
neglect the Instruction Space and Environmental Stack.
Calculating the Space Complexity
► For calculating the space complexity, we need to know the value of memory used by different type of datatype
variables, which generally varies for different operating systems, but the method for calculating the space
complexity remains the same.
Type Size
bool, char, unsigned char, signed char, __int8 1 byte
__int16, short, unsigned short, wchar_t, __wchar_t 2 bytes
1. Algorithm Sum(a,n) 0 - 0
2.{ 0 - 0
3. S=0.0; 1 1 1
4. for i=1 to n do 1 n+1 n+1
5. s=s+a[I]; 1 n n
6. return s; 1 1 1
7. } 0 - 0
Total 2n+3
KINDS OF ANALYSIS
1.Worst-case: (usually)
• T(n) = maximum time of algorithm on any input of size n.
2.Average-case: (sometimes)
• T(n) = expected time of algorithm over all inputs of
size n.
• Need assumption of statistical distribution of inputs.
3.Best-case:
• T(n) = minimum time of algorithm on any input of size n.
COMPLEXITY:
Complexity refers to the rate at which the storage time grows as a
function of the problem size
Analysis of recursive algorithms through recurrence relations
► The analysis of the complexity of a recurrence relation involves finding the asymptotic upper bound on
the running time of a recursive algorithm.
► This is usually done by finding a closed-form expression for the number of operations performed by the
algorithm as a function of the input size, and then determining the order of growth of the expression as
the input size becomes large.
► Here are the general steps to analyze the complexity of a recurrence relation:
► Substitute the input size into the recurrence relation to obtain a sequence of terms.
► Identify a pattern in the sequence of terms, if any, and simplify the recurrence relation to obtain a
closed-form expression for the number of operations performed by the algorithm.
► Determine the order of growth of the closed-form expression by using techniques such as the Master
Theorem, or by finding the dominant term and ignoring lower-order terms.
► Use the order of growth to determine the asymptotic upper bound on the running time of the
algorithm, which can be expressed in terms of big O notation.
Analysis of recursive algorithms through recurrence
relations:
► The solution of recurrences is important because it provides
information about the running time of a recursive algorithm. By
solving a recurrence, we can determine the asymptotic upper bound
on the number of operations performed by the algorithm, which is
crucial for evaluating the efficiency and scalability of the algorithm
Recurrence relation for the time
complexity of the binary search
► The recurrence relation for the time complexity of the binary search is T(n) =
T(n/2) + k, where k is constant. At every iteration, we divide the array into 2
hence reducing the problem size by 2. K is the constant time required for
comparison at each iteration and making a decision accordingly
► Binary search algorithm searches an element by comparing it with the middle most
element of the array. Then, following three cases are possible-
Case-01:
► If the element being searched is found to be the middle most element, its index is returned.
Case-02:
► If the element being searched is found to be greater than the middle most element,
then its search is further continued in the right sub array of the middle most element.
Case-03:
► If the element being searched is found to be smaller than the middle most element,
then its search is further continued in the left sub array of the middle most element.
This iteration keeps on repeating on the sub arrays until the desired element is found
OR size of the sub array reduces to zero
► ALGORITHM: BinarySearch(Data,l,r,Loc,Mid,Item)
► DATA is a sorted array with lower bound LB(0) & upper bound UB(size-1), and Item is given item of
information to be searched. The variables l, r and mid denote, respectively, the beginning, the end and the
middle locations of an array. This algorithm finds the location Loc of Item in Data or sets Loc to NULL.
1) Set l:= o,r:=size-1 and mid:=int((l+r)/2).
2) Repeat steps 3 & 4 while l≤r and Data[mid]≠Item.
3) If Item <DATA[MID],then:
► Set r:=mid-1
► else
► Set l:=mid+1
1) Set mid:=int((l+r)/2).
2) If Data[mid]=Item, then Set Loc:=mid
► else
► Set Loc:=NULL
1) Exit.
Substitution Method:
► 1.We make a guess for the solution and
► 2.Then we use mathematical induction to prove the guess is correct or incorrect.
► For example consider the recurrence T(n) = 2T(n/2) + n
► We guess the solution as T(n) = O(nLogn). Now we use induction to prove our guess.
► We need to prove that T(n) <= cnLogn. We can assume that it is true for values smaller than n.
► T(n) = 2T(n/2) + n
<= 2cn/2Log(n/2) + n
= cnLogn – cnLog2 + n
= cnLogn – cn + n
<= cnLogn
Recurrence Tree Method:
► In this method, we draw a recurrence tree and calculate the time
taken by every level of the tree. Finally, we sum the work done at all
levels. To draw the recurrence tree, we start from the given
recurrence and keep drawing till we find a pattern among levels.
► The pattern is typically arithmetic or geometric series. For example, consider the recurrence relation
► cn2
/ \
T(n/4) T(n/2)
► cn2
/ \
c(n2)/16 c(n2)/4
/ \ / \
T(n/16) T(n/8) T(n/8) T(n/4)
► cn2
/ \
c(n )/16
2
c(n2)/4
/ \ / \
c(n )/256 c(n )/64 c(n )/64 c(n2)/16
2 2 2
/ \ / \ / \ / \
► To know the value of T(n), we need to calculate the sum of tree
nodes level by level. If we sum the above tree level by level,
► To get an upper bound, we can sum the infinite series. We get the sum
as (n2)/(1 – 5/16) which is O(n2)
Master Method:
► Master Method is a direct way to get the solution. The master method works only for
the following type of recurrences or for recurrences that can be transformed into the
following type:
► T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
►
There are the following three cases: