functions definition & purpose notation functions on binary / returning binary values finite...

33
Functions Definition & purpose • Notation Functions on binary / returning binary values Finite automaton model We haven’t completely left the world of counting. Sometimes we wish to know how many functions / how many inputs …

Upload: ann-henry

Post on 27-Dec-2015

221 views

Category:

Documents


1 download

TRANSCRIPT

Functions

• Definition & purpose• Notation

• Functions on binary / returning binary values

• Finite automaton model

• We haven’t completely left the world of counting. Sometimes we wish to know how many functions / how many inputs …

Introduction• Function: any procedure that takes input, produces

output, and has exactly 1 possible output value for a given input.– Example: A vending machine takes money, and if the

correct amount is entered, there is output. As the machine “runs” it must keep track of the money so it knows the instant that the right amount has been reached.

• The code directs the computation to make transitions from one state to another, depending on the input or result of some calculation.

• Alternate definition: set of ordered pairs (x, y) in which the x’s are not repeated.

continued

• Functions are not really a new concept to you…• For each input value, we expect one output value.

– If you run the same program on the same input data, you expect the same output.

• Notation– Name : domain co-domain– For example: f : Z Z– The domain is akin to the parameter type– The co-domain is the return type.– Don’t confuse co-domain with range.

• At a minimum, to specify a function, you need to give the domain and rule.

ExamplesMathematical notation C/C++/Java notation

square : Z Zsquare(x) = x2

int square(int x){ return x * x;}

sum : Z Z Zsum (x, y) = x + y

int sum(int x, int y){ return x + y;}

pow : R R Rpow(x, y) = xy

double pow(double x, double y){ // implementation goes here // return a value of type double}

Note the format of these definitions.Usual mathematical convention: Domain = set of all values where rule is defined. Co-domain = set of real numbers.

Defining a function

• Sometimes the domain needs to be restricted in some way.– If a function’s parameter type is int, does this mean that

any integer value can be sent? – What should we do if not?

• Make sure your definition is “well formed.”– Each value in the input (domain) has exactly one output

(range) drawn from the co-domain.

• “Not well formed” could mean:– f(x) is undefined for some x– f(x) is ambiguous for some x

Binary examples

• A Boolean function– Let B = { T, F }– f : B B B– f(x, y) = xy’

• Parity bit– Let B = { 0, 1 }– parity : B8 B9 (or you could say parity : Z Z)– Rule for parity:

if x7 + x6 + … x0 is even, then x8 = 0

else x8 = 1.

More about binary

• It turns out that in CS, many important functions are those on binary input (of arbitrary length)– And if the function returns true/false, we may wish to

know which inputs make the function return true.

• Truth table is not feasible!• Alphabet Σ = { 0, 1 } or { a, b }.• Example

– Let L = Σ3 Σ4 and let boolean = { true, false }– f : L boolean– f(x) = x begins with 0– Describe in your own words.

More examples

• A way to reverse a string– reverse : Σ* Σ*– reverse(x) = the reversal of the bits of x

• The “equal” language– f : Σ* boolean– f(x) = x has an equal number of 0’s and 1’s– For instance:

f(ε) = truef(1) = falsef(1010) = true

– Can you give more examples of x where f(x) = true?

Counting them

• How many functions exist?• Let D = domain, C = co-domain• Each value in the domain can choose 1 value in co-

domain. In other words, it has |C| choices.• Number of functions is |C||D|.• Special case: C and D are the same set, or just the

same size, n elements: Then, the number of functions is nn.

• Invertible function: each value in range has to be different. Then, the number of functions is n!.

Finite automata

• Automata is plural; automaton is singular• Also called a finite state machine, or just FA• Very common type of function used in CS, of the

form Σ* boolean• When an FA is given input, it can’t immediately

determine whether to determine true/false. So we must have various states and transitions to assist in the computation.

• We can specify an FA by either– A drawing– A state table

Example

• Start state• Accept/happy state(s)

– If we are in one of these when input is done, we accept string.

• Transitions• “finite” refers to # of

states. More on limitations later.

s1 s2

1 0, 1

0

Example

• The FA could also be expressed in a table.

• The table tells us where to go on each possible input bit.

From Input 0

Input 1

s1 s2 s1

s2 s2 s2

s1 s2

1 0, 1

0

Example

• Let’s try sample input like 101.

• Can you figure out the language of this machine?– i.e. How do we get to the

happy state?

s1 s2

1 0, 1

0

More FA’s

• Finite automata: functions on a binary string that return boolean– Determine behavior of given FA– Draw our own FA that recognizes some type of input (e.g.

even number of 0’s)

• Properties of functions– One-to-one– Onto– Counting question: how many functions are there?

Example #2

• What is language of this FA?• Note: if we change which state is the accept state,

we’d have a different language! Inverting accept & nonaccept states gives us the opposite language.

s1 s2 s3

0

0

1

0, 11

What languages?

A B C0 0

1 1 0, 1

A C D

B

0

1

0, 1

01

1

0

Practice with FA’s

• Goals:– Drawing of FA figure out its language– Language description draw corresponding FA

• FA’s are good when conceptualizing how to read a program’s input. For example, the format of a floating-point number.– States: “need sign”, “need digit”, “need point”, “need

digit”, “happy”

• Note: if a transition you need is not specified by an FA, we say that it “crashes” – immediately rejects.

• When done reading input, see if in happy state.

Draw our own

• Anything beginning with 01– States: “need 01”, “need 1”, “dead”, “happy”

• Begin with 0, contains an odd number of 1’s– States: “need 0 & odd 1’s”, “even 1’s”, “dead”, “happy”

• Don’t forget to finish putting 0/1 transitions on all states. Be sure to identify the start state.

Function properties

• Sometimes a (mathematical) function has a special property– One-to-one– Onto

• For a function to be a reversible process, both these properties must hold.– Useful in cryptography– Decompiling a program where you have lost the source

code.– The “undo” button!

Definitions

• One-to-one: All values in the range are unique.– For all a, b in the domain, f(a) = f(b) a = b– No value in co-domain has 2+ arrows pointing to it.– “horizontal line test”– Consider negation: there exist distinct a, b in domain for which:

f(a) = f(b).

• Onto: All values in the co-domain are achieved.– For all y in co-domain, there is an x in the domain such that f(x) = y.– Every value in co-domain has arrow(s) pointing to it.– For a real number function, the graph goes vertically up to + and

down to – .– Consider negation: there exists y in co-domain such that for all x in

domain, f(x) y.

Example

• Define f: Z Z with the rule f(x) = 4x + 3.• Is f one-to-one?

For all a and b, f(a) = f(b) should imply a=b.4a + 3 = 4b + 3a = b Yes!

• Is f onto?For all y, there is an x such that f(x) = y.In other words, we can always solve for x. y = 4x + 3x = (y – 3)/4 Not necessarily true for integers!What would be a good counterexample?

Notes

• To be 1-1, |domain| |co-domain|– For example, we can define the function y = 3x with

domain and co-domain as { 1, 2, 3 } { 3, 4, 5, 6, 7, 8, 9 }

• To be onto, |domain| |co-domain|– For example, consider y = x mod 2 for the domain and co-

domain:{ 1, 2, 3, 4 } { 0, 1 }

Classify

• For the following real-number functions, determine whether each is 1-1 or onto or both.

1. y = x3

2. y = x3 – x3. y = sqrt(x)4. y = x2

5. Integer function: y = 2x + 16. Integer function: y = x mod 5

– How can we restrict this function so that it is 1-1 and onto?

Counting functions

• Given sizes of domain and co-domain: let’s say they are d and c, respectively.

• How many 1-1 functions?– For each value in domain, determine how many possible

values in co-domain are eligible as the functional value.– We have c choices for first value in D, c – 1 values for

second value, c – 2 for third, etc.– Total is P(c, d).

• How many onto functions? …

# Onto functions

• A more difficult question than for 1-1.• Note: Typically the domain is larger than co-domain.• Example |domain| = 4 and |co-domain| = 2

– i.e. How many onto functions { 1, 2, 3, 4 } { a, b }– Without restrictions: each value 1-4 has 2 choices of a

functional value, so total # functions is 24.– But, need to subtract out the functions that are NOT onto.

This means functions that assign only to a or only to b. There are 2 of these.

– Total onto functions = 16 – 2 = 14.

Larger case

• To motivate a more general solution, consider a larger case: |domain| = 5 and |co-domain| = 3.– { 1, 2, 3, 4, 5 } { a, b, c }– Total # functions is 35.– Need to subtract out functions that only assign to 2 of the

3 values. One of the 3 values is excluded – we choose which one in 3 ways. How many functions assign 5 domain values to just 2 co-domain values? That’s 25. Thus, we need to subtract out 3 * 25.

– *** We should also subtract out functions that assign to just 1 of the 3 values. Already handled. In fact, we overdid it! Need to add back functions that assign to 1 value.

– This turns into a “nested” inclusion/exclusion problem!

General formula

• If the domain has D elements and co-domain has C elements, how many onto functions? (D C)– Total functions: Each of the C values can be mapped to

from any of the D values: CD

– Take out cases where not all C values are used. How many functions assign to just C – 1? (C – 1)D, but we also have to choose which C is omitted: C * (C – 1)D

– Add back cases where C – 2 values are assigned to. Choose which 2 to omit: C(C, 2) * (C – 2)D

– And so on, and we obtainCD – C * (C – 1)D + C(C, 2) * (C – 2)D – C(C, 3) * (C – 3)D + …

Finite/infinite

• Finite set – means that its elements can be numbered. Formally: its elements can be put into a 1-1 correspondence with the sequence 1, 2, 3, … n, where n is some positive integer.

• An infinite set is one that’s not finite.

• However, there are 2 kinds of infinity!

Countable

• Countable set: An infinite set whose elements can be put into a 1-1 correspondence with the positive integers.– Examples we can show are countable:Even numbers, all integers, rational numbers, ordered pairs.

• Uncountable set = ∞ set that’s not countable. – Examples we can show are uncountable:Real numbers, functions of integers, infinite-length strings

Examples

• The set of even numbers is countable.

• The set of integers is countable.

2 4 6 8 10 12 …

1 2 3 4 5 6 …

0 1 -1 2 -2 3 -3 4 -4 …

1 2 3 4 5 6 7 8 9

Ordered pairs

j = 1 j = 2 j = 3 j = 4

i = 1 1 2 4 7 …

i = 2 3 5 8 …

i = 3 6 9 …

i = 4 10 …

The number assigned to (i , j) is (i + j – 1)*(i + j – 2)/2 + i

Real numbers

• Suppose real numbers were countable. The numbering might go something like this:

• The problem is that we can create a value that has no place in the correspondence!

Real # Value

1 2.71828…

2 3.14159…

3 0.55555…

4 -1.23456…

5 5.676767...

… …

X ? . 85667…

Infinite bit strings

# Value

1 00000…

2 100000…

3 011010…

4 0010001…

5 11111001011…

? 11010…