ADA - Unit-I
ADA - Unit-I
1. Introduction
Design algorithm
• An algorithm is a plan, a logical step-by-step process for solving a problem.
• Algorithms are normally written as a flowchart or in pseudocode. The key to any
problem-solving task is to guide your thought process.
Analysis of algorithms
• The analysis of algorithms is the process of finding the computational complexity of
algorithms, the amount of time, storage, or other resources needed to execute them.
Analysis and Design of Algorithms-Introduction
• Example 1 :
• Take 2 arrays A and B of 1010 entries each. Now write an
algorithm to find common entries.
1234 2347
Algorithm:
234 8754
Assume 2 arrays A and B
3456 2154
k=1
1451 1451
for i=1 to n
… …
for j=1 to n
… …
if A[i]=B[j] then C[k] = A[i];
… …
k=k+1
98765 67859
Output the array C
Array A Array B
1010 Entries
Analysis and Design of Algorithms-Introduction
• Example 1 :
• Take 2 arrays A and B of 1010 entries each. Now write an
algorithm to find common entries.
Algorithm:
Assume 2 arrays A and B
k=1
for i=1 to n
for j=1 to n
if A[i]=B[j] then C[k] = A[i];
k=k+1
Output the array C
• Example 1 :
• Take 2 arrays A and B of 1010 entries each. Now write an
algorithm to find common entries.
If the example 1 concept (binary search) is used here. Then algorithm will be
for i=1 to n
look for A[i] in B by doing binary search and
if found store in C
Output the array C
Analysis:
• How many steps it takes. n(log2 n)
• n= 1010
• n(log2 n) = 1010 (log2 1010 )
• 1010 steps in 1 second then it for the above steps, it takes 1010 (log2
2. Fundamentals of Algorithms
What is an algorithm?
It is a finite set of instructions or a sequence of computational
steps that if followed it accomplishes a task.
Characteristics of an algorithm:
• Input : 0 or more input
• Output: 1 or more output
• Definiteness: clear and unambiguous
• Finiteness: termination after finite number of steps
• Effectiveness: solved by pen and paper
Fundamentals of Algorithms
• The skills required to effectively design and analyze algorithms are
tangled with the skills required to effectively describe algorithms.
• A complete description of any algorithm has four components:
What: A precise specification of the problem that the algorithm solves.
How: A precise description of the algorithm itself.
Why: A proof that the algorithm solves the problem it is supposed to solve.
How fast: An analysis of the running time of the algorithm.
• It is not necessary (or even advisable) to develop these four components in
this particular order.
• Problem specifications, algorithm descriptions, correctness proofs, and
time analyses usually evolve simultaneously, with the development of
each component informing the development of the others.
• Specifying the Problem
• Describing Algorithms
Fundamentals of Algorithms
Specifying the Problem
• To describe an algorithm, the problem is described that the algorithm is
supposed to solve.
• Algorithmic problems are often presented using standard English, in terms
of real-world objects.
• The algorithm designers, restate these problems in terms of formal,
abstract, mathematical objects—numbers, arrays, lists, graphs, trees,
and so on.
• If the problem statement carries any hidden assumptions, state those
assumptions explicitly.
• Specification to be refined as we develop the algorithm.
• The specification should include just enough detail that someone else could
use our algorithm as a black box, without knowing how or why the
algorithm actually works.
Fundamentals of Algorithms
Specifying the Problem (Contd..)
• In particular, the type and meaning of each input parameter, and exactly how the
eventual output depends on the input parameters are described.
• On the other hand, specification should deliberately hide any details that are not
necessary to use the algorithm as a black box.
• Example: Given two non-negative integers x and y, each represented as an array of
digits, compute the product x · y, also represented as an array of digits. To someone
using these algorithms, the choice of algorithm is completely irrelevant.
Fundamentals of Algorithms
Describing Algorithms
structured English.
to break algorithms into primitive steps; the primitive steps themselves can be
• Well written pseudocode reveals the internal structure of the algorithm but
Correctness
• In some application settings, it is acceptable for programs to behave correctly most of
the time, on all “reasonable” inputs.
• But we require algorithms that are always correct, for all possible inputs.
• We must prove that our algorithms are correct; trusting our instincts, or trying a few
test cases, isn’t good enough.
• In particular, correctness proofs usually involve induction.
Running Time
• The most common way of ranking different algorithms for the same problem is by
how quickly they run.
• Preferably, we want the fastest possible algorithm for any particular problem.
Pseudocode Conventions
1. Comments //
3. An identifier begins with a letter. The data types of variables are not explicitly
declared.
Result = 87
A[5] = 99
3. Correctness of Algorithm
A proof of correctness requires that the solution be stated in two forms.
• First form is usually as a program which is annotated by a set of declarations about
the input and output variables of the program. These assertions are often
expressed in the predicate calculus.
• The second form is called a specification, and this may also be expressed in the
predicate calculus.
A proof consists of showing that these two forms are equivalent in that for
every given legal input, they describe the same output.
Proof by:
Counterexample (indirect proof )
Induction (direct proof )
Loop Invariant
Other approaches:
proof by cases/enumeration
proof by chain of iffs
proof by contradiction
proof by contrapositive
For any algorithm, we must prove that it always returns the desired output for all
legal instances of the problem. For sorting, this means even if the input is already
sorted or it contains repeated elements.
Proof by Mathematical Induction
Mathematical induction (MI) is an essential tool for proving the statement
that proves an algorithm's correctness. The general idea of MI is to prove that a
statement is true for every natural number n.
It contains 3 steps:
1.Induction Hypothesis: Define the rule we want to prove for every n, let's call
the rule f(n).
2.Induction Base: Proving the rule is valid for an initial value, or rather a
starting point - this is often proven by solving the Induction
Hypothesis f(n) for n=1 or whatever initial value is appropriate.
3.Induction Step: Proving that if we know that f(n) is true, we can step one
step forward and assume f(n+1) is correct.
Proof by Mathematical Induction
Example
If we define S(n) as the sum of the first n natural numbers, (for
example S(3) = 3+2+1), prove that the following formula can be applied
to any n:
2.Induction Base: In this step we have to prove that S(1) = 1, i.e n=1
3. Induction Step: In this step we need to prove that if the formula applies
to S(n), it also applies to S(n+1) as follows:
Note that S(n+1) = S(n) + (n+1) just means we are recursively calculating the
sum.
Example with literals:
S(3) = S(2) + 3= S(1) + 2 + 3 = 1 + 2 + 3 = 6
Hence proved
4. Time complexity analysis
• The time complexity of an algorithm is the amount of computer time it needs to run
to complete.
T(P) = compile time + execution time
T(P) = Tp(execution time)
• The compiled program will be run several times without recompilation. Thus, the run
time of a program only considered. This run time is denoted by t p (instance characteristics).
• If the compiler characteristics and the time taken for addition, subtraction, division,
multiplication etc.. are known then we can find tp as:
Step count:
For algorithm heading → 0
For braces → 0
For expressions → 1
For any looping statements → number of times the loop is repeating
4. Time complexity analysis
Table method
Example
Statements s/e Frequency Total Steps
Algorithm abc(a,b,c) 0 - 0
{ 0 - 0
return a+b+b*c+(a+b-c)/(a+b)+4.0; 1 1 1
} 0 - 0
Total =1
4. Time complexity analysis
Example
Statements s/e Frequency Total Steps
Sum(a,b) 0 - 0
{ 0 - 0
return a+b; 1 1 1
} 0 - 0
Total =1
4. Time complexity analysis
Example
Statements s/e Frequency Total Steps
A() 0 - 0
{ 0 - 0
for (int i=1 to n) 1 n+1 n+1
print(“ AIML”); 0 - 0
}
0 - 0
Total =n+1
4. Time complexity analysis
Example
Statements s/e Frequency Total Steps
Algorithm Sum(a, n) 0 - 0
{ 0 - 0
s:=0.0; 1 1 1
for i:=1 to n do 1 n+1 n+1
s := s + a[i]; 1 n n
return s; 1 1 1
} 0 - 0
Total =2n+3
4. Time complexity analysis
Example:
Statements s/e Frequency Total Steps
Algorithm Sum(a, n) 0 - 0
{ 0 - 0
s:=0.0; 1 1 1
for i:=1 to n do 1 n+1 n+1
s := s + a[i]; 1 n n
return s; 1 1 1
} 0 - 0
Total =2n+3
Insertion sort
• Session Learning Outcome - SLO – To Understand
the concept of Insertion sort, the time complexity of
an algorithm and Algorithm design paradigms.
Insertion sort
• Insertion Sort Algorithm sorts array by shifting elements one
by one and inserting the right element at the right position
• The insertion sort method sorts a list of elements by inserting
each successive elements in the previously sorted sub list.
• It performs n-1 passes.
• For pass1 through n-1 insertion sort ensures that the elements
in position 0 through p are in sorted order.
• In pass P, we move the pth element to left until its correct
place is found among the first element.
Insertion sort
• ALGORITHM DESIGN AND ANALYSIS
Efficiency of the algorithm’s design is totally dependent on the
understanding of the problem.
The important parameters to be considered in understanding
the problem are:
• Input
• Output
• Order of Instructions
• Check for repetition of Instructions
• Check for the decisions based on conditions
Insertion sort Procedure
• We start by making the second element of the given array, i.e. element
at index 1, the key.
• We compare the key element with the element(s) before it, i.e., element
at index 0:
• If the key element is less than the first element, we insert
the key element before the first element.
• If the key element is greater than the first element, then we insert it
after the first element.
• Then, we make the third element of the array as key and will compare it
with elements to it's left and insert it at the right position.
• And we go on repeating this, until the array is sorted.
Bubble sort
• Bubble sort is a simple sorting algorithm.
• This sorting algorithm is comparison-based algorithm in
which each pair of adjacent elements is compared and the
elements are swapped if they are not in order.
• This algorithm is not suitable for large data sets as its
average and worst case complexity are of Ο(n 2) where n is
the number of items.
• Time Complexity analysis for the following
concepts.
1. Insertion Sort
2. Bubble Sort
3. Merge Sort
4. Linear search
5. Binary search
Asymptotic Analysis
c n 1
c n 1
T (n) T (n)
n
2T c n 1 n
2 aT cn n 1
b
Recurrence Relations
Recurrence Examples
• T(n) = T(n-1) + n Θ(n2)
• Recursive algorithm that loops through the input to eliminate one
item.
Step- 1:
• Draw a recursion tree based on the given recurrence relation.
Step- 2:
Determine-
• Cost of each level
• Total number of levels in the recursion tree
• Number of nodes in the last level
• Cost of the last level
Step- 3:
• Add cost of all the levels of the recursion tree and simplify the expression so
obtained in terms of asymptotic notation.
Solve the Recurrence Relation using Substitution Method
3.
4.
5.
Solve the Recurrence Relation using Recursion Tree and prove the
guess using substitution method
{
1. 1 𝑛=1
7. 𝑇 ( 𝑛 ) =
2𝑇 ( )
𝑛
2
+ n 𝑛>1
{
2. 1 𝑛=1
8. 𝑇 ( 𝑛 ) =
3𝑇 ( )
𝑛
4
2
+𝑐 𝑛 𝑛 >1
3.
{
1 𝑛=1
9. 𝑇 ( 𝑛 ) =
3𝑇 ( )
𝑛
4
+ cn 𝑛> 1
{
1 𝑛=1
{
4. 𝑇 ( 𝑛 ) =
𝑇 ( )
𝑛
+1 𝑛>1
10. 𝑇 ( 𝑛 ) =
1 𝑛=1
2
3𝑇
𝑛
2
+n 𝑛>1( )
5 . 𝑇 ( 𝑛) =
{ 1 𝑛=0
2 𝑇 ( n − 1)+1 𝑛> 0
{
1 𝑛=1
6. 𝑇 ( 𝑛 ) =
𝑇 ( )𝑛
2
+n 𝑛>1