05-Recursion02 - Ch Chapter 3: Recursion This material is...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 3: Recursion This material is based on contents the book: “Data structures, Algorithms, and Software Principles in C”, from T.A. Standish. Parts also under permission from David McCaughan, University of Guelph, Canada. USF – COP4530 – Data Structures Summer 2009 1of21 a d cCaug a , U e s ty o Gue p , Ca ada
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Fibonacci numbers Is the following sequence of numbers – 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, … First two Fibonacci numbers: – 0, 1. Each other number is the sum of the previous two How can the recursive function be defined? – First, the mathematical definition eases thinking i f ( n < = 1 ) Fibonacci(n) = Fibonacci (n-1) + Fibonacci(n-2) otherwise n if (n <= 1) USF – COP4530 – Data Structures Summer 2009 2of21
Background image of page 2
Fibonacci numbers How is the call tree of this function? int Fibonacci (int n){ f (n <= 1) { Now The recursive function if (n <= 1) { return n; } else { return Fibonacci(n-1) + Fibonacci(n-2) } } USF – COP4530 – Data Structures Summer 2009 3of21
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pascal’s triangle is the coefficient of the x k term in the polynomial expansion of the binomial power k n (1+x) n ascal’s Triangle: Pascal s Triangle: – A geometric arrangement of binomial coefficients idely use in Algebra Probability Widely use in Algebra, Probability, … 1 21 1 2 1 1 3 3 1 1 4 6 4 1 5 10 10 5 1 USF – COP4530 – Data Structures Summer 2009 4of21 1 5 10 10 5 1
Background image of page 4
Pascal’s triangle Again the mathematical function eases thinking + = k n k n k n 1 1 1 For the recursive implementation, two pproaches are possible approaches are possible – One will be shown here USF – COP4530 – Data Structures Summer 2009 5of21
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Pascal’s triangle Pascal’s triangle – first approach first approach n\k 0 1 2 3 4 5 6 0 1 1 1 1 2 1 2 1 3 1 3 3 1 4 1 4 6 4 1 5 10 10 5 1 5 1 5 10 10 5 6 1 6 15 20 15 6 1 The recursive function int pascalT(int n, int k){ //2 Conditions in one for the base case if((k == 0) || (n == k)){ return 1; } else { // 2 function calls return (pascalT(n-1, k) + pascalT(n-1, k-1)); USF – COP4530 – Data Structures Summer 2009 6of21 } }
Background image of page 6
Quantitative Aspects of recursions Recursive solution of Towers of Hanoi – Move disks from one peg to another peg
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/28/2009 for the course COP COP 4350 taught by Professor Jimeno during the Summer '09 term at University of South Florida - Tampa.

Page1 / 21

05-Recursion02 - Ch Chapter 3: Recursion This material is...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online