Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Maths 11. Recursion Objective

Similar presentations


Presentation on theme: "Discrete Maths 11. Recursion Objective"— Presentation transcript:

1 Discrete Maths 11. Recursion Objective
242/ , Semester 2, 11. Recursion Objective to show the close connection between recursive definitions and recursive functions

2 Overview Recursive Definitions Recursive Functions Lists Recursively
Trees Recursively Further Information

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.

4 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. 1 big triangle  3 smaller triangles

5 3D Sierpinski Gasket

6 1 big pyramid  5 smaller pyramids

7 Menger Sponge remove center square repeat for the 8 small squares

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

9 1.1. Factorial, Recursively
Remember that n! is 1*2*3*...*n. A recursive definition of n!: Basis. 1! = 1 Recursive. n! = n * (n-1)! for n > 1 the meaning of n! is defined using the smaller (n-1)!

10 Prove Recursive Definition of ! Multiplication Def of ! 1 ! = 1 ?
n ! = n * (n-1)! Multiplication Def of ! n ! = 1 * 2 * 3 * ... * n ? == S(n): is recursive n! the same as 1 *2 * ... * n for all values of n? continued

11 Basis. S(1) is clearly true.
Implication. Assume that S(n) is true, which means that n! = 1*2*3...*n The recursive definition states that: n! = n * (n-1)! so (n+1)! = (n+1) * n! so (n+1)! = n! * (n+1) substitute in r.h.s. of S(n) to replace n!, so (n+1)! = 1*2*3*...n*(n+1) This is S(n+1), so S(n)  S(n+1) is true. continued

12 This means that S(n) is true for all n >= 1:
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

13 2. Recursive Functions The recursive definition of n! was:
Basis. 1! = 1 Recursive. n! = n * (n-1)! As a function: int fact(int n) { if (n <= 1) return 1; /* basis */ else return n * fact(n-1); /* recursive */ } a simple translation

14 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); }

15 fact Diagram n 4 return... fact main n 3 return... res=fact(4) fact n 2 return... The trick is to remember that there are multiple calls to fact(). fact n 1 return...

16 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?

17 3.1. Recursive Definition A list can be: In pictures: Basis (no list):
Basis. Empty Recursive. A non-empty list consists of one node (the head), followed by a smaller list (the tail). In pictures: Basis (no list): Recursive: head smaller list (tail) bigger list

18 Building a list Start with empty list; add head nodes to make bigger lists. add '6' 6 add '5' 6 5 6 add '9' 5 6 5 9 6

19 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.

20 The LIST Type Note. LIST can only hold integer elements. I'm going to
assume you remember this from your C course. struct CELL { int element; struct CELL *next; } typedef struct CELL *LIST; Note. LIST can only hold integer elements.

21 Diagrams of C Lists a 3-element list 19 5 4 w NULL NULL w
an empty list

22 Drawn Using our Recursive Pics
5 4 19

23 Code Example void main() { LIST w = NULL; /* an empty list */ w = makeList(); /* build a list */ printList(w); int len = length(w); printf("Length of list: %d\n", len); if (hasElem(w, 19)) printf("Found 19\n"); else printf("19 not in the list\n");

24 3.3. The length of a list Write a recursive version of length() which takes a list as input and returns the number of nodes in the list. 19 5 4 length(w) returns 3 w NULL NULL length(w) returns 0 w

25 From Pics to Code length(list) returns an int
There are only two kinds of list: empty, recursive. length( ) returns an int length( ) returns an int and is recursive, which means it calls length() on a smaller list, the tail: it calls length( ) h tail tail

26 In more detail length( ) returns 0 length( ) returns 1 + length( ) h

27 In words: The recursive definition of length():
Basis. The length of an empty list is 0. Recursive. The length of a non-empty list is 1 + the length of the list tail.

28 Translating to C Empty list becomes null
There are two parts to a recursive list, w becomes w->element becomes w->next h tail h tail

29 length() int length(LIST w) { if (w == NULL) /* is w empty? */ return 0; else return 1 + length(w->next); }

30 3.4. Is element x in the list? Write a recursive version of hasElem() which takes a list and number as inputs and returns 1 if the number is in the list, 0 otherwise. 19 5 4 hasElem(w, 5) returns 1 w NULL hasElem(w, 5) returns 0 NULL w

31 From Pics to Code hasElem(list, x) returns 0 or 1
There are only two kinds of list: empty, recursive. hasElem( , x) returns 0 or 1 hasElem( , x) returns 0 or 1 and is recursive, which means it calls hasElem() on a smaller list, the tail: it calls hasElem( , x) h tail tail

32 In more detail hasElem( , x) returns 0
hasElem( , x) returns if == x else it carries on searching by calling hasElem( , x) h tail h tail

33 In words A recusive definition:
Basis. If the list is empty, then x is not in the list. Return 0. Recursive. If the list is non-empty then: 1. If x is the same as the list head, return 1 2. If x is not the same as the head, then return the result of examining the tail.

34 hasElement() int hasElem(LIST w, int x) { if (w == NULL) /* empty? */ return 0; else if (w->element == x) return 1; else return hasElem(w->next, x); }

35 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.

36 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.

37 4.1. Recursive Definition A binary tree can be: In pictures:
Basis. Empty Recursive. A non-empty tree consists of a head node, and smaller left and right sub-trees (which may be empty). In pictures: Basis (no tree): h bigger tree Recursive: left smaller trees right

38 Building a tree Start with 2 empty trees; add head node to make a 1-element tree 5 add '5'

39 Building bigger trees Combine two smaller trees and a new head node: 7
5 5 add '7' a 2-element tree

40 4.2. The TREE Type Note. TREE can only hold integer element.
struct CELL { int element; struct CELL *left; struct cell *right; } typedef struct CELL *TREE; Note. TREE can only hold integer element.

41 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

42 Drawn Using our Recursive Pics
2 1 4 5

43 Example void main() { TREE t = NULL; /* an empty tree */ t = makeTree(); /* build a tree */ printTree(t); int n = numElems(t); printf("No. of nodes: %d\n", n); if (hasElem(t, 19)) printf("Found 19\n"); else printf("19 not in the tree\n");

44 4.3. The number of elements in a tree
Write a recursive version of numElems() which takes a tree as input and returns the number of nodes.

45 From Pics to Code numElems(tree) returns an int
There are only two kinds of tree: empty, recursive. numElems( ) returns an int numElems( ) returns an int and is recursive, which means it calls numElems() on the smaller left and right tree: it calls numElems( ) and numElems( ) h l r l r

46 In more detail numElems( ) returns 0 numElems( ) returns
1 + numElems( ) + numElems( ) h l r l r

47 In Words The recursive definition of numElems
Basis. The numElems of an empty tree is 0. Recursive. The numElems of a non-empty tree is: numElems of left subtree numElems of right subtree

48 Translating to C Empty tree becomes null
There are 3 parts to a recursive tree, t becomes t->element becomes t->left becomes t->right h l r h l r

49 numElems() int numElems(TREE t) { if (t == NULL) /* is t empty? */ return 0; else return 1 + numElems(t->left) numElems(t->right); }

50 4.4. Is element x in the tree? Write a recursive version of hasElem() which takes a tree and number as inputs and returns 1 if the number is in the tree, 0 otherwise.

51 From Pics to Code hasElem(tree, x) returns 0 or 1
There are only two kinds of tree: empty, recursive. hasElem( , x) returns 0 or 1 hasElem( , x) returns 0 or 1 and is recursive, which means it calls hasElem() on the smaller left and right trees: it calls hasElem( , x) and hasElem( , x) h l r l r

52 In more detail hasElem( , x) returns 0
hasElem( , x) returns if == x else it searches the left tree by calling hasElem( , x) if that returns 0 then search the right tree by calling hasElem( , x) h l r h l r

53 In Words A recusive definition:
Basis. If the tree is empty, then x is not in the tree. Return 0 Recursive. If the tree is non-empty then: 1. If x is the same as the element, return 1, or 2. If x is in the left subtree, return 1, or 3. If x is in the right subtree, return 1, otherwise 0

54 hasElem() int hasElem(TREE t, int x) { if (t == NULL) /* empty? */ return 0; /* false */ else if (t->element == x) return 1; else if (hasElem(t->left, x)) return 1; else return hasElem(t->right, x); }

55 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.

56 5. Further Information Discrete Mathematics and its Applications Kenneth H. Rosen McGraw Hill, 2007, 7th edition chapter 5, sections 5.3 – 5.4

57 “Drawing Hands” M.C. Escher, 1948


Download ppt "Discrete Maths 11. Recursion Objective"

Similar presentations


Ads by Google