500 likes | 672 Views
Discrete Maths. 242-213 , Semester 2, 2013-2014. Objective to show the close connection between recursive definitions and recursive functions. 9 . Recursion. Overview. Recursive Definitions Recursive Functions Lists Recursively Trees Recursively Further Information.
E N D
Discrete Maths 242-213, Semester 2, 2013-2014 • Objective • to show the close connection between recursive definitions and recursive functions 9. Recursion
Overview • Recursive Definitions • Recursive Functions • Lists Recursively • Trees Recursively • Further Information
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.
Examples • A tree is made from left and right subtrees. 23 5 4 leftsubtree right subtree 7 19 5 the leaves are the basis
Use recursion to create strange shapes 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.
Menger Sponge remove center square repeat for the 8 small squares
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
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)!
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. Show recursive n! is the same as a series of multiplications. continued
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
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
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
Inductive Rules • 1. If E1 and E2 are expression, and @ is a binary operator (e.g., +, *), then E1@E2 is an expression. • 2. If E is an expression, then (E) is an expression. • Examples: 5, X, 2+s, (X*Y) - 1
An Induction Proof Using Length • S(n): A binary operator expression E of length n has one more operand than operators. • For example: len operands ops • X 1 1 0 • (2*y)+3 7 3 2 continued
the length of the expression • 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. continued
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
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
a) If by rule (2), E = (E1) • Assume true: no of operands no of ops| E1 x+1 x • Prove E E = (E1) x+1 x • So S(n+1) holds when E has the form (E1) continued
b) If by rule (1), then E = E1@E2 • Assume true: no of operands no of ops| E1 a+1 a E2 b+1 b • Prove E E = E1@E2a+b+2 a+b+1 • So S(n+1) holds when E has the form E1@E2 continued
S(n+1) is true for both forms of E • this was proved by assuming that smaller expressions (E1, E2) were true • any smaller expression can be used since we are using complete induction continued
complete induction • 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
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.
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.
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
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); }
fact n Diagram 4 return... fact main n • The trick is to remember that there are multiple calls to fact(). 3 return... res=fact(4) fact n 2 return... fact n 1 return...
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?
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 7 19 5 4 head tail
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
The LIST Type • struct CELL { int element; struct CELL *next;}typedef struct CELL *LIST; • Note. LIST can only hold integer elements.
Diagrams of C Lists a 3-element list 19 5 4 w NULL NULL w an empty list
Example void main(){ LIST w = NULL; /* an empty list */ w = makeList(); /* build a list */ : if (w != NULL) printList(w); : :
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.
length() int length(LIST w){ if (w == NULL) /* is w empty? */ return 0; else return 1 + length(w->next); }
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.
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);}
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.
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.
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 right subtree 5 4 leftsubtree 7 19 5
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.
Diagrams of C Trees NULL p 2 p an empty tree 1 4 N N N 5 a 4-element tree (N means NULL) N N
Example void main(){ TREE p = NULL; /* an empty tree */ p = makeTree(); /* build a tree */ : if (p != NULL) printTree(p); : :
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
numElem() int numElem(TREE w){ if (w == NULL) /* is w empty? */ return 0; else return 1 + numElem(w->left) + numElem(w->right); }
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.
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.
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.
5. Further Information • Discrete Mathematics and its ApplicationsKenneth H. RosenMcGraw Hill, 2007, 7th edition • chapter 5, sections 5.3 – 5.4
“Drawing Hands” M.C. Escher, 1948