Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Notations and Drawings

3 a b c d 2 1 AB 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

4 Domain and Codomain Range Image Pre-image

5 “Arithmetic” on Functions If f 1 and f 2 are functions whose codomain is the real numbers, then we can define f 1 + f 2 and f 1 f 2

6 Image of a Set

7 One-to-One (Injective) Functions: Onto (Surjective) Functions:

8 One-to-One Correspondences and Inverse Functions

9

10 f is increasing provided whenever f is decreasing provided whenever f is strictly increasing provided whenever f is strictly decreasing provided whenever

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

12

13 Composition of Functions

14 The “Graph” of a Function

15 Some Important Functions “floor” and “ceiling” functions

16 2.4 Sequences and Summations Sequences and sequence notation

17 Geometric progressions Arithmetic progressions

18 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, …

19 Summations and Summation Notation

20 Sum of a finite geometric series

21 Summation over members of a set Double summation

22 Other useful sums

23 Cardinality

24 Countability of the Rationals

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

26 Example: x= π↓ x =219534768

27 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}

28 Properties of an Algorithm Input Output Definiteness Correctness Finiteness Effectiveness Generality

29 Algorithm 1: Finding the Maximum Element in a Finite Sequence

30

31

32

33

34 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?

35 Greedy Algorithm for Travelling from one City to Another start finish 10km 6km 3km 7km 4km 13km 12km

36

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

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

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

40 3.2 The Growth of Functions

41

42

43 Example

44 Functions of the Same Order

45 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) = 1 g(x) = x g(x) = x 2 g(x) = log x etc.

46 Theorem

47 Some Important “Ideal Functions” g(n) = 1 g(n) = n g(n) = n 2 (and other polynomial functions n 3, n 4, etc.) g(n) = log n g(n) = n log n g(n) = 2 n g(n) = a n (any constant a > 1) g(n) = n! g(n) = n n

48 Combinations If f(x) = f 1 (x)+f 2 (x) where f 1 (x) = O(g 1 (x)) and f 2 (x) = O(g 2 (x)), then f(x) is O(max(g 1 (x),g 2 (x)). Example: f(x) = 2 x + log x is If f(x) = f 1 (x)f 2 (x) where f 1 (x) = O(g 1 (x)) and f 2 (x) = O(g 2 (x)), then f(x) is O(g 1 (x)g 2 (x)). Example: f(n) = (log n + 17)n 2 is

49 More Examples

50

51 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

52 The “Size of the Problem”

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

54 Operation Count Analysis

55 Example procedure search(x, a 1, a 2, …, a n : integers) index := 0 i := 1 while index = 0 and i ≤ n do begin if x = a i 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. } Using comparison for equality as the critical operation, what is the worst-case critical operation count f(n)?

56 procedure search(x, a 1, a 2, …, a n : integers) index := 0 i := 1 while index = 0 and i ≤ n do begin if x = a i 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. }

57 Worst-Case and Average-Case Analysis for Linear Search Both are for the simple linear search algorithm. (As a matter of fact both are.)

58 Another Example procedure SelectionSort(a 1, a 2, …, a n : integers) for i:=1 to n-1 do begin s := i for j:=i+1 to n do if a j < a s then s := j swap a i and a s end {The elements a 1, a 2, …, a n are now in ascending order.} Using comparisons for order (, ≤, ≥) as the critical operation, what is the worst-case critical operation count f(n)?

59 procedure SelectionSort(a 1, a 2, …, a n : integers) for i:=1 to n-1 do begin s := i for j:=i+1 to n do if a j < a s then s := j t := a i a i := a s a s := t end {The elements a 1, a 2, …, a n are now in ascending order.}

60 Worst-Case, Average-Case, and Best- Case Analyses for Selection Sort All analyses are and for Selection Sort

61 Example 2 procedure BetterInsertionSort(a 1, a 2, …, a n : integers) for j := 2 to n do begin m  := a j { Insert a j into the sorted sequence a 1, a 2, …, a j-1 } i := j-1 while i > 0 and a i > m do begin a i+1 := a i i := i-1 a i+1 := m end { The sequence a 1, a 2, …, a n now contains all the original values, but in nondecreasing order. }

62 Worst-Case and Average-Case Analysis for Insertion Sort Worst-Case is Average-Case is “Best-Case” is

63 Commonly Used Terminology for the Complexity of Algorithms ComplexityTerminology Constant Complexity Logarithmic Complexity Linear Complexity Polynomial Complexity Exponential Complexity Factorial Complexity

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

65 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

66 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


Download ppt "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."

Similar presentations


Ads by Google