66
2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map Examples: Each pixel on this screen is assigned exactly one integer: its color as a mixture of various levels of red, green, and blue. Each person is assigned exactly one birth mother. Each non-negative real number is

2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Embed Size (px)

Citation preview

Page 1: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

2.3 Functions

• A function is an assignment of each element of one set to a specific element of some other set.

• Synonymous terms: function, assignment, map• Examples:

– Each pixel on this screen is assigned exactly one integer: its color as a mixture of various levels of red, green, and blue.

– Each person is assigned exactly one birth mother.– Each non-negative real number is assigned exactly

one square root.

Page 2: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Notations and Drawings• If is a function from set to set , we state

that fact with the symbolism .• For each element of set , we represent its

assigned element of setas

Page 3: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

a

b

c

d

2

1

A B

a

b

c

d

2

1

A B

a

b

c

d

2

1

3

A B

a

b

c

d

2

1

3

A B

Page 4: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Terminology: Given

• Domain and Codomain

• Range

• Image

• Pre-image

Page 5: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

“Arithmetic” on Functions

• If f1 and f2 are functions whose codomain is the real numbers, then we can define f1+ f2 and f1f2

Page 6: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Image of a SetSuppose and Then

Examples:

Page 7: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

One-to-One (Injective) Functions:

is said to be one-to-one (injective) provided that for each whenever then

Onto (Surjective) Functions:

is said to be onto (surjective) provided that

Page 8: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

One-to-One Correspondences and Inverse Functions

• A map that is 1-1 and onto, i.e. both injective and surjective, is called a

• Inverse of (when it exists) is the function

Page 9: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

If is onto then

If is 1-1 then

If is both 1-1 and onto then

Page 10: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Monotonic Functions on f is increasing provided whenever

f is decreasing provided whenever

f is strictly increasing provided whenever

f is strictly decreasing provided whenever

Page 11: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Examples: Proving functions are 1-1, onto and bijections

1. Show defined by is a bijection.

Page 12: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

2. Let be the set of even integers and be the set of odd integers. Define by Determine if is a bijection.

Page 13: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Composition of Functions

Page 14: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

The “Graph” of a Function

• The graph of function is the set of all ordered pairs for which .

Page 15: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Some Important Functions

• “floor” and “ceiling” functions

Page 16: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

2.4 Sequences and Summations

• Sequences and sequence notation

Page 17: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

• Geometric progressions

• Arithmetic progressions

Page 18: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

• Finding the pattern…– Examples:

• 3, 10, 31, 94, …

• …

• 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, …

... ,321,16

1,81,4

1,21

Page 19: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

• Summations and Summation Notation

The sum of the first terms of is denoted

Reindexing

Page 20: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

• Sum of a finite geometric series

𝑆=∑𝑖=0

𝑛

𝑎𝑟 𝑖

Page 21: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

• Summation over members of a set

• Double summation

𝑓 : 𝐴→𝑅 ∑𝑖∈ 𝐴

𝑎𝑖

∑𝑖=1

3

∑j=1

4

𝑖− j

Page 22: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

• Other useful sums

Page 23: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Cardinality

• Two sets A and B have the same cardinality iff …

• A set A is countable provided …

• (“Aleph nought”)

Page 24: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Countability of the Rationals

Page 25: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

3.1 Algorithms

“A precise set of written instructions for performing a computation or for solving a problem”

Page 26: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Algorithm 3.12. (x = π↓ x to π↑ x)

• Step 1: Label the descending runs through , with the first on the left and the last on the right.

• Step 2: Starting with , label the leftmost element of as . As a group, move all of the elements of leftward until you reach the largest element to the left of that is still smaller than (i.e. move all of directly to the right of the element where > such that j < i and there is no element with k < i such that < < ). Call the new permutation x(2).

• Step 3: Repeat step 2 in x(2) with .

• Step 4: Continue the process with right successive descending runs (i.e. repeat step 2 in x(i) with for successive i’s) until you create x(r). The permutation x(r) is the top element of the congruence class, x(r) = π↑x.

Example:

x= π↓ x =219534768

Page 27: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Pseudo-code

• The style of the text is Pascal-like. Example:

procedure maxOf3(a, b, c: integers)

max := a

if b > max then max := b

if c > max then max := c

{max now contains the largest of a, b, and c}

Page 28: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Properties of an Algorithm

Input

Output

Definiteness

Correctness

Finiteness

Effectiveness

Generality

Page 29: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Algorithm 1: Finding the Maximum Element in a Finite Sequence

Page 30: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map
Page 31: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map
Page 32: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map
Page 33: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map
Page 34: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Greedy Algorithms

• A greedy algorithm is a class of algorithm used when a problem can be solved by making a sequence of decisions, and each such decision moves us closer to an overall solution to the problem

• The greedy algorithm, at any given stage, always makes the decision that moves us closest to that overall solution

• Do greedy algorithms always produce the “best” solution?

Page 35: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Greedy Algorithm for Travelling from one City to Another

start

finish

10km

6km

3km

7km4km

13km

12km

Page 36: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map
Page 37: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Example: Describe an algorithm that puts the first three terms of a sequence of integers of arbitrary length in increasing order

Page 38: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Example: Describe an algorithm for determining whether a string of n characters is a palindrome.

Page 39: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Example: Devise an algorithm that finds the first term of a sequence of integers that equals some previous term in the sequence.

Page 40: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

3.2 The Growth of Functions

• Suppose we have two functions and mapping into the set of real numbers, each of whose domain is some subset of the set of real numbers

• We say that “” (“is big-oh of ”) provided there exists a positive constant and a positive real number for which, whenever , then

Page 41: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map
Page 42: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

The meaning of

• How do we measure the “rate of growth” of a function? Not just the rate of growth at a single point, but over its entire domain?

• When we say “we are saying that in some general way the rate of growth of is ultimately no worse than that of a constant multiple of

Page 43: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Example

• Show that is where and .

Page 44: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Functions of the Same Order

• Is it possible to have two functions and for which each is “big-oh” of the other?

• Yes, and in that case we say the two functions are functions of the same order.

Page 45: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Use a Simpler Function as a “Yardstick”

• Whenever possible, we want to use as our g(x) function a relatively simple function whose behavior we are quite familiar with.

• Examples:g(x) = 1g(x) = xg(x) = x2

g(x) = log xetc.

Page 46: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

TheoremSuppose is a polynomial of degree , i.e. f(x) = anxn+an-1xn-1+…+a1x+a0 for some series of constants a0 , a1 , …, an . Then f(x) is

Page 47: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Some Important “Ideal Functions”

g(n) = 1g(n) = ng(n) = n2 (and other polynomial functions n3, n4, etc.)g(n) = log ng(n) = n log ng(n) = 2n

g(n) = an (any constant a > 1)g(n) = n!g(n) = nn

Page 48: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Combinations

If f(x) = f1(x)+f2(x) where f1(x) = O(g1(x)) and f2(x) = O(g2(x)), then f(x) is O(max(g1(x),g2(x)).

Example: f(x) = 2x + log x is

If f(x) = f1(x)f2(x) where f1(x) = O(g1(x)) and f2(x) = O(g2(x)), then f(x) is O(g1(x)g2(x)).

Example: f(n) = (log n + 17)n2 is

Page 49: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

More Examples

• is

• Find a big-oh upper bound for …

(x3+1)log x + (x2+7)(log x)2

Page 50: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Big- and Big-

• We say that is (“big omega of ”) precisely when is

• To say that is (“big theta of ”) is to say that is and is

• In other words, big-theta is a symmetric relationship. It means and are functions of the same order

Page 51: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

3.3 Complexity of Algorithms

• Complexity is loosely defined as the degree of sensitivity of an algorithm to the size of the problem to be solved– Time complexity

– Space complexity

Page 52: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

The “Size of the Problem”

• Can usually be reduced to a single integer variable, say .

• Examples:– Payroll program– Sort– High-resolution Graphics

• What is depends on the algorithm

Page 53: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Critical Operations

• In any algorithm there is usually at least one critical operation, i.e. an operation which is performed at least as often as any other operation appearing in the algorithm

• Examples:– Comparison– Swap– Arithmetic operation such as +, *, etc.

Page 54: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Operation Count Analysis

• Suppose we have an algorithm for which the “size of the problem” is measured by some integer variable .

• Suppose also that a particular critical operation has been chosen.

• Let be defined as the number of times the critical operation is performed for a problem of size .

• An analysis of the time complexity of the algorithm can be performed by studying the nature of the function

Page 55: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Example

procedure search(x, a1, a2, …, an: integers)

index := 0

i := 1

while index = 0 and i ≤ n do

begin

if x = ai then index := i

i := i+1

end

{ ‘index’ contains 0 if x is not in the list; otherwise index is the first value of i between 1 and n for which ai = x. }

Using comparison for equality as the critical operation, what is the worst-case critical operation count f(n)?

Page 56: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

procedure search(x, a1, a2, …, an: integers)

index := 0

i := 1

while index = 0 and i ≤ n do

begin

if x = ai then index := i

i := i+1

end

{ ‘index’ contains 0 if x is not in the list; otherwise index is the first value of i between 1 and n for which a i = x. }

Page 57: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Worst-Case and Average-Case Analysis for Linear Search

• Both are for the simple linear search algorithm. (As a matter of fact both are .)

Page 58: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Another Example

procedure SelectionSort(a1, a2, …, an: integers)for i:=1 to n-1 do

begins := ifor j:=i+1 to n do

if aj < as then s := j

swap ai and as

end

{The elements a1, a2, …, an are now in ascending order.}

Using comparisons for order (<, >, ≤, ≥) as the critical operation, what is the worst-case critical operation count f(n)?

Page 59: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

procedure SelectionSort(a1, a2, …, an: integers)for i:=1 to n-1 do

begins := ifor j:=i+1 to n do

if aj < as then s := j

t := ai

ai := as

as := tend

{The elements a1, a2, …, an are now in ascending order.}

Page 60: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Worst-Case, Average-Case, and Best-Case Analyses for Selection Sort

• All analyses are and for Selection Sort

Page 61: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Example 2

procedure BetterInsertionSort(a1, a2, …, an: integers)for j := 2 to n do begin m:= aj

{ Insert aj into the sorted sequence a1, a2, …, aj-1 } i := j-1 while i > 0 and ai> m do begin ai+1 := ai

i := i-1 ai+1 := m endend{ The sequence a1, a2, …, an now contains all the

original values, but in nondecreasing order. }

Page 62: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Worst-Case and Average-Case Analysis for Insertion Sort

• Worst-Case is • Average-Case is• “Best-Case” is

Page 63: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Commonly Used Terminology for the Complexity of Algorithms

Complexity Terminology

Constant Complexity

Logarithmic Complexity

Linear Complexity

Complexity

Polynomial Complexity

, where Exponential Complexity

Factorial Complexity

Page 64: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Classes of Problems

• Intractable problem: A problem is intractable if there is a mathematical proof that no polynomial algorithm exists for solving it

• Unsolvable problem: A problem is unsolvable if there is a mathematical proof that no algorithm at all exists for solving it.

Page 65: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Classes P and NP

• Class P: A problem is in class P if there is a known algorithm that solves the problem in polynomial time

• Class NP: A problem is in class NP if there is no known polynomial algorithm for solving it, but it is known that a non-deterministic Turing Machine can check the correctness of a potential solution in polynomial time

Page 66: 2.3 Functions A function is an assignment of each element of one set to a specific element of some other set. Synonymous terms: function, assignment, map

Class NP-Complete

• A problem is in class NP-Complete if it belongs to a certain set of NP problems for which, if any one of them is found to be solvable with a polynomial algorithm, then all of them can be solved in polynomial time

• Examples– Traveling salesman problem– 3-coloring problem