Docsity

Algorithms in Discrete Mathematics: A Comprehensive Guide, Slides of Discrete Mathematics

Partial preview of the text

Download Algorithms in Discrete Mathematics: A Comprehensive Guide and more Slides Discrete Mathematics in PDF only on Docsity!

Discrete Mathematics, Chapter 3:

Algorithms

Richard Mayr

University of Edinburgh, UK

Outline

(^1) Properties of Algorithms

(^2) The Growth of Functions

(^3) Complexity of Algorithms

Properties of Algorithms

Input: An algorithm has input values from a specified set. Output: From the input values, the algorithm produces the output values from a specified set. The output values are the solution. Correctness: An algorithm should produce the correct output values for each set of input values. Finiteness: An algorithm should produce the output after a finite number of steps for any input. Effectiveness: It must be possible to perform each step of the algorithm correctly and in a finite amount of time. Generality: The algorithm should work for all problems of the desired form.

Example: Linear Search

Prose: Locate an item in a list by examining the sequence of list elements one at a time, starting at the beginning. More formal prose: Find item x in the list [a 1 , a 2 ,... , an]. First compare x with a 1. If they are equal, return the position 1. If not, try a 2. If x = a 2 , return the position 2. Keep going, and if no match is found when the entire list is scanned, return 0. Pseudocode: Algorithm 1: Linear Search Input : x : integer , [a 1 ,... , an] : list of distinct integers Output : Index i s.t. x = ai or 0 if x is not in the list. i := 1; while i ≤ n and x 6 = ai do i := i + 1; if i ≤ n then result := i else result := 0; return result;

Binary Search

Pseudocode: Algorithm 2: Binary Search Input : x : integer , [a 1 ,... , an] : strictly increasing list of integers Output : Index i s.t. x = ai or 0 if x is not in the list. i := 1; // i is the left endpoint of the interval j := n; // j is the right endpoint of the interval while i < j do m := b(i + j)/ 2 c; if x > am then i := m + 1 else j := m; if x = ai then result := i else result := 0; return result;

Example: Binary Search

Find target 19 in the list: 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 (^1) The list has 16 elements, so the midpoint is 8. The value in the 8th position is 10. As 19 > 10, search is restricted to positions 9-16. 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 (^2) The midpoint of the list (positions 9 through 16) is now the 12th position with a value of 16. Since 19 > 16, further search is restricted to the 13th position and above. 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 (^3) The midpoint of the current list is now the 14th position with a value of 19. Since 19 6 > 19, further search is restricted to the portion from the 13th through the 14th positions. 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 (^4) The midpoint of the current list is now the 13th position with a value of 18. Since 19 > 18, search is restricted to position 14. 1 2 3 5 6 7 8 10 12 13 15 16 18 19 20 22 (^5) Now the list has a single element and the loop ends. Since 19 = 19, the location 14 is returned.

Example: Greedy Scheduling

We have a group of proposed talks with start and end times. Construct a greedy algorithm to schedule as many as possible in a lecture hall, under the following assumptions: When a talk starts, it continues till the end. (Indivisible). No two talks can occur at the same time. (Mutually exclusive.) A talk can begin at the same time that another ends. Once we have selected some of the talks, we cannot add a talk which is incompatible with those already selected because it overlaps at least one of these previously selected talks. How should we make the “best choice” at each step of the algorithm? That is, which talk do we pick? I (^) The talk that starts earliest among those compatible with already chosen talks? I (^) The talk that is shortest among those already compatible? I (^) The talk that ends earliest among those compatible with already chosen talks?

Greedy Scheduling

Picking the shortest talk doesn’t work. But picking the one that ends soonest does work. The algorithm is specified on the next page.

The Growth of Functions

Given functions f : N → R or f : R → R. Analyzing how fast a function grows. Comparing two functions. Comparing the efficiently of different algorithms that solve the same problem. Applications in number theory (Chapter 4) and combinatorics (Chapters 6 and 8).

Big-O Notation

Definition

Let f , g : R → R. We say that f is O(g) if there are constants C and k such that ∀x > k. |f (x)| ≤ C|g(x)|

This is read as “f is big-O of g” or “g asymptotically dominates f ”. The constants C and k are called witnesses to the relationship between f and g. Only one pair of witnesses is needed. (One pair implies many pairs, since one can always make k or C larger.) Common abuses of notation: Often one finds this written as “f (x) is big-O of g(x)” or “f (x) = O(g(x))”. This is not strictly true, since big-O refers to functions and not their values, and the equality does not hold. Strictly speaking O(g) is the class of all functions f that satisfy the condition above. So it would be formally correct to write f ∈ O(g).

Properties of Big-O Notation

If f is O(g) and g is O(f ) then one says that f and g are of the same order. If f is O(g) and h(x) ≥ g(x) for all positive real numbers x then f is O(h). The O-notation describes upper bounds on how fast functions grow. E.g., f (x) = x^2 + 3 x is O(x^2 ) but also O(x^3 ), etc. Often one looks for a simple function g that is as small as possible such that still f is O(g). (The word ‘simple’ is important, since trivially f is O(f ).)

Example

Bounds on functions. Prove that f (x) = anxn^ + an− 1 xn−^1 + · · · + a 1 x + a 0 is O(xn). 1 + 2 + · · · + n is O(n^2 ). n! = 1 × 2 × · · · × n is O(nn). log(n!) is O(n log(n)).

Useful Big-O Estimates

If d > c > 1, then nc^ is O(nd^ ), but nd^ is not O(nc^ ). If b > 1 and c and d are positive, then (logb n)c^ is O(nd^ ), but nd^ is not O((logb n)c^ ). If b > 1 and d is positive, then nd^ is O(bn), but bn^ is not O(nd^ ). If c > b > 1, then bn^ is O(cn), but cn^ is not O(bn). If f 1 (x) is O(g 1 (x)) and f 2 (x) is O(g 2 (x)) then (f 1 + f 2 )(x) is O(max(|g 1 (x)|, |g 2 (x)|)). If f 1 is O(g 1 ) and f 2 is O(g 2 ) then (f 1 ◦ f 2 ) is O(g 1 ◦ g 2 ). Note: These estimates are very important for analyzing algorithms. Suppose that g(n) = 5 n^2 + 7 n − 3 and f is a very complex function that you cannot determine exactly, but you know that f is O(n^3 ). Then you can still derive that n · f (n) is O(n^4 ) and g(f (n)) is O(n^6 ).

Big-Omega Notation

Definition

Let f , g : R → R. We say that f is Ω(g) if there are constants C and k such that ∀x > k. |f (x)| ≥ C|g(x)|

This is read as “f is big-Omega of g”. The constants C and k are called witnesses to the relationship between f and g. Big-O gives an upper bound on the growth of a function, while Big-Omega gives a lower bound. Big-Omega tells us that a function grows at least as fast as another. Similar abuse of notation as for big-O. f is Ω(g) if and only if g is O(f ). (Prove this by using the definitions of O and Ω.)