04-recurrences-divconq.ppt

04-recurrences-divconq.ppt - CS 4102, Algorithms:...

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

View Full Document Right Arrow Icon
CS 4102, Algorithms: Recurrences, D & C • First design strategy: Divide and Conquer • Examples… • Recursive algorithms • Counting basic operations in recursive algorithms: Solving recurrence relations • By iteration method • Recursion trees (quick view) • The “Main” and “Master” Theorems • Mergesort • Trominos
Background image of page 1

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

View Full DocumentRight Arrow Icon
Recursion: Basic Concepts and Review • Recursive definitions in mathematics • Factorial: n! = n (n-1)! and 0! = 1! = 1 • Fibonacci numbers: F(0) = F(1) = 1 F(n) = F(n-1) + F(n-2) for n > 1 • Note base case • In programming, recursive functions can be implemented • First, check for simple solutions and solve directly • Then, solve simpler subproblem(s) by calling same function • Must make progress towards base cases • Design strategy: method99 “mental trick”
Background image of page 2
Designing Recursive Procedures • Think Inductively! • converging to a base case (stopping the recursion) • identify some unit of measure (running variable) • identify base cases • How to solve p for all inputs from size 0 through 100 • Assume method99 solves sub-problem all sizes 0 through 99 • if p detect a case that is not base case it calls works and is called when: 1. The sub-problem size is less than p’s problem size 2. The sub-problem size is not below the base case 3. The sub-problem satisfies all other preconditions of (which are the same as the preconditions of p)
Background image of page 3

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

View Full DocumentRight Arrow Icon
Recursion: Good or Evil? • It depends… • Sometimes recursion is an efficient design strategy, sometimes not • Important! we can define recursively and implement non-recursively • Note that many recursive algorithms can be re- written non-recursively • Use an explicit stack • Remove tail-recursion (compilers often do this for you) • Consider: factorial, binary search, Fibonacci • Let’s consider Fibonacci carefully…
Background image of page 4
Implement Fibonacci numbers • It’s beautiful code, no? long fib(int n) { assert(n >= 0); if ( n == 0 ) return 1; if ( n == 1 ) return 1; return fib(n-1) + fib(n-2); } • Let’s run and time it. • Let’s trace it.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Towers of Hanoi • Ah, the legend: • 64 golden disks • Those diligent priests • The world ends!
Background image of page 7

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

View Full DocumentRight Arrow Icon
Towers of Hanoi • Back in the commercial Western world… • Game invented by the French mathematician, Edouard Lucas, in 1883. • Now, for only $19.95, call now!
Background image of page 8
• Write a recursive function for the Towers of Hanoi. • Number each peg: 1, 2, 3 • Function signature: hanoi ( n, source, dest, aux) where: n is number of disks (from the top), and other parameters are peg values In function body print: Move a disk from <peg> to <peg> • Do this in pairs. Then pairs group and compare. Find bugs, issues, etc.
Background image of page 9

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

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

Page1 / 31

04-recurrences-divconq.ppt - CS 4102, Algorithms:...

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

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