CS483 Design and Analysis of Algorithms

Similar documents
Randomization in Algorithms

5 ProbabilisticAnalysisandRandomized Algorithms

Graduate Algorithms CS F-02 Probabilistic Analysis

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Data Structures and Algorithm Analysis (CSC317) Randomized algorithms

Random Variable. Pr(X = a) = Pr(s)

CS483 Design and Analysis of Algorithms

Design and Analysis of Algorithms

Introduction to Randomized Algorithms: Quick Sort and Quick Selection

CS483 Design and Analysis of Algorithms

CS Analysis of Recursive Algorithms and Brute Force

CS Asymptotic Notations for Algorithm Analysis

CS483 Design and Analysis of Algorithms

Lecture 5: Loop Invariants and Insertion-sort

CS Asymptotic Notations for Algorithm Analysis Outline

CS261: Problem Set #3

Overview. CSE 21 Day 5. Image/Coimage. Monotonic Lists. Functions Probabilistic analysis

Discrete Mathematics and Probability Theory Fall 2017 Ramchandran and Rao Midterm 2 Solutions

Lecture 4. Quicksort

U.C. Berkeley CS294: Beyond Worst-Case Analysis Handout 3 Luca Trevisan August 31, 2017

CS684 Graph Algorithms

CS 361, Lecture 14. Outline

Notes on Discrete Probability

Advanced Algorithms (2IL45)

Recitation 6. Randomization. 6.1 Announcements. RandomLab has been released, and is due Monday, October 2. It s worth 100 points.

Math 447. Introduction to Probability and Statistics I. Fall 1998.

CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

Quicksort. Where Average and Worst Case Differ. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

CS483 Design and Analysis of Algorithms

Aditya Bhaskara CS 5968/6968, Lecture 1: Introduction and Review 12 January 2016

Solutions to the Mathematics Masters Examination

Analysis of Algorithms CMPSC 565

CS70: Jean Walrand: Lecture 26.

CSE 21 Practice Exam for Midterm 2 Fall 2017

CS483 Design and Analysis of Algorithms

Note that r = 0 gives the simple principle of induction. Also it can be shown that the principle of strong induction follows from simple induction.

Business Statistics. Lecture 3: Random Variables and the Normal Distribution

Lecture 10: Random Walks in One Dimension

Randomized algorithms. Inge Li Gørtz

Algorithm Design and Analysis Homework #5 Due: 1pm, Monday, December 26, === Homework submission instructions ===

Design and Analysis of Algorithms

Outline. 1 Introduction. 3 Quicksort. 4 Analysis. 5 References. Idea. 1 Choose an element x and reorder the array as follows:

Discrete Mathematics & Mathematical Reasoning Induction

Discrete Mathematics. Spring 2017

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Hash tables. Hash tables

Lecture 4: Two-point Sampling, Coupon Collector s problem

CS280, Spring 2004: Final

2. This exam consists of 15 questions. The rst nine questions are multiple choice Q10 requires two

CMPUT 675: Approximation Algorithms Fall 2014

UCSD CSE 21, Spring 2014 [Section B00] Mathematics for Algorithm and System Analysis

1.225J J (ESD 205) Transportation Flow Systems

CSEP 521 Applied Algorithms. Richard Anderson Winter 2013 Lecture 1

CS Non-recursive and Recursive Algorithm Analysis

11.1 Set Cover ILP formulation of set cover Deterministic rounding

Great Theoretical Ideas in Computer Science

Homework 4 Solutions

COS 341: Discrete Mathematics

Lecture 5: Introduction to Probability

Discrete Mathematics & Mathematical Reasoning Induction

Lecture 5: Two-point Sampling

Math.3336: Discrete Mathematics. Mathematical Induction

Example 1. The sample space of an experiment where we flip a pair of coins is denoted by:

Introduction to discrete probability. The rules Sample space (finite except for one example)

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

Integer Linear Programs

EECS 126 Probability and Random Processes University of California, Berkeley: Spring 2015 Abhay Parekh February 17, 2015.

Math 261A Probabilistic Combinatorics Instructor: Sam Buss Fall 2015 Homework assignments

Lecture 3. Big-O notation, more recurrences!!

Theorem 1.7 [Bayes' Law]: Assume that,,, are mutually disjoint events in the sample space s.t.. Then Pr( )

Kousha Etessami. U. of Edinburgh, UK. Kousha Etessami (U. of Edinburgh, UK) Discrete Mathematics (Chapter 7) 1 / 13

PRIMES Math Problem Set

Carleton University. Final Examination Fall DURATION: 2 HOURS No. of students: 195

Searching Algorithms. CSE21 Winter 2017, Day 3 (B00), Day 2 (A00) January 13, 2017

Ch. 2: Lec. 1. Basics: Outline. Importance. Usages. Key problems. Three ways of looking... Colbert on Equations. References. Ch. 2: Lec. 1.

Problem set 1. (c) Is the Ford-Fulkerson algorithm guaranteed to produce an acyclic maximum flow?

Lecture 1/25 Chapter 2

CS 125 Section #12 (More) Probability and Randomized Algorithms 11/24/14. For random numbers X which only take on nonnegative integer values, E(X) =

CS1800 Discrete Structures Final Version A

Discrete Mathematics and Probability Theory Fall 2011 Rao Midterm 2 Solutions

CS 124 Math Review Section January 29, 2018

Today s Menu. Administrativia Two Problems Cutting a Pizza Lighting Rooms

Analysis of Algorithms. Randomizing Quicksort

Algorithm Design and Analysis

Pentagon Walk Revisited. Double Heads Revisited Homework #1 Thursday 26 Jan 2006

CS70: Jean Walrand: Lecture 19.

AMSC/MATH 673, CLASSICAL METHODS IN PDE, FALL Required text: Evans, Partial Differential Equations second edition

Section 4.2: Mathematical Induction 1

CSE525: Randomized Algorithms and Probabilistic Analysis April 2, Lecture 1

Carleton University. Final Examination Fall DURATION: 2 HOURS No. of students: 223

Math.3336: Discrete Mathematics. Chapter 9 Relations

Welcome to Physics 161 Elements of Physics Fall 2018, Sept 4. Wim Kloet

Discrete Mathematics and Probability Theory Summer 2015 Chung-Wei Lin Midterm 2

CS 374: Algorithms & Models of Computation, Spring 2017 Greedy Algorithms Lecture 19 April 4, 2017 Chandra Chekuri (UIUC) CS374 1 Spring / 1

Static-Priority Scheduling. CSCE 990: Real-Time Systems. Steve Goddard. Static-priority Scheduling

Massachusetts Institute of Technology Department of Electrical Engineering & Computer Science 6.041/6.431: Probabilistic Systems Analysis

Lecture 1: Asymptotics, Recurrences, Elementary Sorting

CS 341: Algorithms. Douglas R. Stinson. David R. Cheriton School of Computer Science University of Waterloo. January 16, 2019

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Def :Corresponding to an underlying experiment, we associate a probability with each ω Ω, Pr{ ω} Pr:Ω [0..1] such that Pr{ω} = 1

Transcription:

CS483 Design and Analysis of Algorithms Lectures 4-5 Randomized Algorithms Instructor: Fei Li lifei@cs.gmu.edu with subject: CS483 Office hours: STII, Room 443, Friday 4:00pm - 6:00pm or by appointments Course web-site: http://www.cs.gmu.edu/ lifei/teaching/cs483 fall08/ Figures unclaimed are from books Algorithms and Introduction to Algorithms 1 / 28

Announcements 1 Assignment 3 is updated (with 2 more questions). 2 Lecture note 3 is updated. 3 Assignment 3 is due on September 24, next Wednesday. 4 Solutions of the first 3 assignments will be given in class (September 24). Thus, late submission will not be accepted. Please inform your classmates. 5 Regarding to Assignment 4 to Assignment 8: Each assignment deserves 6 points. 2 / 28

Chapter 5 of CLRS Probabilistic Analysis and Randomized Algorithms 1 Example 2 Indicator Random Variables 3 Probabilistic Analysis 4 Randomized Algorithms 3 / 28

Example Hiring Problem Need an office assistant and minimize the cost a. Agency sends one candidate every day pay C i b. Interview the person, either hire him/her (and fire the old one), or keep old one pay C h C i c. Always want the best person hire if interviewee is better than current person Solution? 4 / 28

Example Hiring Problem function hire(n) best = 0; for (i = 1 to n) interview candidate i; if i is better than best best = i; hire i; m people hired. Total cost Worst-case cost:? Best-case cost:? n C i + m c h 5 / 28

Probabilistic Analysis function hire(n) best = 0; for (i = 1 to n) interview candidate i; if i is better than best best = i; hire i; m people hired. Total cost n C i + m c h Average-case cost 1 Assume applicants come in random order 2 Each permutation of applicants is equally likely 6 / 28

Probabilistic Analysis versus Randomized Algorithms 1 Randomness internal to the algorithm or not 2 Input distribution is known or not (CLSR 5.1-3) A Random-Number-Generator outputs 1 with some probability p and 0 with probability 1 p, where 0 < p < 1. You do not know p. Give an algorithm that uses Random-Number-Generator as a subroutine, and returns an unbiased answer, returning 0 with probability 1/2 and 1 with probability 1/2 What is the running time of your algorithm? 7 / 28

Probability Review Indicator Random Variable 8 / 28

Probability Review Indicator Random Variable 1 Indicator variable associated with event A in sample space {H, T }. It converts between probabilities and expectations. { 1, if A occurs, I {A} = 0, otherwise. 9 / 28

Probability Review Indicator Random Variable 1 Indicator variable associated with event A in sample space {H, T }. It converts between probabilities and expectations. { 1, if A occurs, I {A} = 0, otherwise. 2 X is a random variable in a sample space {H, T }. X H is the expected number of H in flipping coins { 1, if H occurs, X H = I {X = H} = 0, otherwise. E[X H ] = E[I {X = H}] = 1 Pr{H} + 0 Pr{T } = 1 (1/2) + 0 (1/2) = 1/2 10 / 28

Probability Review Lemma Given a sample space S and an event A in the sample space S, let X A = I {A} (X A is an indicator random variable). Then E[X A ] = Pr{A}. Proof. By the definition of an indicator random variable and the definition of expected value, we have E[X A ] = E[I {A}] = 1 Pr{A} + 0 Pr{Ā} = Pr{A} Linearity of expectation E[X + Y ] = E[X ] + E[Y ] Let X i = I {the ith flip results in event H} Let X be the random variable denoting the total number of heads in n coin flips X =? n X i i=1 11 / 28

Practice Hiring Problem Let X i be the indicator variable associated with the event that ith candidate is hired { 1, if candidate i is hired, X i = I {candidate i is hired} = 0, otherwise. Let X be the random variable whose value equals the number of times we hire a new assistant E[X ] = n x Pr{X = x} i=1 X = X 1 + X 2 +... + X n E[X i ] = Pr{candidate i is hired} =? 12 / 28

Practice Hiring Problem n E[X ] = E[ X i ] = = i=1 n E[X i ] i=1 n (1/i) i=1 = ln n + O(1) Theorem Assuming the candidates are presented in a random order (i.e., we know the distribution), algorithm HIRE has a total (expected) hiring cost of O(c h ln n) 13 / 28

Randomized Algorithms Instead of assuming a distribution, we impose a distribution, which is internal to the algorithm function rand-hire(n) random permutate the list of candidates; best = 0; for (i = 1 to n) interview candidate i; if i is better than best best = i; hire i; Theorem Assuming the candidates are presented in a random order (i.e., we know the distribution), algorithm RAND-HIRE has a total expected hiring cost of O(c h ln n) 14 / 28

Producing a Uniform Random Permutation Definition A uniform random permutation is one in which each of the n! possible permutations are equally likely Definition Given a set of n elements, a k-permutation is a sequence n! containing k of the n elements. There are (n k)! such k-permutations 15 / 28

Producing a Uniform Random Permutation Definition A uniform random permutation is one in which each of the n! possible permutations are equally likely Definition Given a set of n elements, a k-permutation is a sequence n! containing k of the n elements. There are (n k)! such k-permutations function permutate-by-sorting(a) n = length[a]; for i = 1 to n P[i] = RANDOM(1, nˆ3); Choose a number between 1 and n^3 sort A, using P as sort keys; return A; 16 / 28

Producing a Uniform Random Permutation function permutate-by-sorting(a) n = length[a]; for i = 1 to n P[i] = RANDOM(1, nˆ3); Choose a number between 1 and n^3 sort A, using P as sort keys; return A; Lemma Procedure permutate-by-sorting produces a uniform random permutation of the input, assuming that all priorities are distinct. Proof.? What is the running time? 17 / 28

Background: Mathematical Induction Theorem Principle of Mathematical Induction. Let P be a property of positive integer such that: 1 Base case: P(1) is true; and 2 Induction step: If P(n) is true, then P(n + 1) is true. Then P(n) is true for all positive integers. Exercise Prove that the sum of the n first odd positive integers is n 2, i.e., 1 + 3 + 5 +... + (2 n 1) = n 2 Proof.? Exercise Find the largest number R(n) of regions in which the plane can be divided by n straight lines. Proof.? 18 / 28

Background: Mathematical Inductions 1 Generalization of the base case. Exercise Prove that 2 n + 1 2 n, for n 3. 19 / 28

Background: Mathematical Inductions 1 Generalization of the base case. Exercise Prove that 2 n + 1 2 n, for n 3. 2 Strong form of mathematical induction. Theorem Let P be a property of positive integers such that 1 Base case: P(1) is true, and 2 Induction step: If P(k) is true for all 1 k n, then P(n + 1) is true. Then P(n) is true for all positive integers. Exercise Prove that every integer n 2 is prime or a product of primes. Proof.? 20 / 28

Background: Loop Invariant Proof We use loop invariant to help us understand why an algorithm is correct. We must show three things about a loop invariant: 1 Initialization. It is true prior to the first iteration of the loop. 2 Maintenance. If it is true before an iteration of the loop, it remains true before the next iteration. 3 Termination. When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct. This property is perhaps the most important one, since we are using the loop invariant to show correctness. It also differs from the usual use of mathematical induction, in which the inductive step is used infinitely; here, we stop the induction when the loop terminates. 21 / 28

Producing a Uniform Random Permutation Definition A uniform random permutation is one in which each of the n! possible permutations are equally likely Definition Given a set of n elements, a k-permutation is a sequence containing k of the n n! elements. There are such k-permutations (n k)! function rand-in-space(a) n = length[a]; for i = 1 to n swap A[i] with A[RANDOM(i, n)]; 22 / 28

Proof Using Loop Invariants Remark For each possible (i 1)-permutation, the sub-array A[1, 2,..., i 1] contains this (i 1)-permutation with probability (n i+1)! n! Proof.? Maintenance. We assume that just before the (i 1)-st iteration, each possible (i 1)-permutation appears in the sub-array A[1,..., i 1] with probability (n i+1)!. n! We will show that after the ith iteration, each possible i-permutation appears in the sub-array A[1,..., i] with probability (n i)!. Incrementing i for the next iteration will n! then maintain the loop invariant (Continue) 23 / 28

Proof Using Loop Invariants Remark For each possible (i 1)-permutation, the sub-array A[1, 2,..., i 1] contains this (i 1)-permutation with probability (n i+1)! n! Proof. Let E 1 denote the event in which the first i 1 iterations have created the particular (i 1)-permutation < x 1,..., x i 1 > in A[1,..., i 1]. By the loop invariant, Pr(E 1 ) = (n i+1)! n!. Let E 2 be the event that i-th iteration puts x i in position A[i] Pr{E 2 E 1 } = Pr{E 2 E 1 } Pr{E 1 } = = 1 n i + 1 (n i)! n! (n i + 1)! n! 24 / 28

Online Hiring Problem 25 / 28

Online Hiring Problem Hire exactly once + Maximize the probability of hiring the best candidate? 26 / 28

Online Hiring Problem Hire exactly once + Maximize the probability of hiring the best candidate? function online-hire(k, n) best-score = 0; for i = 1 to k if score(i) > best-score best-score = score(i); for i = k + 1 to n if score(i) > best-score return i; return n; 27 / 28

Analysis of Online Hiring Problem Let S be the event we succeed in choosing the best-qualified candidate s max Let S i be the event we succeed when s max is the i-th candidate we interviewed Pr{S} = n Pr{S i } = i=1 n i=k+1 Pr{S i } Let B i be the event s max is in position i Let O i be the event online-hire does no choose any in a position k + 1 to i 1? Pr{S i } = Pr{B i O i } = Pr{B i } Pr{O i } = 1 n k i 1 Pr{S} = k n 1 n 1 i i=k = k n (ln n ln k 1) k = n e 28 / 28