DAA Unit1
DAA Unit1
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)
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
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.
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