1 / 47

University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter. Slides adapted from Michael P. Frank’s Course Based on the Text Discrete Mathematics & Its Applications (5 th Edition) by Kenneth H. Rosen. Module #16: Recursion. Rosen 5 th ed., §§3.4-3.5

tamar
Download Presentation

University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. University of Aberdeen, Computing ScienceCS3511Discrete MethodsKees van Deemter Slides adapted from Michael P. Frank’s Course Based on the TextDiscrete Mathematics & Its Applications(5th Edition)by Kenneth H. Rosen (c)2001-2003, Michael P. Frank

  2. Module #16:Recursion Rosen 5th ed., §§3.4-3.5 ~34 slides, ~2 lectures (c)2001-2003, Michael P. Frank

  3. NB: This material was shortened compared to Rosen, largely omitting algorithmic aspects. • Reason: algorithms & complexity will be taught in CS4018 (Models of Computation) (c)2001-2003, Michael P. Frank

  4. §3.4: Recursive Definitions • In induction, we prove that all members of an infinite set satisfy some predicate P by: • proving the truth of the predicate for larger members based on that for smaller members. • In recursive definitions, we similarly define a function, a predicate, a set, or a more complex structure over an infinite domain (universe of discourse) by: • defining the function, predicate value, set membership, or structure of larger elements in terms of those of smaller ones. • In structural induction, we inductively prove properties of recursively-defined objects in a way that parallels the objects’ own recursive definitions. (c)2001-2003, Michael P. Frank

  5. Recursion • Recursion is the general term for the practice of defining an object in terms of itself (or of part of itself) • This may seem circular, but it isn’t necessarily. • There are recursive algorithms, recursively defined functions, relations, sets, etc. (c)2001-2003, Michael P. Frank

  6. Recursively Defined Functions • Simplest case: One way to define a function f:NS (for any set S) or series an=f(n) is to: • Define f(0). • For n>0, define f(n) in terms of f(0),…,f(n−1). • E.g.: Define the series an :≡2n precisely (instead of writing “1,2,4,8,…”): • Let a0 :≡1. • For n>0, let an:≡2an-1. (c)2001-2003, Michael P. Frank

  7. Another Example • Suppose we define f(n) for all nN recursively by: • Let f(0)=3 • For all nN, letf(n+1)=2f(n)+3 • What are the values of the following? • f(1)= f(2)= f(3)= f(4)= 21 9 45 93 (c)2001-2003, Michael P. Frank

  8. Spurious examples • It can take close attention to see whether a set of equations constitute a recursive definition. E.g., consider • f(0)= 0 , f(n)= 2(f(n-2)) for n>=1 • f(0)=f(1)-1f(n)= 2(f(n-1)) for n>=1 (c)2001-2003, Michael P. Frank

  9. Recursive definition of Factorial • Give an inductive (recursive) definition of the factorial function,F(n) :≡n! :≡ ∏1≤i≤n i = 12…n. (c)2001-2003, Michael P. Frank

  10. Recursive definition of Factorial • Give an inductive (recursive) definition of the factorial function,F(n) :≡n! :≡ ∏1≤i≤n i = 12…n. • Base case of definition: F(1) :≡ 1 • Recursive part: F(n) :≡n  F(n−1). • F(2)=2 • F(3)=6 (c)2001-2003, Michael P. Frank

  11. More Easy Examples • Recursive definitions exist for: i+n (i integer, n natural) using only s(i) = i+1. a·n (a real, n natural) using only addition an(a real, n natural) using only multiplication (c)2001-2003, Michael P. Frank

  12. The Fibonacci Series • The Fibonacci seriesfn≥0 is a famous series defined by:f0 :≡ 0, f1 :≡ 1, fn≥2 :≡ fn−1 + fn−2 0 1 1 2 3 5 8 13 Leonardo Fibonacci1170-1250 (c)2001-2003, Michael P. Frank

  13. An upper bound on Fib. series • Theorem: fn < 2n. • Proof: By induction. Base cases: f0 = 0 < 20 = 1f1 = 1 < 21 = 2 Inductive step: Use 2nd principle of induction (strong induction). Assume k<n, fk < 2k. Then fn = fn−1 + fn−2 is (because of IH)< 2n−1 + 2n−2 < 2n−1 + 2n−1 = 2. 2n−1 =2n. ■ Implicitly for all nN Note use ofbase cases ofrecursive def’n. (c)2001-2003, Michael P. Frank

  14. (A lower bound on Fibonacci series • Theorem. For all integers n≥ 3, fn > αn−2, where α = (1+51/2)/2 ≈ 1.61803. • Proof. (Using strong induction.) • Let P(n) = (fn > αn−2). • Base cases: For n=3, note thatα < 2 = f3. For n=4, α2 = (1+2·51/2+5)/4 = (3+51/2)/2 ≈ 2.61803 < 3 = f4. • Inductive step: For k≥4, assume P(j) for 3≤j≤k, prove P(k+1). Note α2 = α+1. Thus, αk−1 = (α+1)αk−3 = αk−2 + αk−3. By inductive hypothesis, fk−1> αk−3 and fk > αk−2. So, fk+1 = fk + fk−1 > αk−2 + αk−3 = αk−1. Thus P(k+1). ■) (c)2001-2003, Michael P. Frank

  15. Recursively Defined Sets • An infinite setS may be defined recursively, by giving: • A finite set of base elements of S. • A rule for constructing new elements of S from previously-established elements. • Implicitly, S has no other elements than these. • Example: Let 3S, and let x+yS if x,yS. What is S? (c)2001-2003, Michael P. Frank

  16. Given an alphabet Σ, what is Σ*? ε Σ* (ε :≡ “”, the empty string) w Σ*  x  Σ → wx  Σ* Bookuses λ (c)2001-2003, Michael P. Frank

  17. The Set of All Strings • Given an alphabet Σ, the set Σ* of all strings over Σ can be recursively defined by:ε Σ* (ε :≡ “”, the empty string) w Σ*  x  Σ → wx  Σ* • Exercise: Prove that this definition is equivalent to Bookuses λ (c)2001-2003, Michael P. Frank

  18. Other Easy String Examples • Give recursive definitions for: • The concatenation of strings w1w2. • The length (w) of a string w. • Well-formed formulae of propositional logic involving T, F, propositional variables, and operators in {¬, , , →, ↔}. • Well-formed arithmetic formulae involving variables, numerals, and ops in {+, −, *}. (c)2001-2003, Michael P. Frank

  19. Flashback: The language of propositional logic defined • Atoms: p1, p2, p3, .. • Formulas: • All atoms are formulas • If  is a formula then ¬  is a formula • If  and  are formulas then (  ) is a formula • (Recall that negation and disjunction form a functionally complete set of connectives) (c)2001-2003, Michael P. Frank

  20. Proofs about formulas • This was a recursive definition of the concept ‘formula’ • It can be used to prove theorems using an induction that follows the structure of the definition. E.g., • Theorem: “Every formula containing  contains brackets around the arguments of any occurrence of  ” (c)2001-2003, Michael P. Frank

  21. Proofs about formulas • Theorem: “Every formula containing  contains brackets around the arguments of any occurrence of  ” • Define complexity of a formula as the number of rule applications needed to construct the formula • E.g., complexity(p0)=1 complexity(¬ p0) =2 complexity (p0¬p7) =4 (c)2001-2003, Michael P. Frank

  22. Complexity defined recursively • If  is an atom then complexity()=1 • If  =  then complexity()=complexity()+1 • If  = () then complexity()=complexity()+complexity()+1 (c)2001-2003, Michael P. Frank

  23. Proofs about formulas • Theorem: “Every formula  containing  contains brackets around its arguments” • Proof using complexity of : • Basis step: Complexity()=1. This means that  is a proposition letter. Hence, it contains no  and the property holds for  automatically (i.e. trivially). (c)2001-2003, Michael P. Frank

  24. Proofs about formulas • Inductive step: IH=“property holds when complexity()<n”. Consider formula  whose complexity is n. The first case: • =, where complexity()<nIt follows by IH that the property holds for .Consequently, it also holds for =(because  contains the same disjunctions, disjuncts, and brackets as ). (c)2001-2003, Michael P. Frank

  25. Proofs about formulas • Inductive step: IH=“property holds when complexity()<n”. Consider formula  whose complexity is n. The second case: 2. =(), where complexity() and complexity() < n. It follows by IH that the property holds for  and . It must then also hold for =(), because no brackets are removed from around arguments of  . (c)2001-2003, Michael P. Frank

  26. Proofs about formulas • Why did we use the second (i.e., strong) principle of mathematical induction, instead of the first principle? (c)2001-2003, Michael P. Frank

  27. Proofs about formulas • Why did we use the second (i.e., strong) principle of mathematical induction, instead of the first principle? Recall • =(), where complexity() and complexity() < n. It follows by IH that the property holds for  and . It must then also hold for =(). We don’t know that complexity() = complexity() = n1. (In fact, complexity() + complexity() = n1, and neither is equal to 0 ) (c)2001-2003, Michael P. Frank

  28. Structural Induction • We have seen that normal induction (1st or 2nd principle) applies to recursively defined objects • But we can also use a more dedicated principle, called structural induction : • Basis step: show that proposition holds for all elements specified in basis step of the recursive definition • Recursive step: show that IF proposition holds for each of the elements used for constructing new elements in the recursive step of the definition, THEN the proposition holds for these new elements (c)2001-2003, Michael P. Frank

  29. Structural induction applied to earlier example • Theorem: “Every formula containing  contains brackets around the arguments of any occurrence of  ” • Basis step: proposition holds for ‘basis elements’, since these are atoms. • Recursive step: suppose prop. holds for  and . Prove that it holds for ¬  and for (  ). (Etc.) (c)2001-2003, Michael P. Frank

  30. Structural Induction: • Proving something about a recursively defined object using an inductive proof whose structure mirrors the object’s definition. • Example problem: Let 3S, and let x+yS if x,yS. (Nothing else is an element of S.) Prove: S = 3,6,9,12, ... = {nZ+| (3|n)}. (c)2001-2003, Michael P. Frank

  31. Example continued • Let 3S, and let x+yS if x,yS. Let A = {nZ+| (3|n)}. • Theorem:A=S. Proof: We show that AS and SA. • To show AS, show [nZ+  (3|n)]→ nS. • Inductive proof. Let P(n) :≡ nS. Induction over positive multiples of 3. Base case: n=3, thus 3S by def’n. of S. Inductive step:nS (Induction Hyp);we also have3S, so by definition of S, it follows that n+3S. • To show SA: let nS, show nA. • Structural inductive proof. Let P(n):≡nA. Two cases: n=3 (base case), which is in A, or n=x+y (recursive step). For the recursive step, suppose xA and yA, that is, 3|x and 3|y. It follows that3|(x+y).(For example,if x=3a and y=3b then x+y=3(a+b).)Thus x+y  A. (c)2001-2003, Michael P. Frank

  32. Recursive Algorithms (§3.5) • An algorithm is recursive if it solves a problem by reducing it to an instance of the same problem with a smaller input. • Example: A procedure to compute an. procedurepower(a≠0: real, nN) ifn = 0 then return 1elsereturna · power(a, n−1) (c)2001-2003, Michael P. Frank

  33. Recursive Algorithms (§3.5) procedurepower(2, 3) return2 · power(2, 3−1) return2 · power(2, 3−2) return2 · power(2, 3−3)=1 This calculates that 23 = 2.2.2.1 = 8. (c)2001-2003, Michael P. Frank

  34. Recursive Algorithms (§3.5) • This is just an algorithmic form of a recursively defined function: a0 = 1 an+1 = a.an (c)2001-2003, Michael P. Frank

  35. Efficiency of Recursive Algorithms • Recursive algorithms can be slow • The time complexity of a recursive algorithm may depend critically on the number of recursive calls it makes. (c)2001-2003, Michael P. Frank

  36. Recursive Fibonacci Algorithm procedurefibonacci(n N)ifn=0 return 0ifn=1 return 1returnfibonacci(n−1)+fibonacci(n−2) • Is this an efficient algorithm? • How many additions are performed? (c)2001-2003, Michael P. Frank

  37. Example: computing fibonacci(4) f(4) f(3) f(2) f(2) f(1) f(1) f(0) f(1) f(0) • The problem: the same values are computed again and again. (E.g., f(1) is computed three times) (c)2001-2003, Michael P. Frank

  38. Analysis of Fibonacci Procedure • Theorem: The preceding procedure for fibonacci(n) performs fn+1−1 addition operations. • Proof: By strong structural induction over n, based on the procedure’s own recursive definition. • Base cases:fibonacci(0) performs 0 additions, and f0+1−1 = 1 − 1 = 0. Likewise, fibonacci(1) performs 0 additions, and f1+1−1 = 1−1 = 0. • Inductive step: For n>1, by strong inductive hypothesis, fibonacci(n−1) and fibonacci(n−2) perform fn−1 and fn−1−1 additions respectively. Now fibonacci(n) adds 1 more, that is (fn−1)+ (fn−1−1)+1 = fn+fn−11 =(def of fib)= fn+11. ■ (c)2001-2003, Michael P. Frank

  39. The latter was a sneak preview of next’s year’s Models of Computation course: • Complexity: • “How long does a given algorithm take?”[This is where induction can be used] • “What’s the fastest possible algorithm for a given problem?” • Computability (“How do I know whether a problem can be solved at all using an algorithm?”) (c)2001-2003, Michael P. Frank

  40. Some types of structures that are naturally defined using recursion: • Rooted trees • (Extended Binary Trees) • (Full Binary Trees) (c)2001-2003, Michael P. Frank

  41. Rooted Trees • Trees are covered in more depth in chapter 9. • Briefly, a tree is a graph in which there is exactly one undirected path between each pair of nodes. (c)2001-2003, Michael P. Frank

  42. Rooted Trees • Recursive definition of the set of (rooted) trees: • Basis step: Any single node r is a tree with root r. • Recursive step: If T1, …, Tn are disjoint trees with roots r1, …, rn, and r is a node not in any of the Ti’s, then the graph consisting of r and edged from r to each of r1, …, rn is a tree with root r. (c)2001-2003, Michael P. Frank

  43. Illustrating Rooted Tree Def’n. • How rooted trees can be combined to form a new rooted tree… r … T1 r1 T2 r2 Tn rn (c)2001-2003, Michael P. Frank

  44. A simple theorem about trees • Theorem: in a tree there is exactly one path between each pair of nodes. (c)2001-2003, Michael P. Frank

  45. Theorem: in a tree there is exactly one undirected path between each pair of nodes. Proof: • Base case: (of course) the theorem is true in the case of a tree consisting of just one node • Induction step: suppose the theorem holds for disjoint trees T1, …, Tn with roots r1, …, rn .Then it also holds for the graph consisting of rand edged from r to each of r1, …, rn(see over) (c)2001-2003, Michael P. Frank

  46. proving the induction step There is only one path from ri to rj, Given IH, there is only one path between any two nodes within any ri . Consequently, there is only one path from any node in the whole tree to any other node in the tree. r … T1 r1 T2 r2 Tn rn (c)2001-2003, Michael P. Frank

  47. There is much more ... See Rosen, chapter 3.4 if you’re interested in theorems relating the height of a tree to the number of nodes in it. (Proofs with induction.) (c)2001-2003, Michael P. Frank

More Related