Discrete Mathematics, Chapter 3: Algorithms

Similar documents
Why? A central concept in Computer Science. Algorithms are ubiquitous.

In mathematics, it is often important to get a handle on the error term of an approximation. For instance, people will write

CS/COE

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Efficiency of algorithms. Algorithms. Efficiency of algorithms. Binary search and linear search. Best, worst and average case.

Cost Model: Work, Span and Parallelism. 1 The RAM model for sequential computation:

Lecture 3: Finding integer solutions to systems of linear equations

Analysis of Binary Search algorithm and Selection Sort algorithm

The Union-Find Problem Kruskal s algorithm for finding an MST presented us with a problem in data-structure design. As we looked at each edge,

3. Mathematical Induction

Algorithms. Margaret M. Fleck. 18 October 2010

Problem Set 7 Solutions

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Chapter 3. if 2 a i then location: = i. Page 40

Near Optimal Solutions

Approximation Algorithms

Zabin Visram Room CS115 CS126 Searching. Binary Search

Analysis of Computer Algorithms. Algorithm. Algorithm, Data Structure, Program

Classification - Examples

Introduction to computer science

The Goldberg Rao Algorithm for the Maximum Flow Problem

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Pigeonhole Principle Solutions

1 if 1 x 0 1 if 0 x 1

SIMS 255 Foundations of Software Design. Complexity and NP-completeness

CS 103X: Discrete Structures Homework Assignment 3 Solutions

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

Information Theory and Coding Prof. S. N. Merchant Department of Electrical Engineering Indian Institute of Technology, Bombay

Data Structures. Algorithm Performance and Big O Analysis

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. !-approximation algorithm.

High degree graphs contain large-star factors

Lecture Notes on Linear Search

recursion, O(n), linked lists 6/14

14.1 Rent-or-buy problem

The Running Time of Programs

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Sequential Data Structures

! Solve problem to optimality. ! Solve problem in poly-time. ! Solve arbitrary instances of the problem. #-approximation algorithm.

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Chapter Load Balancing. Approximation Algorithms. Load Balancing. Load Balancing on 2 Machines. Load Balancing: Greedy Scheduling

Lecture 1: Course overview, circuits, and formulas

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

Lecture Notes on Binary Search Trees

A Working Knowledge of Computational Complexity for an Optimizer

ON THE COMPLEXITY OF THE GAME OF SET.

2.1 Complexity Classes

Fairness in Routing and Load Balancing

Factoring & Primality

Notes on Complexity Theory Last updated: August, Lecture 1

ECON 459 Game Theory. Lecture Notes Auctions. Luca Anderlini Spring 2015

Guessing Game: NP-Complete?

Complexity Theory. IE 661: Scheduling Theory Fall 2003 Satyaki Ghosh Dastidar

CSC2420 Fall 2012: Algorithm Design, Analysis and Theory

Computational complexity theory

CS473 - Algorithms I

Testing LTL Formula Translation into Büchi Automata

Loop Invariants and Binary Search

Theoretical Computer Science (Bridging Course) Complexity

Best Monotone Degree Bounds for Various Graph Parameters

Operation Count; Numerical Linear Algebra

Many algorithms, particularly divide and conquer algorithms, have time complexities which are naturally

What Is Recursion? Recursion. Binary search example postponed to end of lecture

MATH 4330/5330, Fourier Analysis Section 11, The Discrete Fourier Transform

Analysis of Algorithms, I

Faster deterministic integer factorisation

AI: A Modern Approach, Chpts. 3-4 Russell and Norvig

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

Data Structures and Algorithms Written Examination

Minimum cost maximum flow, Minimum cost circulation, Cost/Capacity scaling

Notes from Week 1: Algorithms for sequential prediction

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

A Note on Maximum Independent Sets in Rectangle Intersection Graphs

Large induced subgraphs with all degrees odd

Binary search algorithm

9th Max-Planck Advanced Course on the Foundations of Computer Science (ADFOCS) Primal-Dual Algorithms for Online Optimization: Lecture 1

NP-Completeness and Cook s Theorem

the recursion-tree method

Practical Guide to the Simplex Method of Linear Programming

(Refer Slide Time: 01.26)

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

5.1 Bipartite Matching

THE SCHEDULING OF MAINTENANCE SERVICE

Lecture 19: Introduction to NP-Completeness Steven Skiena. Department of Computer Science State University of New York Stony Brook, NY

[Refer Slide Time: 05:10]

Reading 13 : Finite State Automata and Regular Expressions

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

Section IV.1: Recursive Algorithms and Recursion Trees

11 Multivariate Polynomials

NP-Completeness I. Lecture Overview Introduction: Reduction and Expressiveness

Discrete Mathematics & Mathematical Reasoning Chapter 10: Graphs

1 Solving LPs: The Simplex Algorithm of George Dantzig

CSI 333 Lecture 1 Number Systems

Real Roots of Univariate Polynomials with Real Coefficients

Full and Complete Binary Trees

Question: What is the probability that a five-card poker hand contains a flush, that is, five cards of the same suit?

Transcription:

Discrete Mathematics, Chapter 3: Algorithms Richard Mayr University of Edinburgh, UK Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 1 / 28

Outline 1 Properties of Algorithms 2 The Growth of Functions 3 Complexity of Algorithms Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 2 / 28

Algorithms (Abu Ja far Mohammed Ibin Musa Al-Khowarizmi, 780-850) Definition An algorithm is a finite set of precise instructions for performing a computation or for solving a problem. Example: Describe an algorithm for finding the maximum value in a finite sequence of integers. Description of algorithms in pseudocode: Intermediate step between English prose and formal coding in a programming language. Focus on the fundamental operation of the program, instead of peculiarities of a given programming language. Analyze the time required to solve a problem using an algorithm, independent of the actual programming language. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 3 / 28

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. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 4 / 28

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,..., a n ]. 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,..., a n ] : list of distinct integers Output: Index i s.t. x = a i or 0 if x is not in the list. i := 1; while i n and x a i do i := i + 1; if i n then result := i else result := 0; return result; Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 5 / 28

Binary Search Prose description: Assume the input is a list of items in increasing order, and the target element to be found. The algorithm begins by comparing the target with the middle element. If the middle element is strictly lower than the target, then the search proceeds with the upper half of the list. Otherwise, the search proceeds with the lower half of the list (including the middle). Repeat this process until we have a list of size 1. If target is equal to the single element in the list, then the position is returned. Otherwise, 0 is returned to indicate that the element was not found. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 6 / 28

Binary Search Pseudocode: Algorithm 2: Binary Search Input: x : integer, [a 1,..., a n ] : strictly increasing list of integers Output: Index i s.t. x = a i 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 := (i + j)/2 ; if x > a m then i := m + 1 else j := m; if x = a i then result := i else result := 0; return result; Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 7 / 28

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 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. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 8 / 28

Greedy Algorithms Optimization problems minimize or maximize some parameter over all possible inputs. Examples of optimization problems: Finding a route between two cities with the smallest total mileage. Determining how to encode messages using the fewest possible bits. Finding the fiber links between network nodes using the least amount of fiber. Optimization problems can often be solved using a greedy algorithm, which makes the best (by a local criterion) choice at each step. This does not necessarily produce an optimal solution to the overall problem, but in many instances, it does. After specifying what the best choice at each step is, we try to prove that this approach always produces an optimal solution, or find a counterexample to show that it does not. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 9 / 28

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? The talk that starts earliest among those compatible with already chosen talks? The talk that is shortest among those already compatible? The talk that ends earliest among those compatible with already chosen talks? Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 10 / 28

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. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 11 / 28

A Greedy Scheduling Algorithm At each step, choose the talks with the earliest ending time among the talks compatible with those selected. Algorithm 3: Greedy Scheduling by End Time Input: s 1, s 2,..., s n start times and e 1, e 2,..., e n end times Output: An optimal set S {1,..., n} of talks to be scheduled. Sort talks by end time and reorder so that e 1 e 2 e n S := ; for j := 1 to n do if Talk j is compatible with S then S := S {j} return S; Note: Scheduling problems appear in many applications. Many of them (unlike this simple one) are NP-complete and do not allow efficient greedy algorithms. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 12 / 28

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). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 13 / 28

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). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 14 / 28

Illustration of Big-O Notation f (x) = x 2 + 2x + 1, g(x) = x 2. f is O(g) witness k = 1 and C = 4. Abusing notation, this is often written as f (x) = x 2 + 2x + 1 is O(x 2 ). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 15 / 28

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 + 3x 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 ).) Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 16 / 28

Example Bounds on functions. Prove that f (x) = a n x n + a n 1 x n 1 + + a 1 x + a 0 is O(x n ). 1 + 2 + + n is O(n 2 ). n! = 1 2 n is O(n n ). log(n!) is O(n log(n)). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 17 / 28

Growth of Common Functions Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 18 / 28

Useful Big-O Estimates If d > c > 1, then n c is O(n d ), but n d is not O(n c ). If b > 1 and c and d are positive, then (log b n) c is O(n d ), but n d is not O((log b n) c ). If b > 1 and d is positive, then n d is O(b n ), but b n is not O(n d ). If c > b > 1, then b n is O(c n ), but c n is not O(b n ). 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) = 5n 2 + 7n 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 ). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 19 / 28

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 Ω.) Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 20 / 28

Big-Theta Notation Definition Let f, g : R R. We say that f is Θ(g) if f is O(g) and f is Ω(g). We say that f is big-theta of g and also that f is of order g and also that f and g are of the same order. f is Θ(g) if and only if there exists constants C 1, C 2 and k such that C 1 g(x) < f (x) < C 2 g(x) if x > k. This follows from the definitions of big-o and big-omega. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 21 / 28

Example Show that the sum 1 + 2 + + n of the first n positive integers is Θ(n 2 ). Solution: Let f (n) = 1 + 2 + + n. We have previously shown that f (n) is O(n 2 ). To show that f (n) is Ω(n 2 ), we need a positive constant C such that f (n) > Cn 2 for sufficiently large n. Summing only the terms greater than n/2 we obtain the inequality 1 + 2 + + n n/2 + ( n/2 + 1) + + n n/2 + n/2 + + n/2 = (n n/2 + 1) n/2 (n/2)(n/2) = n 2 /4 Taking C = 1/4, f (n) > Cn 2 for all positive integers n. Hence, f (n) is Ω(n 2 ), and we can conclude that f (n) is Θ(n 2 ). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 22 / 28

Complexity of Algorithms Given an algorithm, how efficient is this algorithm for solving a problem given input of a particular size? How much time does this algorithm use to solve a problem? How much computer memory does this algorithm use to solve a problem? We measure time complexity in terms of the number of operations an algorithm uses and use big-o and big-theta notation to estimate the time complexity. Compare the efficiency of different algorithms for the same problem. We focus on the worst-case time complexity of an algorithm. Derive an upper bound on the number of operations an algorithm uses to solve a problem with input of a particular size. (As opposed to the average-case complexity.) Here: Ignore implementation details and hardware properties. See courses on algorithms and complexity. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 23 / 28

Worst-Case Complexity of Linear Search Algorithm 4: Linear Search Input: x : integer, [a 1,..., a n ] : list of distinct integers Output: Index i s.t. x = a i or 0 if x is not in the list. i := 1; while i n and x a i do i := i + 1; if i n then result := i else result := 0; return result; Count the number of comparisons. At each step two comparisons are made; i n and x a i. To end the loop, one comparison i n is made. After the loop, one more i n comparison is made. If x = a i, 2i + 1 comparisons are used. If x is not on the list, 2n + 1 comparisons are made and then an additional comparison is used to exit the loop. So, in the worst case 2n + 2 comparisons are made. Hence, the complexity is Θ(n). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 24 / 28

Average-Case Complexity of Linear Search For many problems, determining the average-case complexity is very difficult. (And often not very useful, since the real distribution of input cases does not match the assumptions.) However, for linear search the average-case is easy. Assume the element is in the list and that the possible positions are equally likely. By the argument on the previous slide, if x = a i, the number of comparisons is 2i + 1. Hence, the average-case complexity of linear search is 1 n 2i + 1 = n + 2 n Which is Θ(n). i=1 Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 25 / 28

Worst-Case Complexity of Binary Search Algorithm 5: Binary Search Input: x : integer, [a 1,..., a n ] : strictly increasing list of integers Output: Index i s.t. x = a i 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 := (i + j)/2 ; if x > a m then i := m + 1 else j := m; if x = a i then result := i else result := 0; return result; Assume (for simplicity) n = 2 k elements. Note that k = log n. Two comparisons are made at each stage; i < j, and x > a m. At the first iteration the size of the list is 2 k and after the first iteration it is 2 k 1. Then 2 k 2 and so on until the size of the list is 2 1 = 2. At the last step, a comparison tells us that the size of the list is the size is 2 0 = 1 and the element is compared with the single remaining element. Hence, at most 2k + 2 = 2 log n + 2 comparisons are made. Θ(log n). Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 26 / 28

Terminology for the Complexity of Algorithms Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 27 / 28

Further topics See courses on algorithms and complexity for Space vs. time complexity Intractable problems Complexity classes: E.g., P, NP, PSPACE, EXPTIME, EXPSPACE, etc. Undecidable problems and the limits of algorithms. Richard Mayr (University of Edinburgh, UK) Discrete Mathematics. Chapter 3 28 / 28