Download presentation
Presentation is loading. Please wait.
Published byCrystal Miller Modified over 9 years ago
1
Discrete Maths Objective to show the close connection between recursive definitions and recursive functions 242-213, Semester 2, 2014-2015 9. Recursion 1
2
Overview 1. Recursive Definitions 2. Recursive Functions 3. Lists Recursively 4. Trees Recursively 5. Further Information 2
3
1. Recursive Definitions A recursive definition involves: 1. One or more basis rules, in which simple things are defined, and 2. One or more recursive (inductive) rules, where larger things are defined in terms of ‘smaller’ versions of those things. 3
4
Examples A tree is made from left and right subtrees. 23 5 5 7 7 19 5 5 4 4 left subtree right subtree the leaves are the basis 4
5
Sierpinski Gasket Start with a triangle and cut out the middle piece as shown. This results in three smaller triangles to which the process is continued. Use recursion to create strange shapes 5
6
3D Sierpinski Gasket 6
7
Menger Sponge remove center square repeat for the 8 small squares 7
8
Why Bother with Recursive Definitions? For many problems, recursive definitions are the natural way of specifying the problems e.g. search over trees/graphs, parsing, Recursive definitions are very close to inductive statements, and so are usually easier to prove than code using loops 8
9
1.1. Factorial, Recursively Remember that n! is 1*2*3*...*n. A recursive definition of n!: Basis. 1! = 1 Induction. n! = n * (n-1)! it's inductive because the meaning of n! is defined using the smaller (n-1)! 9
10
Prove the Specification Correct Prove the inductive statement S(n): the recursive definition of n!, as defined on the last slide, equals 1*2*3*...*n Basis. S(1) is clearly true. continued Show recursive n! is the same as a series of multiplications. 10
11
Induction. Assume that S(n) is true, which means that n! = 1*2*3...*n The recursive definition states that: (n+1)! = (n+1) * n! so, (n+1)! = n! * (n+1) substitute in n! value from S(n), so (n+1)! = 1*2*3*...n*(n+1) This is S(n+1), so S(n) S(n+1) is true. continued 11
12
In summary: we have shown S(1) to be true we have shown S(n) S(n+1) to be true This means that S(n) is true for all n >= 1: the recursive definition of n! equals 1*2*3*...*n Why is this useful? the correct recursive definition can be easily converted into a correct recursive function 12
13
1.2. Recursive Definition of Expressions We will look at expressions involving binary operators (e.g. +, *, /) e.g X*2, (Y/3)*(W+2), X The variables/numbers in an expressions are called operands. Basis. An operand on its own is an expression (e.g. X). continued 13
14
Inductive Rules 1. If E 1 and E 2 are expression, and @ is a binary operator (e.g., +, *), then E 1 @E 2 is an expression. 2. If E is an expression, then (E) is an expression. Examples: 5, X, 2+s, (X*Y) - 1 14
15
An Induction Proof Using Length S(n): A binary operator expression E of length n has one more operand than operators. For example:len operandsops X11 0 (2*y)+373 2 continued 15
16
Examples: S(1)1 y X a 2 S(3)2+a X*Y 3-s (x) S(5)(1+d) 5*6-2 Note: the examples suggest that S(2), S(4), S(6), etc. may not exist. the length of the expression continued 16
17
The proof of S(n) is by complete induction on the length of the expression: length is counted as the number of operators, operands and parentheses Basis. n=1. E must be a single operand (e.g. X). Since there are no operators, the basis holds. e.g. the first examples on the last slide continued 17
18
Induction: Assume S(1), S(2),..., S(n), and show that: (S(1) or S(2) or … or S(n)) S(n+1) this is complete induction. Let the expression for S(n+1) be called E. How can E be constructed? there are two cases, corresponding to the two inductive rules continued 18
19
a) If by rule (2), E = (E 1 ) Assume true: no of operands no of ops| E 1 x+1x Prove E E = (E 1 )x+1x So S(n+1) holds when E has the form (E 1 ) continued 19
20
b) If by rule (1), then E = E 1 @E 2 Assume true: no of operands no of ops| E 1 a+1a E 2 b+1b Prove E E = E 1 @E 2 a+b+2 a+b+1 So S(n+1) holds when E has the form E 1 @E 2 continued 20
21
S(n+1) is true for both forms of E this was proved by assuming that smaller expressions (E 1, E 2 ) were true any smaller expression can be used since we are using complete induction continued 21
22
In summary: shown S(1) to be true shown (S(1) or S(2).. or S(n)) S(n+1) true This means that S(n) is true for all n >= 1: a binary operator expression E of length n has one more operand than operators Why is this useful? the correct recursive definition can be easily converted into a correct recursive function, which can be used in compilers complete induction 22
23
Notes We used all of S(1),...S(n) in the inductive step, since we considered the subexpressions that made up E. Using subexpressions was only possible because expression was defined recursively in terms of subexpressions. 23
24
2. Recursive Functions A recursive function is one that is called from within its own body direct call: a function F() has a call to F() within itself indirect call: a function F1() calls F2() which calls F3(),... and eventually F1() is called Recursive definitions map easily to recursive functions. 24
25
Factorial Code The recursive definition of n! was: Basis. 1! = 1 Induction. n! = n * (n-1)! As a function: int fact(int n) { if (n <= 1) return 1; /* basis */ else return n * fact(n-1); /*induction*/ } a simple translation 25
26
Understanding Recursive Execution Draw function calls as boxes, each containing the variables (and values) in that function call. Example: void main() { int res = fact(4); printf(“%d”, res); } 26
27
Diagram The trick is to remember that there are multiple calls to fact(). main n res=fact(4) fact 4 return... n fact 3 return... n fact 2 return... n fact 1 return... 27
28
3. Lists Recursively The list data structure has a natural recursive definition (and implementation). When a data structure is recursive, functions for manipulating it are naturally recursive as well. e.g. length of a list e.g. is an element in a list? 28
29
3.1. Recursive Definition A list can be: Basis. Empty Induction. A non-empty list consists of one node (the head), followed by a list (the tail). Example: 23 5 5 7 7 19 5 5 4 4 tail head 29
30
3.2. Recursive Implementation Unfortunately, recursive data structures in C have to be implemented with pointers. this is not true in many other languages, such as Java, Haskell, Prolog, etc. Also, empty pointer data structures are usually coded using NULL. continued 30
31
The LIST Type struct CELL { int element; struct CELL *next; } typedef struct CELL *LIST; Note. LIST can only hold integer elements. 31
32
Diagrams of C Lists w 1954 NULL w an empty list a 3-element list 32
33
Example void main() { LIST w = NULL; /* an empty list */ w = makeList(); /* build a list */ : if (w != NULL) printList(w); : : 33
34
3.3. The length of a list The recursive definition of length follows the recursive definition of the list d.s: Basis. The length of an empty list is 0. Induction. The length of a non-empty list is 1 + the length of the list tail. 34
35
length() int length(LIST w) { if (w == NULL) /* is w empty? */ return 0; else return 1 + length(w->next); } 35
36
3.4. Is element x in the list? A recusive definition: Basis. If the list is empty, then x is not in the list. Return False (0). Induction. If the list is non-empty then: 1. If x is the same as the list head, return True (1). 2. If x is not the same as the head, then return the result of examining the list tail. 36
37
hasElement() int hasElement(LIST w, int x) { if (w == NULL)/* empty? */ return 0;/* false */ else if (x == w->element) return 1;/* true */ else return hasElement(w->next, x); } 37
38
3.5. A General Recursive Format Most list functions have the “shape”: ResultType fun(LIST w,...) { if (w == NULL) return somethingSimple; else { use w->element;... fun(w->next,...); return result; } } Learn (and understand) this. 38
39
4. Trees Recursively The tree data structure has a natural recursive definition (and implementation). Tree functions are naturally recursive: e.g. the number of elements in a tree e.g. is an element in a tree? Using loops in tree functions usually means BIG CODING MISTAKES. 39
40
4.1. Recursive Definition A binary tree can be: Basis. Empty Induction. A non-empty tree consists of a node, and left and right sub-trees (which may be empty). 23 5 5 7 7 19 5 5 4 4 left subtree right subtree 40
41
4.2. The TREE Type struct CELL { int element; struct CELL *left; struct cell *right; } typedef struct CELL *TREE; Note. TREE can only hold integer element. 41
42
Diagrams of C Trees p2 1 4 5 NN N NN a 4-element tree (N means NULL) NULL p an empty tree 42
43
Example void main() { TREE p = NULL; /* an empty tree */ p = makeTree(); /* build a tree */ : if (p != NULL) printTree(p); : : 43
44
4.3. The number of elements in a tree The recursive definition of numElem follows the recursive definition of the tree d.s: Basis. The numElem of an empty tree is 0. Induction. The numElem of a non-empty tree is: 1 + numElem of left subtree + numElem of right subtree 44
45
numElem() int numElem(TREE w) { if (w == NULL) /* is w empty? */ return 0; else return 1 + numElem(w->left) + numElem(w->right); } 45
46
4.4. Is element x in the tree? A recusive definition: Basis. If the tree is empty, then x is not in the tree. Return False (0). Induction. If the tree is non-empty then: 1. If x is the same as the element, return True (1), or 2. If x is in the left subtree, return True (1), or 3. If x is in the right subtree, return True (1), or 4. Return False. 46
47
hasElement() int hasElement(TREE w, int x) { if (w == NULL)/* empty? */ return 0;/* false */ else if (x == w->element) return 1; else if (hasElement(w->left, x)) return 1;/* true */ else return hasElement(w->right, x); } Note: the last else combines cases 3 and 4. 47
48
4.5. A General Recursive Format Most tree functions will have the “shape”: ResultType fun(TREE w,...) { if (w == NULL) return somethingSimple; else { use w->element; fun(w->left,...); fun(w->right,...); return result; } } Learn (and understand) this. 48
49
5. Further Information Discrete Mathematics and its Applications Kenneth H. Rosen McGraw Hill, 2007, 7th edition chapter 5, sections 5.3 – 5.4 49
50
“Drawing Hands” M.C. Escher, 1948 50
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.