0% found this document useful (0 votes)
8 views9 pages

Algorithm:: Analysis of An Algorithm

An algorithm is a finite sequence of clear instructions that produces output from given input and terminates after a finite number of steps. The analysis of algorithms focuses on comparing their running time and memory management, with asymptotic analysis providing a measure of efficiency independent of machine constants. Various algorithm design techniques, such as divide and conquer, are discussed along with examples like quick sort, binary search, and merge sort, highlighting their time complexities and methodologies.

Uploaded by

codejammer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views9 pages

Algorithm:: Analysis of An Algorithm

An algorithm is a finite sequence of clear instructions that produces output from given input and terminates after a finite number of steps. The analysis of algorithms focuses on comparing their running time and memory management, with asymptotic analysis providing a measure of efficiency independent of machine constants. Various algorithm design techniques, such as divide and conquer, are discussed along with examples like quick sort, binary search, and merge sort, highlighting their time complexities and methodologies.

Uploaded by

codejammer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Algorithm:

An Algorithm is a finite sequence of instructions, each of which has a clear meaning and can be
performed with a finite amount of effort in a finite length of time. No matter what the input values may
be, an algorithm terminates after executing a finite number of instructions. In addition every algorithm
must satisfy the following criteria:
 Input: there are zero or more quantities, which are externally supplied;
 Output: at least one quantity is produced
 Definiteness: each instruction must be clear and unambiguous;
 Finiteness: if we trace out the instructions of an algorithm, then for all cases the algorithm will
terminate after a finite number of steps;

Analysis of an Algorithm
 The goal of analysis of an algorithm is to compare algorithm in running time and also Memory
management.
 Running time of an algorithm depends on how long it takes a computer to run the lines of code
of the algorithm.
Running time of an algorithm depends on
1.Speed of computer
2.Programming language
3.Compiler and translator
Examples: binary search, linear search
Different time complexities Suppose ‘M’ is an algorithm, and suppose ‘n’ is the size of the input data.
Clearly the complexity f(n) of M increases as n increases. It is usually the rate of increase of f(n) we want
to examine. This is usually done by comparing f(n) with some standard functions. The most common
computing times are: O(1), O(log2 n), O(n), O(n. log2 n), O(n2 ), O(n3 ), O(2n ), n! and n.

Notes prepared by Mr. Jagdish Goswami Page 1


ASYMPTOTIC ANALYSIS:

 Expressing the complexity in term of its relationship to know function. This type analysis is called
asymptotic analysis.
 The main idea of Asymptotic analysis is to have a measure of efficiency of an algorithm , that
doesn’t depends on
1. Machine constants.
2.Doesn’t require algorithm to be implemented.
3.Time taken by program to be prepare.

ASYMPTOTIC NOTATION:

The mathematical way of representing the Time complexity.


The notation we use to describe the asymptotic running time of an algorithm are defined in terms of
functions whose domains are the set of natural numbers.
Definition : It is the way to describe the behavior of functions in the limit or without bounds.
Asymptotic growth: The rate at which the function grows…
“growth rate” is the complexity of the function or the amount of resource it takes up to compute.
Growth rate  Time +memory

There are 3 asymptotic notations are mostly used to represent time complexity of algorithm.
1.Big oh (O)notation
2.Big omega (Ω) notation
3.Theta(Θ) notation
4.Little oh notation
5.Little omega(Ω) notation

Notes prepared by Mr. Jagdish Goswami Page 2


1.Big oh (O)notation
1.Big oh (O)notation : Asymptotic “less than”(slower rate).This notation mainly represent upper
bound of algorithm run time. Big oh (O)notation is useful to calculate maximum amount of time
of execution.By using Big-oh
oh notation we have to calculate worst ccase
ase time complexity.
Formula : f(n)<=c g(n) n>=n 0 , c>0 ,n0 >=1
Definition: Let f(n) ,g(n) be two non negative (positive) function now the f(n)=O(g(n)) if there
exist two positive constant c,n0 such that
f(n)<= c.g(n) for all value
ue of n>0 & c>0

2.Ω-Omega notation
Ω-Omega notation : Asymptotic “greater than”(faster rate). It represent Lower bound of algorithm run
time. By using Big Omega notation we can calculate minimum amount of time. We can say that it is best
case time complexity. Formula : f(n)>=c g(n) n>=n0 , c>0 ,n0 >=1
where c is constant, n is function, Lower bound and Best case.

Notes prepared by Mr. Jagdish Goswami Page 3


3.  -Theta notation

Theta (Θ) notation : Asymptotic “Equality”(same rate).


It represent average bond of algorithm running time.
By using theta notation we can calculate average amount of time.
So it called average
verage case time complexity of algorithm.
Formula : c1 g(n)<=f(n)<=c2 g(n)

where c is constant, n is function


 Average bound

Notes prepared by Mr. Jagdish Goswami Page 4


Algorithm Design Techniques:
1.DIVIDE AND CONQUER
Divide and conquer is a design strategy which is well known to breaking down efficiency barriers. When
the method applies, it often leads to a large improvement in time complexity. For example, from O (n2)
to O (n log n) to sort the elements.

Divide and conquer strategy is as follows: divide the problem instance into two or more smaller
instances of the same problem, solve the smaller instances recursively, and assemble the solutions to
form a solution of the original instance. The recursion stops when an instance is reached which is too
small to divide. When dividing the instance, one can either use whatever division comes most easily to
hand or invest time in making the division carefully so that the assembly is simplified.

Divide and conquer algorithm consists of two parts:


Divide : Divide the problem into a number of sub problems. The sub problems are solved recursively.
Conquer : The solution to the original problem is then formed from the solutions to the sub problems
(patching together the answers).

 Given a function to compute on ‘n’ inputs the divide-and-conquer strategy suggests splitting the
inputs into ‘k’ distinct subsets, 1<k<=n, yielding ‘k’ sub problems.
 These sub problems must be solved, and then a method must be found to combine sub
solutions into a solution of the whole.
 If the sub problems are still relatively large, then the divide-and-conquer strategy can possibly
be reapplied.
 If the problem p and the size is n , sub problems are n1, n2 ….nk, respectively, then the
computing time of D And C is described by the recurrence relation.
 T(n)= { g(n) n small
 T(n1)+T(n2)+……………+T(nk)+f(n);
 “Where T(n) is the time for D And C on any I/p of size n.
 g(n) is the time of compute the answer directly for small I/p s. f(n) is the time for dividing P &
combining the solution to sub pro

Notes prepared by Mr. Jagdish Goswami Page 5


DIVIDE AND CONQUER : GENERAL METHOD

1. Algorithm D And C(P)


2. {
3. if small(P) then return S(P);
4. else
5. {
6. divide P into smaller instances
7. P1, P2… Pk, k>=1;
8. Apply D And C to each of these sub problems;
9. return combine (D And C(P1), D And C(P2),…….,D And C(Pk));
10. }
}

APPLICATIONS

1. Quick sort :
Quick sort is a sorting algorithm. The quick sort algorithm partitions the original array by rearranging it
into two groups. The first group contains those elements less than some arbitrary chosen value taken
from the set and the second group contains those elements greater than or equal to the chosen value.
The algorithm picks a pivot element, rearranges the array elements in such a way that all elements
smaller than the picked pivot element move to left side of pivot, and all greater elements move to right
side. Finally, the algorithm recursively sorts the sub arrays on left and right of pivot element.

 In Quick sort, the division into 2 sub arrays is made so that the sorted sub arrays do not need to
be merged later.
 This is accomplished by rearranging the elements in a[1:n] such that a[I]<=a[j] for all I between 1
& n and all j between (m+1) & n for some m, 1<=m<=n.
 Thus the elements in a[1:m] & a[m+1:n] can be independently sorted.
 No merge is needed. This rearranging is referred to as partitioning.

Notes prepared by Mr. Jagdish Goswami Page 6


 Quick sort :
1. Algorithm Partition(a,p,r)
2. { X = a[r];
3. i = p-1;
4. For(j=p to r-1)
5. { if (a[j] <= X)
6. { i =i+1;
7. Exchange a[i] with a[j] }
8. }
9. Exchange a[i+1] with a[r]
10. Return i+1
11. }

2. Binary Search
Binary Search is a searching algorithm. In each step, the algorithm compares the input element x with
the value of the middle element in array. If the values match, return the index of middle. Otherwise, if x
is less than the middle element, then the algorithm recurs for left side of middle element, else recurs for
right side of middle element.
1. Algorithm Bin search(a,n,x)
2. // Given an array a[1:n] of elements in non-decreasing
3. //order, n>=0,determine whether x is present and
4. // if so, return j such that x=a[j]; else return 0.
5. {
6. low:=1; high:=n;
7. while (low<=high) do
8. {
9. mid:=[(low+high)/2];
10. if (x<a[mid]) then high;
11. else if(x>a[mid]) then
12. low=mid+1;
13. else return mid;
14. }
return 0; }

Notes prepared by Mr. Jagdish Goswami Page 7


3.Merge Sort
Merge Sort is also a sorting algorithm. The algorithm divides the array in two halves, recursively sorts
them and finally merges the two sorted halves.

ALGORITHM FOR MERGE SORT


 Algorithm MergeSort(low,high)
 //a[low:high] is a global array to be sorted
 //Small(P) is true if there is only one element
 //to sort. In this case the list is already sorted.
 {
 if (low<high) then //if there are more than one element
 {
 //Divide P into subproblems
 //find where to split the set
 mid = [(low+high)/2];
 //solve the subproblems.
 mergesort (low,mid);
 mergesort(mid+1,high); //combine the solutions .
 merge(low,mid,high);
 }
 }

Notes prepared by Mr. Jagdish Goswami Page 8


2. Brute force approach

A brute force approach is an approach that finds all the possible solutions to find a satisfactory solution
to a given problem. The brute force algorithm tries out all the possibilities till a satisfactory solution is
not found. Let's understand the brute force search through an example.

Brute force search considers each and every state of a tree, and the state is represented in the form of a
node. As far as the starting position is concerned, we have two choices, i.e., A state and B state. We can
either generate state A or state B. In the case of B state, we have two states, i.e., state E and F.

In the case of brute force search, each state is considered one by one. As we can observe in the above
tree that the brute force search takes 12 steps to find the solution.

The following are the advantages of the brute


brute-force algorithm:

o This algorithm finds all the possible solutions, and it also guarantees that it finds the correct
solution to a problem.
o This type of algorithm is applicab
applicable to a wide range of domains.
o It is mainly used for solving simpler and small problems.
o It can be considered a comparison benchmark to solve a simple problem and does not require
any particular domain knowledge.
The following are the disadvantages of the brute-force algorithm:

o It is an inefficient algorithm as it requires solving each and every state.


o It is a very slow algorithm to find the correct solution as it solves each state without considering
whether the solution is feasible or not.
o The brute forcee algorithm is neither constructive nor creative as compared to other algorithms.
Applications :
Selection Sort, Bubble Sort, Sequential Search, String Matching, Depth
Depth-First
First Search and Breadth-First
Breadth
Search, Closest-Pair and Convex-Hull
Hull Problems can be solved by Brute Force.

Notes prepared by Mr. Jagdish Goswami Page 9

You might also like