functions discrete structure. l62 functions. basic-terms. def: a function f : a b is given by a...
Post on 19-Jan-2016
217 Views
Preview:
TRANSCRIPT
Functions
Discrete Structure
L6 2
Functions. Basic-Terms.DEF: A function f : A B is given by a
domain set A, a codomain set B, and a rule which for every element a of A, specifies a unique element f (a) in B. f (a) is called the image of a, while a is called the pre-image of f (a). The range (or image) of f is defined byf (A) = {f (a) | a A }.
3
Functions
Let us take a look at the function f:PC withP = {Linda, Max, Kathy, Peter}C = {Boston, New York, Hong Kong,
Moscow}
f(Linda) = Moscowf(Max) = Bostonf(Kathy) = Hong Kongf(Peter) = New York
Here, the range of f is C.
L6 4
Functions. Basic-Terms.
f : Z R is given by f (x ) = x 2
A1: domain is Z, co-domain is RA2: image of -3 = f (-3) = 9A3: pre-images of 3: none as 3 isn’t
an integer! pre-images of 4: -2 and 2
A4: range is the set of perfect squares f (Z) = {0,1,4,9,16,25,…}
L6 6
One-to-One, Onto, Bijection. Intuitively.
Represent functions using “node and arrow” notation:One-to-One means that no clashes occur.
BAD: a clash occurred, not 1-to-1
GOOD: no clashes, is 1-to-1
Onto means that every possible output is hit BAD: 3rd output missed, not onto
GOOD: everything hit, onto
L6 7
One-to-One, Onto, Bijection. Intuitively.
Bijection means that when arrows reversed, a function results. Equivalently, that both one-to-one’ness and onto’ness occur. BAD: not 1-to-1. Reverse
over-determined:
BAD: not onto. Reverseunder-determined:
GOOD: Bijection. Reverseis a function:
8
Properties of Functions
Is f injective?Yes.Is f surjective?Yes.Is f bijective?Yes.
LindaLinda
MaxMax
KathyKathy
PeterPeter
BostonBoston
New YorkNew York
Hong KongHong Kong
MoscowMoscow
LLüübeckbeckHelenaHelena
L6 9
One-to-One, Onto, Bijection. Formal
Definition.DEF: A function f : A B is:
one-to-one (or injective) if different elements of A always result in different images in B. onto (or surjective) if every element in B is hit by f. I.e., f (A ) = B.a one-to-one correspondence (or a bijection, or invertible) if f is both one-to-one as well as onto. If f is invertible, its inverse f -1 : B A is well defined by taking the unique element in the pre-image of b, for each b B.
L6 10
One-to-One, Onto, Bijection. Examples.
1. f : Z R, f (x ) = x 2: none2. f : Z Z, f (x ) = 2x : 1-13. f : R R, f (x ) = x 3: 1-1, onto,
bijection, inverse is f (x ) = x (1/3)
4. f : Z N, f (x ) = |x |: onto5. f (x ) = the father of x : none
L6 11
CompositionWhen a function f spits out elements of
the same kind that another function g eats, f and g may be composed by letting g immediately eat each output of f.
DEF: Suppose that g : A B and f : B C are functions. Then the composite f g : A C is defined by setting
f g (a) = f ( g (a) )
L6 12
Composition. Examples.
Q: Compute g f where 1. f : Z R, f (x ) = x 2
and g : R R, g (x ) = x 3
2. f : Z Z, f (x ) = x + 1and g = f -1 so g (x ) = x – 1
3. f : {people} {people},f (x ) = the father of x, and g = f
L6 13
Composition. Examples.1. f : Z R, f (x ) = x 2
and g : R R, g (x ) = x 3
f g : Z R , f g (x ) = x 6
2. f : Z Z, f (x ) = x + 1and g = f -1
f g (x ) = x (true for any function composed with its inverse)
3. f : {people} {people},f (x ) = g(x ) = the father of x
f g (x ) = grandfather of x from father’s side
14
Composition
Example:
f(x) = 7x – 4, g(x) = 3x,f:RR, g:RR
(fg)(5) = f(g(5)) = f(15) = 105 – 4 = 101
(fg)(x) = f(g(x)) = f(3x) = 21x - 4
Ceiling and FloorDEF: Given a real number x : The
floor of x is the biggest integer which is smaller or equal to x The ceiling of x is the smallest integer greater or equal to x.
NOTATION: floor(x) = x , ceiling(x) = x
Q: Compute 1.7, -1.7, 1.7, -1.7.
L6 16
Ceiling and Floor
A: 1.7 = 1, -1.7 = -2, 1.7 = 2, -1.7 = -1
Q: What’s the difference between the floor function and the (int) casting function in Java?
a0
a1
a2
anan-1
a0=0a1=2a2=6a3=12a4=20
Recursive Definitions
F(0) = 0; F(n + 1) = F(n) + 1;
F(0) = 1; F(n + 1) = 2 F(n);
F(0) = 1; F(n + 1) = 2F(n)
The First-Order Linear Recurrence Relation
There are many sequences that satisfy a a nn n 1 3 0, .
For example, 5,15,45,135,... or 7,21,63,189,.... To pinpointthe particular sequence described, we need to know one of theterms of the sequence. (boundary condition, or initial conditionsince usually a0 is specified)
a a n an n 1 03 0 5, ,
The general solution of the recurrence relation is a constant, and is unique and is given by
+
0
a dan d a A
a Ad n
n n
nn
10
0
,, ,
, .
The First-Order Linear Recurrence Relation
nonhomogeneous linear recurrence relation
Ex. 10.4 time complexity of bubble sort algorithm
an=an-1+(n-1), n>1, a1=0, where an=the number of comparisons to sort n numbers
an- an-1= n-1an-1- an-2= n-2an-2- an-3= n-3
a2- a1= 1+
an =1+2+3+...+(n-1)=(n2-n)/2
The Second-Order Linear Homogeneous Recurrence Relation with Constant Coefficients
Be careful not to draw conclusions from a few (or even,perhaps, many) particular instances.Ex. 10.14 Arrange pennies contiguously in each row where eachpenny above the bottom row touches two pennies in the row below it.
a1=1,a2=1,a3=2,a4=3,a5=5,a6=8,... Is an=Fn? NO
SequencesDEF: Given a set S, an (infinite) sequence in S
is a function N S. Symbolically, a sequence is represented using
the subscript notation ai . This gives a way of specifying formulaically
Note: Other sets can be taken as ordering models.
Q: Give the first 5 terms of the sequence defined by the formula
)2
πcos( iai
Sequence ExamplesA: Plug in for i in sequence 0, 1, 2, 3, 4:
Formulas for sequences often represent patterns in the sequence.
Q: Provide a simple formula for each sequence:
a) 3,6,11,18,27,38,51, … b) 0,2,8,26,80,242,728,…c) 1,1,2,3,5,8,13,21,34,…
1,0,1,0,1 43210 aaaaa
L6 24
Sequence ExamplesA: Try to find the patterns between numbers.a) 3,6,11,18,27,38,51, … a1=6=3+3, a2=11=6+5, a3=18=11+7, … and in
general ai +1 = ai +(2i +3). This is actually a good enough formula. Later we’ll learn techniques that show how to get the more explicit formula:
ai = 6 + 4(i –1) + (i –1)2
b) 0,2,8,26,80,242,728,… If you add 1 you’ll see the pattern more clearly.
ai = 3i –1c) 1,1,2,3,5,8,13,21,34,…This is the famous Fibonacci sequence given by
ai +1 = ai + ai-1
Definition of Finite Automaton
The finite automaton has 5-tuple (Q, ∑, δ,
q0, F)
Q is a set of states
∑ is a set of input alphabet
δ is a transition function or rule for moving
q0 is a start state
F is a set of accept states
Finite automaton
If the input is: 0101010101 & 0100 & 110000 & 0101000000 & 101000What is the outputs?
q3q2q1 1
0 10
0, 1
The finite automaton M is (Q, ∑, δ, q0, F)
Where Q = {q1, q2, q3} ∑ = {0, 1}
q0 is the state q1 F = {q2}
q3q2q1 1
0 10
0, 1
δ is describes as
0 1q1 q1 q2q2 q3 q2q3 q2 q2
q3q2q1 1
00
0, 1
Example
r2
q1
sba
b a
r1
q2
b
a
a a b
b
For the start state, is even because it is
possible to input 0 so far
qevenqodd
10
1
0
For the accept state, we consider qodd
because we want to test odd numbers
qodd
Example
The inputs a, b, baba, baa, bbb,
q2
q1a
q3
bb
a
a, b
00,1
00
1
1
1
The machine accepts a string if the process ends in a double circle
Anatomy of a Deterministic Finite Automaton
states
states
q0
q1
q2
q3start state (q0)
accept states (F)
Anatomy of a Deterministic Finite Automaton
00,1
00
1
1
1
q0
q1
q2
q3
The alphabet of a finite automaton is the set where the symbols come from:
The language of a finite automaton is the set of strings that it accepts
{0,1}
Q = {q0, q1, q2, q3}
Σ = {0,1}
: Q Σ → Q transition function*q0 Q is start state
F = {q1, q2} Q accept states
M = (Q, Σ, , q0, F) where
0 1
q0 q0 q1
q1 q2 q2
q2 q3 q2
q3 q0 q2
*
q2
00,1
00
1
1
1
q0
q1
q3
M
top related