1 / 64

Recursive Definitions: functions, structures, languages

Recursive Definitions: functions, structures, languages. Sections 5.3 & 5.4 Tuesday, June 10. 1. Recursively Defined Sets. A set S can be defined recursively as follows: Specify one or more basis elements .

lesley
Download Presentation

Recursive Definitions: functions, structures, languages

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. Recursive Definitions: functions, structures, languages Sections 5.3 & 5.4 Tuesday, June 10 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS 1

  2. Recursively Defined Sets • A set S can be defined recursively as follows: • Specify one or more basis elements. • Specify one or more recursive formation rules indicating how to create an element of S from other elements in S. • An element x belongs to S iff either • x is a basis element or • x is obtained by applying a formation rule to some elements, x1, x2, …, xk , all of which belong to S. 2 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  3. Example • Define set S recursively, as follows: • Basis elements: 2 S • Recursive formation rules: R1: If x S and y S then x + y SR2: If x S and y S then x − y S • What set is this? 3 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  4. Example • Define set A recursively, as follows: • Basis element: 2 A • Recursive Rules: R1: If x A and n N then xn A • What set is this? 4 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  5. Exercise • Define set A recursively, as follows: • Basis element: 2 A and 3 A • Recursive Rules: R1: If x, y A then x− y A • What set is this? 5 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  6. Numeric expressions basis The set of numeric expressions is defined recursively: • An integer is a numeric expression • If e is a numeric expression, then the following are both numeric expressions: – e (e) • if e1 and e2 are numeric expressions, then the following are all numeric expressions: e1 + e2 e1 – e2 e1 * e2 e1 dive2 e1 mode2  e1 / e2   e1 / e2 recursive formation rules RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  7. Exercise • According to this definition which of the following are numeric expressions? 36 – (6 div 2) 1 = 16 (mod 5) 3.15 * 100 + 9 4 / 0div 3 4 / 0 div 3 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  8. Strings • A stringover an alphabet Σis a finite sequence of symbols from Σ, e.g. • Bit strings are strings over {0, 1}, e.g., 0 1 00 01 10 11… • Strings over {a, b, … z}: helloworld • The set of stringsΣ*over the alphabet Σ is defined recursively by: • Basis element: λΣ*, whereλ, called the empty string, denotes the sequence containing no elements. • Formation rule: If wΣ* and xΣ then wxΣ*(By convention, when w =λ, we also write x for λx.) 8 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  9. Function on Recursively Defined Set • To define a function f on a recursively defined set S: • For each basis element x:Define f (x) • For each formation rule specifying how x is created from elements, x1, x2, …, xk , of S: Give a formation rule specifying how f (x) is created from f (x1), f (x2), …, f (xk). RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  10. Length of a string • The length function, l : Σ* N, computes the length of a string and is defined recursively as follows: • l(λ) = 0, and • l(wx) = l(w) + 1, if wΣ* and xΣ. • Example: • l(0) = l(λ0) = l(λ) + 1 = 0 + 1 = 1 • l(00) = l(0) + 1 = 1 + 1 = 2 • l(001011) = l(00101) + 1 = l(0010) + 2 = … = 6 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  11. Example: Palindromes • The empty string is a palindrome. • A string of length 1 is a palindrome. • If w is a palindrome and a is a character, then the string awa is a palindrome. • If w is a palindrome and b is the space character, then the strings bw and wb are both palindromes • (Nothing else is a palindrome.) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  12. Example • “kayak” is a palindrome since • “y” is a palindrome by rule 2 • “aya” is a palidrome by rule 3 and previous • “kayak” is a palindrome by rule 3 and previous • Show: “race car” is a palindrome • Exercise: Sketch a procedure is_palindrome(w), which determines if the input string w is a palindrome. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  13. Concatenation of Strings • Defn: Given an alphabet Σ, the concatenation of two strings fromΣ*is defined recursively by: • Basis: If wΣ*, then w∙λ = w, where λ is the empty string. • Recursive rule: If w1Σ* and w2Σ* andxΣ, then w1∙(w2 x) = (w1∙w2 ) x • Example: Consider Σ = {0, 1}. • 00∙1 = 00∙(λ1) = (00∙λ) 1 = 001 • 00∙11 = (00∙1)1 = 0011 • 10100∙1100 = … = 101001100 • In general, if w1Σ* and w2Σ*, then w1∙w2 consists of the symbols in w1 followed by the symbols in w2 13 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  14. Structural Induction • To prove P(s), where s ranges over a recursively defined set S, use structural induction. • Basis step: For each basis element b, show P(b) is true. • Induction step: For each formation rule that says how to construct an element s in S from other elements, s1, …, sk, in S: • Assume P(s1), …, P(sk), are true. (IH) • Show that, under this assumption, then P(s) is also true. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  15. Exercise: Structural Induction Worksheet Defn [Bit Strings]. The set of bit strings B*, where B = { 0, 1 }, is defined inductively, as follows Basis: The empty string, denoted λ , is in B* — that is, λ  B*. Recursive rule: If w  B* and x  B then wx  B*. Nothing else is a bit string. Additionally, the bit string λx is abbreviated as just x. Defn [Concatenation]. The concatenation of two bit strings is defined recursively as follows: Basis: If w  B*, then w∙λ = w, where λ is the empty string. Recursive rule: If w1 B* and w2 B*and x  B, then w1∙(w2 x) = (w1∙w2)x Defn [Length]. The length l of a bit string is defined recursively as follows: Basis: l(λ) = 0. Recursive rule: If w  B* and x  B, then l(wx) = l(w) + 1. Prove the following claim using structural induction: Claim: If w1 B* and w2 B*, then l(w1∙w2) = l(w1) + l(w2). RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  16. Exercise: Structural Induction Worksheet • State what you need to prove for the basis step: l(w1∙ λ) = l(w1) + l(λ). • Proof: l(w1∙ λ) = l(w1) by defn of concatenation (basis) = l(w1) + 0 by arithmetic = l(w1) + l(λ) by defn of length function (basis) • P(w2) : l(w1∙w2) = l(w1) + l(w2) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  17. Exercise: Structural Induction Worksheet • P(w2) : l(w1∙w2) = l(w1) + l(w2) • Induction step : Under the assumption that wB*and xB, and that l(w1∙w) = l(w1) + l(w), we need to prove that l(w1∙(wx)) = l(w1) + l(wx). • Proof: l(w1∙(wx)) = l((w1∙w)x) by defn of concatenation (rec rule) = l(w1∙w) + 1 by defn of l (rec rule) = (l(w1) + l(w)) + 1 induction hypothesis = l(w1) + (l(w) + 1) by arithmetic (assoc. law) = l(w1) + l(wx) by defn of l (rec rule) Induction Hypothesis RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  18. Example: Recursive definition of a list A list l is either • The empty list: [ ] • A nonempty list: A first element, l[0], called the head of the list.followed by a list,l[1:len(l)],called the tail of the list. basis element recursive formation rule l = [5, -3, 2 ] 5 -3 2 [ ] l[0] = 5 l[1:3] = [-3, 2 ] l[1:3](tail of l ) l[0](head of l ) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  19. Function on Recursively Defined Set • To define a function f on a recursively defined set S: • For each basis element x:Define f (x) • For each formation rule specifying how x is created from elements, x1, x2, …, xk , of S: Give a formation rule specifying how f (x) is created from f (x1), f (x2), …, f (xk). • Example: • Definition of the sum function s on a list l:s(l) = 0, if l = []; and s(l) = s[0] + s(l[1:len(l)]), if l ≠ []. • Definition of the member function:m(l, x) = false, if l = []; m(l, x) = true, if l ≠ [] and x = l[0];m(l, x) = m(l[1:len(l)], x), if l ≠ [] and x ≠ l[0]. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  20. Recursive summing of elements in a list def sumlist(l): if l == [ ]: return 0 else: return l [0] + sumlist(l [1:len(l)]) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  21. Recursive linear search of list L for element E def member(L, X): if L == [ ]: return False elif L[0] == X: return True else: return member(L[1:len(L)], X) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  22. Prove the Python member function is correct: Let P(L) be the statement: member(L, X) = True iff X is in L. • Base case: By the Python def, member([],X) = False. As X is not an element of [], this proves the base case (i.e, P([]). • Inductive step: Assume that L is non-empty and that member(L[1:len(L)], X) = True iff X is in L[1:len(L)] (IH). We need to prove: member(L, X) = True iff X is in L (*). Consider the two possible outcomes of the test “L[0] == X”: If L[0] = X, then member(L, X) = True and X is (the first element) in L, so (*) holds. Otherwise, L[0] ≠ X, and so X is in L iff X is in L[1:len(L)] andmember(L,X) = member(L[1:len(L)], X), by the Python def. Thus: X is in L iff X is in L[1:len(L)] iff member(L[1:len(L)], X) = True (by IH) iff member(L, X) = True. which proves (*) in this case, also. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  23. Rooted Trees • A rooted tree is a special kind of graph • one vertex is distinguished as the root, and • a (possibly empty) set of edges connects the root to a set of child vertices, which are themselves roots of disjoint trees. • a vertex with no children is called a leaf. • The set of rooted trees is defined recursively by: Basis elements: A single vertex r is a rooted tree with root r. Formation rule: If T1, T2, …,Tn are disjoint rooted trees with roots r1, r2,…,rn, respectively. Then the graph formed by starting with a new root r, not in any of T1, T2, …,Tn, and adding an edge from r to each of the vertices r1, r2,…,rn, is also a rooted tree. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  24. Building Up Rooted Trees RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  25. Full Binary Trees • A full binary tree is a rooted tree in which each vertex either is a leaf or has two child vertices, which are roots of a left subtree and a right subtree. • The set of full binary trees can be defined recursively: • Basis element: A single vertex r is a full binary tree. • Formation rule: If T1 and T2 are disjoint full binary trees, then T1∙T2 denotes a full binary tree consisting of a new root r together with two new edges: an edge connecting r to the root of the left subtree T1 and an edge connecting r tothe right subtree T2. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  26. Example: Construction of a FBT T9 = T5∙T8 T8 = T6∙T7 T5 T9 T2 T4 T7 T5 = T3∙T4 T3 T1 T6 T8 T3 = T1∙T2 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  27. Building Up Full Binary Trees RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  28. Functions on full binary trees • Define the number of verticesn(T) of a full binary tree T recursively as follows: • The number of vertices of a full binary tree T consisting of only a root r is n(T) = 1. • If T1 and T2are full binary trees, then n(T1∙T2) = 1 + n(T1) + n(T2). RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  29. Exercise: Use this definition to find n(T9) n(T9)= n(T5)+ n(T8)+ 1 = n(T5)+ n(T6)+ n(T7)+ 2 = n(T5)+ 4 = n(T3)+ n(T4)+ 5 = n(T3)+ 6 = n(T1)+ n(T2)+ 7 = 9 T5 T9 T2 T4 T7 T3 T1 T6 T8 The number of vertices of a full binary tree T consisting of only a root r is n(T) = 1. If T1 and T2are full binary trees, then n(T1∙T2) = 1 + n(T1) + n(T2). RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  30. Functions on full binary trees • Define the heighth(T)of a full binary tree T recursively as follows: • The height of a full binary tree T consisting of only a root r is h(T) = 0. • If T1 and T2are full binary trees, then h(T1∙T2) = 1 + max(h(T1),h(T2)). RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  31. Exercise: Use this definition to find h(T9) h(T1)= h(T2)= h(T4) = h(T6)= h(T7)= 0 h(T3) = max(h(T1),h(T2))+ 1 = 1 h(T5) = max(h(T3),h(T4))+ 1 = 1 + 1 = 2 . . . h(T9) = max(h(T5),h(T8))+ 1 = 2 + 1 = 3 T5 T9 T2 T4 T7 T3 T1 T6 T8 The height of a full binary tree T consisting of only a root r is h(T) = 0. If T1 and T2are full binary trees, then h(T1∙T2) = 1 + max(h(T1),h(T2)) RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  32. Proving results about full binary trees using structural induction • To prove P(T) where T ranges over the set of full binary trees • Basis Step: Prove P(T) is true when T is a single vertex. • Induction Step: Assume T1 and T2 are full binary trees and P(T1) and P(T2) are true. (IH) Prove that, under these assumptions, P(T1∙T2) is also true. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  33. Example: structural induction on FBT’s Theorem: If T is a full binary tree, then n(T) ≤ 2h(T)+1– 1. Proof: Use structural induction. • BASIS STEP: If T is a full binary tree consisting only of a root, then n(T) = 1 and h(T) = 0. Hence, n(T) = 1 ≤ 20+1– 1 = 2h(T)+1– 1. • RECURSIVE STEP: Assume n(T1) ≤ 2h(T1)+1– 1 and n(T2) ≤ 2h(T2)+1 – 1 where T1 and T2are full binary trees. Then: n(T1∙T2) = 1 + n(T1) + n(T2) (by recursive definition of n(T)) ≤ 1 + (2h(T1)+1– 1) + (2h(T2)+1 – 1) (by induction hypothesis) = 2h(T1)+1 + 2h(T2)+1– 1 ≤ 2∙max(2h(T1)+1, 2h(T2)+1 )– 1 (since x + y ≤2∙max(x, y)) = 2∙2max(h(T1)+1,h(T2)+1) – 1 (since max(2x , 2y)= 2max(x,y) ) = 2∙2max(h(T1),h(T2))+1– 1 (since max(x+a, y+a)= max(x,y)+a ) = 2∙2h(T)– 1 (by recursive definition of h(T)) = 2h(T)+1– 1 RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  34. Example: Generalized Recursion • C(n, k): the number of subsets of cardinality k that can be formed using elements from a set of cardinality n. • Example: C(4, k) the number of k-element subsets that can be formed using elements of {1, 2, 3, 4}. k = 0 {1, 2, 3, 4} { } C(4,0) = 1 {1, 2, 3, 4} {1} {2} {3} {4} k = 1 C(4,1) = 4 {1, 2, 3, 4} {1, 2} {1, 3} {2, 3} {1, 4} {2, 4} {3, 4} C(4,2) = 6 k = 2 {1, 2, 3, 4} {1, 2, 3} {1, 2, 4} {1, 2, 4} {2, 3, 4} C(4,3) = 4 k = 3 k = 4 {1, 2, 3, 4} C(4,4) = 1 {1, 2, 3, 4} RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  35. C(n, k): Combinations of n objects k at a time • For n ≥ 0 and n ≥ k ≥ 0, we can define C(n, k) recursively as follows: • C(n, 0) = 1 • C(n, n) = 1 • C(n, k) = C(n −1, k) + C(n −1, k −1), if n > k > 0 Number of subsets containing n Number of subsets not containing n RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  36. This is the structure of the Pascal Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Notes: element k in row n is C(n, k). It is the sum of the 2 closest elements in the previous row. The elements in row n account for all the subsets of sizes {0, 1, 2, 3, … , n }. Thus, the sum of elements in row n is 2n RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  37. Remaining slides are optional. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  38. A recursive fill or coloring algorithm Common in computer graphics and image processing. We will prove that the algorithm is correct. RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  39. paint/fill algorithm • Object region must be bounded by color C • Start at any pixel [r,c] inside boundary of C • Recursively color neighbors RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  40. Note: what is a binary image? It has Rows = {1, 2, 3, …, R} It has Columns={1,2,3, …, C} A binary image I is a function that maps Rows  Columns into {0, 1}. In defining a set of “ON” pixels, it is also a “characteristic function” RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  41. A recursive fill algorithm Fills a bounded region in a 2D image with a given color C; Must have starting pixel location P=[r,c]; Color P with color C, then color all neighbors RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  42. Why a good example? • important image operation • recursive example • can prove correctness • very general base algorithm with extensions to determine connectedness, solve a maze, find objects, etc RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  43. Paint/fill algorithm overview • if image pixel I[r, c] already colored, return • color pixel: I[r, c] = C; • recurse on each neighbor of I[r, c] Note: In C++, the image reference will be I[r][c] RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  44. Pixel neighborhoods 4-neighbors of PP 8-neighbors of PP N4 N3 N2 N2 r N3 PP N1 N5 PP N1 N6 N7 N8 N4 c RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  45. Pre and Post conditions • Pre: region boundary marked with C is closed (so N-neighbors cannot leak out) • Pre: rest of image has color not C • Pre: pixel [r, c] is inside boundary • Post: every pixel inside boundary has color C • We will prove that the algorithm does yield the post condition RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  46. Fill algorithm: next level of detail void fill ( image& I, const int r, c, const color C ) { if I[r, c] == C then return; I[r, c] = C; // set the pixel to the fill color for all neighbors I[r + delta_row, c + delta_col]: fill ( image& I, r+delta_row, c+delta_col, C ) } e.g., take (delta_row, delta_col) to be, in order: (1,0), (0,1), (-1,0), (0,-1); “right, up, left, down” RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  47. Visiting pixels using a stack: optional void paint ( Image& I, int r, int c, int color ) { push r, c onto STACK; // replaces call stack of recursive fill while ( STACK not empty ) // are there more region pixels to visit? { pop c, r off STACK; if ( I[r][c] != background 0 or the current color ) { I[r][c] = color; // paint the current pixel for all neighbors of this pixel, push r+deltar, c+deltac onto STACK } } // when STACK is empty, all connected pixels have been visited } RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  48. Worksheet for practice • consider given examples • trace algorithm and label pixels with color C and order k = 1, 2, 3, 4, … • consider how to compute area and bounding box • consider how to compute centroid RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  49. Example 0 1 2 3 4 5 c 0 Start at pixel [ 3, 2 ] Use neighbor order right, up, left, down as (1, 2, 3, 4) Number each pixel that will be colored C with the order number in which it is reached and colored using the above recursive fill algorithm 1 C C C 2 C C r 3 C C 4 C C C 5 C RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

  50. Correctness proof by contradiction • suppose the pre conditions hold • prove the post condition follows • by contradiction: show that negative of post condition leads to a contradiction P RECURSIVE DEFINITIONS, STRUCTURES & ALGORITHMS

More Related