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

PDF
Closest pair problems (Divide and Conquer)
Gem WeBlog
 
PPTX
Topological Sorting
ShahDhruv21
 
DOC
Time and space complexity
Ankit Katiyar
 
PDF
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
PPTX
Asymptotic Notation
Protap Mondal
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
All pair shortest path
Arafat Hossan
 
PPT
Dinive conquer algorithm
Mohd Arif
 
PPTX
Tree in data structure
ghhgj jhgh
 
PPT
Hashing PPT
Saurabh Kumar
 
PPT
Algorithm analysis
sumitbardhan
 
PPT
Unit 1 chapter 1 Design and Analysis of Algorithms
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
PDF
Matrix chain multiplication
Kiran K
 
PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PPTX
Asymptotic notations
Nikhil Sharma
 
PPT
Heaps
Hafiz Atif Amin
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPTX
Knapsack Problem
Jenny Galino
 
PDF
Expression trees
Salman Vadsarya
 
PPTX
Hashing Technique In Data Structures
SHAKOOR AB
 
Closest pair problems (Divide and Conquer)
Gem WeBlog
 
Topological Sorting
ShahDhruv21
 
Time and space complexity
Ankit Katiyar
 
Algorithms Lecture 6: Searching Algorithms
Mohamed Loey
 
Asymptotic Notation
Protap Mondal
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
All pair shortest path
Arafat Hossan
 
Dinive conquer algorithm
Mohd Arif
 
Tree in data structure
ghhgj jhgh
 
Hashing PPT
Saurabh Kumar
 
Algorithm analysis
sumitbardhan
 
Unit 1 chapter 1 Design and Analysis of Algorithms
P. Subathra Kishore, KAMARAJ College of Engineering and Technology, Madurai
 
Matrix chain multiplication
Kiran K
 
Binary Tree Traversal
Dhrumil Panchal
 
Asymptotic notations
Nikhil Sharma
 
Sorting Algorithms
Mohammed Hussein
 
Knapsack Problem
Jenny Galino
 
Expression trees
Salman Vadsarya
 
Hashing Technique In Data Structures
SHAKOOR AB
 

Similar to Recursive algorithms (20)

PDF
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
PDF
Algorithm review
chidabdu
 
PDF
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
 
PDF
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
PPTX
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
PDF
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
PPTX
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
PPT
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
PDF
jn;lm;lkm';m';;lmppt of data structure.pdf
VinayNassa3
 
PDF
Iare ds ppt_3
AlugatiRajitha
 
PPTX
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PDF
Daa chapter 2
B.Kirron Reddi
 
PPTX
Algorithms - Rocksolid Tour 2013
Gary Short
 
PPTX
Lecture 7_introduction_algorithms.pptx
ngBinh3
 
PPTX
Recursion in Data Structure
khudabux1998
 
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
PDF
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
PPTX
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
PPTX
3. D&C and Recurrence Relation.ppYtxVVVV
NetraBansal3
 
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
Algorithm review
chidabdu
 
lec 03wweweweweweweeweweweewewewewee.pdf
Huma Ayub
 
Disign and Analysis for algorithm in computer science and technology
ritikkumarchaudhury7
 
T2311 - Ch 4_Part1.pptx
GadaFarhan
 
Cs6402 design and analysis of algorithms may june 2016 answer key
appasami
 
Algorithm Design and Complexity - Course 3
Traian Rebedea
 
3-Chapter Three - Divide and Conquer.ppt
mershaabdisa
 
jn;lm;lkm';m';;lmppt of data structure.pdf
VinayNassa3
 
Iare ds ppt_3
AlugatiRajitha
 
Recursion and Sorting Algorithms
Afaq Mansoor Khan
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Daa chapter 2
B.Kirron Reddi
 
Algorithms - Rocksolid Tour 2013
Gary Short
 
Lecture 7_introduction_algorithms.pptx
ngBinh3
 
Recursion in Data Structure
khudabux1998
 
Lecture 5 6_7 - divide and conquer and method of solving recurrences
jayavignesh86
 
01 Notes Introduction Analysis of Algorithms Notes
Andres Mendez-Vazquez
 
DS Unit-1.pptx very easy to understand..
KarthikeyaLanka1
 
3. D&C and Recurrence Relation.ppYtxVVVV
NetraBansal3
 
Ad

More from subhashchandra197 (17)

PPT
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
subhashchandra197
 
PPTX
Unit-I- Introduction- Traits of Big Data-Final.pptx
subhashchandra197
 
PPTX
Data Science : Unit-I -Hypothesis and Inferences.pptx
subhashchandra197
 
PPTX
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
subhashchandra197
 
PPTX
Data science Lecture Notes: Reporting vs Analysis.pptx
subhashchandra197
 
PDF
Unit ii divide and conquer -4
subhashchandra197
 
PDF
Unit ii divide and conquer -3
subhashchandra197
 
PDF
Unit ii divide and conquer -2
subhashchandra197
 
PDF
Unit ii divide and conquer -1
subhashchandra197
 
PDF
Bs,qs,divide and conquer 1
subhashchandra197
 
PDF
Recurrence relation solutions
subhashchandra197
 
PDF
Asymptotic notations
subhashchandra197
 
PDF
P,NP Hard, NP-Complete problems
subhashchandra197
 
PDF
Turing machine material
subhashchandra197
 
PDF
Turing machine types
subhashchandra197
 
PDF
Introduction to algorithms
subhashchandra197
 
PPTX
Disjoint sets union, find
subhashchandra197
 
Unit-I Objects,Attributes,Similarity&Dissimilarity.ppt
subhashchandra197
 
Unit-I- Introduction- Traits of Big Data-Final.pptx
subhashchandra197
 
Data Science : Unit-I -Hypothesis and Inferences.pptx
subhashchandra197
 
UNIT-1 Data pre-processing-Data cleaning, Transformation, Reduction, Integrat...
subhashchandra197
 
Data science Lecture Notes: Reporting vs Analysis.pptx
subhashchandra197
 
Unit ii divide and conquer -4
subhashchandra197
 
Unit ii divide and conquer -3
subhashchandra197
 
Unit ii divide and conquer -2
subhashchandra197
 
Unit ii divide and conquer -1
subhashchandra197
 
Bs,qs,divide and conquer 1
subhashchandra197
 
Recurrence relation solutions
subhashchandra197
 
Asymptotic notations
subhashchandra197
 
P,NP Hard, NP-Complete problems
subhashchandra197
 
Turing machine material
subhashchandra197
 
Turing machine types
subhashchandra197
 
Introduction to algorithms
subhashchandra197
 
Disjoint sets union, find
subhashchandra197
 
Ad

Recently uploaded (20)

PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPTX
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
PDF
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
PDF
Electrical Engineer operation Supervisor
ssaruntatapower143
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
PDF
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
PDF
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
PPT
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
PDF
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PDF
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PDF
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
PPTX
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
PPTX
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
PDF
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
PDF
Digital water marking system project report
Kamal Acharya
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
fatigue in aircraft structures-221113192308-0ad6dc8c.pptx
aviatecofficial
 
MODULE-5 notes [BCG402-CG&V] PART-B.pdf
Alvas Institute of Engineering and technology, Moodabidri
 
Electrical Engineer operation Supervisor
ssaruntatapower143
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
Submit Your Papers-International Journal on Cybernetics & Informatics ( IJCI)
IJCI JOURNAL
 
methodology-driven-mbse-murphy-july-hsv-huntsville6680038572db67488e78ff00003...
henriqueltorres1
 
New_school_Engineering_presentation_011707.ppt
VinayKumar304579
 
AN EMPIRICAL STUDY ON THE USAGE OF SOCIAL MEDIA IN GERMAN B2C-ONLINE STORES
ijait
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
Data structures notes for unit 2 in computer science.pdf
sshubhamsingh265
 
Design Thinking basics for Engineers.pdf
CMR University
 
Halide Perovskites’ Multifunctional Properties: Coordination Engineering, Coo...
TaameBerhe2
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
WD2(I)-RFQ-GW-1415_ Shifting and Filling of Sand in the Pond at the WD5 Area_...
ShahadathHossain23
 
2025 CGI Congres - Surviving agile v05.pptx
Derk-Jan de Grood
 
How Industrial Project Management Differs From Construction.pptx
jamespit799
 
mbse_An_Introduction_to_Arcadia_20150115.pdf
henriqueltorres1
 
Digital water marking system project report
Kamal Acharya
 

Recursive 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