0% found this document useful (0 votes)
11 views36 pages

DAA Unit1

Uploaded by

shriyaonkar15
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)
11 views36 pages

DAA Unit1

Uploaded by

shriyaonkar15
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/ 36

CY43/CI43 – DAA

Algorithm
• An algorithm is a sequence of unambiguous instructions for solving a
computational problem, i.e., for obtaining a required output for any
legitimate input in a finite amount of time.
Examples of Algorithms
• Computing Greatest Common Divisor of Two non-
negative, not-both zero Integers
• gcd(m, n): the largest integer that divides both m and
n
• Euclid’s Algorithm:
• gcd(m, n) = gcd(n, m mod n
Greatest Common Divisor (Euclid’s Algorithm),
gcd(m, n)

• Step 1: If n = 0, return value of m as the answer and stop;


otherwise, proceed to Step 2.
• Step 2: Divide m by n and assign the value of the remainder
to r.
• Step 3: Assign the value of n to m and the value of r to n. Go
to Step 1
Pseudocode for (Euclid’s Algorithm), gcd(m, n)
• ALGORITHM Euclid(m, n)
• // Computes gcd(m, n) by Euclid’s algorithm
• // Input: Two nonnegative, not-both-zero integers m and n
• //Output: Greatest common divisor of m and n
while n ≠ 0 do
r = m mod n
m=n
n=r
return m

Question: GCD(50, 30) how many division Operations are required to compute GCD
using Euclid algorithm ?
Middle-school procedure, gcd(m, n)
• Step 1: Find prime factors of m.
• Step 2: Find prime factors of n.
• Step 3: Identify all common prime factors of m and n
• Step 4: Compute product of all common factors and return
product as the answer.
Consecutive Integer Checking, gcd(m, n)
• Step 1: Assign the value of min(m, n) to t.
• Step 2: Divide m by t. If the remainder is 0, go to Step 3;
otherwise, go to Step 4.
• Step 3: Divide n by qt If the remainder is 0, return the value
of t as the answer and stop; otherwise, proceed to Step 4.
• Step 4: Decrease the value of t by 1. Go to Step 2.
Algorithm Analysis
• Same problem, but different algorithms, based on
different ideas and having dramatically different
speeds.
C++ Program - Analysis of the methods to
find the GCD of two numbers
#include<iostream.h> r=m%n;
#include<conio.h> m=n;
#include<time.h> n=r;
long int euclid(long int m,long int n) }
{ end=clock();
clock_t start,end; cout<<endl<<"Time
start=clock(); taken:"<<(end-start)/CLK_TCK<<" sec";
long int r; return m;
while(n!=0) }
{
Contd…..
long int con(long int m,long int n) if(r==0)
{ {
clock_t start,end; r=n%t;
start=clock(); if(r==0)
long int t,r,g; g=t;
if(m>n) else
{ t=n; } {
else t--;
{ t=m; } goto a;
a:do }
{ }
r=m%t; end=clock();
if(r!=0) cout<<"Time taken :"<<(endstart)/CLK_TCK<<" sec";
t--; return g;
} while(r!=0); } /*End of the function con*
Contd…. Analysis of 2 algorithms
void main() cout<<endl<<endl<<"-----------------------
{ -------------------------";
long int x,y; cout<<endl<<endl<<"GCD -
CONSECUTIVE INTEGER CHECKING ALG
clrscr(); :
cout<<"\t\t ANALYSIS OF THE TWO "<<endl<<endl;
ALGORITHMS"<<endl<<endl;
cout<<endl<<endl<<"GCD :
cout<<"GCD - EUCLID'S ALG : "<<endl; "<<con(x,y);
cout<<"enter two numbers:"; getch();
cin>>x>>y; }
cout<<endl<<endl<<"GCD :
"<<euclid(x,y);
Fundamentals of Algorithmic
Problem Solving

• Sequence of steps in the


process of design and
analysis of algorithms
Understanding the problem
• Ask questions, do a few small examples by hand, think about special cases,
etc.
• An input is an instance of the problem the algorithm solves
• Specify exactly the set of instances the algorithm needs to handle
• Decide on
• Exact vs. approximate solution
• Approximate algorithm: Cannot solve exactly, e.g., extracting square roots,
solving nonlinear equations, etc.
• Appropriate Data Structure
Analyze algorithm
• Time efficiency: How fast it runs
• Space efficiency: How much extra memory it uses
• Simplicity: Easier to understand, usually contains fewer
bugs, sometimes simpler is more efficient, but not always.

Coding algorithm
• Write in a programming language for a real machine
• Standard tricks:
• Compute loop invariant (which does not change value in the loop)
outside loop
• Replace expensive operation by cheap ones.
Analysis of Algorithms
• Reasons to Analyze Algorithms
• Predict Performance
• Compare Algorithms
• Provide Guarantees
• Understand theoretical basis.
• Primary Practical Reason: Avoid Performance Bugs
Performance measure of the algorithm
• Two kinds of efficiency:
• Space Efficiency or Space Complexity
• Time Efficiency or Time Complexity
Two kinds of Algorithm Efficiency
• Analyzing the efficiency of an algorithm (or the complexity of an algorithm)
means establishing the amount of computing resources needed to execute
the algorithm.
There are two types of resources:
• Memory space. It means the amount of space used to store all data
processed by the algorithm.
• Running time. It means the time needed to execute all the operations
specified in the algorithm.

• Space efficiency: Deals with the space required by the algorithm


• Time efficiency: It indicates how fast an algorithm runs.
What is Space complexity?
• For any algorithm, memory is required for the following purposes
• Memory required to store program instructions
• Memory required to store constant values
• Memory required to store variable values

• Space complexity of an algorithm can be defined as follows


• Total amount of computer memory required by an algorithm to
complete its execution is called as space complexity of that algorithm
What is Space complexity?
• Generally, when a program is under execution it uses the computer
memory for THREE reasons.
They are:
• Instruction Space: It is the amount of memory used to store compiled
version of instructions.
• Data Space: It is the amount of memory used to store all the variables and
constants.
• Environmental Stack: It is the amount of memory used to store information
of partially executed functions at the time of function call.
Space Complexity= Instruction space + Data space + Stack space
Stack Allocation of Space
Temporary values, such as those arising from the evaluation of
expressions, in cases where those temporaries cannot be held in
registers.

A saved machine status, with information about the state of the


machine just before the call to the procedure. This information
typically includes the return address (value of the program counter,
to which the called procedure must return).

An "access link" may be needed to locate data needed by the called


procedure but found elsewhere, e.g., in another activation record.

A control link, pointing to the activation record of the caller.

The actual parameters used by the calling procedure. Commonly,


these values are not placed in the activation record but rather in
registers, when possible, for greater efficiency.
Calculating Space Complexity
• To calculate the space complexity, we must know the memory
required to store different datatype values (according to the
compiler). For example, the C Programming Language compiler
requires the following.
• 1 byte to store Character value,
• 2 bytes to store Integer value,
• 4 bytes to store Floating Point value,
• 6 or 8 bytes to store double value
Calculating Space Complexity
Calculating the Data Space required for the following given code
int square(int a)
{
return a*a;
}
Data Space Required:
• For int a  2 Bytes
• For returning a*a  2 Bytes
Total 4 Bytes
Data Space Required:
• This code requires 2 bytes of memory to store variable 'a' and another 2 bytes of memory is used for return
value.
• That means, totally it requires 4 bytes of memory to complete its execution. And this 4 bytes of memory is
fixed for any input value of 'a'. This space complexity is said to be Constant Space Complexity.
• If any algorithm requires a fixed amount of space for all input values then that space complexity is said to be
Constant Space Complexity.
Performance measure of the algorithm
• What is Time complexity?
• Every algorithm requires some amount of computer time to execute
its instruction to perform the task. This computer time required is
called time complexity.
• Time complexity of an algorithm can be defined as follows.
• The time complexity of an algorithm is the total amount of time
required by an algorithm to complete its execution
What is Time complexity?
• Generally, running time of an algorithm depends upon the following.
• Whether it is running on Single processor machine or Multi processor
machine.
• Whether it is a 32 bit machine or 64 bit machine
• Read and Write speed of the machine.
• The time it takes to perform Arithmetic operations, logical operations,
return value and assignment operations etc.,
• Input data
Calculating Time Complexity
Calculating Time Complexity

If any program requires fixed amount of time for all input values
then its time complexity is said to be Constant Time Complexity.
Calculating Time Complexity
Calculating Time Complexity
Calculating Time Complexity
• Cost is the amount of computer time required for a single operation in each line.
• Repetition is the amount of computer time required by each operation for all its
repetitions.
• Total is the amount of computer time required by each operation to execute.
• So above code requires '4n+4' Units of computer time to complete the task. Here
the exact time is not fixed. And it changes based on the n value. If we increase the n
value then the time required also increases linearly.
• Totally it takes '4n+4' units of time to complete its execution and it is Linear Time
Complexity.
• If the amount of time required by an algorithm is increased with the increase of
input value then that time complexity is said to be Linear Time Complexity

You might also like