Unit 1 Introduction 188
Unit 1 Introduction 188
in
DAA
Unit-1
Introduction
Algorithms are defined to be a step by step method to solve certain problems. For
example if you need to write a program to calculate the age of a person let us say after 5
years so in programming we need to take some input, process it and then provide an
output.
Algorithm for the above scenario will be the steps used in order to calculate age after 5
years.
Step 2: - Perform the calculation i.e. add 5 years to his current age
Step 3: - Output will be a value obtained after calculation which is his age after 5 years.
Characteristics of algorithm
● Input: - Any given algorithm must contain an input the format of the input may
differ from one another based on the type of application, certain application needs a
single input while others may need “n” inputs at initial state or might be in the
intermediate state.
● Output: - Every algorithm which takes the input leads to certain output. An
application for which the algorithm is written may give output after the complete
execution of the program or some of them may give output at every step.
● Finiteness: - Every algorithm must have a proper end, this proper end may come
after certain no of steps. Finiteness here can be in terms of the steps used in the
algorithm or it can be in the term of input and output size (which should be finite every
time).
Key takeaway :
1.2 Asymptotic analysis of complexity bounds – best, average and worst-case behavior
● Space efficiency:- how much extra memory the algorithm needs to complete its
execution
● Generality: - It fixes the range of inputs it can accept. The generality of problems
which algorithms can solve.
It is used to specify the running time and space complexity of an algorithm and to frame
the run time performance in mathematical computation.
As the algorithm has three cases according to time taken by program to perform a
certain task.
3. Average case : Average time taken (number of times it would take for the basic
operation Executed) to address all possible instances of the input (random).
Following are the asymptotic notations which are used to calculate the running time
complexity of algorithm:
● It shows the worst case time complexity or largest amount of time taken by the
algorithm to perform a certain task.
● It shows the best case time complexity or best of time taken by the algorithm to
perform a certain task.
Here, f ( n ) >= g ( n )
● It shows the average case time complexity or average of time taken by the
algorithm to perform a certain task.
Fig 3: Theta notation
The time efficiencies of a large number of algorithms fall into only a few classes
Key takeaway :
The general framework for analyzing the efficiency of algorithms could be the time and
space efficiency.
Time efficiency indicates how fast an algorithm in question runs. Space efficiency deals
with the extra space the algorithm requires.
Unit for measuring running time would be of primary consideration when estimating an
algorithm’s performance is the number of basic operations required by the algorithm to
process an input of a certain size.
The terms “basic operations” and “size” are both rather vague and depend on the
algorithm being analysed. Size is often the number of inputs processed.
For example, when comparing sorting algorithms, the size of the problem is typically
measured by the number of records to be sorted. A basic operation must have the
property that it’s time to complete does not depend on the particular values of its
operands.
Adding or comparing two integer variables are examples of basic operations in most
programming languages. Summing the contents of an array containing n integers is not,
because the cost depends on the value of n (i.e., the size of the input).
Example: Consider a simple algorithm to solve the problem of finding the largest value
in an array of n integers. The algorithm looks at each integer in turn, saving the position
of the largest value seen so far. This algorithm is called the largest-value sequential
search and is illustrated by the following Java function:
l i // b i ii
return curlarge; // Return largest position
The basic operation is to compare an integer’s value to that of the largest value 60. It is
reasonable to assume that it takes a fixed amount of time to do one such comparison,
regardless of the value of the two integers or their positions in the array.
As the most important factor affecting running time is normally size of the input, for a
given input size n we often express the time T to run the algorithm as a function of n,
written as T(n). We will always assume T(n) is a non-negative value.
Let us call c the amount of time required to compare two integers in function largest.
We do not care right now what the precise value of c might be. Nor are we concerned
with the time required to increment variable i because this must be done for each value
in the array, or the time for the actual assignment when a larger value is found, or the
little bit of extra time taken to initialize curlarge.
We just want a reasonable approximation for the time taken to execute the algorithm.
The total time to run largest is therefore approximately cn, because we must make n
comparisons, with each comparison costing c time.
We say that function largest (and the largest-value sequential search algorithm in
general) has a running time expressed by the equation
T(n) = cn.
This equation describes the growth rate for the running time of the largest value
sequential search algorithm.
The growth rate for an algorithm is the rate at which the cost of the algorithm grows as
the size of its input grows. Figure shows a graph for six equations, each meant to
describe the running time for a particular program or algorithm.
A variety of growth rates representative of typical algorithms are shown. The two
equations labeled 10n and 20n are graphed by straight lines. A growth rate of cn (for c
any positive constant) is often referred to as a linear growth rate or running time.
This means that as the value of n grows, the running time of the algorithm grows in the
same proportion. Doubling the value of n roughly doubles the running time. An
algorithm whose running- time equation has a highest-order term containing a factor of
n2 is said to have a quadratic growth rate.
In Figure, the line labeled 2n2 represents a quadratic growth rate. The line labeled 2n
represents an exponential growth rate. This name comes from the fact that n appears in
the exponent. The line labeled n! is also growing exponentially.
Fig 4: Two views of a graph illustrating the growth rates for six equations.
The bottom view shows in detail the lower-left portion of the top view. The horizontal
axis represents input size. The vertical axis can represent time, space, or any other
measure of cost.
Both time and space efficiencies are measured as functions of the algorithm's input size.
Time efficiency is measured by counting the no. of times the algorithm’s basic
operation is executed. Space efficiency is measured by counting the number of extra
memory units consumed by the algorithm.
The efficiencies of some algorithms may differ significantly for inputs of the same size.
For such algorithms, one has to distinguish between the worst case, average case and
best case efficiencies.
The framework’s primary interest lies in the order of growth of the algorithm’s running
time as its input size goes to infinity.
Key takeaway :
The general framework for analysing the efficiency of algorithm could be the
time and space efficiency.
Time efficiency indicates how fast an algorithm in question runs. Space efficiency
deals with the extra space the algorithm requires.
The terms “basic operations” and “size” are both rather vague and depend on the
algorithm being analysed.
● For example: time complexity of merge sort is O(n log n)in all cases. But requires
an auxiliary array.
● In quick sort complexity is O(n log n) for best and average case but it doesn’t
require auxiliary space.
● Input enhancement - To store some info, preprocess the input (or its portion) to be
used in solving the issue later
● counting sorts
Key takeaway :
For instance, the recurrence describes the worst case running time T(n) of the MERGE
SORT Procedures.
2T + θ (n) if n>1
Substitution Method
T (n) = T +n
Solution :
T (n) ≤c logn.
T (n) ≤c log +1
● A good guess is sometimes hard to come up with. Each root and child in the
Recursion tree represents the cost of a single sub-problem.
● In order to obtain a set of pre-level costs, we sum up the costs within each of the
tree levels and then sum up all pre-level costs to determine the total cost of all recursion
levels.
Master’s Theorem
To solve the following recurrence forms, the Master Method is used
T (n) = a T + f (n) with a≥1 and b≥1 be constant & f(n) be a function and can be
interpreted as
T (n) = a T + f (n)
The constants and functions take on the following sense in the function for the study of
a recursive algorithm:
● F(n) is the sum of the work performed beyond the recursive calls, namely the sum
of the problem division and the sum of the solutions to the subproblems combined.
● The function can not always be bound according to the requirement, so we make
three instances that will tell us what sort of bound we can apply to the function.
Master Theorem
T (n) = Θ
Case3: If it is true f(n) = Ω for some constant ε >0 and it also true that : a f
Example :
T (n) = 2
Solution :
n2 = Ω(n1+1) = Ω(n2)
∀ n ≥1
T (n) = Θ(n2)
Key takeaway :
References:
2. Algorithm Design, 1st Edition, Jon Kleinberg and Éva Tardos, Pearson.
Wesley.