SlideShare a Scribd company logo
1 of 28
Prof. Amr Goneid, AUC 1
Analysis & Design of
Algorithms
(CSCE 321)
Prof. Amr Goneid
Department of Computer Science, AUC
Part 5. Recursive Algorithms
Prof. Amr Goneid, AUC 2
Recursive Algorithms
Prof. Amr Goneid, AUC 3
Recursive Algorithms
 Modeling Recursive Algorithms
 Examples of Recurrences
 Exclude & Conquer
 General Solution of 1st Order Linear
Recurrences
 2nd Order Linear Recurrence
Prof. Amr Goneid, AUC 4
1. Modeling Recursive Algorithms
Recursive Algorithm
Model as a
Recurrence Relation
T(n) in terms of T(n-1)
Solve to obtain T(n) as
a function of n
Prof. Amr Goneid, AUC 5
Recurrence Relations
 A recurrence relation is an equation describing a
function in terms of its value for smaller instances.
 Many algorithms, particularly exclude & conquer
and divide and conquer algorithms, have time
complexities which are naturally modeled by
recurrence relations.
 Many counting problems can be modeled as
recurrences. Solving the recurrence relation
solves the problem.
Prof. Amr Goneid, AUC 6
Recurrence Relations
 Many natural functions are expressed as
recurrences:
)
(Factorial
1
with
al)
(Exponenti
2
1
with
2
l)
(Polynomia
1
with
1
1
1
1
1
1
1
1
!
n
a
a
na
a
a
a
a
a
n
a
a
a
a
n
n
n
n
n
n
n
n
n
n

















Prof. Amr Goneid, AUC 7
Recurrence Relations
To derive the recurrence relation:
 Identify the problem size (n).
 Find the base case size n0
(usually n0 = 0 ,1 or 2) and find T(n0)
 The general case consists of “a” recursive
calls to do smaller sub-problems (usually of
size n-1) plus a cost “b” for all the work
needed other than in the recursive calls.
Prof. Amr Goneid, AUC 8
Recurrence Relations
Hence, the recurrence relation will have the
form:











0
0
0
)
1
(
)
(
)
(
n
n
for
b
n
aT
n
n
for
n
T
n
T
base case
cost of base case
no. of
recursive
calls
cost of a
recursive
call
cost of
other
operations
Prof. Amr Goneid, AUC 9
T(0) = 0
 A Recursive Factorial Function:
factorial (n)
{
if (n == 0) return 1;
else return ( factorial (n-1) * n);
}
Find T(n) = number of integer multiplications
2. Examples of Recurrences
T(n)
T(n-1) 1
Prof. Amr Goneid, AUC 10
Examples
 The base case with n = 0 does zero
multiplications. Hence T(0) = 0.
 The general case will cost T(n-1) plus one
multiplication.
 Recurrence Relation:
0
T(0)
0 with
n
for
1
1)
-
T(n
T(n) 



Prof. Amr Goneid, AUC 11
Examples
Recurrence relations of this kind can be solved by
successive substitution:
 Substitute several times (m) to show a pattern.
 Choose (m) so that each T( ) in which (m) appears
reduces to T(n0)
 Substitute for T(n0)
 Solve the resulting equation
Prof. Amr Goneid, AUC 12
Examples
For example, for the recurrence relation of the
recursive factorial:
)
n
(
)
n
(
T
Hence
n
n
)
(
T
)
n
(
T
then
n
m
With
m
)
m
n
(
T
....
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
T
)
(
T
with
)
n
(
T
)
n
(
T
























0
3
3
2
2
1
1
2
0
0
1
1
Prof. Amr Goneid, AUC 13
 A Recursive Function to return xn:
power (x, n)
{
if (n == 0 ) return 1.0;
else return ( power(x,n-1)*x );
}
Find T(n) = number of arithmetic operations
T(n) = T(n-1) + 1 for n > 0 with T(0) = 0
Hence T(n) = n = (n)
Examples
Prof. Amr Goneid, AUC 14
3. Exclude and Conquer
n
1 n-1
Base
1
1
Prof. Amr Goneid, AUC 15
// Assume the data are in locations s….e of array a[0..n-1]
// A problem of size (n) arises when the function is invoked as
// selectsort(a,0,n-1), i.e. with s = 0 and e = n-1
Selectsort (a[ ], s, e)
{
if (s < e) {
m = minimum (a , s , e) ;
swap (a[s] , a[m]);
Selectsort (a,s+1,e); }
}
Example: Recursive Selection Sort
T(s,e)
T(s+1,e)
comp
s
e )
( 
swap
1
Prof. Amr Goneid, AUC 16
Selection Sort (continued)
Recurrence Relation: ( problem of size n when s = 0 , e = n-1):
)
1
n
(
)
n
(
T
that
So
0
)
1
(
T
with
1
n
for
1
)
1
n
(
T
)
n
(
T
Also
2
)
1
n
(
n
i
)
i
n
(
)
1
(
T
)
n
(
T
,
1
n
m
Set
)
i
n
(
)
m
n
(
T
)
1
n
(
)
2
n
(
)
2
n
(
T
)
n
(
T
Hence
0
)
1
(
T
with
1
n
for
)
1
n
(
)
1
n
(
T
)
n
(
T
1
n
e
,
0
s
Let
)
s
e
(
)
e
,
1
s
(
T
)
e
,
s
(
T
swap
swap
swap
1
n
1
i
1
n
1
i
comp
m
1
i
comp
comp
comp
comp











 




 



























Prof. Amr Goneid, AUC 17
4. General Solution of 1st Order
Linear Recurrences
Many recursive algorithms are modeled to the
following general 1st order linear recurrence
relation:
e.g. the factorial algorithm gives an = 1 and
bn = 1, T(0) given.
Select sort gives an = 1 and bn = (n-1),
T(1) given.
T(1)
or
T(0)
given
,
b
1)
-
T(n
a
T(n) n
n 

Prof. Amr Goneid, AUC 18
General Solution of 1st Order
Linear Recurrences
Such recurrence can be solved by successive
substitution. e.g. for T(0) given:
n
n
i
j
j
n
i
i
n
i
i
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
n
b
a
b
a
)
(
T
...
b
b
a
b
a
a
)
n
(
T
a
a
a
........
b
b
a
)
n
(
T
a
a
b
}
b
)
n
(
T
a
{
a
b
)
n
(
T
a
)
n
(
T






































1
1
1
1
1
2
1
2
1
1
1
1
1
0
3
2
2
1
Prof. Amr Goneid, AUC 19
General Solution of 1st Order
Linear Recurrences
Successive substitution gives for the cases of
T(0) given and T(1) given:
n
n
i
j
j
n
i
i
n
i
i
n
n
i
j
j
n
i
i
n
i
i
b
a
b
a
)
(
T
)
n
(
T
b
a
b
a
)
(
T
)
n
(
T






















1
1
2
2
1
1
1
1
1
0
Prof. Amr Goneid, AUC 20
Examples
 For the recursive factorial function, an=1 so that ai=1 and
similarly bi=1 with T(0)=0. Substitution in the general
solution gives:
hence T(n) = n as before.
 For the selection sort algorithm, ai=1,
bi= (i-1) with T(1) = 0 so that
n
)
n
(
T
n
i
n
i
n
i
j




 



 
 1
1
1 1
1
1
1
1


n
comp
i
comp
T ( n ) ( i ) ... ( n )
T ( n ) n( n ) /

        
 

2
1 1 2 3 4 1
1 2
Prof. Amr Goneid, AUC 21
Examples
 T(n) = 2 T(n-1) + 1 with T(0) = 0
 T(n) = n T(n-1) with T(0) = 1
)
2
(
1
2
2
1
2
1
)
(
1
0
1
1 1
n
n
n
k
k
n
i
n
i
j
O
n
T 




 
 



 

)
!
(
!
)
0
(
)
(
1
1
n
O
n
i
a
T
n
T
n
i
n
i
i 


 
 

Prof. Amr Goneid, AUC 22
Exercise(1): Exclude & Conquer
with a Linear Process
int FOO (int a[ ], int n)
{
if (n > 0)
{
int m = 5 * Process (a, n);
return m * FOO(a,n-1);
}
}
Show that the number of integer multiplications if Process
makes 2n such operations is:
)
n
(
O
n
n
)
n
(
T 2
2
3 


Prof. Amr Goneid, AUC 23
Solution
)
n
(
O
n
n
}
n
/
)
n
(
n
{
)
i
(
b
b
b
)
n
(
T
)
i
(
b
,
a
)
(
T
with
)
n
(
)
n
(
T
)
n
(
T
n
i
n
i
i
n
n
i
i
i
i
2
2
1
1
1
1
3
2
1
2
1
2
1
2
1
0
0
1
2
1






 


















Prof. Amr Goneid, AUC 24
Exercise(2): History Problem
double Eval ( int n )
{ int k ; double sum = 0.0;
if (n == 0) return 1.0 ;
else
{ for (k = 0; k < n; k++) sum += Eval (k);
return 2.0 * sum / n + n ; }
}
Show that the number of double arithmetic
operations performed as a function of (n) is:
)
(
O
)
n
(
T n
n
2
1
2 


Prof. Amr Goneid, AUC 25
Solution
1
1
2
1
1
1
2
1
3
0
0
3
1
2
0
1
0
1
0




























)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
T
)
n
(
)
k
(
T
)
n
(
T
)
n
(
)
k
(
T
)
n
(
T
)
(
T
with
}
)
k
(
T
{
)
n
(
T
n
k
n
k
n
k
Prof. Amr Goneid, AUC 26
Solution
)
(
O
b
a
b
)
n
(
T
)
(
T
,
b
,
a
n
n
n
i
i
n
i
i
n
n
n
i
j
j
n
i
i
i
i
2
1
2
2
1
2
0
0
1
2
1
0
1
1
1
1
1
























Prof. Amr Goneid, AUC 27
Exercise (3): Hanoi Towers Game
Find the number of moves required for a Tower of
Hanoi with n discs. An animation is available at:
http://www.cosc.canterbury.ac.nz/people/mukundan/dsal
/ToHdb.html
Prof. Amr Goneid, AUC 28
5. 2nd Order Linear Recurrence
Example: Fibonacci Sequence
F(n) = F(n-1) + F(n-2) , F(0) = 0 F(1) = 1
Let to obtain the characteristic equation
Solutions:
n
n n
n n
F(n) r r r
,
F( n )
With F( ) and F( ) then and
F( n ) , Golden Ratio .
  
 
  
 

   
 
   
 
    

  
2
1 0
1 5 1 5
1
2 2
1
0 0 1 1
5
1 618034
5

More Related Content

Similar to 5. Recursive.ppt

Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
Sri Prasanna
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
Lecture 4 - Growth of Functions (1).ppt
Lecture 4 - Growth of Functions (1).pptLecture 4 - Growth of Functions (1).ppt
Lecture 4 - Growth of Functions (1).ppt
ZohairMughal1
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
Abbas Ali
 

Similar to 5. Recursive.ppt (20)

03 Cap 2 - fourier-analysis-2015.pdf
03 Cap 2 - fourier-analysis-2015.pdf03 Cap 2 - fourier-analysis-2015.pdf
03 Cap 2 - fourier-analysis-2015.pdf
 
Analysis Of Algorithms Ii
Analysis Of Algorithms IiAnalysis Of Algorithms Ii
Analysis Of Algorithms Ii
 
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
Ad2014 calvec-industrial-jllf.ps14000302.curvas (1)
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
Time complexity.ppt
Time complexity.pptTime complexity.ppt
Time complexity.ppt
 
Time complexity
Time complexityTime complexity
Time complexity
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Lecture 4 - Growth of Functions (1).ppt
Lecture 4 - Growth of Functions (1).pptLecture 4 - Growth of Functions (1).ppt
Lecture 4 - Growth of Functions (1).ppt
 
Lecture 4 - Growth of Functions.ppt
Lecture 4 - Growth of Functions.pptLecture 4 - Growth of Functions.ppt
Lecture 4 - Growth of Functions.ppt
 
Unit 3
Unit 3Unit 3
Unit 3
 
Unit 3
Unit 3Unit 3
Unit 3
 
Signal Processing Homework Help
Signal Processing Homework HelpSignal Processing Homework Help
Signal Processing Homework Help
 
impulse(GreensFn), Principle of Superposition
impulse(GreensFn), Principle of Superpositionimpulse(GreensFn), Principle of Superposition
impulse(GreensFn), Principle of Superposition
 
Lec03 04-time complexity
Lec03 04-time complexityLec03 04-time complexity
Lec03 04-time complexity
 
NONLINEAR DIFFERENCE EQUATIONS WITH SMALL PARAMETERS OF MULTIPLE SCALES
NONLINEAR DIFFERENCE EQUATIONS WITH SMALL PARAMETERS OF MULTIPLE SCALESNONLINEAR DIFFERENCE EQUATIONS WITH SMALL PARAMETERS OF MULTIPLE SCALES
NONLINEAR DIFFERENCE EQUATIONS WITH SMALL PARAMETERS OF MULTIPLE SCALES
 
Copy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquerCopy of y16 02-2119divide-and-conquer
Copy of y16 02-2119divide-and-conquer
 
Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...Low rank tensor approximation of probability density and characteristic funct...
Low rank tensor approximation of probability density and characteristic funct...
 
3. convolution fourier
3. convolution fourier3. convolution fourier
3. convolution fourier
 
Ep 5512 lecture-02
Ep 5512 lecture-02Ep 5512 lecture-02
Ep 5512 lecture-02
 
Introducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmosIntroducción al Análisis y diseño de algoritmos
Introducción al Análisis y diseño de algoritmos
 

More from ssuser3aa461

More from ssuser3aa461 (6)

Embedded system timing analysis.pptx
Embedded system timing analysis.pptxEmbedded system timing analysis.pptx
Embedded system timing analysis.pptx
 
Border Gateway Protocol & IPV6.pptx
Border Gateway Protocol & IPV6.pptxBorder Gateway Protocol & IPV6.pptx
Border Gateway Protocol & IPV6.pptx
 
unit2-210710110327.pdf
unit2-210710110327.pdfunit2-210710110327.pdf
unit2-210710110327.pdf
 
intro-to-cnn-April_2020.pptx
intro-to-cnn-April_2020.pptxintro-to-cnn-April_2020.pptx
intro-to-cnn-April_2020.pptx
 
COMPONENT INTERFACING.pptx
COMPONENT INTERFACING.pptxCOMPONENT INTERFACING.pptx
COMPONENT INTERFACING.pptx
 
Simple CPU Instruction Set Design.pptx
Simple CPU Instruction Set Design.pptxSimple CPU Instruction Set Design.pptx
Simple CPU Instruction Set Design.pptx
 

Recently uploaded

Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
EADTU
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MysoreMuleSoftMeetup
 

Recently uploaded (20)

Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdfDiuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
Diuretic, Hypoglycemic and Limit test of Heavy metals and Arsenic.-1.pdf
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
Transparency, Recognition and the role of eSealing - Ildiko Mazar and Koen No...
 
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
MuleSoft Integration with AWS Textract | Calling AWS Textract API |AWS - Clou...
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111Details on CBSE Compartment Exam.pptx1111
Details on CBSE Compartment Exam.pptx1111
 
OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...OS-operating systems- ch05 (CPU Scheduling) ...
OS-operating systems- ch05 (CPU Scheduling) ...
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Simple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdfSimple, Complex, and Compound Sentences Exercises.pdf
Simple, Complex, and Compound Sentences Exercises.pdf
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 

5. Recursive.ppt

  • 1. Prof. Amr Goneid, AUC 1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms
  • 2. Prof. Amr Goneid, AUC 2 Recursive Algorithms
  • 3. Prof. Amr Goneid, AUC 3 Recursive Algorithms  Modeling Recursive Algorithms  Examples of Recurrences  Exclude & Conquer  General Solution of 1st Order Linear Recurrences  2nd Order Linear Recurrence
  • 4. Prof. Amr Goneid, AUC 4 1. Modeling Recursive Algorithms Recursive Algorithm Model as a Recurrence Relation T(n) in terms of T(n-1) Solve to obtain T(n) as a function of n
  • 5. Prof. Amr Goneid, AUC 5 Recurrence Relations  A recurrence relation is an equation describing a function in terms of its value for smaller instances.  Many algorithms, particularly exclude & conquer and divide and conquer algorithms, have time complexities which are naturally modeled by recurrence relations.  Many counting problems can be modeled as recurrences. Solving the recurrence relation solves the problem.
  • 6. Prof. Amr Goneid, AUC 6 Recurrence Relations  Many natural functions are expressed as recurrences: ) (Factorial 1 with al) (Exponenti 2 1 with 2 l) (Polynomia 1 with 1 1 1 1 1 1 1 1 ! n a a na a a a a a n a a a a n n n n n n n n n n                 
  • 7. Prof. Amr Goneid, AUC 7 Recurrence Relations To derive the recurrence relation:  Identify the problem size (n).  Find the base case size n0 (usually n0 = 0 ,1 or 2) and find T(n0)  The general case consists of “a” recursive calls to do smaller sub-problems (usually of size n-1) plus a cost “b” for all the work needed other than in the recursive calls.
  • 8. Prof. Amr Goneid, AUC 8 Recurrence Relations Hence, the recurrence relation will have the form:            0 0 0 ) 1 ( ) ( ) ( n n for b n aT n n for n T n T base case cost of base case no. of recursive calls cost of a recursive call cost of other operations
  • 9. Prof. Amr Goneid, AUC 9 T(0) = 0  A Recursive Factorial Function: factorial (n) { if (n == 0) return 1; else return ( factorial (n-1) * n); } Find T(n) = number of integer multiplications 2. Examples of Recurrences T(n) T(n-1) 1
  • 10. Prof. Amr Goneid, AUC 10 Examples  The base case with n = 0 does zero multiplications. Hence T(0) = 0.  The general case will cost T(n-1) plus one multiplication.  Recurrence Relation: 0 T(0) 0 with n for 1 1) - T(n T(n)    
  • 11. Prof. Amr Goneid, AUC 11 Examples Recurrence relations of this kind can be solved by successive substitution:  Substitute several times (m) to show a pattern.  Choose (m) so that each T( ) in which (m) appears reduces to T(n0)  Substitute for T(n0)  Solve the resulting equation
  • 12. Prof. Amr Goneid, AUC 12 Examples For example, for the recurrence relation of the recursive factorial: ) n ( ) n ( T Hence n n ) ( T ) n ( T then n m With m ) m n ( T .... ) n ( T ) n ( T ) n ( T ) n ( T ) ( T with ) n ( T ) n ( T                         0 3 3 2 2 1 1 2 0 0 1 1
  • 13. Prof. Amr Goneid, AUC 13  A Recursive Function to return xn: power (x, n) { if (n == 0 ) return 1.0; else return ( power(x,n-1)*x ); } Find T(n) = number of arithmetic operations T(n) = T(n-1) + 1 for n > 0 with T(0) = 0 Hence T(n) = n = (n) Examples
  • 14. Prof. Amr Goneid, AUC 14 3. Exclude and Conquer n 1 n-1 Base 1 1
  • 15. Prof. Amr Goneid, AUC 15 // Assume the data are in locations s….e of array a[0..n-1] // A problem of size (n) arises when the function is invoked as // selectsort(a,0,n-1), i.e. with s = 0 and e = n-1 Selectsort (a[ ], s, e) { if (s < e) { m = minimum (a , s , e) ; swap (a[s] , a[m]); Selectsort (a,s+1,e); } } Example: Recursive Selection Sort T(s,e) T(s+1,e) comp s e ) (  swap 1
  • 16. Prof. Amr Goneid, AUC 16 Selection Sort (continued) Recurrence Relation: ( problem of size n when s = 0 , e = n-1): ) 1 n ( ) n ( T that So 0 ) 1 ( T with 1 n for 1 ) 1 n ( T ) n ( T Also 2 ) 1 n ( n i ) i n ( ) 1 ( T ) n ( T , 1 n m Set ) i n ( ) m n ( T ) 1 n ( ) 2 n ( ) 2 n ( T ) n ( T Hence 0 ) 1 ( T with 1 n for ) 1 n ( ) 1 n ( T ) n ( T 1 n e , 0 s Let ) s e ( ) e , 1 s ( T ) e , s ( T swap swap swap 1 n 1 i 1 n 1 i comp m 1 i comp comp comp comp                                              
  • 17. Prof. Amr Goneid, AUC 17 4. General Solution of 1st Order Linear Recurrences Many recursive algorithms are modeled to the following general 1st order linear recurrence relation: e.g. the factorial algorithm gives an = 1 and bn = 1, T(0) given. Select sort gives an = 1 and bn = (n-1), T(1) given. T(1) or T(0) given , b 1) - T(n a T(n) n n  
  • 18. Prof. Amr Goneid, AUC 18 General Solution of 1st Order Linear Recurrences Such recurrence can be solved by successive substitution. e.g. for T(0) given: n n i j j n i i n i i n n n n n n n n n n n n n n n n n n n n b a b a ) ( T ... b b a b a a ) n ( T a a a ........ b b a ) n ( T a a b } b ) n ( T a { a b ) n ( T a ) n ( T                                       1 1 1 1 1 2 1 2 1 1 1 1 1 0 3 2 2 1
  • 19. Prof. Amr Goneid, AUC 19 General Solution of 1st Order Linear Recurrences Successive substitution gives for the cases of T(0) given and T(1) given: n n i j j n i i n i i n n i j j n i i n i i b a b a ) ( T ) n ( T b a b a ) ( T ) n ( T                       1 1 2 2 1 1 1 1 1 0
  • 20. Prof. Amr Goneid, AUC 20 Examples  For the recursive factorial function, an=1 so that ai=1 and similarly bi=1 with T(0)=0. Substitution in the general solution gives: hence T(n) = n as before.  For the selection sort algorithm, ai=1, bi= (i-1) with T(1) = 0 so that n ) n ( T n i n i n i j             1 1 1 1 1 1 1 1   n comp i comp T ( n ) ( i ) ... ( n ) T ( n ) n( n ) /              2 1 1 2 3 4 1 1 2
  • 21. Prof. Amr Goneid, AUC 21 Examples  T(n) = 2 T(n-1) + 1 with T(0) = 0  T(n) = n T(n-1) with T(0) = 1 ) 2 ( 1 2 2 1 2 1 ) ( 1 0 1 1 1 n n n k k n i n i j O n T                ) ! ( ! ) 0 ( ) ( 1 1 n O n i a T n T n i n i i        
  • 22. Prof. Amr Goneid, AUC 22 Exercise(1): Exclude & Conquer with a Linear Process int FOO (int a[ ], int n) { if (n > 0) { int m = 5 * Process (a, n); return m * FOO(a,n-1); } } Show that the number of integer multiplications if Process makes 2n such operations is: ) n ( O n n ) n ( T 2 2 3   
  • 23. Prof. Amr Goneid, AUC 23 Solution ) n ( O n n } n / ) n ( n { ) i ( b b b ) n ( T ) i ( b , a ) ( T with ) n ( ) n ( T ) n ( T n i n i i n n i i i i 2 2 1 1 1 1 3 2 1 2 1 2 1 2 1 0 0 1 2 1                          
  • 24. Prof. Amr Goneid, AUC 24 Exercise(2): History Problem double Eval ( int n ) { int k ; double sum = 0.0; if (n == 0) return 1.0 ; else { for (k = 0; k < n; k++) sum += Eval (k); return 2.0 * sum / n + n ; } } Show that the number of double arithmetic operations performed as a function of (n) is: ) ( O ) n ( T n n 2 1 2   
  • 25. Prof. Amr Goneid, AUC 25 Solution 1 1 2 1 1 1 2 1 3 0 0 3 1 2 0 1 0 1 0                             ) n ( T ) n ( T ) n ( T ) n ( T ) n ( T ) n ( ) k ( T ) n ( T ) n ( ) k ( T ) n ( T ) ( T with } ) k ( T { ) n ( T n k n k n k
  • 26. Prof. Amr Goneid, AUC 26 Solution ) ( O b a b ) n ( T ) ( T , b , a n n n i i n i i n n n i j j n i i i i 2 1 2 2 1 2 0 0 1 2 1 0 1 1 1 1 1                        
  • 27. Prof. Amr Goneid, AUC 27 Exercise (3): Hanoi Towers Game Find the number of moves required for a Tower of Hanoi with n discs. An animation is available at: http://www.cosc.canterbury.ac.nz/people/mukundan/dsal /ToHdb.html
  • 28. Prof. Amr Goneid, AUC 28 5. 2nd Order Linear Recurrence Example: Fibonacci Sequence F(n) = F(n-1) + F(n-2) , F(0) = 0 F(1) = 1 Let to obtain the characteristic equation Solutions: n n n n n F(n) r r r , F( n ) With F( ) and F( ) then and F( n ) , Golden Ratio .                                 2 1 0 1 5 1 5 1 2 2 1 0 0 1 1 5 1 618034 5