SlideShare a Scribd company logo
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

More Related Content

What's hot

Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsEhtisham Ali
 
Topic 1.4: Randomized Algorithms
Topic 1.4: Randomized AlgorithmsTopic 1.4: Randomized Algorithms
Topic 1.4: Randomized AlgorithmsKM Bappi
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesFellowBuddy.com
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)jehan1987
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysisSoujanya V
 
I.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AII.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AIvikas dhakane
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programminghodcsencet
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.mohanrathod18
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithmsguest084d20
 
Introduction to Garbage Collection
Introduction to Garbage CollectionIntroduction to Garbage Collection
Introduction to Garbage CollectionArtur Mkrtchyan
 

What's hot (20)

Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
 
Formal Logic in AI
Formal Logic in AIFormal Logic in AI
Formal Logic in AI
 
AI: Logic in AI
AI: Logic in AIAI: Logic in AI
AI: Logic in AI
 
Deadlock
DeadlockDeadlock
Deadlock
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Topic 1.4: Randomized Algorithms
Topic 1.4: Randomized AlgorithmsTopic 1.4: Randomized Algorithms
Topic 1.4: Randomized Algorithms
 
Timestamp protocols
Timestamp protocolsTimestamp protocols
Timestamp protocols
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
P vs NP
P vs NP P vs NP
P vs NP
 
Design & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture NotesDesign & Analysis of Algorithms Lecture Notes
Design & Analysis of Algorithms Lecture Notes
 
Algorithm analysis (All in one)
Algorithm analysis (All in one)Algorithm analysis (All in one)
Algorithm analysis (All in one)
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Asymptotic analysis
Asymptotic analysisAsymptotic analysis
Asymptotic analysis
 
Daa notes 2
Daa notes 2Daa notes 2
Daa notes 2
 
I.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AII.BEST FIRST SEARCH IN AI
I.BEST FIRST SEARCH IN AI
 
unit-4-dynamic programming
unit-4-dynamic programmingunit-4-dynamic programming
unit-4-dynamic programming
 
Fundamental of Algorithms
Fundamental of Algorithms Fundamental of Algorithms
Fundamental of Algorithms
 
Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.Mathematical Analysis of Recursive Algorithm.
Mathematical Analysis of Recursive Algorithm.
 
Parallel algorithms
Parallel algorithmsParallel algorithms
Parallel algorithms
 
Introduction to Garbage Collection
Introduction to Garbage CollectionIntroduction to Garbage Collection
Introduction to Garbage Collection
 

Similar to Daa unit 1

2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdfishan743441
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdfGOWTHAMR721887
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 introchidabdu
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptxShimoFcis
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptISHANAMRITSRIVASTAVA
 
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...Naoki Shibata
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Techglyphs
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptxssuser586772
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptBhargaviDalal4
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introductionLow Ying Hao
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codehamza javed
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
Algorithm types performance steps working
Algorithm types performance steps workingAlgorithm types performance steps working
Algorithm types performance steps workingSaurabh846965
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesNirmalavenkatachalam
 

Similar to Daa unit 1 (20)

UNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.pptUNIT-2-PPTS-DAA.ppt
UNIT-2-PPTS-DAA.ppt
 
DATA STRUCTURE.pdf
DATA STRUCTURE.pdfDATA STRUCTURE.pdf
DATA STRUCTURE.pdf
 
DATA STRUCTURE
DATA STRUCTUREDATA STRUCTURE
DATA STRUCTURE
 
2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf2-Algorithms and Complexit data structurey.pdf
2-Algorithms and Complexit data structurey.pdf
 
19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf19IS402_LP1_LM_22-23.pdf
19IS402_LP1_LM_22-23.pdf
 
Sienna 1 intro
Sienna 1 introSienna 1 intro
Sienna 1 intro
 
lab-8 (1).pptx
lab-8 (1).pptxlab-8 (1).pptx
lab-8 (1).pptx
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
(Slides) Efficient Evaluation Methods of Elementary Functions Suitable for SI...
 
Daa chapter 1
Daa chapter 1Daa chapter 1
Daa chapter 1
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1Bt0080 fundamentals of algorithms1
Bt0080 fundamentals of algorithms1
 
01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx01 Introduction to analysis of Algorithms.pptx
01 Introduction to analysis of Algorithms.pptx
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Introduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.pptIntroduction to Design Algorithm And Analysis.ppt
Introduction to Design Algorithm And Analysis.ppt
 
Kk20503 1 introduction
Kk20503 1 introductionKk20503 1 introduction
Kk20503 1 introduction
 
Algorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo codeAlgorithm and flowchart with pseudo code
Algorithm and flowchart with pseudo code
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Algorithm types performance steps working
Algorithm types performance steps workingAlgorithm types performance steps working
Algorithm types performance steps working
 
Divide and Conquer / Greedy Techniques
Divide and Conquer / Greedy TechniquesDivide and Conquer / Greedy Techniques
Divide and Conquer / Greedy Techniques
 

Recently uploaded

Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Abhinav Gaur Kaptaan
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...Denish Jangid
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTechSoup
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxricssacare
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxjmorse8
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfVivekanand Anglo Vedic Academy
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesTechSoup
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleCeline George
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya - UEM Kolkata Quiz Club
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxakshayaramakrishnan21
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxShibin Azad
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxbennyroshan06
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportAvinash Rai
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...sanghavirahi2
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsCol Mukteshwar Prasad
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345beazzy04
 

Recently uploaded (20)

Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
Research Methods in Psychology | Cambridge AS Level | Cambridge Assessment In...
 
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...Basic Civil Engineering Notes of Chapter-6,  Topic- Ecosystem, Biodiversity G...
Basic Civil Engineering Notes of Chapter-6, Topic- Ecosystem, Biodiversity G...
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptxJose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
Jose-Rizal-and-Philippine-Nationalism-National-Symbol-2.pptx
 
Morse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptxMorse OER Some Benefits and Challenges.pptx
Morse OER Some Benefits and Challenges.pptx
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Keeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security ServicesKeeping Your Information Safe with Centralized Security Services
Keeping Your Information Safe with Centralized Security Services
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General QuizPragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
Pragya Champions Chalice 2024 Prelims & Finals Q/A set, General Quiz
 
Salient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptxSalient features of Environment protection Act 1986.pptx
Salient features of Environment protection Act 1986.pptx
 
Gyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptxGyanartha SciBizTech Quiz slideshare.pptx
Gyanartha SciBizTech Quiz slideshare.pptx
 
B.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdfB.ed spl. HI pdusu exam paper-2023-24.pdf
B.ed spl. HI pdusu exam paper-2023-24.pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
Industrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training ReportIndustrial Training Report- AKTU Industrial Training Report
Industrial Training Report- AKTU Industrial Training Report
 
The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...The impact of social media on mental health and well-being has been a topic o...
The impact of social media on mental health and well-being has been a topic o...
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 

Daa unit 1

  • 1. Design And Analysis of Algorithms
  • 2. 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
  • 3. 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
  • 4. Lets Understand withsimple diagram problem algorithm compute r input OutPut
  • 5. 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.
  • 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  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
  • 8. Development of a model  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 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
  • 11. 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; }
  • 12. 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(); }
  • 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 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.
  • 16. 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.
  • 17. 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)‹-
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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)
  • 22. 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
  • 23. 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
  • 24. 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.
  • 25. 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.
  • 26. 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
  • 27. 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.
  • 28. 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.
  • 29. 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.
  • 30. 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.
  • 31. 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.
  • 32. 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
  • 33. 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.
  • 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 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
  • 39. 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
  • 40. 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
  • 41. 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
  • 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 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; } }
  • 44.  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