0% found this document useful (0 votes)
15 views

Alg 1

Ppt MCA notes

Uploaded by

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

Alg 1

Ppt MCA notes

Uploaded by

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

Algorithms

Introduction
• What is an algorithm?

Dept of MCA, NIE, Mysore


-Unambigious step is called definite instruction(clearly defined)
-Effectiveness of an instruction implies the successful in
producing a desired result.
-Termination means the act of ending something.
Every alg should have the following characteristic features.
-Input
-Output
-Definiteness
-Effectiveness
-Termination
-More than one way of solving a problem
-Then one may think of more than one algorithm
for the problem-
Eg: To find GCD of two numbers
Fundamentals of Algorithmic problem
solving

Dept of MCA, NIE, Mysore


Understand the problem

.Read the problem’s description carefully and understand clearly


. If you have any doubts about the problem, do a few small
examples by hand, Think about special cases and ask questions if
needed.
. It is very important to specify exactly the set of instances(Input)
the alg needs to handle.
Ascertaining the capabilities of a computational device
.After completely understanding a problem you decide about the
system on which you are going to execute your alg.
.Single processor system called as random-access machine which
executes your alg sequentially .
.Accordingly algs designed to be executed on such a machine are
called as “Sequential Algs”
. There are computers which can execute operations
concurrently i.e in parallel
. Algs which takes the advantage of this capability are called
“parallel Algs”.
. Speed , Time , Memory
Choosing between exact & approximate
problem solving

.Decide whether we need exact output(Exact


algorithm) or approximate output (Approximation
alg).

. There are situations where we may not get exact


answers like extracting square roots , solving non-
linear equations...in such cases we prefer
approximate alg.
Deciding on approximate data structures
. Decide about the data structures like
int,char,float,struct ...etc. Which are going to use
while designing alg.

Alg design techniques


.Alg design techniques provide a powerful set of
general approaches to algorithmic problem
solving.
.Designing an alg for a particular problem may still
be a challenging task.
Methods of specifying an algorithm
. After designing we need to specify an alg either using
words(Natural language) or using pseudocode or
flowchart.
. After designing we need to check the correctness of the
alg using a technique called mathematical induction.
i.e you have to prove that the alg yields a required result
for every legitimate input in a finite amount of time.
Analyzing an algorithm
. After correctness we need to check its efficiency in terms of time
& space
. Time efficiency indicates how fast an alg work
. Space efficiency indicates how much extra memory the alg needs.
. The two more characteristic features we analyze are simplicity &
generality.
. If you are not satisfied with any of these characteristics you need
to redesign the algorithm.
Coding
. It is the process of converting an alg into a form so that it can be
executed on a computer.
Fundamental data structures
.Organizing data in a particular way plays an important role in
ADA.
. DS is a particular scheme of organizing related data items.
Linear DS
. Arrays & Linked lists are two important Linear DS.
. Array is a sequence of n items of same data types stored
continously which can be accessed using an index(0 to n-1).
. Each & every element is accessed in the same constant of
time
Fundamental data structures

. A Linked list is a sequence of zero or more elements called


nodes, each containing two kinds of information data and one
or more links called pointers to other nodes of the linked list.
. In a SLL, each node except the last one contains a single
pointer to the next element.
.The time needed to access an element of a SLL, depends on
where in the list the element is located.
. On the positive side, Linked lists do not require any
preliminary reservation of the computer memory.
. Insertions and deletions can be made quite efficiently in a
linked list by reconnecting a few appropriate pointers.
. Start a linked list with a special node called the header. This
node may contain information about the linked list itself, such
as its current length, in addition to a pointer to the first
element, a pointer to the last element.
. DLL , in which every node, except the first and the last ,
contains pointers to both its successor and its predecessor.
. The array and linked list are two principal choices in
representing a more abstract data structure called a linear list
or simply a list.
. A list is a finite sequence of data items i.e a collection of data
items arranged in a linear order.
. The basic operations performed on this DS are searching ,
inserting, and deleting an element.
. Two special types of lists- stacks and queues.
. Many important applications require selection of an item of
the highest priority among dynamically changing set of
candidates- Priority queue
Fundamentals of the analysis of algorithm
efficiency

“Analysis of algorithm” means an investigation of an algorithm’s


efficiency with respect to two resources; running time & memory
space.
i.e We analyze algorithm with respect to two kinds of efficiencies-
Time efficiency and Space efficiency.
-Time efficiency indicates how fast an algorithm in question runs .
- Space efficiency deals with the extra space the alg requires.
- The observation shows that algs run longer on larger inputs. Eg: It
takes longer to sort larger arrays, multiply larger matrices and so
on.
- Therefore it is logical to investigate algs efficiency as a function of
some parameter ‘n’ indicating algs input size.
Measuring running time

. To measure running time of an alg we can use some


standard unit of time- a second, a millisecond and so on.
. Drawback of this approaches are , here the running time
depend on the speed of a particular computer, quality of the
program, compiler used etc.
. Since we would like to have a metric that does not depend
on these extraneous factors.
. One possible approach is to count the number of times each of the alg
operations is executed, which is excessively difficult and usually
unnecessary.
. So we identify the most important operation of the algorithm called
“Basic operation” which consumes more time of the algorithm execution
and compute number of times the basic operation is executed.
. The problem with this approach is that we cannot differentiate running
times on small inputs.
. As ‘n’ increases the number of times the basic operation executed
increases , for large values of n, it is the functions order of growth that
counts.
Order of growth function which are important for analysis of
algorithm are as follows:
1 -constant
Log2n -Logorithmic
n -linear
nlogn -n-log-n
n2 -Quadratic
n3 - Cubic
2n -Exponential
n! -factorial
. The function growing the slowest among these is the constant
function.
. On the other end of the spectrum , exponential and factorial
functions grows so fast that their values become astronomically large
even for rather small values of n.
Orders of Growth

Dept of MCA, NIE, Mysore


Dept of MCA, NIE, Mysore
. Till now we have been measuring algorithms efficiency as a
function of a parameter indicating the size of the algs input.
.But there are many algs for which running time depends
not only on an input size but also on the specifics of a
particular input.
Eg: Sequential search
.Running time of this alg varies with the ’key value’ for the
same sized array, which indicates running time does not
entirely depend on the input size n only.
.Therefore we analyze alg with different scenarios like best,
worst and avg case efficiencies.
Worst case efficiency

.The efficiency of the alg for the worst case input of size n, for
which alg runs the largest among all possible inputs of that
size.
. We analyze the alg to see what kind of input yields the
largest value for basic operations count among all possible
inputs of size n .
Eg: c worst(n)=n
Best case efficiency
. The alg runs the fastest among all possible inputs of that
size.

. We determine the kind of input for which the count c(n)


will be smallest among all possible inputs of size n .
Eg: c best(n)=1

. However neither the worst-case analysis nor its best-case


counterpart yields the necessary information about an algs
behaviour on a “typical” or “random input”.

. This is the information that the avg-case efficiency seeks


to provide.
. Divide all instances of size ‘n’ into several classes so that for
each instances of the classes the number of times the basic
operation is executed is the same.
. Then a prob. Distribution of inputs is obtained so that the
expected value of the basic operation’s count can be found.
.Eg: Sequential search
The standard assumptions are that
(a) The probability of a successful search is equal to p(0 to 1)
(b) The prob. Of the first match occurring in the i th position
of the list is the same for every i.
In the case of unsuccessful search, the number of
comparisons will be n with the prob. Of search being (1-p).
Asymptotic Notations
. We know that order of growth of an alg’s basic operation count is
the principal indication of the alg’s efficiency.
. To compare and rank such orders of growth we use three notations:
O(big oh)
Ω(big omega)
Θ(big theta)
Let t(n) and g(n) can be any nonnegative functions defined on the set
of natural numbers.
t(n) will be an algorithm’s running time indicated by its basic
operation count C(n).
g(n) will be some simple function to compare the count with.
Informal introduction
. O(g(n) ) is the set of all functions with a
lower or same order of growth as g(n).
. Ω(g(n)) stands for the set of all functions with a higher or
same order of growth as g(n).
.Θ(g(n)) is the set of all functions that have the same order of
growth as g(n).
O(big
oh)

. The function t(n)=O(g(n)) iff there exist positive constants c


and no such that t(n)≤c*g(n) for all n, n≥no
Ω(big
omega)
.
The function t(n)=Ω(g(n) iff there exist positive constants c
and no such that t(n)≥c*g(n) for all n, n≥no
Θ(big theta)

Θ
The function t(n)= (g(n) iff there exist positive constants c1, c2
and no such that c1*g(n)≤t(n)≤c2*g(n) for all n, n≥no
. The theta notation is more precise than both
the big-oh and omega notations.
. The function t(n)=Θ(g(n)) iff g(n) is both an
upper and lower bound on t(n).
Mathematical Analysis of Non-recursive Algs

To analyse non-recursive algs we follow these steps


. Decide on a parameter(s) indicating an input’s size
. Identify the algs basic operation(Inner most loop)
. Check whether the number of times the basic operation is
executed depends only on the size of an input. If it also depends on
some additional property the worst-case, avg-case and if necessary ,
best-case efficiencies have to be investigated separately.
. Set up a sum expressing the number of times the algs basic
operation is executed.
. Using standard formulas and rules of sum manipulation ,establish
its order of growth.
Mathematical Analysis of Recursive Algs

. Decide on a parameter indicating an input’s size


. Identify the algs basic operation
. Check whether the basic operation is depends only on
nor any other parameters.
. Set up a recurrence relation with an appropriate initial
condition for the number of times the basic operation is
executed.
. Solve the recurrence relation to get the order of
growth.
.
.
.
.
.
Selection sort
#include <stdio.h>

int main()
{

.
int a[10],n,i,j,temp,min;

printf("enter n");
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-2;i++)
{
min=i;
for(j=i+1;j<=n-1;j++)
{
if (a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}

for(i=0;i<n;i++)
printf("%d \t",a[i]);
#include <stdio.h>
int main()
{
int a[10],n,i,j,temp,min;
printf("enter n");
scanf("%d",&n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2-i;j++)
{
if (a[j+1]<a[j])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf("%d \t",a[i]);
return 0;
#include <stdio.h>
#include<string.h>
int main()
{

char t[50]="nobody-noticed-him";
char p[50]="not";
int m,n,i,j,f=0;
n=strlen(t);

m=strlen(p);

for(i=0;i<=n-m;i++)
{
j=0;
while((j<m) && (p[j]==t[i+j]))
j=j+1;

if (j==m)
{
printf("matching location=%d",i);
f=1;
}

You might also like