Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
10 views29 pages

DAA Unit1

The document provides an introduction to algorithms, defining them as finite sets of unambiguous instructions for solving problems. It covers properties of algorithms, methods of expressing them (such as flowcharts and pseudocode), and discusses performance analysis including time and space complexity. Additionally, it outlines the importance of efficiency in algorithms and provides examples of common algorithms and their complexities.

Uploaded by

kakadepatil09
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)
10 views29 pages

DAA Unit1

The document provides an introduction to algorithms, defining them as finite sets of unambiguous instructions for solving problems. It covers properties of algorithms, methods of expressing them (such as flowcharts and pseudocode), and discusses performance analysis including time and space complexity. Additionally, it outlines the importance of efficiency in algorithms and provides examples of common algorithms and their complexities.

Uploaded by

kakadepatil09
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/ 29

UNIT-1 :Introduction to

Algorithms
A.H.Telgaonkar
Structure
Content
• Definition of Algorithms, Properties of
Algorithms, Expressing Algorithm, Sorting
techniques – Bubble Sort, Insertion sort,
Selection sort, Heap sort, Order of Growth,
Asymptotic Notations, Performance Analysis of
Recursive and Non- recursive algorithms
Introduction
• Algorithm has come to refer to a method that
can be used by a computer for the solution of
problem.

• This is what makes algorithm different from


world such as process ,technique ,or method
Engineered for
Tomorrow

WHAT IS AN ALGORITHM?
An algorithm is a sequence of unambiguous instructions
for solving a problem, i.e., for obtaining a required
output for any legitimate input in a finite amount of time.
problem

algorithm

input “computer” output


Algorithm
• Definition
An algorithm is a finite set of instructions that
accomplishes a particular task. In addition ,all
algorithms must satisfy the following criteria:
– Input : zero or more quantities are externally supplied
– Output : at least one quantity is produced
– definiteness: clear and unambiguous
– finiteness: terminate after a finite number of steps
– effectiveness: instruction is basic enough to be carried out
Properties of Algorithm

• Non Ambiguity
Each step in an algorithm should be non-ambiguous.
That means each instruction should be clear and
precise.
• Range of Input
The range of input should be specified.
• Multiplicity
The same algorithm can be represented into several
different ways. for solving the same problem we can
write several different algorithms.
• Speed
The algorithms written using some specified
ideas. Bus such algorithm should be efficient
and should produce the output with fast speed.
• Finiteness
The algorithm should be finite. That means
after performing required operations it should
be terminate.
Expressing Algorithm
• an algorithm can express using flow charts,
pseudo code, programs.
• A flowchart is a diagrammatic representation
of an algorithm.
• A flowchart can be helpful for both writing
programs and explaining the program to
others.
• THE ALGORITHM FOR THE GREATEST NUMBER OF
THREE ::-
• STEP 1:- START
• STEP 2:- Declare three variables X,Y,Z.
• STEP 3:-If X>Y&X>Z print X is the GREATEST.
• STEP 4:-Otherwise if Y>Z&Y>X print Y is the
GREATEST.
• STEP 5:- Otherwise print Z is GREATEST.
• STEP 6:- STOP
• Pseudocode
• Pseudocode, or structured English, allows a
programmer to use English-like sentences to
write an explanation of what a program is
supposed to do.
• Pseudocode

Start
Declare Integer a, b, sum
Output "Sum of Two Numbers"
Input a
Input b
Assign sum = a+b
Output "The total sum of " & a & " and " & b & " is " & sum &"."
Stop

Algorithm

Step 1: Start
Step 2: Declare variables A,B,SUM
Step 3: Input two numbers say A and B
Step 4: SUM = A + B
Step 5: Display SUM
Step 6: Stop
• A program is a set of instructions that a
computer uses to perform a specific function.
#include<stdio.h>
int main() {
int a, b, sum;
printf("\nEnter two no: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum : %d", sum);
return(0);
• Algorithm : Systematic logical approach which is a
well-defined, step-by-step procedure that allows a
computer to solve a problem.
Pseudocode : It is a simpler version of a
programming code in plain English which uses short
phrases to write code for a program before it is
implemented in a specific programming language.
Program : It is exact code written for
problem following all the rules of the programming
language.
Various steps in developing algorithm
• The study of algorithm includes many
important and active areas of research
• 1. How to devise algorithms: creating an
algorithm is a art which may never be fully
automated.
• 2. How to validate algorithms: once an
algorithm is devised, it is necessary to show
that it computes the correct answer for all
possible legal input.
3. How to analyze algorithms: as an algorithm is
executed, it uses the computers CPU to
perform operation & memory to hold the
program & data.
• Analysis of algorithm or performance analysis
refer to the task of determining how much
computing time & storage an algorithm
requires.
4. How to test program: testing program consist
of two phases:
• Debugging & performance measurement
Efficiency of an algorithm
• Efficiency of an algorithm denotes the rate at which
an algorithm solves a problem of size n.
• It is measured by the amount of resources it uses,
the time and the space.
• The time refers to the number of steps the
algorithm executes while the space refers to the
number of unit memory storage it requires.
• An algorithm’s complexity is measured by
calculating the time taken and space required for
performing the algorithm.
Time Complexity of an Algorithm
• Time Complexity of an algorithm is the amount of time(or the
number of steps) needed by a program to complete its task (to
execute a particular algorithm)
• The way in which the number of steps required by an algorithm
varies with the size of the problem it is solving.
• The time taken for an algorithm is comprised of two times
• Compilation Time
• Run Time
• Compilation time is the time taken to compile an algorithm.
While compiling it checks for the syntax and semantic errors in
the program and links it with the standard libraries , your
program has asked to.
• Run Time: It is the time to execute the
compiled program. The run time of an
algorithm depend upon the number of
instructions present in the algorithm. Usually
we consider, one unit for executing one
instruction.
• Time complexity of an algorithm is generally
classified as three types. (i) Worst case (ii)
Average Case (iii) Best Case
• Worst Case: It is the longest time that an algorithm will
use over all instances of size n for a given problem to
produce a desired result.
• Average Case: It is the average time( or average space)
that the algorithm will use over all instances of size n for
a given problem to produce a desired result. It depends
on the probability distribution of instances of the
problem.
• Best Case: It is the shortest time ( or least space ) that
the algorithm will use over all instances of size n for a
given problem to produce a desired result.
Space Complexity
•  Space Complexity of a program is the
amount of memory consumed by the
algorithm ( apart from input and output, if
required by specification) until it completes its
execution.
• The way in which the amount of storage space
required by an algorithm varies with the size
of the problem to be solved.
Algorithm swap(a,b)
Start: time space
temp=a --------------1 a
a=b --------------------1 b
b=temp ---------------1 temp
Stop: f(n)=3 s(n)=3
O(1)
Frequency count method
Algorithm sum(A,n) time space
{
s=0;---------------------------- 1 A------ n
for(i=0;i<n;i++)------------- n+1 n------ 1
{
s=s+A[i];------------------- n s------ 1
} i------- 1
Return s;----------------------- 1 s(n)=n+3
} f(n)=2n+3 O(n)
O(n)
Sum of matrix
Algorithm add(A,B,n)
For(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
c[i,j]=A[i,j]+B[i,j];
}
}
Algorithm add(A,B,n)
For(i=0;i<n;i++)---------- n+1 ------------ A-----n^2
{
for(j=0;j<n;j++)--------- n*(n+1) ------- B ------n^2
{
c[i,j]=A[i,j]+B[i,j];-----n*n --------- C ----n^2
} ----- n ---1
} f(n)=2n^2+2n+1 ----- i----1
O(n^2) ------j----1
s(n)=3n^2+3
O(n^2)
Multiplication of Matrix
Algorithm multiply(A,B,n)
{
For(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
C[i,j]=0;
for(k=0;k<n;k++)
{
C[i,j]=C[i,j]+A[i,k]*B[k,j];
}
}
}
}
• F(n)= 2n^3+3n^2+2n+1
• O(n^3)
• S(n)=3n^2+4
• O(n^2)
• Time=O(n^3)
• Space=O(n^2)

You might also like