Académique Documents
Professionnel Documents
Culture Documents
• Performance of Algorithms
• Performance Prediction (Order of Algorithms)
Data Structures – Week #2 • Examples
• Exercises
Algorithm Analysis • Recursion
& • Recurrences
Recursion
November 11, 2005 Borahan Tümer, Ph.D. 7 November 11, 2005 Borahan Tümer, Ph.D. 8
whether or not their index bounds are related for (i=0; i<=n; i++)
to the input size. for (j=0; j<=n; j++)
statement block;
• Assume n is the number of input records
for (i=0; i<=n; i++)
• for (i=0; i<=n; i++) {statement block}, O(?) for (j=0; j<=i; j++)
• for (i=0; i<=m; i++) {statement block}, O(?) statement block;
Exercises Recursion
Find the number of times the statement block is executed! Definition:
for (i=0; i<=n; i++)
for (j=0; j<=i; j*=2)
Recursion is a mathematical concept referring to
statement block; programs or functions calling or using itself.
November 11, 2005 Borahan Tümer, Ph.D. 11 November 11, 2005 Borahan Tümer, Ph.D. 12
Recursion Recursion… cont’d
Concept: • Base case: the simplest version of the problem
• A recursive function divides the problem into that is not further reducable. The function
two conceptual pieces: actually knows how to solve this version of the
– a piece that the function knows how to solve (base problem.
case),
– a piece that is very similar to the original problem, • To make the recursion feasible, the latter piece
hence still unknown how to solve by the function must be slightly simpler.
(call(s) of the function to itself).
November 11, 2005 Borahan Tümer, Ph.D. 13 November 11, 2005 Borahan Tümer, Ph.D. 14
November 11, 2005 Borahan Tümer, Ph.D. 17 November 11, 2005 Borahan Tümer, Ph.D. 18
Function Call in MM… cont’d Towers of Hanoi… cont’d
Example: Hanoi(4,i,j)
413
312 323
• Following problems arise: 213 221
123
112
– how to keep the return address from the function called 013 021
November 11, 2005 Borahan Tümer, Ph.D. 19 November 11, 2005 Borahan Tümer, Ph.D. 20
November 11, 2005 Borahan Tümer, Ph.D. 21 November 11, 2005 Borahan Tümer, Ph.D. 22
November 11, 2005 Borahan Tümer, Ph.D. 25 November 11, 2005 Borahan Tümer, Ph.D. 26
k
tn ¦c n
i 1
i
i 1 n
r
November 11, 2005 Borahan Tümer, Ph.D. 27 November 11, 2005 Borahan Tümer, Ph.D. 28
November 11, 2005 Borahan Tümer, Ph.D. 29 November 11, 2005 Borahan Tümer, Ph.D. 30
Examples Examples
Homogeneous Recurrences Homogeneous Recurrence
Example 1. Example 2.
tn + 5tn-1 + 4 tn-2 = 0; sol’ns of the form tn = xn
tn-6 tn-1+12tn-2-8tn-3=0; tn = xn
xn + 5xn-1+ 4xn-2 = 0; n-2 trivial sol’ns
xn-6xn-1+12xn-2-8xn-3= 0; n-3 trivial sol’ns
(x2+5x+4) = 0; characteristic equation (CE)
x1=-1; x2=-4; nontrivial sol’ns
CE: (x3-6x2+12x-8) = (x-2)3= 0; by polynomial division
tn = c1(-1)n+ c2(-4)n ; general sol’n x1= x2= x3 = 2; roots not distinct!!!
tn = c12n+ c2n2n + c3n22n; general sol’n
November 11, 2005 Borahan Tümer, Ph.D. 31 November 11, 2005 Borahan Tümer, Ph.D. 32
Examples Examples
Homogeneous Recurrence Example 3… cont’d
Example 3. We use as many ti values
tn = tn-1+ tn-2; Fibonacci Series
as ci
xn-xn-1-xn-2 = 0; CE: x2-x-1 = 0; 0 0
§1 5 · §1 5 ·
1 r 5 ; distinct roots!!! t0 0 c1 ¨¨ ¸ c2 ¨
¸ ¨ 2 ¸
¸ c1 c2 0 c1 c2
x1, 2 © 2 ¹ © ¹
2 n n 1 1
§1 5 · §1 5 · t1
§1 5 ·
1 c1 ¨¨
§1 5 ·
¸ c2 ¨ ¸
§1 5 ·
c1 ¨¨
§1 5 ·
¸ c1 ¨ ¸
¨ 2 ¸ c1
1
, c2
1
tn c1 ¨¨ ¸ c2 ¨
¸ ¨ 2 ¸
¸ © 2 ¹
¸ ¨ 2 ¸
© ¹ © 2 ¹
¸
© ¹ 5 5
© 2 ¹ ; general sol’n!!
© ¹
We find coefficients ci using initial values t0 and t1 of Check it out using t2!!!
Fibonacci series on the next slide!!! n n
1 §1 5 · 1 §1 5 ·
tn ¨ ¸ ¨ ¸
5 ¨© 2 ¸¹ 5 ¨© 2 ¸¹
November 11, 2005 Borahan Tümer, Ph.D. 33 November 11, 2005 Borahan Tümer, Ph.D. 34
Examples Examples
Example 3… cont’d Example 4.
tn = 2tn-1 - 2tn-2; n t 2; t0 = 0; t1 = 1;
What do n and tn represent? CE: x2-2x+2 = 0;
Complex roots: x1,2=1ri
n is the location and tn the value of any Fibonacci number in the series.
What is the meaning of tn in recursive function As in differential equations, we represent the complex roots as a
fib(n) on page 22 ? vector in polar coordinates by a combination of a real radius r
and a complex argument T:
tn is the number of times the condition in the if structure (chosen as a barometer) is checked in fib(n).
z=r*eT i;
Here,
1+i=2 * e(S/4)i
1-i=2 * e(-S/4)i
November 11, 2005 Borahan Tümer, Ph.D. 35 November 11, 2005 Borahan Tümer, Ph.D. 36
Examples Examples
Example 4… cont’d Inhomogeneous Recurrences
Solution: Example 1. (From Example 3)
tn = c1 (2)n/2 e(nS/4)i + c2 (2)n/2 e(-nS/4)i; We would like to know how many times fib(n)
From initial values t0 = 0, t1 = 1, on page 22 is executed in terms of n. To find out:
tn = 2n/2 sin(nS/4); (prove that!!!) 1. choose a barometer in fib(n);
2. devise a formula to count up the number of
times the barometer is excuted.
November 11, 2005 Borahan Tümer, Ph.D. 37 November 11, 2005 Borahan Tümer, Ph.D. 38
Examples Examples
Example 1… cont’d Example 1… cont’d
In fib(n), the only statement is the if statement. tn - tn-1 - tn-2 = 1; inhomogeneous recurrence
Hence, if condition is chosen as the barometer. The homogeneous part comes directly from
Suppose fib(n) takes tn time units to execute, Fibonacci Series example on page 33.
where the barometer takes one time unit and the RHS of recurrence is 1 which can be expressed
function calls fib(n-1) and fib(n-2), tn-1 and tn-2, as 1nx0. Then, from the equation on page 29,
respectively. Hence, the recurrence to solve is CE: (x2-x-1)(x-1) = 0; from page 30,
n n
§1 5 · §1 5 ·
tn = tn-1 + tn-2 + 1 tn c1 ¨¨ ¸ c2 ¨
¸
¸
¨ 2 ¸ c31
n
© 2 ¹ © ¹
November 11, 2005 Borahan Tümer, Ph.D. 39 November 11, 2005 Borahan Tümer, Ph.D. 40
Examples Examples
Example 1… cont’d Example 1… cont’d
n n
§1 5 · §1 5 · n
§ 1 5 · §1 5 ·
n
tn c1 ¨¨ ¸ c2 ¨
¸
¸
¨ 2 ¸ c3
tn c1 ¨¨ ¸ c2 ¨
¸
¸
¨ 2 ¸ c3 ; t0 t1 1, t 2 3
© 2 ¹ © ¹
© 2 ¹ © ¹
5 1 5 1
Now, we have to find c1,…,c3. c1 ; c2 ; c3 1
5 5
n n
Initial values: for both n=0 and n=1, if condition is ª 5 1º § 1 5 · ª 5 1º § 1 5 ·
tn « »¨¨ ¸ «
¸ »¨¨ ¸ 1;
¸
¬ 5 ¼© 2 ¹ ¬ 5 ¼© 2 ¹
checked once and no recursive calls are done.
For n=2, if condition is checked once and recursive Here, tn provides the number of times the
calls fib(1) and fib(0) are done. barometer is executed in terms of n. Practically,
this number also gives the number of times fib(n) is
t0 = t1 = 1 and t2 = t0 + t1 + 1 = 3.
called.
November 11, 2005 Borahan Tümer, Ph.D. 41 November 11, 2005 Borahan Tümer, Ph.D. 42