Design And Analysis of Algorithms
Introduction to Design and analysis of
algorithms
 What is an algorithm?
Algorithm is a set of steps to complete a
task
For example
 To make a cup of tea for guest
 From above definition we make algorithm…
• Task:- To make a cup of tea
• Algorithm:
Step-1 :- add water and milk to the kettle,
Step-2 :- boilit, add tea leaves,
Step-3 :- Add sugar, and then serve it in
What is Computer algorithm?
 Although there is no universally agreed-on
wording to describe this notion, there is general
agreement about what the concept means:
Definition
 ’a set of steps to accomplish or complete a
task that is described that how that a
computer can run it’’ and solving a
problem if arise a problem.
 An algorithm is a sequence of
unambiguous instructions for solving a
problem
Lets Understand withsimple diagram
problem
algorithm
compute
r
input OutPut
Algorithm Design
 The important aspects of algorithm design include
creating an efficient algorithm to solve a problem
in an efficient way using minimum time and
space.
 To solve a problem, different approaches can be
followed
 can be efficient with respect to time consumption.
 can be efficient with respect to memory efficient.
Problem Development Steps
 Problem definition
 Development of a model
 Specification of an Algorithm
 Designing an Algorithm
 Checking the correctness of an Algorithm
 Analysis of an Algorithm
 Implementation of an Algorithm
 Program testing
 Documentation
Problem definition
 What information is given?
 What is to be found out?
 How to recognize a valid solution?
 What information required to define the algorithm is
missing?
 Is any part of the information worthless?
Know the general problem solving strategy
Divide and conquer
Binary doubling
Greedy search..etc
Development of a model
 Which mathematical structure seems best suited
for this problem.
 This structure is able to solve other problem is
arise?
Characteristics of Algorithms
 Algorithms must have a unique name
 Must take an input.
 Must give some output(yes/no,valueetc.)
 Definiteness –each instruction is clear and
unambiguous.
 Finiteness –algorithm terminates after a
finite number of steps.
 Effectiveness –every instruction must be
basic i.e.simple instruction.
Expectation from an algorithm
 Correctness
 Correct: Algorithms must produce correct
result.
 Produce an incorrect answer:Even if it fails to
give correct results all the time still there is a
control on how often it gives wrong result.
 Approximation algorithm: Exact solution is not
found, but near optimal solution can be found
out.
 Less resource usage:
 Algorithms should use less resources (time
Factorial Program and Algorithm
#include<stdio.h>
#include<conio.h>
int fact(int n)
{
if(n >= 1)
return n*fact(n-1);
else
return 1;
}
Factorial Program and Algorithm
void main()
{
int no;
clrscr();
printf("n Enter Number Of fact ::");
scanf("%d",&no);
printf("Factorial Number ::%d",fact(no));
getch();
}
Factorial Algorithm ::
 Step:1 Start
 Step:2 To declare header file
#include<stdio.h>
#include<conio.h>
 Step:3 To declare user define function of
fact() with one argument
int fact(int n)
Factorial Algorithm ::
 Step:4 In function to check condition
if(n >= 1)
if yes than
return n*fact(n-1)
and Repeat until falls
else than
Step:5 go to next step
return 1
Step:6 To declare main function and just
call fact() function with pass one Argument
An overview of Top-down design and
Recursion
 Top down design is also known as a step
wise design.
 Its breaking down problems in to step.
 In top-down design an overview of the
problem is described first, specifying but not
detailing any first-level sub-steps .
 After breaking down problem and then
prove that if each sub problem is solved
correctly and solution are fitted together in a
specified way.
An overview of Top-down design and
Recursion
 This is the general approach to design but in
order to use it for the claimed benefits we
have to use a special discipline of
expressing algorithms called This discipline
is required so that the proof mentioned in
the definition of the top-down design
method.
Lets we understand with examples
g.c.d(greatest common divisor)
 Step 1:- Give integer a and b , fing g.c.d.
 Step 2:-
Note: before proceeding mention the problem
 Gcd(0,0)=0
 Gcd(x,y)=gcd(y,x)
 Gcd(x,y)=gcd(-y,x)
 Step 3:- Set x ‹-- a, y ‹-- b
if y!=0 then
3.1 decrease y and change x in such a way that remain
non negative
Repeat step 2
3.2 If y=0 then set gcd(a,b)‹-
Lets we understand with examples
g.c.d(greatest common divisor)
 Step 4:-
x=(x div y )*y+x mod y
 step 5:-
 5.1 set r ‹- x mod y
 5.2 set x ‹- y
 5.3 set y ‹- r
Complete algorithm
 Set x ‹-- a, y ‹-- b
if y!=0 then
set r ‹- x mod y
set x ‹- y
set y ‹- r
repeat step 2.
If y=0 then set gcd(a,b) ‹- x
Complete Flowchart
 Stage 1 flowchart
 Observe that the figures are annotated with the relations that
should hold before and After the specific statement.
 Stage 2 flowchart
 The decomposition process ends only when the actions
specified in any of the boxes are simple and well understood by
the executing agent.
 Stage 3 flowchart
 If any boxes process still in condition mode expand it
Stage 1
flowchart
Set x to a, y
to b
Y!=0
Set x to a Reduce y
and change x while
preseving gcd
A>b,b>=0
Gcd(a,b)=gcd(x,y)
False
True
X=gcd(x,y)
Gcd(a,b)=gcd(x,
y)
Stage 2
flowchart Set x to a
Y!=0
Set r to x mod
y
A>b,b>=0
Gcd(a,b)=gcd(x,y)
False
True
X=gcd(x,y)
Gcd(a,b)=gcd(x,
y)
Set y to b
When y=0
Set x to y
Set y to r
r=x mod y
x=y
Y=r
Stage 3
flowchart Set x to a, y
to b
Y!=0
Set r to r-y
A>b,b>=0
Gcd(a,b)=gcd(x,y)
False
True
R is the output
r=x mod y
structured programming.
 Structured programming (SP) is a technique to
improve the reliability and clarity of programs.
 Structure programming is useful both in the
development of the algorithm and in the
implementation of the program.
 As above we see the basic structure
programming constructs the sequence and
while-do arise in development of an algorithm.
structured programming.
 flow within the program is controlled
exclusively by only three forms of control,
selection, repetition, and sequencing.
 The basic control structures proposed to
implement these are IF …ELSE for
selection,DO WHILE for repetition, and
simply placing statements one after the
other for sequencing.
 There are various other possible
structures do until, forloop, go to , jump ,
and all branching statement.
Control Construct
In most HLL(high level language)
supporting control constructs
1. If….else
2. Do…while / while
3. For loop
4. case
5. Repeat until.
6. Go to
7. Exit
Control Statements
 If Statement: – Conditionally execute single
statement.
 If-Else Statement: – Conditionally select one
of the two statements and execute it.
 Switch Statement: – Conditionally choose
one of the several statements and execute it.
Repetitive Statements
 It has three types of loop statements:
1. for loop,
2. while loop,
3. do while loop.
 These statements are allowed as nested
statement to be executed repetitively.
 Each execution of nested statement is called
iteration of the loop.
While Loop
 In this case boolean
expression, can be
true or false.
 It is a pretest loop
statement.
 The boolean
expression is tested
start of the each
iteration of the loop.
Do-While Loop
 It is a post-test loop
statement.
 In this case boolean
expression is tested
after each iteration of
the loop.
 If it is false that
means loop is
terminated.
For Loop
 It is a pretest loop
statement. In this
case initialization and
increment is an
expression.
 A boolean
expression also is an
expression that can
be true or false.
Branching
 break The break
keyword is used to
exit a for loop, while
loop, do loop or to
the end of a case
block in a switch
statement.
Continue: Continue
is helpful for skipping
the next iteration of a
do, while or for loop
statement
4) Factors affecting efficiency of
algorithms
 Use of Loops
 As we know all a loop executing one more or
one less time than the intended number is so
common.
 So we must aware of three aspects to construct
a loop
 The initial condition that needs to be true before the
loop begins execution.
 The invariant relation that must hold before, during ,
and after each iteration of the loop.
 The condition under which the loop must terminate.
4.1) initial condition
 Set the loop control variables to values which
are appropriate for solving the smallest
instance of the problem in question.
 For example
 Suppose we want to add elements of the given
array using a loop.
 Loop variables are I the loop control variable
which is also used as the array index and s the
current value of the sum.
 The smallest problem in this case is the sum of 0
number of elements in this case s must be 0 so
4.2) iterative construct
 Try to extend the smallest problem to the next
smallest. In doing this we will come to know that
changes are to be done to the loop variables to
achieve this change.
 For example
 Form above example of summation of elements of an
array, the next smallest in the case of n=1, in which
case s must be equal to a[i].
 So find the solution form n=0 by first considering the
solution for i=1;
I ‹- 1;
S ‹- a[1];
 So now from above we can write
I ‹- I+1;
S ‹- s+a[i];
4.3) loop terminate
 We also care for loop termitnation.
 The simples termination condition occurs
when it is know in advance the number of
times the loop is to be iterated.
 For example
 In the case of if we know how many time loop
execute so we write like
For I ‹- 1 to 20 do
----------------
----------------
End for
4.3) loop terminate
 In every case we can not know the initial value
assigned and the final value are constants.
 The only requirement is that their values
should be known at the start of the loop thus
we can write
 For example
For I ‹- m to n do
----------------
----------------
End for
5) Efficiency of algorithms
 The design and implementation of algorithm
influence on their efficiency.
 For implement must use some of the system
resources to complete its task.
 The resources most relevant to the efficiency
are the use of CPU time and internal memory
RAM
 Now we understand this with example
 In the past we see the computer resources was
the costly and performance is low compare to
5) Efficiency of algorithms
 Now a day we see that the cost of computer
and requirement is more and performance is
also high and so this is done by algorithms
efficiency change and now its efficiency is high
so its time consuming is low and process
speed is high.
 So we must aware of following aspects
 Removing redundant computation outside loop
 Referencing of array elements
 Inefficiency due to late termination
 Early detection of desired output condition
5.1) Removing redundant
computation outside loop
 Most of the inefficiencies that creep into the
implementation of algorithms are due to
redundant computations or unnecessary
storage.
 The effect of redundant computation is serious
when it is embedded within a loop which must
be executed many a time.
 The most common mistake when using loops
in to repeatedly re-calculate the part of an
expression that remains constant throughout
5.1) Removing redundant
computation outside loop
 For Example
x=0;
For i =1 to n do
Begin
x=x+0.01;
y=(a*a*a*c)*x*x+b*b*x;
Writeln(‘x=’,x ,y);
End
as above loop does calculate number of time
computation inside the loop so we must do or write this
program in following way
 For Example
x=0;
a = a*a*a*c;
b= b*b;
For i =1 to n do
Begin
x=x+0.01;
y= a*x*x+b*x;
Writeln(‘x=’,x ,y);
End
5.2) Referencing of array elements
 If care is not exercised while programming redundant
computations can creep into array processing.
 Observe following example in that example to find the
maximum number and its position in an array of
number.
Version - 1
p=0;
For(i=0; i<n; i++)
{
if(a[i] >a[p])
{
p=i;
}
}
Max = a[p];
Version - 2
p=0; i=0;
max=a[i];
For(i=0; i<n; i++)
{
if(a[i] >max)
{
max=a[i];
p=I;
}
}
 As above we see that the version 2 is less
time consuming loop because of simple
variable is there so its does not check all
position of array in condition.
 So version 2 implementation is preferable
because of condition check a[i] >max which
is much more efficient to perform then the
corresponding test in the version 1 of the
program

Daa unit 1

  • 1.
    Design And Analysisof Algorithms
  • 2.
    Introduction to Designand analysis of algorithms  What is an algorithm? Algorithm is a set of steps to complete a task For example  To make a cup of tea for guest  From above definition we make algorithm… • Task:- To make a cup of tea • Algorithm: Step-1 :- add water and milk to the kettle, Step-2 :- boilit, add tea leaves, Step-3 :- Add sugar, and then serve it in
  • 3.
    What is Computeralgorithm?  Although there is no universally agreed-on wording to describe this notion, there is general agreement about what the concept means: Definition  ’a set of steps to accomplish or complete a task that is described that how that a computer can run it’’ and solving a problem if arise a problem.  An algorithm is a sequence of unambiguous instructions for solving a problem
  • 4.
    Lets Understand withsimplediagram problem algorithm compute r input OutPut
  • 5.
    Algorithm Design  Theimportant aspects of algorithm design include creating an efficient algorithm to solve a problem in an efficient way using minimum time and space.  To solve a problem, different approaches can be followed  can be efficient with respect to time consumption.  can be efficient with respect to memory efficient.
  • 6.
    Problem Development Steps Problem definition  Development of a model  Specification of an Algorithm  Designing an Algorithm  Checking the correctness of an Algorithm  Analysis of an Algorithm  Implementation of an Algorithm  Program testing  Documentation
  • 7.
    Problem definition  Whatinformation is given?  What is to be found out?  How to recognize a valid solution?  What information required to define the algorithm is missing?  Is any part of the information worthless? Know the general problem solving strategy Divide and conquer Binary doubling Greedy search..etc
  • 8.
    Development of amodel  Which mathematical structure seems best suited for this problem.  This structure is able to solve other problem is arise?
  • 9.
    Characteristics of Algorithms Algorithms must have a unique name  Must take an input.  Must give some output(yes/no,valueetc.)  Definiteness –each instruction is clear and unambiguous.  Finiteness –algorithm terminates after a finite number of steps.  Effectiveness –every instruction must be basic i.e.simple instruction.
  • 10.
    Expectation from analgorithm  Correctness  Correct: Algorithms must produce correct result.  Produce an incorrect answer:Even if it fails to give correct results all the time still there is a control on how often it gives wrong result.  Approximation algorithm: Exact solution is not found, but near optimal solution can be found out.  Less resource usage:  Algorithms should use less resources (time
  • 11.
    Factorial Program andAlgorithm #include<stdio.h> #include<conio.h> int fact(int n) { if(n >= 1) return n*fact(n-1); else return 1; }
  • 12.
    Factorial Program andAlgorithm void main() { int no; clrscr(); printf("n Enter Number Of fact ::"); scanf("%d",&no); printf("Factorial Number ::%d",fact(no)); getch(); }
  • 13.
    Factorial Algorithm :: Step:1 Start  Step:2 To declare header file #include<stdio.h> #include<conio.h>  Step:3 To declare user define function of fact() with one argument int fact(int n)
  • 14.
    Factorial Algorithm :: Step:4 In function to check condition if(n >= 1) if yes than return n*fact(n-1) and Repeat until falls else than Step:5 go to next step return 1 Step:6 To declare main function and just call fact() function with pass one Argument
  • 15.
    An overview ofTop-down design and Recursion  Top down design is also known as a step wise design.  Its breaking down problems in to step.  In top-down design an overview of the problem is described first, specifying but not detailing any first-level sub-steps .  After breaking down problem and then prove that if each sub problem is solved correctly and solution are fitted together in a specified way.
  • 16.
    An overview ofTop-down design and Recursion  This is the general approach to design but in order to use it for the claimed benefits we have to use a special discipline of expressing algorithms called This discipline is required so that the proof mentioned in the definition of the top-down design method.
  • 17.
    Lets we understandwith examples g.c.d(greatest common divisor)  Step 1:- Give integer a and b , fing g.c.d.  Step 2:- Note: before proceeding mention the problem  Gcd(0,0)=0  Gcd(x,y)=gcd(y,x)  Gcd(x,y)=gcd(-y,x)  Step 3:- Set x ‹-- a, y ‹-- b if y!=0 then 3.1 decrease y and change x in such a way that remain non negative Repeat step 2 3.2 If y=0 then set gcd(a,b)‹-
  • 18.
    Lets we understandwith examples g.c.d(greatest common divisor)  Step 4:- x=(x div y )*y+x mod y  step 5:-  5.1 set r ‹- x mod y  5.2 set x ‹- y  5.3 set y ‹- r
  • 19.
    Complete algorithm  Setx ‹-- a, y ‹-- b if y!=0 then set r ‹- x mod y set x ‹- y set y ‹- r repeat step 2. If y=0 then set gcd(a,b) ‹- x
  • 20.
    Complete Flowchart  Stage1 flowchart  Observe that the figures are annotated with the relations that should hold before and After the specific statement.  Stage 2 flowchart  The decomposition process ends only when the actions specified in any of the boxes are simple and well understood by the executing agent.  Stage 3 flowchart  If any boxes process still in condition mode expand it
  • 21.
    Stage 1 flowchart Set xto a, y to b Y!=0 Set x to a Reduce y and change x while preseving gcd A>b,b>=0 Gcd(a,b)=gcd(x,y) False True X=gcd(x,y) Gcd(a,b)=gcd(x, y)
  • 22.
    Stage 2 flowchart Setx to a Y!=0 Set r to x mod y A>b,b>=0 Gcd(a,b)=gcd(x,y) False True X=gcd(x,y) Gcd(a,b)=gcd(x, y) Set y to b When y=0 Set x to y Set y to r r=x mod y x=y Y=r
  • 23.
    Stage 3 flowchart Setx to a, y to b Y!=0 Set r to r-y A>b,b>=0 Gcd(a,b)=gcd(x,y) False True R is the output r=x mod y
  • 24.
    structured programming.  Structuredprogramming (SP) is a technique to improve the reliability and clarity of programs.  Structure programming is useful both in the development of the algorithm and in the implementation of the program.  As above we see the basic structure programming constructs the sequence and while-do arise in development of an algorithm.
  • 25.
    structured programming.  flowwithin the program is controlled exclusively by only three forms of control, selection, repetition, and sequencing.  The basic control structures proposed to implement these are IF …ELSE for selection,DO WHILE for repetition, and simply placing statements one after the other for sequencing.  There are various other possible structures do until, forloop, go to , jump , and all branching statement.
  • 26.
    Control Construct In mostHLL(high level language) supporting control constructs 1. If….else 2. Do…while / while 3. For loop 4. case 5. Repeat until. 6. Go to 7. Exit
  • 27.
    Control Statements  IfStatement: – Conditionally execute single statement.  If-Else Statement: – Conditionally select one of the two statements and execute it.  Switch Statement: – Conditionally choose one of the several statements and execute it.
  • 28.
    Repetitive Statements  Ithas three types of loop statements: 1. for loop, 2. while loop, 3. do while loop.  These statements are allowed as nested statement to be executed repetitively.  Each execution of nested statement is called iteration of the loop.
  • 29.
    While Loop  Inthis case boolean expression, can be true or false.  It is a pretest loop statement.  The boolean expression is tested start of the each iteration of the loop.
  • 30.
    Do-While Loop  Itis a post-test loop statement.  In this case boolean expression is tested after each iteration of the loop.  If it is false that means loop is terminated.
  • 31.
    For Loop  Itis a pretest loop statement. In this case initialization and increment is an expression.  A boolean expression also is an expression that can be true or false.
  • 32.
    Branching  break Thebreak keyword is used to exit a for loop, while loop, do loop or to the end of a case block in a switch statement. Continue: Continue is helpful for skipping the next iteration of a do, while or for loop statement
  • 33.
    4) Factors affectingefficiency of algorithms  Use of Loops  As we know all a loop executing one more or one less time than the intended number is so common.  So we must aware of three aspects to construct a loop  The initial condition that needs to be true before the loop begins execution.  The invariant relation that must hold before, during , and after each iteration of the loop.  The condition under which the loop must terminate.
  • 34.
    4.1) initial condition Set the loop control variables to values which are appropriate for solving the smallest instance of the problem in question.  For example  Suppose we want to add elements of the given array using a loop.  Loop variables are I the loop control variable which is also used as the array index and s the current value of the sum.  The smallest problem in this case is the sum of 0 number of elements in this case s must be 0 so
  • 35.
    4.2) iterative construct Try to extend the smallest problem to the next smallest. In doing this we will come to know that changes are to be done to the loop variables to achieve this change.  For example  Form above example of summation of elements of an array, the next smallest in the case of n=1, in which case s must be equal to a[i].  So find the solution form n=0 by first considering the solution for i=1; I ‹- 1; S ‹- a[1];  So now from above we can write I ‹- I+1; S ‹- s+a[i];
  • 36.
    4.3) loop terminate We also care for loop termitnation.  The simples termination condition occurs when it is know in advance the number of times the loop is to be iterated.  For example  In the case of if we know how many time loop execute so we write like For I ‹- 1 to 20 do ---------------- ---------------- End for
  • 37.
    4.3) loop terminate In every case we can not know the initial value assigned and the final value are constants.  The only requirement is that their values should be known at the start of the loop thus we can write  For example For I ‹- m to n do ---------------- ---------------- End for
  • 38.
    5) Efficiency ofalgorithms  The design and implementation of algorithm influence on their efficiency.  For implement must use some of the system resources to complete its task.  The resources most relevant to the efficiency are the use of CPU time and internal memory RAM  Now we understand this with example  In the past we see the computer resources was the costly and performance is low compare to
  • 39.
    5) Efficiency ofalgorithms  Now a day we see that the cost of computer and requirement is more and performance is also high and so this is done by algorithms efficiency change and now its efficiency is high so its time consuming is low and process speed is high.  So we must aware of following aspects  Removing redundant computation outside loop  Referencing of array elements  Inefficiency due to late termination  Early detection of desired output condition
  • 40.
    5.1) Removing redundant computationoutside loop  Most of the inefficiencies that creep into the implementation of algorithms are due to redundant computations or unnecessary storage.  The effect of redundant computation is serious when it is embedded within a loop which must be executed many a time.  The most common mistake when using loops in to repeatedly re-calculate the part of an expression that remains constant throughout
  • 41.
    5.1) Removing redundant computationoutside loop  For Example x=0; For i =1 to n do Begin x=x+0.01; y=(a*a*a*c)*x*x+b*b*x; Writeln(‘x=’,x ,y); End as above loop does calculate number of time computation inside the loop so we must do or write this program in following way
  • 42.
     For Example x=0; a= a*a*a*c; b= b*b; For i =1 to n do Begin x=x+0.01; y= a*x*x+b*x; Writeln(‘x=’,x ,y); End
  • 43.
    5.2) Referencing ofarray elements  If care is not exercised while programming redundant computations can creep into array processing.  Observe following example in that example to find the maximum number and its position in an array of number. Version - 1 p=0; For(i=0; i<n; i++) { if(a[i] >a[p]) { p=i; } } Max = a[p]; Version - 2 p=0; i=0; max=a[i]; For(i=0; i<n; i++) { if(a[i] >max) { max=a[i]; p=I; } }
  • 44.
     As abovewe see that the version 2 is less time consuming loop because of simple variable is there so its does not check all position of array in condition.  So version 2 implementation is preferable because of condition check a[i] >max which is much more efficient to perform then the corresponding test in the version 1 of the program