Transcript Lec13RecDef
Lecture 13
3.4 Recursive Definitions
Fractals
fractals are examples of images
where the same elements is being
recursively.
Recursive Functions.
How do we formalize this idea of a recursive function:
We use the set of nonnegative integers N:
Basic Step: Specify the value of the function at n=0: f(0).
Recursive Step: Given the values of f(k), k <= n, give a rule for producing the
the value of f(n+1).
Example: f(0) = 3
f(n+1) = 2f(n) + 3
f(1) = 2x3 + 3 = 9
f(2) = 2x9 + 3 = 21
Example: Recursive definition of n! :
f(0) = 1
f(n+1) = f(n) x (n+1)
Fibonacci Numbers
Fibonacci numbers:
f(0) = 0, f(1) = 1, f(n+1) = f(n) + f(n-1)
for n = 1,2,3,...
f(2) = 1 + 0 = 1;
f(3) = 1 + 1 = 2;
f(4) = 2 + 1 = 3;
f(5) = 3 + 2 = 5;
Suppose a newly-born pair of rabbits, one male, one female, are
put in a field. Rabbits are able to mate at the age of
one month so that at the end of its second month a
female can produce another pair of rabbits. Suppose
that our rabbits never die and that the female always
produces one new pair (one male, one female) every month
from the second month on. The puzzle that Fibonacci posed was...
How many pairs do we have after one year?
More on Fibonacci
More Fibonacci
The left and right going
spirals are neighboring
Fibonacci numbers!
Golden Section
x
y
x y x 1
(1 5) 1.6180 Phi
x
y 2
Recursively defined sets
(Lame’s theorem will not be required material)
Exactly the same idea:
Basis Step: define a basis set (e.g. the empty set).
Recursive Step :Define a rule to produce new elements from already existing
elements.
Example:
Basis Step: 3 is in S.
Recursive Step: if x is in S and y is in S then x+y is in S.
3
3+3=6
3+6 = 9 & 6+6=12
...
Recursively defined sets
Strings:
S = set of strings
A = alphabet
Basic step: empty string is in S
Recursive step: if w is in S and x in A wx is in S
Example: binary strings: A={0,1}
1) empty string
2) 0 & 1
3) 00 & 01 & 10 & 11
4) ...
Recursive Definitions
Definition 3 & examples 9,10,11,13,14,15 & Generalized Induction are not required.
Trees are often very important data-structures for instance to search and sort data.
Rooted Trees: A rooted tree has vertices, a distinguished vertex called the root
and edges which connect the vertices.
Basic Step: A single vertex is a rooted tree.
Recursive Step: Suppose T1,...,Tn,... are rooted trees with roots r1,....rn,...
If we start with a new root r and connect this root to any of the existing roots
r1,...rn,... with a new edge, we construct a new rooted tree.
basis step:
Step 2:
already
exists
Step 1:
...
etc.
Binary Trees
Extended Binary Trees:
Basic Step: the empty set is a binary tree.
Recursive Step: If T1 and T2 are extended binary trees, then the following
tree T1.T2 is also an extended binary tree: pick a new root node and attach T1
with an edge as a left sub-tree and attach T2 as a right sub-tree.
Step 1:
Step 2:
Step 3: e.g.
Binary Trees
Full binary Trees: Only difference in the Basic Step:
Basic Step: A single vertex is a full binary tree.
Recursive Step: As in extended binary trees.
The result is that you cannot attach the empty set on the left or the right.
BASIC:
Step 1:
Full binary trees have only 0 or 2 child-nodes.
Some Defs.
h(T) is the height of a full binary tree:
Recursive Definition:
Basic Step: The height of a tree consisting of a single root node is h(T)=0.
Recursive Step: If T1 and T2 are full binary trees, then the full binary tree
T = T1.T2 has height h(T) = 1+max(h(T1),h(T2)).
n(T) is the number of vertices in the tree.
Recursive definition:
Basic Step: The number of vertices of a tree consisting of a single root node is:
n(T) = 1;
Recursive Step: If T1 and T2 are full binary trees, then the number of vertices of
the tree T1.T2 is n(T) = 1+n(T1)+n(T2).
Huffman Coding
Imagine we like to send data from A to B.
For instance we could want to send strings of letters (i.e. words).
How do we code letters into bits?
Important property: we want short codes for frequent words and we need
long codes for infrequent words: much more efficient (shortest expected code length).
Important constraint: prefix property: we do not want that the first
k bits of a codeword code for another word. This would require separator symbols.
Huffman produced a simple scheme using extended binary trees that
provides just that.
Important application: Data compression (imagine you need to pay a buck for every
0 or 1 you send over the channel).
Recursive Construction
Basic Step:
every symbol
is a tree with
one vertex.
most freq.
letter has
short code
b=0
e=10
c=110
d=1110
a=1111
Recursive Step: Take the two trees with smallest frequencies
and merge them into a single bigger tree. New root represent total frequency.
Huffman Tree for Alphabet
MAX=10100000011000101