




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
Material Type: Notes; Professor: Karki; Class: ADV DATA STRUCTURES; Subject: Computer Science; University: Louisiana State University; Term: Unknown 1989;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Recursive algorithm invokes (makes reference to) itself repeatedly until a certain condition matches Examples: Computing factorial function Tower of Hanoi puzzle Digits in binary representation Non-recursive algorithm is executed only once to solve the problem.
Definition of factorial function F ( n )= n !: n****! = 1 • 2 • … • ( n- 1) • n 0! = 1 Recurrence relation for the number of multiplications : M ( n ) = M ( n -1) + 1 for n > 0 M (0) = 0 initial condition Solve the recurrence relation using the method of backward substitution: M ( n ) = M ( n -1) + 1 = M ( n -2) + 2 = M ( n -3) +3 = ….. = M ( n - i ) + i =…. = M ( n - n ) + n M ( n ) = n Algorithm F ( n ) //Compute n! recursively //Input: A nonnegative integer n //Output: The value of n! if n = 0 return 1 else return F( n-1) * n
Given: n disks of different sizes and three pegs. Initially all disks are on the first peg in order of size, the largest being on the bottom Problem: move all the disks to the third peg, using the second one as an auxiliary. One can move only one disk at a time, and it is forbidden to place a larger disk on the top of a smaller one. Recursive solution: Three steps involved are First move recursively n - 1 disks from peg 1 to peg 2 (with peg 3 as auxiliary) Move the largest disk directly from peg 1 to peg 3 Move recursively n - 1 disks from peg 2 and peg 3 (using peg 1 as auxiliary) Peg (^1) Peg 2 Peg 3 http://www.mazeworks.com/hanoi/
Problem: Investigate a recursive version of binary algorithm, which finds the number of binary digits in the binary representation of a positive decimal integer n. Recursive relation for the number of additions made by the algorithm. A ( n ) = A n /2 + 1 for n > 1 A (1) = 0 initial condition For n = 1, no addition is made because no recursive call is executed. Algorithm BinRec ( n ) //Input: A positive decimal integer n //Output: The number of binary digits in n ’s binary representation if n = 1 return 1 else return BinRec ( n/2 ) +
Use backward substitution method to solve the problem only for n = 2 k****. Smoothness rule implies that the observed order of growth is valid for all values of n. A (2 k ) = A (2 k-^1 ) + 1 = [ A (2 k-^2 ) + 1] + 1 = A (2 k-^2 ) + 2 = [ A (2 k-^3 ) + 1] + 2 = A (2 k-^3 ) + 3 …… = A (2 k-i ) + i …… = A (2 k-k ) + k = k A (2^0 ) = A (1) = 0 = log 2 n n = 2 k A ( n ) = log 2 n ∈ Θ (log n ) Exact solution is A ( n ) = log 2 n