1 / 46

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

4/10/2012. (c)2001-2003, Michael P. Frank. 2. Module

chi
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. 4/10/2012 (c)2001-2003, Michael P. Frank 1 University of Aberdeen, Computing Science CS3511 Discrete Methods Kees van Deemter Slides adapted from Michael P. Franks Course Based on the Text Discrete Mathematics & Its Applications (5th Edition) by Kenneth H. Rosen A word about organization: Since different courses have different lengths of lecture periods, and different instructors go at different paces, rather than dividing the material up into fixed-length lectures, we will divide it up into modules which correspond to major topic areas and will generally take 1-3 lectures to cover. Within modules, we have smaller topics. Within topics are individual slides. The instructor can bring several modules to each lecture with him, to make sure he has enough material to fill the lecture, or in case he wants to preview or review slides from upcoming or recent past lectures.A word about organization: Since different courses have different lengths of lecture periods, and different instructors go at different paces, rather than dividing the material up into fixed-length lectures, we will divide it up into modules which correspond to major topic areas and will generally take 1-3 lectures to cover. Within modules, we have smaller topics. Within topics are individual slides. The instructor can bring several modules to each lecture with him, to make sure he has enough material to fill the lecture, or in case he wants to preview or review slides from upcoming or recent past lectures.

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

    3. 4/10/2012 (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)

    4. 4/10/2012 (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.

    5. 4/10/2012 (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 isnt necessarily. There are recursive algorithms, recursively defined functions, relations, sets, etc.

    6. 4/10/2012 (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.

    7. 4/10/2012 (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, let f(n+1)=2f(n)+3 What are the values of the following? f(1)= f(2)= f(3)= f(4)=

    8. 4/10/2012 (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)-1 f(n)= 2(f(n-1)) for n>=1

    9. 4/10/2012 (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.

    10. 4/10/2012 (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

    11. 4/10/2012 (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. an (a real, n natural) using only addition an (a real, n natural) using only multiplication

    12. 4/10/2012 (c)2001-2003, Michael P. Frank 12 The Fibonacci Series The Fibonacci series fn=0 is a famous series defined by: f0 := 0, f1 := 1, fn=2 := fn-1 + fn-2

    13. 4/10/2012 (c)2001-2003, Michael P. Frank 13 An upper bound on Fib. series Theorem: fn < 2n. Proof: By induction. Base cases: f0 = 0 < 20 = 1 f1 = 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.

    14. 4/10/2012 (c)2001-2003, Michael P. Frank 14 (A lower bound on Fibonacci series Theorem. For all integers n = 3, fn > an-2, where a = (1+51/2)/2 1.61803. Proof. (Using strong induction.) Let P(n) = (fn > an-2). Base cases: For n=3, note that a < 2 = f3. For n=4, a2 = (1+251/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 a2 = a+1. Thus, ak-1 = (a+1)ak-3 = ak-2 + ak-3. By inductive hypothesis, fk-1 > ak-3 and fk > ak-2. So, fk+1 = fk + fk-1 > ak-2 + ak-3 = ak-1. Thus P(k+1). )

    15. 4/10/2012 (c)2001-2003, Michael P. Frank 15 Recursively Defined Sets An infinite set S 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?

    16. 4/10/2012 (c)2001-2003, Michael P. Frank 16 Given an alphabet S, what is S*? e ? S* (e := , the empty string) w ? S* ? x ? S ? wx ? S*

    17. 4/10/2012 (c)2001-2003, Michael P. Frank 17 The Set of All Strings Given an alphabet S, the set S* of all strings over S can be recursively defined by: e ? S* (e := , the empty string) w ? S* ? x ? S ? wx ? S* Exercise: Prove that this definition is equivalent to

    18. 4/10/2012 (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 {+, -, *}.

    19. 4/10/2012 (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)

    20. 4/10/2012 (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 ?

    21. 4/10/2012 (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

    22. 4/10/2012 (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

    23. 4/10/2012 (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).

    24. 4/10/2012 (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(?)<n It follows by IH that the property holds for ?. Consequently, it also holds for ?=?? (because ?? contains the same disjunctions, disjuncts, and brackets as ?).

    25. 4/10/2012 (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 ? .

    26. 4/10/2012 (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?

    27. 4/10/2012 (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 dont know that complexity(?) = complexity(?) = n?1. (In fact, complexity(?) + complexity(?) = n?1, and neither is equal to 0 )

    28. 4/10/2012 (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

    29. 4/10/2012 (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.)

    30. 4/10/2012 (c)2001-2003, Michael P. Frank 30 Structural Induction: Proving something about a recursively defined object using an inductive proof whose structure mirrors the objects 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)}.

    31. 4/10/2012 (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 defn. of S. Inductive step: n?S (Induction Hyp); we also have 3?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 that 3|(x+y). (For example, if x=3a and y=3b then x+y=3(a+b).) Thus x+y ? A.

    32. 4/10/2012 (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. procedure power(a?0: real, n?N) if n = 0 then return 1 else return a power(a, n-1)

    33. 4/10/2012 (c)2001-2003, Michael P. Frank 33 Recursive Algorithms (3.5) procedure power(2, 3) return 2 power(2, 3-1) return 2 power(2, 3-2) return 2 power(2, 3-3)=1 This calculates that 23 = 2.2.2.1 = 8.

    34. 4/10/2012 (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

    35. 4/10/2012 (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.

    36. 4/10/2012 (c)2001-2003, Michael P. Frank 36 Recursive Fibonacci Algorithm procedure fibonacci(n ? N) if n=0 return 0 if n=1 return 1 return fibonacci(n-1)+fibonacci(n-2) Is this an efficient algorithm? How many additions are performed?

    37. 4/10/2012 (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)

    38. 4/10/2012 (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 procedures 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.

    39. 4/10/2012 (c)2001-2003, Michael P. Frank 39 The latter was a sneak preview of nexts years Models of Computation course: Complexity: How long does a given algorithm take? [This is where induction can be used] Whats the fastest possible algorithm for a given problem? Computability (How do I know whether a problem can be solved at all using an algorithm?)

    40. 4/10/2012 (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) Note that its not (only) some specific trees that are defined recursively, but the notion of a tree itselfNote that its not (only) some specific trees that are defined recursively, but the notion of a tree itself

    41. 4/10/2012 (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. See book for less set-theoretically inspired definition.See book for less set-theoretically inspired definition.

    42. 4/10/2012 (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 Tis, then the graph consisting of r and edged from r to each of r1, , rn is a tree with root r. See book for less set-theoretically inspired definition.See book for less set-theoretically inspired definition.

    43. 4/10/2012 (c)2001-2003, Michael P. Frank 43 Illustrating Rooted Tree Defn. How rooted trees can be combined to form a new rooted tree

    44. 4/10/2012 (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.

    45. 4/10/2012 (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 r and edged from r to each of r1, , rn (see over)

    46. 4/10/2012 (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.

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

More Related