


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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