Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Introduction to Algorithms Randomized Algorithms – Ch5 Lecture 5 CIS 670."— Presentation transcript:

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

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

3 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

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

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

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

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

8 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

9 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:

10 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

11 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)

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

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

14 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)?

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

16 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

17 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)]


Download ppt "Introduction to Algorithms Randomized Algorithms – Ch5 Lecture 5 CIS 670."

Similar presentations


Ads by Google