lecture 13 3.4 recursive definitions. fractals fractals are examples of images where the same...

16
Lecture 13 3.4 Recursive Definitions

Post on 21-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Lecture 13

3.4 Recursive Definitions

Page 2: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Fractals

fractals are examples of imageswhere the same elements is beingrecursively.

Page 3: 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)

Page 4: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Fibonacci NumbersFibonacci 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?

Page 5: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

More on Fibonacci

Page 6: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

More Fibonacci

The left and right goingspirals are neighboringFibonacci numbers!

Page 7: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Golden Section

1(1 5) 1.61802

x y xPhi

x y

x y

Page 8: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

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.

33+3=63+6 = 9 & 6+6=12...

Page 9: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Recursively defined sets

Strings:S = set of stringsA = alphabet

Basic step: empty string is in SRecursive step: if w is in S and x in A wx is in S

Example: binary strings: A={0,1}

1) empty string2) 0 & 13) 00 & 01 & 10 & 114) ...

Page 10: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Recursive DefinitionsDefinition 3 & examples 9,10,11,13,14,15 & Generalized Induction are not required.

Rooted Trees: A rooted tree has vertices, a distinguished vertex called the rootand 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 rootsr1,...rn,... with a new edge, we construct a new rooted tree.

basis step:

Step 1:

...

Step 2:

alreadyexists

etc.

Trees are often very important data-structures for instance to search and sort data.

Page 11: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

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 T1with an edge as a left sub-tree and attach T2 as a right sub-tree.

Step 1:

Step 2:

Step 3: e.g.

Page 12: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

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.

Page 13: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

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

Page 14: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

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 needlong codes for infrequent words: much more efficient (shortest expected code length).

Important constraint: prefix property: we do not want that the firstk 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 every0 or 1 you send over the channel).

Page 15: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Recursive Construction

Basic Step:every symbolis a tree with one vertex.

Recursive Step: Take the two trees with smallest frequenciesand merge them into a single bigger tree. New root represent total frequency.

most freq.letter has short code b=0e=10c=110d=1110a=1111

Page 16: Lecture 13 3.4 Recursive Definitions. Fractals fractals are examples of images where the same elements is being recursively

Huffman Tree for Alphabet

MAX=10100000011000101