


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An introduction to recursion in both mathematics and programming, focusing on recursive mathematical equations called recurrences. The concept of recursion, the importance of solving recurrences, and methods for solving them, including forward and backward substitution. Examples and explanations are given to help illustrate the concepts.
Typology: Study notes
1 / 34
This page cannot be seen from the preview
Don't miss anything!
-^ Recursion: When^ a^ function (or
procedure) calls^ itself.^ ‐^ The
Free^ On‐line^ Dictionary
of^ Computing
-^ applies^ to^ both
mathematics^ and
programming
-^ Recursive^ mathematical
equations^ are
called
recurrence^ equations
,^ recurrence^ relations
,^ or
simply^ recurrences
-^ Recurrences
naturally^ occur
in^ the^ analysis
of
recursive^ algorithms.
to^ find^ an^ explicit (non‐recursive)
formula^ for^ x(n),
or^ to^ prove the^ no^ such^ sequence
exists
-^ In^ algorithm
analysis^ we^ need
this^ solution to^ compare^ functions
or to^ plot^ functions – Recurrences^ can
be^ expensive^ to
Appendix B compute^ directly
-^ Start^ from^
the^ initial^ condition
-^ Substitute^
into^ recurrence
-^ Repeat^ until
you^ can^ see^
a^ pattern
-^ x(n)^ =^ 2x(n
‐1)^ +^ 1,^ for^ n^
x(1)^ =^1 • x(1)^ =^^1 x(2)^ =^ 2x(1)^ +
x(3)^ =^ 2x(2)^ +
x(4)^ =^ 2x(3)^ +
x(5)^ =^ 2x(4)^ +
-^ Pattern:^ x(n)
n^ = 2 – 1,^ for^ n=1^ … 5 Still^ need^ to^ prove
that^ it^ is^ true
for^ n>
-^ x(n)^ =^ 2x(n
‐1)^ +^ 1,^ for^ n^
x(1)^ =^1 n^ • x(n)^ =^2 – 1,^
for^ n=1^ … 5
-^ proof^ (by^ substitution
into^ recurrence): x(n)^ =^ 2x(n‐1)
n‐^1 + 1 = 2*(
n^ = (2– 2)^ +^1 n^ = 2 – 1
-^ proof^ (by^ induction):n+1x(n+1)^ =^2
n^ – 1^ =^ 2*(2^ )^ ‐
n 2 + 1 = 2*(
=^ 2x(n)^ +^1
-^ x(n)^ =^ x(n‐1)
+^ n,^ for^ n^ >^1 x(0) = 0
-^ x(n‐1)^ =
x(n‐2)^ +^ (n‐1),
sub^ into^ x(n): x(n)^ =^ (x(n‐2)
+^ (n‐1))^ +^ n = x(n‐2) +^ (n‐1)^ +^ n x(n‐2) = x(n‐3)^ +^ (n‐2),^
sub^ into^ previous: x(n)^ =^ (x(n‐3)
+^ (n‐2))^ +^ (n‐
1)^ +^ n =^ x(n‐3)^ +^ (n‐
2)^ +^ (n‐1)^ +^ n
-^ Pattern?
-^ x(n)^ =^ x(n‐2)
+^ (n‐1)^ +^ n x(n) = x(n‐3) +^ (n‐2)^ +^ (n‐1)
+^ n
-^ Pattern?^ x(n)^ =^ x(n‐i)
+^ (n‐i+1)^ +^ (n
‐i+2)^ +^ … +^ n when^ n^ =^ i,^ n
‐i =^ 0,^ so^ we^ can
use^ initial condition:x(n)^ =^ x(0)^ +^1
+^2 +^ … +^ n^ =
n
) (^1) ( + nn 2
-^ Decrease‐by
‐constant^ algorithm
use^ the solution^ to^ an
n/b^ size^ problem
to^ solve an^ n^ size^ problem
(typically^ b =2)
-^ Typical^ efficiency
recurrence: T ( n )^ =^ T ( n / b )^
+^ f ( n )
-^ Backward^ substitution
yields: k T ( b )^ =^ T(1)^ +
-^ Divide‐and
‐conquer^ algorithms
divide^ the problem^ into
several^ smaller
problems,^ which are^ solved^ recursively,
then^ combines
the solutions • Typical^ efficiency
recurrence: T ( n )^ =^ aT ( n / b
)^ +^ f ( n )
-^ Backward^ substitution
yields: T ( n )^ =^
]/)( loglog ) (^1) ([^1 n i a^ i i
abf
-^ Give^ a^ recursive
algorithm^ to
compute^ the length^ of^ a^ linked
list base^ case:^ empty^ list
has^ length^ zero recursive^ case:^1 +^ length
of^ list^ with^ head
removed
-^ Give^ a^ recursive
algorithm^ to
compute^ the length^ of^ a^ linked
list int length(Node*
head) { if^ (head^ ==
NULL)^ return
return^1 +^ length(head
‐>next); }
-^ Give^ a^ recursive
algorithm^ to
compute^ the sum^ of^ the^ elements
in^ linked^ list double^ sum(Node*
head) { if^ (head^ ==
NULL)^ return
return^ head‐
value^ +^ sum(head
‐>next); }
-^ Give^ a^ recursive
algorithm^ to
count^ the number^ of^ odd
elements^ in^
a^ linked^ list base^ case:empty^ list^ has^0 odd^ elements recursive^ case:if^ head^ node
element^ is^ odd: 1 + number^ of^ odd^ elements in list with^ head^ removedelse: number of odd^ elements in list with^ head^ removed