04-recurrences-divconq.ppt

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

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

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

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

View Full Document
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”
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)

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

View Full Document
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…
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.

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

View Full Document
Towers of Hanoi • Ah, the legend: • 64 golden disks • Those diligent priests • The world ends!

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

View Full Document
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!
• 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.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

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

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

View Full Document
Ask a homework question - tutors are online