Unit - 1 ADA
Unit - 1 ADA
UNIT -1
INTRODUCTION
What is an algorithm?
An algorithm is a set of steps of operations to solve a problem performing
calculation, data processing, and automated reasoning tasks. An algorithm
is an efficient method that can be expressed within finite amount of time
and space.
An algorithm is a set of commands that must be followed for a computer to perform
calculations or other problem-solving operations.
According to its formal definition, an algorithm is a finite set of instructions carried out in
a specific order to perform a particular task.
Input: After designing an algorithm, the algorithm is given the necessary and desired
inputs.
BGS FGC
ADA NEP
Processing unit: The input will be passed to the processing unit, producing the desired
output.
TYPES OF ALGORITHMS
1. Brute Force Algorithm: A straightforward approach that exhaustively tries all possible
solutions, suitable for small problem instances but may become impractical for larger ones
due to its high time complexity.
3. Encryption Algorithm: Utilized to transform data into a secure, unreadable form using
cryptographic techniques, ensuring confidentiality and privacy in digital communications
and transactions.
7. Divide and Conquer Algorithm: Breaks a complex problem into smaller subproblems,
solves them independently, and then combines their solutions to address the original
problem effectively.
BGS FGC
ADA NEP
8. Greedy Algorithm: Makes locally optimal choices at each step in the hope of finding a
global optimum, useful for optimization problems but may not always lead to the best
solution.
BGS FGC
ADA NEP
From a practical perspective, the first thing you need to do before designing an
algorithm is to understand completely the problem given. Read the problem’s
description carefully and ask questions if you have any doubts about the problem,
do a few small examples by hand, think about special cases, and ask questions
again if needed.
This is the first step in designing of algorithm.
• Read the problem’s description carefully to understand the problem statement completely.
• Identify the problem types and use existing algorithm to find solution. • Input (instance) to the problem
and range of the input get fixed.
The central assumption of the RAM model does not hold for some newer
computers that can execute operations concurrently, i.e., in parallel. Algorithms
that take advantage of this capability are called parallel algorithms
Choosing between Exact and Approximate Problem
Solving
→The next principal decision is to choose between solving the problem exactly or solving it
approximately.
→An algorithm used to solve the problem exactly and produce correct result is called an exact
algorithm.
→If the problem is so complex and not able to get exact solution, then we have to choose an algorithm
called an approximation algorithm. i.e., produces an
→Approximate answer. E.g., extracting square roots, solving nonlinear equations, and evaluating
definite integrals.
BGS FGC
ADA NEP
• Implementation of algorithm is possible only with the help of Algorithms and Data Structures
• Algorithmic strategy / technique / paradigm are a general approach by which many problems can be
solved algorithmically. E.g., Brute Force, Divide and Conquer, Dynamic Programming, Greedy
Technique and soon.
While the algorithm design techniques do provide a powerful set of general ap-
proaches to algorithmic problem solving, designing an algorithm for a particular
problem may still be a challenging task. Some design techniques can be simply
inapplicable to the problem in question
Methods of Specifying an Algorithm There are three ways to specify an algorithm.
b. Pseudocode
Natural Language :
But many times specification of algorithm by using natural language is not clear and thereby we get
brief specification.
Step 3: Add the above two numbers and store the result in c.
BGS FGC
ADA NEP
b) Pseudocode:
• For Assignment operation left arrow “←”, for comments two slashes “//”,if condition, for, while loops
are used. Natural Language Pseudocode
ALGORITHM
Sum(a,b)
c←a+b
return c
• An algorithm must yield a required result for every legitimate input in a finite amount of time.
For Example, the correctness of Euclid’s algorithm for computing the greatest common divisor stems
from the correctness of the equality gcd(m, n) = gcd(n, m mod n)
• A common technique for proving correctness is to use mathematical induction because an algorithm’s
iterations provide a natural sequence of steps needed for such proofs.
• The notion of correctness for approximation algorithms is less straightforward than it is for exact
algorithms. The error produced by the algorithm should not exceed a predefined limit.
ANALYZING AN ALGORITHM
• For an algorithm the most important is efficiency. In fact, there are two kinds of algorithm efficiency.
They are: • Time efficiency, indicating how fast the algorithm runs, and
• The efficiency of an algorithm is determined by measuring both time efficiency and space efficiency.
• So factors to analyze an algorithm are:
BGS FGC
ADA NEP
▪ Simplicity of an algorithm
▪ Generality of an algorithm
• The transition from an algorithm to a program can be done either incorrectly or very inefficiently.
Implementing an algorithm correctly is necessary. The Algorithm power should not reduce by in
efficient implementation.
• Standard tricks like computing a loop’s invariant (an expression that does not change its value) outside
the loop, collecting common subexpressions, replacing expensive operations by cheap ones, selection
of programming language and so on should be known to the programmer.
• Typically, such improvements can speed up a program only by a constant factor, whereas a better
algorithm can make a difference in running time by orders of magnitude. But once an algorithm is
selected, a 10–50% speedup may be worth an effort.
• It is very essential to write an optimized code (efficient code) to reduce the burden of compiler.
BGS FGC
ADA NEP
♦ For example, it will be the size of the list for problems of sorting, searching,
finding the list's smallest element, and most other problems dealing with lists.
♦ Since there is a simple formula relating these two measures, we can easily
switch from one to the other, but the answer about an algorithm's efficiency
will be qualitatively different depending on which of the two measures we
use.
3 UNITS FOR MEASURING RUN TIME:
BGS FGC
ADA NEP
♦ One possible approach is to count the number of times each of the algorithm's
operations is executed. This approach is both difficult and unnecessary.
the basic operation, the operation contributing the most to the total running
time, and compute the number of times the basic operation is executed.
BGS FGC
ADA NEP
♦ But there are many algorithms for which running time depends not only on
an input size but also on the specifics of a particular input.
WORST CASE:
i←0
i←i+1
BGS FGC
ADA NEP
♦ In the worst case, when there are no matching elements or the first matching
element happens to be the last one on the list, the algorithm makes the largest
number of key comparisons among all possible inputs of size n:
Cworst (n) = n.
♦ To analyze the algorithm to see what kind of inputs yield the largest value of
the basic operation's count C(n) among all possible inputs of size n and then
compute this worst-case value C worst (n)
♦ First, determine the kind of inputs for which the count C (n) will be the
smallest among all possible inputs of size n. (Note that the best case does not
mean the smallest input; it means the input of size n for which the algorithm
runs the fastest.)
♦ Example- for sequential search, best-case inputs will be lists of size n with
their first elements equal to a search key; accordingly, Cbest(n) = 1.
AVERAGE CASE EFFICIENCY
BGS FGC
ADA NEP
♦ It involves dividing all instances of size n .into several classes so that for
each instance of the class the number of times the algorithm's basic operation
is executed is the same.
♦ In the case of a successful search, the probability of the first match occurring
in the ith position of the list is pin for every i, and the number of comparisons
made by the algorithm in such a situation is obviously i.
BGS FGC
ADA NEP
ORDERS OF GROWTH
The order of growth of an algorithm is an approximation of the time required to run a computer
program as the input size increases. The order of growth ignores the constant factor needed for
fixed operations and focuses instead on the operations that increase proportional to input size. For
example, a program with a linear order of growth generally requires double the time if the input
doubles.
The order of growth is often described using either Big-Theta or Big-O notation, but that notation
is out of scope for this course.
This table summarizes the most common orders of growth:
CONSTANT TIME
When an algorithm has a constant order of growth, it means that it always takes a fixed number of
steps, no matter how large the input size increases
BGS FGC
ADA NEP
LOGARITHMIC TIME
When an algorithm has a logarithmic order of growth, it increases proportionally to
the logarithm of the input size.
The binary search algorithm is an example of an algorithm that runs in logarithmic time.
LINEAR TIME
When an algorithm has a linear order of growth, its number of steps increases in direct proportion
to the input size.
The aptly-named linear search algorithm runs in linear time.
QUADRATIC TIME
When an algorithm has a quadratic order of growth, its steps increase in proportion to the input
size squared.
Several list sorting algorithms run in quadratic time, like selection sort. That algorithm starts from
the front of the list, then keeps finding the next smallest value in the list and swapping it with the
current value.
EXPONENTIAL TIME
When an algorithm has a superpolynomial order of growth, its number of steps increases faster
than a polynomial function of the input size.
An algorithm often requires superpolynomial time when it must look at every permutation of
values. For example, consider an algorithm that generates all possible numerical passwords for a
given password length.
BGS FGC