1 / 27

Functions

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.

kenton
Télécharger la présentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 …

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

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

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

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

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

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

  8. 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(ε) = true f(1) = false f(1010) = true • Can you give more examples of x where f(x) = true?

  9. 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!.

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

  11. 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. 0 s1 s2 1 0, 1

  12. Example • The FA could also be expressed in a table. • The table tells us where to go on each possible input bit. 0 s1 s2 1 0, 1

  13. 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? 0 s1 s2 1 0, 1

  14. 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?

  15. Example #2 0 1 • 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 1 0, 1

  16. What languages? 0 0 A B C 1 1 0, 1 1 0 A C D 0 1 0 1 B 0, 1

  17. 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. 

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

  19. 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!

  20. Definitions • One-to-one: All values in the range are unique. • For all a, b in the domain, f(a) = f(b)  a = b • “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. • For a real number function, the graph has arrows going up to +  and down to – . • Consider negation: there exists y in co-domain such that for all x in domain, f(x)  y.

  21. 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 + 3 a = 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 + 3 x = (y – 3)/4 Not necessarily true for integers! What would be a good counterexample?

  22. 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 }

  23. Classify • For the following real-number functions, determine whether each is 1-1 or onto or both. • y = x3 • y = x3 – x • y = sqrt(x) • y = x2 • Integer function: y = 2x + 1 • Integer function: y = x mod 5 • How can we restrict this function so that it is 1-1 and onto?

  24. 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? …

  25. # 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.

  26. 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!

  27. 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 obtain CD – C * (C – 1)D + C(C, 2) * (C – 2)D – C(C, 3) * (C – 3)D + …

More Related