SlideShare une entreprise Scribd logo
1  sur  28
DESIGN AND ANALYSIS OF
ALGORITHMS
UNIT-I
ABHIMANYU MISHRA
ASSISTANT PROF.(CSE)
JETGI
6/10/2017 1Abhimanyu Mishra(CSE) JETGI
 Notion of an Algorithm
 Fundamentals of Algorithmic Problem Solving
 Important Problem Types
 Fundamentals of the Analysis of Algorithm Efficiency
 Analysis Framework
 Asymptotic Notations and its properties
 Mathematical analysis for Recursive algorithms
 Mathematical analysis for Non-recursive algorithms
CONTENTS
6/10/2017 2Abhimanyu Mishra(CSE) JETGI
NOTION OF 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.
Introduction To Algorithm:
6/10/2017 3Abhimanyu Mishra(CSE) JETGI
Problem to be
solved
Algorithm
Output
Computer
Program
Input
Characteristics of an algorithm:
 Input: Zero / more quantities are externally supplied.
 Output: At least one quantity is produced.
 Definiteness: Each instruction is clear and unambiguous.
 Finiteness: If the instructions of an algorithm is traced then
for all cases the algorithm must terminates after a finite
number of steps.
 Efficiency: Every instruction must be very basic and runs in
short time.
6/10/2017 4Abhimanyu Mishra(CSE) JETGI
An algorithm is a procedure. It has two parts; the first part is head and the second part is
body.
 The Head section consists of keyword Algorithm and Name of the algorithm with
parameter list. E.g. Algorithm name1(p1, p2,…,p3). The head section also has the
following:
//Problem Description
//Input:
//Output:
In the body of an algorithm various programming constructs like if, for, while and some
statements like assignments are used.
The compound statements may be enclosed with { and } brackets. if, for, while can be
closed by endif, endfor, endwhile respectively. Proper indention is must for block.
The identifier should begin by a letter and not by digit. It contains alpha numeric letters
after first letter. No need to mention data types.
Input and Output can be done using read and write.
Steps for writing an algorithm:
6/10/2017 5Abhimanyu Mishra(CSE) JETGI
A sequence of steps involved in designing and analyzing an algorithm is shown
in the figure
FUNDAMENTALS OF ALGORIHTMIC PROBLEM SOLVING
6/10/2017 6Abhimanyu Mishra(CSE) JETGI
Understand the Problem
Decide on: computational means ,
algorithm design technique
Design An algorithm
Prove corectness
Analyze the Algorithm
Code the algorithm
(i) Understanding the Problem
This is the first step in designing of algorithm.
Identify the problem types and use existing algorithm to find solution.
 Input (instance) to the problem and range of the input get fixed.
(ii) Decision making
The Decision making is done on the following:
(a) Ascertaining the Capabilities of the Computational Device
Algorithms designed to be executed on RAM machines are called sequential algorithms.
In some newer computers, operations are executed concurrently, i.e., in parallel. Algorithms that take
advantage of this capability are called parallel algorithms.
Choice of computational devices like Processor and memory is mainly based on space and time efficiency.
(b) Choosing between Exact and Approximate Problem Solving
An algorithm used to solve the problem exactly and produce correct result is called an exact algorithm.
If the problem is so complex and not able to get exact solution, then we have to choose an algorithm called an
approximation algorithm. i.e., produces an approximate answer. E.g., extracting square roots, solving nonlinear
equations, andevaluating definite integrals.
6/10/2017 7Abhimanyu Mishra(CSE) JETGI
(c) Algorithm Design Techniques
 An algorithm design technique (or “strategy” or “paradigm”) is a general approach
to solving problems algorithmically that is applicable to a variety of problems from different areas of
computing.
 Algorithms+ Data Structures = Programs
(iii) Methods of Specifying an Algorithm
There are three ways to specify an algorithm. They are:
a. Natural language:
b. Pseudocode:
c. Flowchart:
6/10/2017 8Abhimanyu Mishra(CSE) JETGI
Algorithmic Specifications
FlowchartPseudocodeNatural Language
6/10/2017 9Abhimanyu Mishra(CSE) JETGI
a. Natural Language
It is very simple and easy to specify an algorithm using natural language. But many times
specification of algorithm by using natural language is not clear and thereby we get brief
specification.
Example: An algorithm to perform addition of two numbers.
Step 1: Read the first number, say a.
Step 2: Read the first number, say b.
Step 3: Add the above two numbers and store the result in c.
Step 4: Display the result from c.
Such a specification creates difficulty while actually implementing it. Hence many programmers
prefer to have specification of algorithm by means of Pseudocode.
b. Pseudocode
Pseudocode is a mixture of a natural language and programming language constructs. Pseudocode is
usually more precise than natural language.
For Assignment operation left arrow “←”, for comments two slashes “//”,if condition, for,while loops
are used.
ALGORITHM Sum(a,b)
//Problem Description: This algorithm performs addition of two numbers
//Input: Two integers a and b
//Output: Addition of two integers
c←a+b
return c
c. Flowchart
Flowchart is a graphical representation of an algorithm. It is a a method of
expressing an algorithm by a collection of connected geometric shapes
containing descriptions of the algorithm’s step
6/10/2017 10Abhimanyu Mishra(CSE) JETGI
(iv) Proving an Algorithm’s Correctness
 Once an algorithm has been specified then its correctness must be proved.
 An algorithm must yields a required result for every legitimate input in a finite
amount of time.
(v) Analyzing an Algorithm
 For an algorithm the most important is efficiency. In fact, there are two kinds of
algorithm efficiency. They are:
 Time efficiency, indicating how fast the algorithm runs, and
 Space efficiency, indicating how much extra memory it uses.
 The efficiency of an algorithm is determined by measuring both time efficiency
and space efficiency.
So factors to analyze an algorithm are:
 Time efficiency of an algorithm
 Space efficiency of an algorithm
 Simplicity of an algorithm
 Generality of an algorithm
6/10/2017 11Abhimanyu Mishra(CSE) JETGI
6/10/2017 12Abhimanyu Mishra(CSE) JETGI
(vi) Coding an Algorithm
 The coding / implementation of an algorithm is done by a suitable programming
language like C, C++, JAVA.
 It is very essential to write an optimized code (efficient code) to reduce the burden of
compiler.
FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM EFFICIENCY:
The efficiency of an algorithm can be in terms of time and space. The algorithm
efficiency can be analyzed by the following ways:-
a. Analysis Framework.
b. Asymptotic Notations and its properties.
c. Mathematical analysis for Recursive algorithms.
d. Mathematical analysis for Non-recursive algorithms.
 Analysis Framework
There are two kinds of efficiencies to analyze the efficiency of any algorithm. They
are:
• Time efficiency, indicating how fast the algorithm runs, and
• Space efficiency, indicating how much extra memory it uses.
The algorithm analysis framework consists of the following:
• Measuring an Input’s Size
• Units for Measuring Running Time
• Orders of Growth
• Worst-Case, Best-Case, and Average-Case Efficiencies
6/10/2017 13Abhimanyu Mishra(CSE) JETGI
Orders of Growth
 A difference in running times on small inputs is not what really distinguishes efficient
 algorithms from inefficient ones.
 For example, the greatest common divisor of two small numbers, it is not immediately clear
how much more efficient Euclid’s algorithm is compared to the other algorithms, the
difference in algorithm efficiencies becomes clear for larger numbers only.
Worst-Case, Best-Case, and Average-Case Efficiencies
Consider Sequential Search algorithm some search key K
ALGORITHM SequentialSearch(A[0..n - 1], K)
//Searches for a given value in a given array by sequential search
//Input: An array A[0..n - 1] and a search key K
//Output: The index of the first element in A that matches K or -1 if there are no
// matching elements
i ←0
while i < n and A[i] ≠ K do
i ←i + 1
if i < n return i
else return -1
Clearly, the running time of this algorithm can be quite different for the same list size n.
6/10/2017 14Abhimanyu Mishra(CSE) JETGI
Worst-case efficiency:-
• The worst-case efficiency of an algorithm is its efficiency for the worst case
input of size n.
• The algorithm runs the longest among all possible inputs of that size.
• For the input of size n, the running time is Cworst(n) = n.
Best case efficiency
• The best-case efficiency of an algorithm is its efficiency for the best case
input of size n.
• The algorithm runs the fastest among all possible inputs of that size n.
• In sequential search, If we search a first element in list of size n. (i.e. first
element equal to a search key), then the running time is
Cbest(n) = 1.
Average case efficiency
• The Average case efficiency lies between best case and worst case.
• To analyze the algorithm’s average case efficiency, we must make some
assumptions aboutpossible inputs of size n.
6/10/2017 15Abhimanyu Mishra(CSE) JETGI
6/10/2017 16Abhimanyu Mishra(CSE) JETGI
The standard assumptions are that
• The probability of a successful search is equal to p (0 ≤ p ≤ 1) and
• The probability of the first match occurring in the ith position of the list is the
same for every i.
6/10/2017 17Abhimanyu Mishra(CSE) JETGI
ASYMPTOTIC NOTATIONS AND ITS PROPERTIES
Asymptotic notation is a notation, which is used to take meaningful statement about the
efficiency of a program.
The efficiency analysis framework concentrates on the order of growth of an
algorithm’s basic operation count as the principal indicator of the algorithm’s efficiency.
To compare and rank such orders of growth, computer scientists use three notations,
they
are:
 O - Big oh notation
 Ω - Big omega notation
 Θ - Big theta notation
Let t(n) and g(n) can be any nonnegative functions defined on the set of natural
numbers.
The algorithm’s running time t(n) usually indicated by its basic operation count C(n),
and g(n),some simple function to compare with the count.
6/10/2017 Abhimanyu Mishra(CSE) JETGI 18
(i) O - Big oh notation
A function t(n) is said to be in O(g(n)), denoted t (n) ∈ O(g(n)),if t (n) is bounded above
by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c
and some non-negative integer n0 such that
t(n) ≤ c g(n) for all n ≥ n0
Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers.
O = Asymptotic upper bound = Useful for worst case analysis = Loose bound
Big-oh notation: t(n) ∈O(g(n))
6/10/2017 19Abhimanyu Mishra(CSE) JETGI
(ii) Ω - Big omega notation
A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is bounded
below by some positive constant multiple of g(n) for all large n, i.e., if there exist
some positive constant c and some nonnegative integer n0 such that
t (n) ≥ cg(n) for all n ≥ n0.
Where t(n) and g(n) are nonnegative functions defined on the set of natural
numbers.
Ω = Asymptotic lower bound = Useful for best case analysis = Loose bound
Big-omega notation: t (n) ∈ Ω (g(n)).
6/10/2017 Abhimanyu Mishra(CSE) JETGI 20
(iii) Θ - Big theta notation
A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)), if t(n) is bounded both
above and below by some positive constant multiples of g(n) for all large n, i.e., if there
exist some positive constants c1 and c2 and some nonnegative integer n0 such that
c2g(n) ≤ t (n) ≤ c1g(n) for all n ≥ n0.
Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers.
Θ = Asymptotic tight bound = Useful for average case analysis
Big-theta notation: t (n) ∈ Θ(g(n))
6/10/2017 21Abhimanyu Mishra(CSE) JETGI
General Plan for Analyzing the Time Efficiency of Recursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation.
3. Check whether the number of times the basic operation is executed can vary on
different inputs of the same size; if it can, the worst-case, average-case, and best-
case efficiencies must be investigated separately.
4. Set up a recurrence relation, with an appropriate initial condition, for the number of
times the basic operation is executed.
5. Solve the recurrence or, at least, ascertain the order of growth of its solution.
MATHEMATICAL ANALYSIS FOR RECURSIVE ALGORITHMS
Recurrence relations:
The reccurence equation is an eequation that defines a sequence recursively.It normally belongs
in the form:-
T(n)=T(n-1)+n for all n>0
Methods For Solving Recurrence Relation:
 Substitution method
 Recurrence Tree Method
 Master’s Theorem
6/10/2017 22Abhimanyu Mishra(CSE) JETGI
MATHEMATICAL ANALYSIS FOR NON-RECURSIVE ALGORITHMS
General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms
1. Decide on a parameter (or parameters) indicating an input’s size.
2. Identify the algorithm’s basic operation (in the innermost loop).
3. Check whether the number of times the basic operation is executed depends only
on the size of an input. If it also depends on some additional property, the worst-case,
average-case,and, if necessary, best-case efficiencies have to be investigated
separately.
4. Set up a sum expressing the number of times the algorithm’s basic operation is
executed.
5. Using standard formulas and rules of sum manipulation either find a closed form
formula for the count or at the least, establish its order of growth.
6/10/2017 Abhimanyu Mishra(CSE) JETGI 23
Example For Substitution Method
We make a guess for the solution and then we use mathematical induction to prove the the
guess is correct or incorrect.
For example:
consider the recurrence T(n) = 2T(n/2) + n
We guess the solution as T(n) = O(nLogn).
Now we use induction to prove our guess.
We need to prove that T(n) <= cnLogn.
We can assume that it is true for values smaller than n.
T(n) = 2T(n/2) + n
<= cn/2Log(n/2) + n
<= cnLogn - cnLog2 + n
= cnLogn - cn + n
<= cnLogn
6/10/2017 Abhimanyu Mishra(CSE) JETGI 24
Example For Recurrence Tree Method:
In this method, we draw a recurrence tree and calculate the time taken by every level of
tree. Finally, we sum the work done at all levels. To draw the recurrence tree, we start from
the given recurrence and keep drawing till we find a pattern among levels. The pattern is
typically a arithmetic or geometric series.
For example consider the recurrence relation T(n) = T(n/4) + T(n/2) + cn2
cn2
T(n/4) T(n/2)
If we further break down the expression T(n/4) and T(n/2), we get following recursion
tree.
cn2
c(n2)/16 c(n2)/4
T(n/16) T(n/8) T(n/8) T(n/4)
6/10/2017 Abhimanyu Mishra(CSE) JETGI 25
Breaking down further gives us following
cn2
c(n2)/16 c(n2)/4
c(n2)/256 c(n2)/64 c(n2)/64 c(n2)/16
To know the value of T(n), we need to calculate sum of tree nodes level by level.
If we sum the above tree level by level, we get the following series
T(n) = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + ....
The above series is geometrical progression with ratio 5/16. To get an upper
bound, we can sum the infinite series.
We get the sum as (n2)/(1 - 5/16) which is O(n2)
6/10/2017 Abhimanyu Mishra(CSE) JETGI 26
Example For Master Method:
The master method works only for following type of recurrences or for recurrences that can
be transformed to following type.
T(n) = aT(n/b) + f(n) where a >= 1 and b > 1
There are following three cases:
1. If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLog
b
a)
2. If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLog n)
3.If f(n) = Θ(nc) where c > Logba then T(n) = Θ(f(n))
How does this work?
Master method is mainly derived from recurrence tree method. If we draw recurrence tree of
T(n) = aT(n/b) + f(n), we can see that the work done at root is f(n) and work done at all leaves
is Θ(nc) where c is Logba. And the height of recurrence tree is Logbn
(Contd….)
6/10/2017 Abhimanyu Mishra(CSE) JETGI 27
In recurrence tree method, we calculate total work done. If the work done at leaves is polynomially more,
then leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done
at leaves and root is asymptotically same, then our result becomes height multiplied by work done at any
level (Case 2). If work done at root is asymptotically more, then our result becomes work done at root
(Case 3).
6/10/2017 Abhimanyu Mishra(CSE) JETGI 28
Notes:
1) It is not necessary that a recurrence of the form T(n) = aT(n/b) + f(n) can be solved
using Master Theorem. The given three cases have some gaps between them. For
example, the recurrence T(n) = 2T(n/2) + n/Logn cannot be solved using master method.
2) Case 2 can be extended for f(n) = Θ(ncLogkn)
If f(n) = Θ(ncLogkn) for some constant k >= 0 and c = Logba, then T(n) = Θ(ncLogk+1n)
Examples of some standard algorithms whose time complexity can be evaluated using
Master Method
Merge Sort: T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and Logba] is also 1. So the
solution is Θ(n Logn)
Binary Search: T(n) = T(n/2) + Θ(1). It also falls in case 2 as c is 0 and Logba is also 0. So
the solution is Θ(Logn)
All the best - There is no substitute for hard work

Contenu connexe

Tendances

Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithmsDr Geetha Mohan
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencySaranya Natarajan
 
I. Mini-Max Algorithm in AI
I. Mini-Max Algorithm in AII. Mini-Max Algorithm in AI
I. Mini-Max Algorithm in AIvikas dhakane
 
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with DeadlinesYashiUpadhyay3
 
Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solvingPrabhakaran V M
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplicationKumar
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.mohanrathod18
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxSyed Zaid Irshad
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventionssaranyatdr
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 

Tendances (20)

Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Problems, Problem spaces and Search
Problems, Problem spaces and SearchProblems, Problem spaces and Search
Problems, Problem spaces and Search
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
Backtracking
Backtracking  Backtracking
Backtracking
 
Design and analysis of algorithms
Design and analysis of algorithmsDesign and analysis of algorithms
Design and analysis of algorithms
 
Evolutionary Computing
Evolutionary ComputingEvolutionary Computing
Evolutionary Computing
 
Fundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm EfficiencyFundamentals of the Analysis of Algorithm Efficiency
Fundamentals of the Analysis of Algorithm Efficiency
 
I. Mini-Max Algorithm in AI
I. Mini-Max Algorithm in AII. Mini-Max Algorithm in AI
I. Mini-Max Algorithm in AI
 
Unit 1 chapter 1 Design and Analysis of Algorithms
Unit 1   chapter 1 Design and Analysis of AlgorithmsUnit 1   chapter 1 Design and Analysis of Algorithms
Unit 1 chapter 1 Design and Analysis of Algorithms
 
Job sequencing with Deadlines
Job sequencing with DeadlinesJob sequencing with Deadlines
Job sequencing with Deadlines
 
Algorithmic problem solving
Algorithmic problem solvingAlgorithmic problem solving
Algorithmic problem solving
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.Mathematical Analysis of Non-Recursive Algorithm.
Mathematical Analysis of Non-Recursive Algorithm.
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 

Similaire à Daa unit 1

Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01Prashanth Shivakumar
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfNayanChandak1
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptxSayanSen36
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdfMemMem25
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptxwondmhunegn
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxesuEthopi
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptxrajesshs31r
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxrajesshs31r
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptxjinkhatima
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithmlilyMalar1
 
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
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of AlgorithmsBulbul Agrawal
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - IntroductionMadhu Bala
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdfpasinduneshan
 

Similaire à Daa unit 1 (20)

chapter 1
chapter 1chapter 1
chapter 1
 
Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01Data Structures and Algorithms Unit 01
Data Structures and Algorithms Unit 01
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Unit ii algorithm
Unit   ii algorithmUnit   ii algorithm
Unit ii algorithm
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
DA lecture 3.pptx
DA lecture 3.pptxDA lecture 3.pptx
DA lecture 3.pptx
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Algorithm Analysis.pdf
Algorithm Analysis.pdfAlgorithm Analysis.pdf
Algorithm Analysis.pdf
 
Chapter 1 Data structure.pptx
Chapter 1 Data structure.pptxChapter 1 Data structure.pptx
Chapter 1 Data structure.pptx
 
Cs2251 daa
Cs2251 daaCs2251 daa
Cs2251 daa
 
Introduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptxIntroduction to Data Structure and algorithm.pptx
Introduction to Data Structure and algorithm.pptx
 
Analysis of Algorithm full version 2024.pptx
Analysis of Algorithm  full version  2024.pptxAnalysis of Algorithm  full version  2024.pptx
Analysis of Algorithm full version 2024.pptx
 
Design Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptxDesign Analysis of Alogorithm 1 ppt 2024.pptx
Design Analysis of Alogorithm 1 ppt 2024.pptx
 
Unit 1, ADA.pptx
Unit 1, ADA.pptxUnit 1, ADA.pptx
Unit 1, ADA.pptx
 
What is algorithm
What is algorithmWhat is algorithm
What is algorithm
 
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
 
Analysis and Design of Algorithms
Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis and Design of Algorithms
 
Algorithm Design and Analysis
Algorithm Design and AnalysisAlgorithm Design and Analysis
Algorithm Design and Analysis
 
Algorithm - Introduction
Algorithm - IntroductionAlgorithm - Introduction
Algorithm - Introduction
 
complexity analysis.pdf
complexity analysis.pdfcomplexity analysis.pdf
complexity analysis.pdf
 

Plus de Abhimanyu Mishra

Plus de Abhimanyu Mishra (20)

Cd unit i
Cd unit iCd unit i
Cd unit i
 
Presentation1(JIT gnomio)
Presentation1(JIT gnomio)Presentation1(JIT gnomio)
Presentation1(JIT gnomio)
 
Sta unit 5(abimanyu)
Sta unit 5(abimanyu)Sta unit 5(abimanyu)
Sta unit 5(abimanyu)
 
Sta unit 3(abimanyu)
Sta unit 3(abimanyu)Sta unit 3(abimanyu)
Sta unit 3(abimanyu)
 
Sta unit 4(abimanyu)
Sta unit 4(abimanyu)Sta unit 4(abimanyu)
Sta unit 4(abimanyu)
 
Sta unit 3(abimanyu)
Sta unit 3(abimanyu)Sta unit 3(abimanyu)
Sta unit 3(abimanyu)
 
Sta unit 2(abimanyu)
Sta unit 2(abimanyu)Sta unit 2(abimanyu)
Sta unit 2(abimanyu)
 
Unit1
Unit1Unit1
Unit1
 
Daa unit 4
Daa unit 4Daa unit 4
Daa unit 4
 
Daa unit 3
Daa unit 3Daa unit 3
Daa unit 3
 
Daa unit 2
Daa unit 2Daa unit 2
Daa unit 2
 
Software Engineering unit 5
Software Engineering unit 5Software Engineering unit 5
Software Engineering unit 5
 
Software Engineering unit 4
Software Engineering unit 4Software Engineering unit 4
Software Engineering unit 4
 
Software Engineering unit 3
Software Engineering unit 3Software Engineering unit 3
Software Engineering unit 3
 
Software Engineering unit 2
Software Engineering unit 2Software Engineering unit 2
Software Engineering unit 2
 
Software Engineering Unit 1
Software Engineering Unit 1Software Engineering Unit 1
Software Engineering Unit 1
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5
 
Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2Theory of Automata and formal languages unit 2
Theory of Automata and formal languages unit 2
 

Dernier

TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catcherssdickerson1
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - GuideGOPINATHS437943
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction managementMariconPadriquez1
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringJuanCarlosMorales19600
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEroselinkalist12
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptJasonTagapanGulla
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 

Dernier (20)

TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor CatchersTechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
TechTAC® CFD Report Summary: A Comparison of Two Types of Tubing Anchor Catchers
 
Transport layer issues and challenges - Guide
Transport layer issues and challenges - GuideTransport layer issues and challenges - Guide
Transport layer issues and challenges - Guide
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
computer application and construction management
computer application and construction managementcomputer application and construction management
computer application and construction management
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Piping Basic stress analysis by engineering
Piping Basic stress analysis by engineeringPiping Basic stress analysis by engineering
Piping Basic stress analysis by engineering
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETEINFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
INFLUENCE OF NANOSILICA ON THE PROPERTIES OF CONCRETE
 
Solving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.pptSolving The Right Triangles PowerPoint 2.ppt
Solving The Right Triangles PowerPoint 2.ppt
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 

Daa unit 1

  • 1. DESIGN AND ANALYSIS OF ALGORITHMS UNIT-I ABHIMANYU MISHRA ASSISTANT PROF.(CSE) JETGI 6/10/2017 1Abhimanyu Mishra(CSE) JETGI
  • 2.  Notion of an Algorithm  Fundamentals of Algorithmic Problem Solving  Important Problem Types  Fundamentals of the Analysis of Algorithm Efficiency  Analysis Framework  Asymptotic Notations and its properties  Mathematical analysis for Recursive algorithms  Mathematical analysis for Non-recursive algorithms CONTENTS 6/10/2017 2Abhimanyu Mishra(CSE) JETGI
  • 3. NOTION OF 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. Introduction To Algorithm: 6/10/2017 3Abhimanyu Mishra(CSE) JETGI Problem to be solved Algorithm Output Computer Program Input
  • 4. Characteristics of an algorithm:  Input: Zero / more quantities are externally supplied.  Output: At least one quantity is produced.  Definiteness: Each instruction is clear and unambiguous.  Finiteness: If the instructions of an algorithm is traced then for all cases the algorithm must terminates after a finite number of steps.  Efficiency: Every instruction must be very basic and runs in short time. 6/10/2017 4Abhimanyu Mishra(CSE) JETGI
  • 5. An algorithm is a procedure. It has two parts; the first part is head and the second part is body.  The Head section consists of keyword Algorithm and Name of the algorithm with parameter list. E.g. Algorithm name1(p1, p2,…,p3). The head section also has the following: //Problem Description //Input: //Output: In the body of an algorithm various programming constructs like if, for, while and some statements like assignments are used. The compound statements may be enclosed with { and } brackets. if, for, while can be closed by endif, endfor, endwhile respectively. Proper indention is must for block. The identifier should begin by a letter and not by digit. It contains alpha numeric letters after first letter. No need to mention data types. Input and Output can be done using read and write. Steps for writing an algorithm: 6/10/2017 5Abhimanyu Mishra(CSE) JETGI
  • 6. A sequence of steps involved in designing and analyzing an algorithm is shown in the figure FUNDAMENTALS OF ALGORIHTMIC PROBLEM SOLVING 6/10/2017 6Abhimanyu Mishra(CSE) JETGI Understand the Problem Decide on: computational means , algorithm design technique Design An algorithm Prove corectness Analyze the Algorithm Code the algorithm
  • 7. (i) Understanding the Problem This is the first step in designing of algorithm. Identify the problem types and use existing algorithm to find solution.  Input (instance) to the problem and range of the input get fixed. (ii) Decision making The Decision making is done on the following: (a) Ascertaining the Capabilities of the Computational Device Algorithms designed to be executed on RAM machines are called sequential algorithms. In some newer computers, operations are executed concurrently, i.e., in parallel. Algorithms that take advantage of this capability are called parallel algorithms. Choice of computational devices like Processor and memory is mainly based on space and time efficiency. (b) Choosing between Exact and Approximate Problem Solving An algorithm used to solve the problem exactly and produce correct result is called an exact algorithm. If the problem is so complex and not able to get exact solution, then we have to choose an algorithm called an approximation algorithm. i.e., produces an approximate answer. E.g., extracting square roots, solving nonlinear equations, andevaluating definite integrals. 6/10/2017 7Abhimanyu Mishra(CSE) JETGI
  • 8. (c) Algorithm Design Techniques  An algorithm design technique (or “strategy” or “paradigm”) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing.  Algorithms+ Data Structures = Programs (iii) Methods of Specifying an Algorithm There are three ways to specify an algorithm. They are: a. Natural language: b. Pseudocode: c. Flowchart: 6/10/2017 8Abhimanyu Mishra(CSE) JETGI Algorithmic Specifications FlowchartPseudocodeNatural Language
  • 9. 6/10/2017 9Abhimanyu Mishra(CSE) JETGI a. Natural Language It is very simple and easy to specify an algorithm using natural language. But many times specification of algorithm by using natural language is not clear and thereby we get brief specification. Example: An algorithm to perform addition of two numbers. Step 1: Read the first number, say a. Step 2: Read the first number, say b. Step 3: Add the above two numbers and store the result in c. Step 4: Display the result from c. Such a specification creates difficulty while actually implementing it. Hence many programmers prefer to have specification of algorithm by means of Pseudocode. b. Pseudocode Pseudocode is a mixture of a natural language and programming language constructs. Pseudocode is usually more precise than natural language. For Assignment operation left arrow “←”, for comments two slashes “//”,if condition, for,while loops are used. ALGORITHM Sum(a,b) //Problem Description: This algorithm performs addition of two numbers //Input: Two integers a and b //Output: Addition of two integers c←a+b return c
  • 10. c. Flowchart Flowchart is a graphical representation of an algorithm. It is a a method of expressing an algorithm by a collection of connected geometric shapes containing descriptions of the algorithm’s step 6/10/2017 10Abhimanyu Mishra(CSE) JETGI
  • 11. (iv) Proving an Algorithm’s Correctness  Once an algorithm has been specified then its correctness must be proved.  An algorithm must yields a required result for every legitimate input in a finite amount of time. (v) Analyzing an Algorithm  For an algorithm the most important is efficiency. In fact, there are two kinds of algorithm efficiency. They are:  Time efficiency, indicating how fast the algorithm runs, and  Space efficiency, indicating how much extra memory it uses.  The efficiency of an algorithm is determined by measuring both time efficiency and space efficiency. So factors to analyze an algorithm are:  Time efficiency of an algorithm  Space efficiency of an algorithm  Simplicity of an algorithm  Generality of an algorithm 6/10/2017 11Abhimanyu Mishra(CSE) JETGI
  • 12. 6/10/2017 12Abhimanyu Mishra(CSE) JETGI (vi) Coding an Algorithm  The coding / implementation of an algorithm is done by a suitable programming language like C, C++, JAVA.  It is very essential to write an optimized code (efficient code) to reduce the burden of compiler.
  • 13. FUNDAMENTALS OF THE ANALYSIS OF ALGORITHM EFFICIENCY: The efficiency of an algorithm can be in terms of time and space. The algorithm efficiency can be analyzed by the following ways:- a. Analysis Framework. b. Asymptotic Notations and its properties. c. Mathematical analysis for Recursive algorithms. d. Mathematical analysis for Non-recursive algorithms.  Analysis Framework There are two kinds of efficiencies to analyze the efficiency of any algorithm. They are: • Time efficiency, indicating how fast the algorithm runs, and • Space efficiency, indicating how much extra memory it uses. The algorithm analysis framework consists of the following: • Measuring an Input’s Size • Units for Measuring Running Time • Orders of Growth • Worst-Case, Best-Case, and Average-Case Efficiencies 6/10/2017 13Abhimanyu Mishra(CSE) JETGI
  • 14. Orders of Growth  A difference in running times on small inputs is not what really distinguishes efficient  algorithms from inefficient ones.  For example, the greatest common divisor of two small numbers, it is not immediately clear how much more efficient Euclid’s algorithm is compared to the other algorithms, the difference in algorithm efficiencies becomes clear for larger numbers only. Worst-Case, Best-Case, and Average-Case Efficiencies Consider Sequential Search algorithm some search key K ALGORITHM SequentialSearch(A[0..n - 1], K) //Searches for a given value in a given array by sequential search //Input: An array A[0..n - 1] and a search key K //Output: The index of the first element in A that matches K or -1 if there are no // matching elements i ←0 while i < n and A[i] ≠ K do i ←i + 1 if i < n return i else return -1 Clearly, the running time of this algorithm can be quite different for the same list size n. 6/10/2017 14Abhimanyu Mishra(CSE) JETGI
  • 15. Worst-case efficiency:- • The worst-case efficiency of an algorithm is its efficiency for the worst case input of size n. • The algorithm runs the longest among all possible inputs of that size. • For the input of size n, the running time is Cworst(n) = n. Best case efficiency • The best-case efficiency of an algorithm is its efficiency for the best case input of size n. • The algorithm runs the fastest among all possible inputs of that size n. • In sequential search, If we search a first element in list of size n. (i.e. first element equal to a search key), then the running time is Cbest(n) = 1. Average case efficiency • The Average case efficiency lies between best case and worst case. • To analyze the algorithm’s average case efficiency, we must make some assumptions aboutpossible inputs of size n. 6/10/2017 15Abhimanyu Mishra(CSE) JETGI
  • 16. 6/10/2017 16Abhimanyu Mishra(CSE) JETGI The standard assumptions are that • The probability of a successful search is equal to p (0 ≤ p ≤ 1) and • The probability of the first match occurring in the ith position of the list is the same for every i.
  • 17. 6/10/2017 17Abhimanyu Mishra(CSE) JETGI ASYMPTOTIC NOTATIONS AND ITS PROPERTIES Asymptotic notation is a notation, which is used to take meaningful statement about the efficiency of a program. The efficiency analysis framework concentrates on the order of growth of an algorithm’s basic operation count as the principal indicator of the algorithm’s efficiency. To compare and rank such orders of growth, computer scientists use three notations, they are:  O - Big oh notation  Ω - Big omega notation  Θ - Big theta notation Let t(n) and g(n) can be any nonnegative functions defined on the set of natural numbers. The algorithm’s running time t(n) usually indicated by its basic operation count C(n), and g(n),some simple function to compare with the count.
  • 18. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 18 (i) O - Big oh notation A function t(n) is said to be in O(g(n)), denoted t (n) ∈ O(g(n)),if t (n) is bounded above by some constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some non-negative integer n0 such that t(n) ≤ c g(n) for all n ≥ n0 Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers. O = Asymptotic upper bound = Useful for worst case analysis = Loose bound Big-oh notation: t(n) ∈O(g(n))
  • 19. 6/10/2017 19Abhimanyu Mishra(CSE) JETGI (ii) Ω - Big omega notation A function t(n) is said to be in Ω(g(n)), denoted t(n) ∈ Ω(g(n)), if t(n) is bounded below by some positive constant multiple of g(n) for all large n, i.e., if there exist some positive constant c and some nonnegative integer n0 such that t (n) ≥ cg(n) for all n ≥ n0. Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers. Ω = Asymptotic lower bound = Useful for best case analysis = Loose bound Big-omega notation: t (n) ∈ Ω (g(n)).
  • 20. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 20 (iii) Θ - Big theta notation A function t(n) is said to be in Θ(g(n)), denoted t(n) ∈ Θ(g(n)), if t(n) is bounded both above and below by some positive constant multiples of g(n) for all large n, i.e., if there exist some positive constants c1 and c2 and some nonnegative integer n0 such that c2g(n) ≤ t (n) ≤ c1g(n) for all n ≥ n0. Where t(n) and g(n) are nonnegative functions defined on the set of natural numbers. Θ = Asymptotic tight bound = Useful for average case analysis Big-theta notation: t (n) ∈ Θ(g(n))
  • 21. 6/10/2017 21Abhimanyu Mishra(CSE) JETGI General Plan for Analyzing the Time Efficiency of Recursive Algorithms 1. Decide on a parameter (or parameters) indicating an input’s size. 2. Identify the algorithm’s basic operation. 3. Check whether the number of times the basic operation is executed can vary on different inputs of the same size; if it can, the worst-case, average-case, and best- case efficiencies must be investigated separately. 4. Set up a recurrence relation, with an appropriate initial condition, for the number of times the basic operation is executed. 5. Solve the recurrence or, at least, ascertain the order of growth of its solution. MATHEMATICAL ANALYSIS FOR RECURSIVE ALGORITHMS Recurrence relations: The reccurence equation is an eequation that defines a sequence recursively.It normally belongs in the form:- T(n)=T(n-1)+n for all n>0 Methods For Solving Recurrence Relation:  Substitution method  Recurrence Tree Method  Master’s Theorem
  • 22. 6/10/2017 22Abhimanyu Mishra(CSE) JETGI MATHEMATICAL ANALYSIS FOR NON-RECURSIVE ALGORITHMS General Plan for Analyzing the Time Efficiency of Nonrecursive Algorithms 1. Decide on a parameter (or parameters) indicating an input’s size. 2. Identify the algorithm’s basic operation (in the innermost loop). 3. Check whether the number of times the basic operation is executed depends only on the size of an input. If it also depends on some additional property, the worst-case, average-case,and, if necessary, best-case efficiencies have to be investigated separately. 4. Set up a sum expressing the number of times the algorithm’s basic operation is executed. 5. Using standard formulas and rules of sum manipulation either find a closed form formula for the count or at the least, establish its order of growth.
  • 23. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 23 Example For Substitution Method We make a guess for the solution and then we use mathematical induction to prove the the guess is correct or incorrect. For example: consider the recurrence T(n) = 2T(n/2) + n We guess the solution as T(n) = O(nLogn). Now we use induction to prove our guess. We need to prove that T(n) <= cnLogn. We can assume that it is true for values smaller than n. T(n) = 2T(n/2) + n <= cn/2Log(n/2) + n <= cnLogn - cnLog2 + n = cnLogn - cn + n <= cnLogn
  • 24. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 24 Example For Recurrence Tree Method: In this method, we draw a recurrence tree and calculate the time taken by every level of tree. Finally, we sum the work done at all levels. To draw the recurrence tree, we start from the given recurrence and keep drawing till we find a pattern among levels. The pattern is typically a arithmetic or geometric series. For example consider the recurrence relation T(n) = T(n/4) + T(n/2) + cn2 cn2 T(n/4) T(n/2) If we further break down the expression T(n/4) and T(n/2), we get following recursion tree. cn2 c(n2)/16 c(n2)/4 T(n/16) T(n/8) T(n/8) T(n/4)
  • 25. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 25 Breaking down further gives us following cn2 c(n2)/16 c(n2)/4 c(n2)/256 c(n2)/64 c(n2)/64 c(n2)/16 To know the value of T(n), we need to calculate sum of tree nodes level by level. If we sum the above tree level by level, we get the following series T(n) = c(n^2 + 5(n^2)/16 + 25(n^2)/256) + .... The above series is geometrical progression with ratio 5/16. To get an upper bound, we can sum the infinite series. We get the sum as (n2)/(1 - 5/16) which is O(n2)
  • 26. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 26 Example For Master Method: The master method works only for following type of recurrences or for recurrences that can be transformed to following type. T(n) = aT(n/b) + f(n) where a >= 1 and b > 1 There are following three cases: 1. If f(n) = Θ(nc) where c < Logba then T(n) = Θ(nLog b a) 2. If f(n) = Θ(nc) where c = Logba then T(n) = Θ(ncLog n) 3.If f(n) = Θ(nc) where c > Logba then T(n) = Θ(f(n)) How does this work? Master method is mainly derived from recurrence tree method. If we draw recurrence tree of T(n) = aT(n/b) + f(n), we can see that the work done at root is f(n) and work done at all leaves is Θ(nc) where c is Logba. And the height of recurrence tree is Logbn (Contd….)
  • 27. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 27 In recurrence tree method, we calculate total work done. If the work done at leaves is polynomially more, then leaves are the dominant part, and our result becomes the work done at leaves (Case 1). If work done at leaves and root is asymptotically same, then our result becomes height multiplied by work done at any level (Case 2). If work done at root is asymptotically more, then our result becomes work done at root (Case 3).
  • 28. 6/10/2017 Abhimanyu Mishra(CSE) JETGI 28 Notes: 1) It is not necessary that a recurrence of the form T(n) = aT(n/b) + f(n) can be solved using Master Theorem. The given three cases have some gaps between them. For example, the recurrence T(n) = 2T(n/2) + n/Logn cannot be solved using master method. 2) Case 2 can be extended for f(n) = Θ(ncLogkn) If f(n) = Θ(ncLogkn) for some constant k >= 0 and c = Logba, then T(n) = Θ(ncLogk+1n) Examples of some standard algorithms whose time complexity can be evaluated using Master Method Merge Sort: T(n) = 2T(n/2) + Θ(n). It falls in case 2 as c is 1 and Logba] is also 1. So the solution is Θ(n Logn) Binary Search: T(n) = T(n/2) + Θ(1). It also falls in case 2 as c is 0 and Logba is also 0. So the solution is Θ(Logn) All the best - There is no substitute for hard work