Recursive Definitions and Structural Induction ICS 6 D

  • Slides: 23
Download presentation
Recursive Definitions and Structural Induction ICS 6 D Sandy Irani

Recursive Definitions and Structural Induction ICS 6 D Sandy Irani

Recursive Definitions • A recursive definition defines a sequence or set in terms of

Recursive Definitions • A recursive definition defines a sequence or set in terms of smaller instances. – A recursively defined sequence (recurrence relations) defines each term as a function of earlier terms in the sequence Example: fn = fn-1 + fn-2 – A recursively defined set defines elements in the set by building them up from smaller elements in the set

Recursively Defined Set Example: properly nested parens • Consider a string of left and

Recursively Defined Set Example: properly nested parens • Consider a string of left and right parens – When is it properly nested? • (())() • ())(() • ((())()())

Recursively Defined Set Example: properly nested parens • Consider a string of left and

Recursively Defined Set Example: properly nested parens • Consider a string of left and right parens – When is it properly nested? • Basis: () is properly nested. • Recursive Rules: if x and y are properly nested strings then: – Rule 1: (x) is properly nested – Rule 2: xy is properly nested • Exclusion statement: a string of parens is properly nested only if it can be created by applying the recursive rules to the string in the basis.

Recursively Defined Set Example: properly nested parens

Recursively Defined Set Example: properly nested parens

Recursively Defined Set Example: properly nested parens ((())()()) ())((()())

Recursively Defined Set Example: properly nested parens ((())()()) ())((()())

Ingredients for a recursive definition of a set • A basis explicitly states that

Ingredients for a recursive definition of a set • A basis explicitly states that one or more specific elements are in the set. • A recursive rule shows how to construct larger elements in the set from smaller elements. – (There is often more than one recursive rule). • An exclusion statement states that an element is in the set only if it is given in the basis or can be constructed by applying the recursive rules repeatedly to elements given in the basis. – (Often omitted) •

Binary Strings • An alternate recursive definition for the set of all binary strings:

Binary Strings • An alternate recursive definition for the set of all binary strings: – {0, 1}* = { , 0, 1, 00, 01, 10, 11, 000, 001, …} • Recursive definition: – Basis: S – Recursive rules: • If x S, then x 0 S x 1 S

Another recursively defined set of binary strings • Basis: λ S and 0 S

Another recursively defined set of binary strings • Basis: λ S and 0 S • Recursive rule: if x S, then – x 1 S – x 10 S

Trees • Collection of – Vertices (denoted by circles) – Edges (lines connecting vertices)

Trees • Collection of – Vertices (denoted by circles) – Edges (lines connecting vertices) • Here are some examples of trees:

Perfect Binary Trees • Each perfect binary tree has a designated vertex called the

Perfect Binary Trees • Each perfect binary tree has a designated vertex called the root. • A single vertex is a perfect binary tree. • If T is a perfect binary tree, then construct a new perfect binary tree T’ by taking two copies of T a new vertex v with connecting v to the roots of the copes of T. v is the root of T’.

Structural Induction • Structural induction is a type of induction used to prove facts

Structural Induction • Structural induction is a type of induction used to prove facts about recursively defined sets. • Definition: if T is a tree, then v(T) is the number of vertices in T. • Theorem: If T is a perfect binary tree, then v(T) = 2 n -1 for some positive integer n.

Structural Induction • Base case: T is a single vertex. – v(T) = 1

Structural Induction • Base case: T is a single vertex. – v(T) = 1 = 21 - 1 • Induction step: – Suppose that T’ is made from T using the recursive rule for perfect binary trees. – Will show that if v(T) = 2 n-1 for some integer n, then v(T’) = 2 m-1 for some integer m.

Structural Induction • Will show that if v(T) = 2 n-1 for some integer

Structural Induction • Will show that if v(T) = 2 n-1 for some integer n, then v(T’) = 2 m-1 for some integer m.

Structural Induction: Properly Nested Partheses • Definition: if x is a string of parentheses,

Structural Induction: Properly Nested Partheses • Definition: if x is a string of parentheses, – Right[x] = the number of right parens in x. – Left[x] = the number of left parens in x. • Theorem: if x is a properly nested string of parentheses, then Right[x] = Left[x]

Recursively Defined Set Example: properly nested parens • Consider a string of left and

Recursively Defined Set Example: properly nested parens • Consider a string of left and right parens – When is it properly nested? • Basis: () is properly nested. • Recursive Rules: if x and y are properly nested strings then: – Rule 1: (x) is properly nested – Rule 2: xy is properly nested • Exclusion statement: a string is properly nested only if it can be created by applying the recursive rules to the string in the basis.

Structural Induction: Properly Nested Partheses • Base case: x = ( ) Left[x] =

Structural Induction: Properly Nested Partheses • Base case: x = ( ) Left[x] = 1 = Right[x] = 1 • Inductive step: – If x is properly nested, it was constructed by applying a sequence of recursive rules, starting with (). – We consider two cases, depending on whether rule 1 or rule 2 was the last rule used to construct x.

Structural Induction: Properly Nested Partheses • Inductive step: case 1. – x = (u),

Structural Induction: Properly Nested Partheses • Inductive step: case 1. – x = (u), where u is properly nested. – By the inductive hypothesis: Left[u] = Right[u] Left[ x ] = Left[ (u) ] = 1 + Left[ u ] = 1 + Right[ u ] (by the inductive hypothesis) = Right[ (u) ] = Right[ x ]

Structural Induction: Properly Nested Partheses • Inductive step: case 2. – x = uv,

Structural Induction: Properly Nested Partheses • Inductive step: case 2. – x = uv, where u and v are properly nested. – By the inductive hypothesis: Left[u] = Right[u] Left[v] = Right[v] Left[ x ] = Left[ uv ] = Left[ u ] + Left[ v ] = Right[ u ] + Right[ v ] (by the inductive hypothesis) = Right[uv ] = Right[ x ]

Another recursively defined set of binary strings • Basis: λ S and 0 S

Another recursively defined set of binary strings • Basis: λ S and 0 S • Recursive rule: if x S, then – x 1 S – x 10 S Theorem: S is the set of all binary strings that do not have any consecutive 0’s.

 • Basis: λ S and 0 S • Recursive rule: if x S,

• Basis: λ S and 0 S • Recursive rule: if x S, then – x 1 S – x 10 S Theorem: S is the set of all binary strings that do not have any consecutive 0’s. If x S, then x does not have 2 consecutive 0’s anywhere in the string If x does not have any 2 consecutive 0’s, then x S

 • Basis: λ S and 0 S • Recursive rule: if x S,

• Basis: λ S and 0 S • Recursive rule: if x S, then – x 1 S – x 10 S If x S, then x does not have any 2 consecutive 0’s

 • Basis: λ S and 0 S • Recursive rule: if x S,

• Basis: λ S and 0 S • Recursive rule: if x S, then – x 1 S – x 10 S If x does not have any 2 consecutive 0’s, then x S