lecture26 - Lecture 26 Recursion, Counting Recap Induction...

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

View Full Document Right Arrow Icon
Lecture 26 Recursion, Counting
Background image of page 1

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

View Full DocumentRight Arrow Icon
Recap Induction Strong Induction Recursive structures, and structural induction
Background image of page 2
Complete Binary Trees Basis: a single vertex is a complete binary tree Recursive step: if T 1 and T 2 are complete binary trees, then the tree T 1 T 2 obtained by connecting a root r to the roots of T 1 and T 2 is also a complete binary tree r
Background image of page 3

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

View Full DocumentRight Arrow Icon
Prove or Disprove A tree with n vertices has n-1 edges
Background image of page 4
Recursive Algorithms An algorithm is called recursive if it solves a problem by reducing it to a smaller instance of the same problem.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Computing n! Procedure factorial(n) If n=0, return 1 Else return n* factorial(n-1)
Background image of page 6
Computing GCD gcd(a,b) /* assumption a < b */ If a=0, then return b Else return gcd(b mod a, a)
Background image of page 7

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

View Full DocumentRight Arrow Icon
Proving Correctness Correctness of recursive algorithms can be usually proven by induction. To prove that factorial(n) is correct: Induction on n Basis n = 0 ; factorial(0) returns 1, the correct answer. Inductive step: Suppose factorial(k) returns the correct answer: factorial(k+1) = (k+1)*factorial(k) = (k+1)*k! By the Ind. Hyp = (k+1)!
Background image of page 8
Fibonnaci(n) If n = 0, return 0 Else If n = 1, return 1 Else return Fibonacci(n-1) + Fibonacci(n-2)
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 / 28

lecture26 - Lecture 26 Recursion, Counting Recap Induction...

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