Introduction to Algorithms Randomized Algorithms – Ch5 Lecture 5 CIS 670.

Slides:



Advertisements
Similar presentations
Comp 122, Spring 2004 Order Statistics. order - 2 Lin / Devi Comp 122 Order Statistic i th order statistic: i th smallest element of a set of n elements.
Advertisements

Theory of Computing Lecture 3 MAS 714 Hartmut Klauck.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: CLRS “Intro.
1 Today’s Material Medians & Order Statistics – Ch. 9.
CPSC 411, Fall 2008: Set 10 1 CPSC 411 Design and Analysis of Algorithms Set 10: Randomized Algorithms Prof. Jennifer Welch Fall 2008.
Probability theory and average-case complexity. Review of probability theory.
Probabilistic Analysis and Randomized Algorithm. Worst case analysis Probabilistic analysis  Need the knowledge of the distribution of the inputs Indicator.
Chapter 5. Probabilistic Analysis and Randomized Algorithms
UMass Lowell Computer Science Analysis of Algorithms Spring, 2002 Chapter 5 Lecture Randomized Algorithms Sections 5.1 – 5.3 source: textbook.
CSE 2331/5331 Topic 5: Prob. Analysis Randomized Alg.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different approaches –Probabilistic analysis of a deterministic algorithm –Randomized.
Analysis of Algorithms CS 477/677 Randomizing Quicksort Instructor: George Bebis (Appendix C.2, Appendix C.3) (Chapter 5, Chapter 7)
Quicksort Ack: Several slides from Prof. Jim Anderson’s COMP 750 notes. UNC Chapel Hill1.
1 Hiring Problem and Generating Random Permutations Andreas Klappenecker Partially based on slides by Prof. Welch.
Probabilistic (Average-Case) Analysis and Randomized Algorithms Two different but similar analyses –Probabilistic analysis of a deterministic algorithm.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 4 Comparison-based sorting Why sorting? Formal analysis of Quick-Sort Comparison.
Probabilistic Analysis and Randomized Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
CSE 830: Design and Theory of Algorithms
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
How should a computer shuffle?. Intro - 2 Comp 122, Goal  Input: Given n items to shuffle (cards, …)  Output: Return some list of exactly those n items;
Qsort - 1 Lin / Devi Comp 122 d(n)d(n) Today’s puzzle  How far can you reach with a stack of n blocks, each 2 units long?
1 Randomized Algorithms Andreas Klappenecker [using some slides by Prof. Welch]
1 Discrete Probability Hsin-Lung Wu Assistant Professor Advanced Algorithms 2008 Fall.
Information Theory and Security
Design & Analysis of Algorithms COMP 482 / ELEC 420 John Greiner.
Ch. 8 & 9 – Linear Sorting and Order Statistics What do you trade for speed?
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
Sets, Combinatorics, Probability, and Number Theory Mathematical Structures for Computer Science Chapter 3 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesProbability.
Lecture 2 We have given O(n 3 ), O(n 2 ), O(nlogn) algorithms for the max sub-range problem. This time, a linear time algorithm! The idea is as follows:
1 9/8/2015 MATH 224 – Discrete Mathematics Basic finite probability is given by the formula, where |E| is the number of events and |S| is the total number.
Analysis of Algorithm Lecture 3 Recurrence, control structure and few examples (Part 1) Huma Ayub (Assistant Professor) Department of Software Engineering.
1 9/23/2015 MATH 224 – Discrete Mathematics Basic finite probability is given by the formula, where |E| is the number of events and |S| is the total number.
Randomized Algorithms (Probabilistic algorithm) Flip a coin, when you do not know how to make a decision!
Lecture 17 Nov 7, 2012 Discrete event simulation discrete time systems system changes with time, in discrete steps uncertainty (modeled by probability)
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
The Selection Problem. 2 Median and Order Statistics In this section, we will study algorithms for finding the i th smallest element in a set of n elements.
BY Lecturer: Aisha Dawood. The hiring problem:  You are using an employment agency to hire a new office assistant.  The agency sends you one candidate.
CSCE 411 Design and Analysis of Algorithms Set 9: Randomized Algorithms Prof. Jennifer Welch Fall 2014 CSCE 411, Fall 2014: Set 9 1.
Introduction to Behavioral Statistics Probability, The Binomial Distribution and the Normal Curve.
Introduction to Algorithms Jiafen Liu Sept
1 Online algorithms Typically when we solve problems and design algorithms we assume that we know all the data a priori. However in many practical situations.
CSC 211 Data Structures Lecture 13
Randomized Algorithms CSc 4520/6520 Design & Analysis of Algorithms Fall 2013 Slides adopted from Dmitri Kaznachey, George Mason University and Maciej.
Probabilistic Analysis and Randomized Algorithm. Average-Case Analysis  In practice, many algorithms perform better than their worse case  The average.
QuickSort (Ch. 7) Like Merge-Sort, based on the three-step process of divide- and-conquer. Input: An array A[1…n] of comparable elements, the starting.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 7.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
ICS 353: Design and Analysis of Algorithms
MATH 256 Probability and Random Processes Yrd. Doç. Dr. Didem Kivanc Tureli 14/10/2011Lecture 3 OKAN UNIVERSITY.
COSC 3101A - Design and Analysis of Algorithms 4 Quicksort Medians and Order Statistics Many of these slides are taken from Monica Nicolescu, Univ. of.
+ Chapter 5 Overview 5.1 Introducing Probability 5.2 Combining Events 5.3 Conditional Probability 5.4 Counting Methods 1.
Master Method (4. 3) Recurrent formula T(n) = a  T(n/b) + f(n) 1) if for some  > 0 then 2) if then 3) if for some  > 0 and a  f(n/b)  c  f(n) for.
CSC317 1 Randomized algorithms Hiring problem We always want the best hire for a job! Using employment agency to send one candidate at a time Each day,
Introduction to Algorithms Probability Review – Appendix C CIS 670.
Probability theory and average-case complexity. Review of probability theory.
Randomized Algorithms (Probabilistic algorithm) Flip a coin, when you do not know how to make a decision!
1 Chapter 7 Quicksort. 2 About this lecture Introduce Quicksort Running time of Quicksort – Worst-Case – Average-Case.
CIS 2033 based on Dekking et al. A Modern Introduction to Probability and Statistics B: Michael Baron. Probability and Statistics for Computer Scientists,
PROBABILITY AND COMPUTING RANDOMIZED ALGORITHMS AND PROBABILISTIC ANALYSIS CHAPTER 1 IWAMA and ITO Lab. M1 Sakaidani Hikaru 1.
Probabilistic analysis
Lecture 11 Sections 5.1 – 5.2 Objectives: Probability
CS 583 Analysis of Algorithms
CSCE 411 Design and Analysis of Algorithms
CS 583 Analysis of Algorithms
Applied Discrete Mathematics Week 12: Discrete Probability
Programming with data Lecture 3
Randomized Algorithms
Randomized Algorithms
Sets, Combinatorics, Probability, and Number Theory
Presentation transcript:

Introduction to Algorithms Randomized Algorithms – Ch5 Lecture 5 CIS 670

Comp 122 The Hiring Problem You are using an employment agency to hire a new assistant.You are using an employment agency to hire a new assistant. The agency sends you one candidate each day.The agency sends you one candidate each day. You interview the candidate and must immediately decide whether or not to hire that person. But if you hire, you must also fire your current office assistant—even if it’s someone you have recently hired.You interview the candidate and must immediately decide whether or not to hire that person. But if you hire, you must also fire your current office assistant—even if it’s someone you have recently hired. Cost to interview is c i per candidate.Cost to interview is c i per candidate. Cost to hire is c h per candidate.Cost to hire is c h per candidate. You want to have, at all times, the best candidate seen so far.You want to have, at all times, the best candidate seen so far. When you interview a candidate who is better than your current assistant, you fire the current assistant and hire the candidate.When you interview a candidate who is better than your current assistant, you fire the current assistant and hire the candidate. You will always hire the first candidate that you interview.You will always hire the first candidate that you interview. Problem: What is the cost of this strategy?Problem: What is the cost of this strategy?

Pseudo-code to Model the Scenario Cost Model: Slightly different from the model considered so far. However, analytical techniques are the same. Want to determine the total cost of hiring the best candidate. If n candidates interviewed and m hired, then cost is nc i +mc h. Have to pay nc i to interview, no matter how many we hire. So, focus on analyzing the hiring cost mc h. m varies with order of candidates. Hire-Assistant (n) best  0 ;;Candidate 0 is a least qualified sentinel candidate for i  1 to n do interview candidate i if candidate i is better than candidate best then best  i hire candidate i Hire-Assistant (n) best  0 ;;Candidate 0 is a least qualified sentinel candidate for i  1 to n do interview candidate i if candidate i is better than candidate best then best  i hire candidate i

Worst-case Analysis In the worst case, we hire all n candidates.In the worst case, we hire all n candidates. This happens if each candidate is better than all those who came before. Candidates come in increasing order of quality.This happens if each candidate is better than all those who came before. Candidates come in increasing order of quality. Cost is nc i +nc h.Cost is nc i +nc h. If this happens, we fire the agency. What should happen in the typical or average case?If this happens, we fire the agency. What should happen in the typical or average case?

Probabilistic Analysis We need a probability distribution of inputs to determine average-case behavior over all possible inputs.We need a probability distribution of inputs to determine average-case behavior over all possible inputs. For the hiring problem, we can assume that candidates come in random order.For the hiring problem, we can assume that candidates come in random order. Assign a rank rank(i), a unique integer in the range 1 to n to each candidate.Assign a rank rank(i), a unique integer in the range 1 to n to each candidate. The ordered list  rank(1), rank(2), …, rank(n)  is a permutation of the candidate numbers  1, 2, …, n .The ordered list  rank(1), rank(2), …, rank(n)  is a permutation of the candidate numbers  1, 2, …, n . Let’s assume that the list of ranks is equally likely to be any one of the n! permutations.Let’s assume that the list of ranks is equally likely to be any one of the n! permutations. The ranks form a uniform random permutation.The ranks form a uniform random permutation. Determine the number of candidates hired on an average, assuming the ranks form a uniform random permutation.Determine the number of candidates hired on an average, assuming the ranks form a uniform random permutation.

Example we can represent a particular input by listing, in order, the ranks of the candidates, i.e.,.we can represent a particular input by listing, in order, the ranks of the candidates, i.e.,. Given the rank list A 1 =, a new office assistant will always be hired 10 times, since each successive candidate is better than the previous one, and lines 5-6 will be executed in each iteration of the algorithm.Given the rank list A 1 =, a new office assistant will always be hired 10 times, since each successive candidate is better than the previous one, and lines 5-6 will be executed in each iteration of the algorithm. Given the list of ranks A 2 =, a new office assistant will be hired only once, in the first iteration.Given the list of ranks A 2 =, a new office assistant will be hired only once, in the first iteration. Given a list of ranks A 3 =, a new office assistant will be hired three times, upon interviewing the candidates with ranks 5, 8, and 10.Given a list of ranks A 3 =, a new office assistant will be hired three times, upon interviewing the candidates with ranks 5, 8, and 10.

Randomized Algorithm Impose a distribution on the inputs by using randomization within the algorithm.Impose a distribution on the inputs by using randomization within the algorithm. Used when input distribution is not known, or cannot be modeled computationally.Used when input distribution is not known, or cannot be modeled computationally. For the hiring problem:For the hiring problem: We are unsure if the candidates are coming in a random order.We are unsure if the candidates are coming in a random order. To make sure that we see the candidates in a random order, we make the following change.To make sure that we see the candidates in a random order, we make the following change. The agency sends us a list of n candidates in advance.The agency sends us a list of n candidates in advance. Each day, we randomly choose a candidate to interview.Each day, we randomly choose a candidate to interview. Thus, instead of relying on the candidates being presented in a random order, we enforce it.Thus, instead of relying on the candidates being presented in a random order, we enforce it.

Comp 122 Randomized Hire-Assistant How many times do you find a new maximum? Randomized-Hire-Assistant (n) Randomly permute the list of candidates best  0 ;;Candidate 0 is a least qualified dummy candidate for i  1 to n do interview candidate i if candidate i is better than candidate best then best  i hire candidate i Randomized-Hire-Assistant (n) Randomly permute the list of candidates best  0 ;;Candidate 0 is a least qualified dummy candidate for i  1 to n do interview candidate i if candidate i is better than candidate best then best  i hire candidate i

Indicator Random Variables In order to analyze many algorithms, including the hiring problem, we will use indicator random variables. Indicator random variables provide a convenient method for converting between probabilities and expectationsIn order to analyze many algorithms, including the hiring problem, we will use indicator random variables. Indicator random variables provide a convenient method for converting between probabilities and expectations A simple yet powerful technique for computing the expected value of a random variable.A simple yet powerful technique for computing the expected value of a random variable. Takes only 2 values, 1 and 0.Takes only 2 values, 1 and 0. Suppose we are given a sample space S and an event A. Then the indicator random variable I {A} associated with event A is defined as:Suppose we are given a sample space S and an event A. Then the indicator random variable I {A} associated with event A is defined as:

Indicator Random Variable -Example Let X be the random variable denoting the total number of heads in the n coin flips, so that We wish to compute the expected number of heads, so we take the expectation of both sides of the above equation to obtain

Analysis of the Hiring Problem (Probabilistic analysis of the deterministic algorithm) X – RV (random variable) that denotes the number of times we hire a new office assistant.X – RV (random variable) that denotes the number of times we hire a new office assistant. Define indicator RV’s X 1, X 2, …, X n.Define indicator RV’s X 1, X 2, …, X n. X = X 1 + X 2 + …+ X nX = X 1 + X 2 + …+ X n Need to compute Pr{candidate i is hired}.Need to compute Pr{candidate i is hired}. Pr{candidate i is hired}Pr{candidate i is hired} i is hired only if i is better than 1, 2,…,i-1.i is hired only if i is better than 1, 2,…,i-1. By assumption, candidates arrive in random orderBy assumption, candidates arrive in random order Candidates 1, 2, …, i arrive in random order.Candidates 1, 2, …, i arrive in random order. Each of the i candidates has an equal chance of being the best so far.Each of the i candidates has an equal chance of being the best so far. Candidate i has a probability of 1/i of being better qualified than candidates 1 through i - 1 and thus a probability of 1/i of being hired.Candidate i has a probability of 1/i of being better qualified than candidates 1 through i - 1 and thus a probability of 1/i of being hired. Pr{candidate i is the best so far} = 1/i.Pr{candidate i is the best so far} = 1/i. E[X i ] = 1/i. (By Lemma 5.1)E[X i ] = 1/i. (By Lemma 5.1)

Comp 122 Analysis of the Hiring Problem Compute E[X], the number of candidates we expect to hire.Compute E[X], the number of candidates we expect to hire. By Equation (A.7) of the sum of a harmonic series. Expected hiring cost = O(c h ln n).

Randomized Algoritms Generally, we call an algorithm randomized if its behavior is determined not only by its input but also by values produced by a random-number generator.Generally, we call an algorithm randomized if its behavior is determined not only by its input but also by values produced by a random-number generator. We shall assume that we have at our disposal a random-number generator RANDOM.We shall assume that we have at our disposal a random-number generator RANDOM. A call to RANDOM(a, b) returns an integer between a and b, inclusive, with each such integer being equally likely.A call to RANDOM(a, b) returns an integer between a and b, inclusive, with each such integer being equally likely. For example, RANDOM(0, 1) produces 0 with probability 1/2, and it produces 1 with probability 1/2.For example, RANDOM(0, 1) produces 0 with probability 1/2, and it produces 1 with probability 1/2. A call to RANDOM(3, 7) returns either 3, 4, 5, 6 or 7, each with probability 1/5.A call to RANDOM(3, 7) returns either 3, 4, 5, 6 or 7, each with probability 1/5. Each integer returned by RANDOM is independent of the integers returned on previous calls.Each integer returned by RANDOM is independent of the integers returned on previous calls. You may imagine RANDOM as rolling a (b - a + 1)-sided die to obtain its output. (In practice, most programming environments offer a pseudorandom-number generator: a deterministic algorithm returning numbers that "look" statistically random.)You may imagine RANDOM as rolling a (b - a + 1)-sided die to obtain its output. (In practice, most programming environments offer a pseudorandom-number generator: a deterministic algorithm returning numbers that "look" statistically random.)

Randomly Permuting Arrays Many randomized algorithms randomize the input by permuting the given input array. Many randomized algorithms randomize the input by permuting the given input array. How should a computer shuffle (or randomize the input)? How should a computer shuffle (or randomize the input)?

Comp 122, Goal Input: Given n items to shuffle (cards, …)Input: Given n items to shuffle (cards, …) Output: Return some list of exactly those n items; all n! lists should be equally likely.Output: Return some list of exactly those n items; all n! lists should be equally likely. Possible methods?Possible methods? PERMUTE-BY-SORTING (Choose keys and sort)PERMUTE-BY-SORTING (Choose keys and sort) RANDOMIZE-IN-PLACE (Swap a pair of randomly chosen cards)?RANDOMIZE-IN-PLACE (Swap a pair of randomly chosen cards)? Swap each card with a randomly chosen card?Swap each card with a randomly chosen card?

PERMUTE-BY-SORTING One common method is to assign each element A[i] of the array a random priority P[i], and then sort the elements of A according to these priorities.One common method is to assign each element A[i] of the array a random priority P[i], and then sort the elements of A according to these priorities. For example if our initial array is A = and we choose random priorities P =, we would produce an array B =, since the second priority is the smallest, followed by the fourth, then the first, and finally the third.For example if our initial array is A = and we choose random priorities P =, we would produce an array B =, since the second priority is the smallest, followed by the fourth, then the first, and finally the third. We call this procedure PERMUTE-BY-SORTING:We call this procedure PERMUTE-BY-SORTING: PERMUTE-BY-SORTING(A) 1 n ← length[A] 2 for i ← 1 to n 3 do P[i] = RANDOM(1, n 3 ) 4 sort A, using P as sort keys 5 return A Line 3 chooses a random number between 1 and n 3. We use a range of 1 to n 3 to make it likely that all the priorities in P are unique. The time-consuming step in this procedure is the sorting in line 4. The sorting takes Θ(n lg n) time

RANDOMIZE-IN-PLACE A better method for generating a random permutation is to permute the given array in place.A better method for generating a random permutation is to permute the given array in place. The procedure RANDOMIZE-IN-PLACE does so in O(n) time.The procedure RANDOMIZE-IN-PLACE does so in O(n) time. In iteration i, the element A[i] is chosen randomly from among elements A[i] through A[n]. Subsequent to iteration i, A[i] is never altered.In iteration i, the element A[i] is chosen randomly from among elements A[i] through A[n]. Subsequent to iteration i, A[i] is never altered. RANDOMIZE-IN-PLACE(A) 1 n ← length[A] 2 for i ← to n 3 do swap A[i] ↔ A[RANDOM(i, n)]