240 likes | 413 Views
Functions. A function f from a set X to a set Y is a relation from X to Y such that x X is related to one and only one y Y X is called the domain & Y is called the range. We say x is mapped into y. f = {(a, 3), (b, 3), (c, 5), (d, 1)}. Function description. Functions are described as:
E N D
Functions • A function f from a set X to a set Y is a relation from X to Y such that x X is related to one and only one y Y • X is called the domain & Y is called the range. We say x is mapped into y. • f = {(a, 3), (b, 3), (c, 5), (d, 1)} Discrete Mathematics: Functions
Function description • Functions are described as: • Set of ordered pairs, example f as given before • Using a formula such as f(x) = expression, example: f (x) = 2x + 2 • Illustration as in the example below Discrete Mathematics: Functions
a b c d e 1 2 3 4 5 6 7 Properties of functions • A function is called one to one or (injection) if different elements in the domain are related to different elements in the range. Discrete Mathematics: Functions
a b c d e f 1 2 3 4 5 Properties of functions • A function is called onto or a surjection if each element of the range is related to at least one element in the domain. Discrete Mathematics: Functions
a b c d e 1 2 3 4 5 Properties of functions • A function is called bijection ifit is one to one onto • For each element in the domain there is one and only one element in the range • Number of elements in the domain equals that in the range Discrete Mathematics: Functions
Sequences • A sequence is a function with domain is a set of consecutive integers. • Example: S(i) = i2 for i ≥ 1 The ith term of the sequence i2 is denoted by Si • What is the second term of the sequence 22 = 4 The fourth is 42 = 16 Discrete Mathematics: Functions
Finite & infinite Sequences • If the domain of the sequence is finite, then the elements can be listed as a tuple for example: • If the domain of the previous example S(i) is {1, 2, 3,…., 9, 10}, then the sequence elements are {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} • The domain of S(i) is the integers, in this case the sequence elements are infinite {1, 4, 9, … , i2, …., } Discrete Mathematics: Functions
Summing Finite Sequence Example: If n = 3, then the sum = 14 • Is there a formula to compute the sum of finite sequence elements? For the example S(i). • How about the sequence 1 + 2 + 3 +…. + n Discrete Mathematics: Functions
Identity function • For any domain X, the identity function I:XX is the unique function such that aX: I(a)=a. • Note that the identity function is both one-to-one and onto (bijective). Discrete Mathematics: Functions
• • • • • • • • • Identity Function Illustrations • The identity function: Domain and range Discrete Mathematics: Functions
Other Functions • In discrete math, we will frequently use the following functions over real numbers: • x (“floor of x”) is the largest (most positive) integer x. • x (“ceiling of x”) is the smallest (most negative) integer x. Discrete Mathematics: Functions
Visualizing Floor & Ceiling • Real numbers “fall to their floor” or “rise to their ceiling.” • Note that if xZ,x x &x x • Note that if xZ, x = x = x. 3 . 1.6=2 2 . 1.6 . 1 1.6=1 0 . 1.4= 1 1 . 1.4 . 2 1.4= 2 . . . 3 3 3=3= 3 Discrete Mathematics: Functions
x y Cartesian graphs of relations & Functions • Discovering properties of relations and functions from graphs Example: This is a relation but not a function vertical line crosses the graph in two points Discrete Mathematics: Functions
Plotting floor function • Plot of graph of function f(x) = x/3: f(x) Set of points (x, f(x)) +2 3 x +3 2 Discrete Mathematics: Functions
0 1 Comparing functions • A function f > g for finite number of points but less than for others • Example the identity function and x2 Discrete Mathematics: Functions
Growth rate function • The time needed to execute a program depends on several factors among them the number of input values. • If T is the running time then it is given as a function of number of input elements as T(n) • For example if L is a list of elements & it is needed to search L for some value. The running time depends on the size of L. Discrete Mathematics: Functions
A linear time algorithm • The running time T(n) is of the form of the line equation. T(n) = a*n + b • Searching the list L for a given value: • Start with the first position • Compare the key with the list element in the first position if found then, DONE and exit • If not found try the next position and repeat the above step Discrete Mathematics: Functions
A linear time algorithm • What is the number of comparisons needed to find out that the key is not found in the list? • What is the number of comparisons to find the key in the first position? • What is the average time of finding the key using this algorithm? • A program that implements the algorithm takes linear time. Discrete Mathematics: Functions
One more linear algorithm • Let L be a list of integer values. Is it possible to find the smallest element and then bring it to the first position? • Start from the second position • For each position i from 2 to n • If the element in the ith position is less than the one in the first exchange it with the number in position 1 • Next i Continue Discrete Mathematics: Functions
Linear algorithm • What is the number of comparisons that will be made to accomplish the task of producing the list with the smallest number in the first position? If each comparison needs a time then, the time needed will be (n-1)*a • If each exchange needs b time, then • If no exchange takes place, then T(n)=(n-1)*a • If the exchange takes place each time, then • T(n) = (n-1)*a + (n-1)*b = (n-1)*(a+b) • So (n-1)*a ≤ T(n) ≤ (n-1)*(a+b) Discrete Mathematics: Functions
A sorting algorithm • If the process in the previous example is repeated and the smallest integer in the remaining values is placed in the second position, then in the same way this process needs n-2 number of comparisons. • Repeating the process for the third, fourth & so on. Leads to SORTING the list in ascending order Discrete Mathematics: Functions
T(n) for the sorting algorithm • T(n) in the minimum needs: (n-1)*a + (n-2)*a + (n-3)*a …… 1*a • T(n) in the maximum needs: (n-1)*(a+b) + (n-2)*(a+b) + …1*(a+b) Discrete Mathematics: Functions
Comparison between the algorithms • How the two algorithms are compared? • If T1(n) = a*n + b (linear) & T2(n) = a*n2 + b*n + c (quadratic), then how do they compare? • Example: T1(n) = 100*n +50 and T2(n) = n2. Discrete Mathematics: Functions
Order of running time • A function f(n) is of order O(g(n) if there is a constant c and a number n0 such that for all n > n0, f(n) ≤ c *g(n) • Running time functions are ordered using the O notation. • Example T1 comes before T2 in the previous example. • The order of the functions: constant, log, linear, n*log(n), n2, n3, ….2n, …. Discrete Mathematics: Functions