SlideShare a Scribd company logo
1 of 43
Download to read offline
UNIT-I
Analysis of the Recursive and Iterative Algorithms
by
Dr. N. Subhash Chandra,
Professor, CVRCE.
Source: Web resources and Computer Algorithms by Horowitz, Sahani &
Rajasekaran
7/27/2020 1
ANALYSIS OF RECURSIVE ALGORITHMS
BRUITE FORCE MEHOD
STORE AND REUSE METHOD
ALGORITHM CORRECTNESS
CONTENTS
7/27/2020 2
Difference between Recursive
and Iterative Algorithms
PROPERTY RECURSION ITERATION
Definition Function calls itself.
A set of instructions repeatedly
executed.
Application For functions. For loops.
Termination
Through base case, where there will
be no function call.
When the termination condition for
the iterator ceases to be satisfied.
Usage
Used when code size needs to be
small, and time complexity is not an
issue.
Used when time complexity needs to
be balanced against an expanded
code size.
Code Size Smaller code size Larger Code Size.
Time Complexity
Very high(generally exponential) time
complexity.
Relatively lower time
complexity(generally polynomial-
logarithmic).
7/27/2020 3
ANALYSIS OF RECURSIVE ALGORITHMS
7/27/2020 4
Recursive
Algorithms
Recursive sum
Factorial
Binary search
Tower of Hanoi
Permutation Generator
7/27/2020 5
Example: Recursive Sum
7/27/2020 6
Example: Recursive Sum
7/27/2020 7
Recursive Factorial Algorithm
Form and solve the recurrence relation for the running time
of factorial Algorithm and hence determine its big-O
complexity:
T(0) = c (1)
T(n) = 1 + T(n - 1) (2)
= 1+ 1 + T(n - 2) by substituting T(n – 1) in (2)
= 1 +1 +1+ T(n - 3) by substituting T(n – 2) in (2)
…
= k + T(n - k)
The base case is reached when n – k = 0  k = n, we then have:
T(n) = n + T(n - n)
= n + T(0)
= n + c
Therefore the method factorial is O(n)
Algorithm factorial ( n) {
if (n == 0) then
return 1;
else
return n * factorial (n – 1);
}
7/27/2020 8
Recursive Binary Search
• The recurrence relation for the running time of the method is:
T(1) = a if n = 1 (one element array)
T(n) = T(n / 2) + b if n > 1
Algorithm BinarySearch (target,array, low, high) {
if (low > high)then
return ;
else {
middle = (low + high)/2;
if (array[middle] == target)then
return middle;
else if(array[middle] < target)then
return BinarySearch(target, array, middle + 1, high);
else
return BinarySearch(target, array, low, middle - 1);
}
}
7/27/2020 9
Analysis of Recursive Binary Search
Expanding:
T(1) = a (1)
T(n) = T(n / 2) + b (2)
= [T(n / 22) + b] + b = T (n / 22) + 2b by substituting T(n/2) in (2)
= [T(n / 23) + b] + 2b = T(n / 23) + 3b by substituting T(n/22) in (2)
= ……..
= T( n / 2k) + kb
The base case is reached when n / 2k = 1  n = 2k  k = log2 n, we then
have:
T(n) = T(1) + b log2 n
= a + b log2 n
Therefore, Recursive Binary Search is O(log n)
Without loss of generality, assume n, the problem size, is a multiple of 2, i.e., n = 2k
7/27/2020 10
Recursive Fibonacci number Algorithm
T(n) = c if n = 1 or n = 2 (1)
T(n) = T(n – 1) + T(n – 2) + b if n > 2 (2)
We determine a lower bound on T(n):
Expand : T(n) = T(n - 1) + T(n - 2) + b
≥ T(n - 2) + T(n-2) + b
= 2T(n - 2) + b
= 2[T(n - 3) + T(n - 4) + b] + b by substitute T(n - 2) in (2)
 2[T(n - 4) + T(n - 4) + b] + b
= 22T(n - 4) + 2b + b
= 22[T(n - 5) + T(n - 6) + b] + 2b + b by substitute T(n - 4) in (2)
≥ 23T(n – 6) + (22 + 21 + 20)b
. . .
 2kT(n – 2k) + (2k-1 + 2k-2 + . . . + 21 + 20)b
= 2kT(n – 2k) + (2k – 1)b
The base case is reached when n – 2k = 2  k = (n - 2) / 2
Hence, T(n) ≥ 2 (n – 2) / 2 T(2) + [2 (n - 2) / 2 – 1]b
= (b + c)2 (n – 2) / 2 – b
= [(b + c) / 2]*(2)n/2 – b  Récursive Fibonacci is exponentiel
Algorithm Fibonacci (n) { // Recursively calculates
Fibonacci number
if( n == 1 || n == 2)then
return 1;
else
return Fibonacci(n – 1) + Fibonacci(n – 2);
}
7/27/2020 11
Tower of Hanoi
Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than
one rings is as depicted
Rules
The mission is to move all the disks to some another tower without violating the sequence of
arrangement. A few rules to be followed for Tower of Hanoi are
• Only one disk can be moved among the towers at any given time.
• Only the "top" disk can be removed.
• No large disk can sit over a small disk.
7/27/2020 12
TOH
7/27/2020 13
Algorithm TOH
The steps to follow are −
Step 1 − Move n-1 disks from source (X)to aux(Y)
Step 2 − Move nth disk from source(X) to dest (Z)
Step 3 − Move n-1 disks from aux (Y) to dest (Z)
Algorithm TOH(n, x, y, z)
// Move the top n discs from x to z, where x is source, Y auxiliary tower, Z is destination
{
if(n>=1)then {
TOH(n-1, x, z, y);
write( Move a disc from x to z);
TOH(n-1, y, x, z);
}
}
7/27/2020 14
Tracing 3 Discs with algorithm
7/27/2020 15
Analysis of Recursive Towers of Hanoi Algorithm
Expanding:
T(1) = a (1)
T(n) = 2T(n – 1) + b if n > 1 (2)
= 2[2T(n – 2) + b] + b = 22 T(n – 2) + 2b + b by substituting T(n – 1) in (2)
= 22 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 22b + 2b + b by substituting T(n-2) in (2)
= 23 [2T(n – 4) + b] + 22b + 2b + b = 24 T(n – 4) + 23 b + 22b + 21b + 20b by substituting
T(n – 3) in (2)
= ……
= 2k T(n – k) + b[2k- 1 + 2k– 2 + . . . 21 + 20]
The base case is reached when n – k = 1  k = n – 1, we then have:
Therefore, The method hanoi is O(2n)
7/27/2020 16
Permutation Generator
7/27/2020 17
Permutation Generator
7/27/2020 18
Permutation Generator
7/27/2020 19
Algorithm for Permutation generator
7/27/2020 20
Analysis of Permutation Generator
7/27/2020 21
ANALYSIS OF ITERATIVE ALGORITHMS
7/27/2020 22
7/27/2020 23
7/27/2020 24
7/27/2020 25
7/27/2020 26
7/27/2020 27
7/27/2020 28
7/27/2020 29
7/27/2020 30
7/27/2020 31
• Proving an Algorithm’s Correctness
• Brute Force Approach
• Store and Reuse Method
7/27/2020 32
Proving an Algorithm’s
Correctness
Once an algorithm has been specified then its correctness must be proved. An
algorithm must yield a required result for every legitimate input in a finite
amount of time.
7/27/2020 33
Proving Technique for
Algorithm correctness
A common technique for proving correctness is to use mathematical
induction because an algorithm’s iterations provide a natural sequence of
steps needed for such proofs.
The notion of correctness for approximation algorithms is less
straightforward than it is for exact algorithms. The error produced by the
algorithm should not exceed a pre-defined limit.
7/27/2020 34
Brute Force Approach
• A brute force algorithm is a straightforward approach to solving a problem. It is
usually based on problem statement and definitions
• This approach consider all possible solutions.
• It is based on trial and error where the programmer tries to merely utilize the
computer’s fast processing power to solve a problem.
• It is a naive approach adopted by most beginner programmers in the initial stages of
solving a problem, however it might not be the most optimal approach, as it might
increase both space and time complexity.
Ex: TSP, Convex hull, Knapsack, Graph colouring, Closest distance etc
7/27/2020 35
Example 1
Linear Search: When each element of an array is compared with the
data to be searched, it might be termed as a brute force approach, as it is the
most direct and simple way one could think of searching the given data in the
array.
However, there are better ways to search data which make use of more
optimum algorithms like Binary search, etc. which adopt a more innovative
way to solve the same problem.
7/27/2020 36
Some Examples
7/27/2020 37
Brute Force Solution
Algorithm FindKey()
{
for i:=0 to 9 do {
for j:=0 to 9 do{
for k:=0 to 9 do {
for l:=0 to 9 do{
key:=char(i)+char(j)+char(k)+char(l); // character concatenation operations
if iskey(key) then return key;
}
}
}
}
}
7/27/2020 38
Store and Reuse method:
In this algorithm the data is storing using MapReduce
method and the algorithm's access the data through
MapReduce method.
7/27/2020 39
Example store and reuse in
algorithms
7/27/2020 40
Example: Store Method
For example a remote sensing image data save in No-SQL
database can be described as follows:
step 1: Input the entire remote sensing image, the image contains more than one
spatial-bands.
step 2: Split the image into sub data according to bands.
Step 3: For each sub data band, split it into lines
Step 4: For each line, the pixels of line construct the value content.
Through this algorithm we can save a remote sensing image into HBase and Hbase
deploy on HDFS.
7/27/2020 41
Reuse Method
• Content reuse is not one technique, but a collection of many techniques.
• There are therefore several reuse algorithms each of which requires specific
content structures in the subject and document domains.
• The simplest content reuse technique is to cut and paste content from one
source to another. This approach is quick and easy to implement, but it
creates a host of management problems. So when people talk about content
reuse they generally mean any and every means of reusing content other
than cutting and pasting.
7/27/2020 42
Reuse method
• Reusing content, then, really means storing a piece of content in one place
and inserting it into more than one publication by reference. “Reusing” suggests that
this activity is somewhat akin to rummaging through that jar of old nuts and bolts we
have in the garage looking for one that is the right size to fix our lawnmower.
• While we can do it that way, that approach is neither efficient nor reliable.
The efficient and reliable approach involves deliberately creating content for use in
multiple locations. When we deliberately create content for reuse, we need to place
constraints on the content to be reused and the content that reuses it, and that puts
we in the realm of structured writing.
7/27/2020 43

More Related Content

What's hot

What's hot (20)

Hashing
HashingHashing
Hashing
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Hashing
HashingHashing
Hashing
 
Strassen's matrix multiplication
Strassen's matrix multiplicationStrassen's matrix multiplication
Strassen's matrix multiplication
 
Big o notation
Big o notationBig o notation
Big o notation
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 
Hashing in datastructure
Hashing in datastructureHashing in datastructure
Hashing in datastructure
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Stressen's matrix multiplication
Stressen's matrix multiplicationStressen's matrix multiplication
Stressen's matrix multiplication
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Divide and Conquer
Divide and ConquerDivide and Conquer
Divide and Conquer
 

Similar to Analysis of Recursive and Iterative Algorithms

Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxPJS KUMAR
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationGeoffrey Fox
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutionssubhashchandra197
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithmsrajatmay1992
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsMAHERMOHAMED27
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsemGopi Saiteja
 
PSO and Its application in Engineering
PSO and Its application in EngineeringPSO and Its application in Engineering
PSO and Its application in EngineeringPrince Jain
 
Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...
Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...
Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...SSA KPI
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxGadaFarhan
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Pramit Kumar
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...
Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...
Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...Chiheb Ben Hammouda
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusNANDINI SHARMA
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESsuthi
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesSreedhar Chowdam
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdfabay golla
 

Similar to Analysis of Recursive and Iterative Algorithms (20)

Unit 2
Unit 2Unit 2
Unit 2
 
Unit 2
Unit 2Unit 2
Unit 2
 
Introduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptxIntroduction to data structures and complexity.pptx
Introduction to data structures and complexity.pptx
 
Parallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel applicationParallel Computing 2007: Bring your own parallel application
Parallel Computing 2007: Bring your own parallel application
 
Recurrence relation solutions
Recurrence relation solutionsRecurrence relation solutions
Recurrence relation solutions
 
Ch24 efficient algorithms
Ch24 efficient algorithmsCh24 efficient algorithms
Ch24 efficient algorithms
 
Ch-2 final exam documet compler design elements
Ch-2 final exam documet compler design elementsCh-2 final exam documet compler design elements
Ch-2 final exam documet compler design elements
 
Ee693 sept2014midsem
Ee693 sept2014midsemEe693 sept2014midsem
Ee693 sept2014midsem
 
PSO and Its application in Engineering
PSO and Its application in EngineeringPSO and Its application in Engineering
PSO and Its application in Engineering
 
Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...
Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...
Efficient Solution of Two-Stage Stochastic Linear Programs Using Interior Poi...
 
T2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptxT2311 - Ch 4_Part1.pptx
T2311 - Ch 4_Part1.pptx
 
Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)Matrix Multiplication(An example of concurrent programming)
Matrix Multiplication(An example of concurrent programming)
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...
Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...
Hierarchical Deterministic Quadrature Methods for Option Pricing under the Ro...
 
Algorithms Exam Help
Algorithms Exam HelpAlgorithms Exam Help
Algorithms Exam Help
 
Basic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV SyllabusBasic Computer Engineering Unit II as per RGPV Syllabus
Basic Computer Engineering Unit II as per RGPV Syllabus
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Design and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture NotesDesign and Analysis of Algorithms Lecture Notes
Design and Analysis of Algorithms Lecture Notes
 
Chapter One.pdf
Chapter One.pdfChapter One.pdf
Chapter One.pdf
 

More from subhashchandra197

More from subhashchandra197 (11)

Unit ii divide and conquer -4
Unit ii divide and conquer -4Unit ii divide and conquer -4
Unit ii divide and conquer -4
 
Unit ii divide and conquer -3
Unit ii divide and conquer -3Unit ii divide and conquer -3
Unit ii divide and conquer -3
 
Unit ii divide and conquer -2
Unit ii divide and conquer -2Unit ii divide and conquer -2
Unit ii divide and conquer -2
 
Unit ii divide and conquer -1
Unit ii divide and conquer -1Unit ii divide and conquer -1
Unit ii divide and conquer -1
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
P,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problemsP,NP Hard, NP-Complete problems
P,NP Hard, NP-Complete problems
 
Turing machine material
Turing machine materialTuring machine material
Turing machine material
 
Turing machine types
Turing machine typesTuring machine types
Turing machine types
 
Introduction to algorithms
Introduction to algorithmsIntroduction to algorithms
Introduction to algorithms
 
Disjoint sets union, find
Disjoint sets  union, findDisjoint sets  union, find
Disjoint sets union, find
 

Recently uploaded

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINESIVASHANKAR N
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAbhinavSharma374939
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)Suman Mia
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 

Recently uploaded (20)

High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
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
 
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINEMANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
MANUFACTURING PROCESS-II UNIT-2 LATHE MACHINE
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Analog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog ConverterAnalog to Digital and Digital to Analog Converter
Analog to Digital and Digital to Analog Converter
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
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
 
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)Software Development Life Cycle By  Team Orange (Dept. of Pharmacy)
Software Development Life Cycle By Team Orange (Dept. of Pharmacy)
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 

Analysis of Recursive and Iterative Algorithms

  • 1. UNIT-I Analysis of the Recursive and Iterative Algorithms by Dr. N. Subhash Chandra, Professor, CVRCE. Source: Web resources and Computer Algorithms by Horowitz, Sahani & Rajasekaran 7/27/2020 1
  • 2. ANALYSIS OF RECURSIVE ALGORITHMS BRUITE FORCE MEHOD STORE AND REUSE METHOD ALGORITHM CORRECTNESS CONTENTS 7/27/2020 2
  • 3. Difference between Recursive and Iterative Algorithms PROPERTY RECURSION ITERATION Definition Function calls itself. A set of instructions repeatedly executed. Application For functions. For loops. Termination Through base case, where there will be no function call. When the termination condition for the iterator ceases to be satisfied. Usage Used when code size needs to be small, and time complexity is not an issue. Used when time complexity needs to be balanced against an expanded code size. Code Size Smaller code size Larger Code Size. Time Complexity Very high(generally exponential) time complexity. Relatively lower time complexity(generally polynomial- logarithmic). 7/27/2020 3
  • 4. ANALYSIS OF RECURSIVE ALGORITHMS 7/27/2020 4
  • 5. Recursive Algorithms Recursive sum Factorial Binary search Tower of Hanoi Permutation Generator 7/27/2020 5
  • 8. Recursive Factorial Algorithm Form and solve the recurrence relation for the running time of factorial Algorithm and hence determine its big-O complexity: T(0) = c (1) T(n) = 1 + T(n - 1) (2) = 1+ 1 + T(n - 2) by substituting T(n – 1) in (2) = 1 +1 +1+ T(n - 3) by substituting T(n – 2) in (2) … = k + T(n - k) The base case is reached when n – k = 0  k = n, we then have: T(n) = n + T(n - n) = n + T(0) = n + c Therefore the method factorial is O(n) Algorithm factorial ( n) { if (n == 0) then return 1; else return n * factorial (n – 1); } 7/27/2020 8
  • 9. Recursive Binary Search • The recurrence relation for the running time of the method is: T(1) = a if n = 1 (one element array) T(n) = T(n / 2) + b if n > 1 Algorithm BinarySearch (target,array, low, high) { if (low > high)then return ; else { middle = (low + high)/2; if (array[middle] == target)then return middle; else if(array[middle] < target)then return BinarySearch(target, array, middle + 1, high); else return BinarySearch(target, array, low, middle - 1); } } 7/27/2020 9
  • 10. Analysis of Recursive Binary Search Expanding: T(1) = a (1) T(n) = T(n / 2) + b (2) = [T(n / 22) + b] + b = T (n / 22) + 2b by substituting T(n/2) in (2) = [T(n / 23) + b] + 2b = T(n / 23) + 3b by substituting T(n/22) in (2) = …….. = T( n / 2k) + kb The base case is reached when n / 2k = 1  n = 2k  k = log2 n, we then have: T(n) = T(1) + b log2 n = a + b log2 n Therefore, Recursive Binary Search is O(log n) Without loss of generality, assume n, the problem size, is a multiple of 2, i.e., n = 2k 7/27/2020 10
  • 11. Recursive Fibonacci number Algorithm T(n) = c if n = 1 or n = 2 (1) T(n) = T(n – 1) + T(n – 2) + b if n > 2 (2) We determine a lower bound on T(n): Expand : T(n) = T(n - 1) + T(n - 2) + b ≥ T(n - 2) + T(n-2) + b = 2T(n - 2) + b = 2[T(n - 3) + T(n - 4) + b] + b by substitute T(n - 2) in (2)  2[T(n - 4) + T(n - 4) + b] + b = 22T(n - 4) + 2b + b = 22[T(n - 5) + T(n - 6) + b] + 2b + b by substitute T(n - 4) in (2) ≥ 23T(n – 6) + (22 + 21 + 20)b . . .  2kT(n – 2k) + (2k-1 + 2k-2 + . . . + 21 + 20)b = 2kT(n – 2k) + (2k – 1)b The base case is reached when n – 2k = 2  k = (n - 2) / 2 Hence, T(n) ≥ 2 (n – 2) / 2 T(2) + [2 (n - 2) / 2 – 1]b = (b + c)2 (n – 2) / 2 – b = [(b + c) / 2]*(2)n/2 – b  Récursive Fibonacci is exponentiel Algorithm Fibonacci (n) { // Recursively calculates Fibonacci number if( n == 1 || n == 2)then return 1; else return Fibonacci(n – 1) + Fibonacci(n – 2); } 7/27/2020 11
  • 12. Tower of Hanoi Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings is as depicted Rules The mission is to move all the disks to some another tower without violating the sequence of arrangement. A few rules to be followed for Tower of Hanoi are • Only one disk can be moved among the towers at any given time. • Only the "top" disk can be removed. • No large disk can sit over a small disk. 7/27/2020 12
  • 14. Algorithm TOH The steps to follow are − Step 1 − Move n-1 disks from source (X)to aux(Y) Step 2 − Move nth disk from source(X) to dest (Z) Step 3 − Move n-1 disks from aux (Y) to dest (Z) Algorithm TOH(n, x, y, z) // Move the top n discs from x to z, where x is source, Y auxiliary tower, Z is destination { if(n>=1)then { TOH(n-1, x, z, y); write( Move a disc from x to z); TOH(n-1, y, x, z); } } 7/27/2020 14
  • 15. Tracing 3 Discs with algorithm 7/27/2020 15
  • 16. Analysis of Recursive Towers of Hanoi Algorithm Expanding: T(1) = a (1) T(n) = 2T(n – 1) + b if n > 1 (2) = 2[2T(n – 2) + b] + b = 22 T(n – 2) + 2b + b by substituting T(n – 1) in (2) = 22 [2T(n – 3) + b] + 2b + b = 23 T(n – 3) + 22b + 2b + b by substituting T(n-2) in (2) = 23 [2T(n – 4) + b] + 22b + 2b + b = 24 T(n – 4) + 23 b + 22b + 21b + 20b by substituting T(n – 3) in (2) = …… = 2k T(n – k) + b[2k- 1 + 2k– 2 + . . . 21 + 20] The base case is reached when n – k = 1  k = n – 1, we then have: Therefore, The method hanoi is O(2n) 7/27/2020 16
  • 20. Algorithm for Permutation generator 7/27/2020 20
  • 21. Analysis of Permutation Generator 7/27/2020 21
  • 22. ANALYSIS OF ITERATIVE ALGORITHMS 7/27/2020 22
  • 32. • Proving an Algorithm’s Correctness • Brute Force Approach • Store and Reuse Method 7/27/2020 32
  • 33. Proving an Algorithm’s Correctness Once an algorithm has been specified then its correctness must be proved. An algorithm must yield a required result for every legitimate input in a finite amount of time. 7/27/2020 33
  • 34. Proving Technique for Algorithm correctness A common technique for proving correctness is to use mathematical induction because an algorithm’s iterations provide a natural sequence of steps needed for such proofs. The notion of correctness for approximation algorithms is less straightforward than it is for exact algorithms. The error produced by the algorithm should not exceed a pre-defined limit. 7/27/2020 34
  • 35. Brute Force Approach • A brute force algorithm is a straightforward approach to solving a problem. It is usually based on problem statement and definitions • This approach consider all possible solutions. • It is based on trial and error where the programmer tries to merely utilize the computer’s fast processing power to solve a problem. • It is a naive approach adopted by most beginner programmers in the initial stages of solving a problem, however it might not be the most optimal approach, as it might increase both space and time complexity. Ex: TSP, Convex hull, Knapsack, Graph colouring, Closest distance etc 7/27/2020 35
  • 36. Example 1 Linear Search: When each element of an array is compared with the data to be searched, it might be termed as a brute force approach, as it is the most direct and simple way one could think of searching the given data in the array. However, there are better ways to search data which make use of more optimum algorithms like Binary search, etc. which adopt a more innovative way to solve the same problem. 7/27/2020 36
  • 38. Brute Force Solution Algorithm FindKey() { for i:=0 to 9 do { for j:=0 to 9 do{ for k:=0 to 9 do { for l:=0 to 9 do{ key:=char(i)+char(j)+char(k)+char(l); // character concatenation operations if iskey(key) then return key; } } } } } 7/27/2020 38
  • 39. Store and Reuse method: In this algorithm the data is storing using MapReduce method and the algorithm's access the data through MapReduce method. 7/27/2020 39
  • 40. Example store and reuse in algorithms 7/27/2020 40
  • 41. Example: Store Method For example a remote sensing image data save in No-SQL database can be described as follows: step 1: Input the entire remote sensing image, the image contains more than one spatial-bands. step 2: Split the image into sub data according to bands. Step 3: For each sub data band, split it into lines Step 4: For each line, the pixels of line construct the value content. Through this algorithm we can save a remote sensing image into HBase and Hbase deploy on HDFS. 7/27/2020 41
  • 42. Reuse Method • Content reuse is not one technique, but a collection of many techniques. • There are therefore several reuse algorithms each of which requires specific content structures in the subject and document domains. • The simplest content reuse technique is to cut and paste content from one source to another. This approach is quick and easy to implement, but it creates a host of management problems. So when people talk about content reuse they generally mean any and every means of reusing content other than cutting and pasting. 7/27/2020 42
  • 43. Reuse method • Reusing content, then, really means storing a piece of content in one place and inserting it into more than one publication by reference. “Reusing” suggests that this activity is somewhat akin to rummaging through that jar of old nuts and bolts we have in the garage looking for one that is the right size to fix our lawnmower. • While we can do it that way, that approach is neither efficient nor reliable. The efficient and reliable approach involves deliberately creating content for use in multiple locations. When we deliberately create content for reuse, we need to place constraints on the content to be reused and the content that reuses it, and that puts we in the realm of structured writing. 7/27/2020 43